cocos2dx游戏循环流程

来源:互联网 发布:淘宝店一颗钻值多少钱 编辑:程序博客网 时间:2024/05/16 02:11
/**

Time: 2013-1028 qmy3

 

Requirements:

1. 记录关键点与代码段;

2. 标志重点代码,添加关键注释;

 

*/

 

一,CCApplication类,我称之为应用启动,循环类

 

int CCApplication::run(){    PVRFrameEnableControlWindow(false);    // Main message loop:    MSG msg;    LARGE_INTEGER nFreq;    LARGE_INTEGER nLast;    LARGE_INTEGER nNow;    QueryPerformanceFrequency(&nFreq);    QueryPerformanceCounter(&nLast);    // Initialize instance and cocos2d.    if (!applicationDidFinishLaunching())    {        return 0;    }    CCEGLView* pMainWnd = CCEGLView::sharedOpenGLView();    pMainWnd->centerWindow();    ShowWindow(pMainWnd->getHWnd(), SW_SHOW);    while (1)    {        if (! PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))        {            // Get current time tick.            QueryPerformanceCounter(&nNow);            // If it's the time to draw next frame, draw it, else sleep a while.            if (nNow.QuadPart - nLast.QuadPart > m_nAnimationInterval.QuadPart)            {                nLast.QuadPart = nNow.QuadPart;                CCDirector::sharedDirector()->mainLoop();            }            else            {                Sleep(0);            }            continue;        }        if (WM_QUIT == msg.message)        {            // Quit message loop.            break;        }        // Deal with windows message.        if (! m_hAccelTable || ! TranslateAccelerator(msg.hwnd, m_hAccelTable, &msg))        {            TranslateMessage(&msg);            DispatchMessage(&msg);        }    }    return (int) msg.wParam;}

 

二,CCEGLView类,窗口事件处理,图形绘制类

LRESULT CCEGLView::WindowProc(UINT message, WPARAM wParam, LPARAM lParam){    BOOL bProcessed = FALSE;    switch (message)    {    case WM_LBUTTONDOWN:#if(_MSC_VER >= 1600)        // Don't process message generated by Windows Touch        if (m_bSupportTouch && (s_pfGetMessageExtraInfoFunction() & MOUSEEVENTF_FROMTOUCH) == MOUSEEVENTF_FROMTOUCH) break;#endif /* #if(_MSC_VER >= 1600) */        if (m_pDelegate && MK_LBUTTON == wParam)        {            POINT point = {(short)LOWORD(lParam), (short)HIWORD(lParam)};            CCPoint pt(point.x, point.y);            pt.x /= m_fFrameZoomFactor;            pt.y /= m_fFrameZoomFactor;            CCPoint tmp = ccp(pt.x, m_obScreenSize.height - pt.y);            if (m_obViewPortRect.equals(CCRectZero) || m_obViewPortRect.containsPoint(tmp))            {                m_bCaptured = true;                SetCapture(m_hWnd);                int id = 0;                handleTouchesBegin(1, &id, &pt.x, &pt.y);            }        }        break;    case WM_MOUSEMOVE:#if(_MSC_VER >= 1600)        // Don't process message generated by Windows Touch        if (m_bSupportTouch && (s_pfGetMessageExtraInfoFunction() & MOUSEEVENTF_FROMTOUCH) == MOUSEEVENTF_FROMTOUCH) break;#endif /* #if(_MSC_VER >= 1600) */        if (MK_LBUTTON == wParam && m_bCaptured)        {            POINT point = {(short)LOWORD(lParam), (short)HIWORD(lParam)};            CCPoint pt(point.x, point.y);            int id = 0;            pt.x /= m_fFrameZoomFactor;            pt.y /= m_fFrameZoomFactor;            handleTouchesMove(1, &id, &pt.x, &pt.y);        }        break;    case WM_LBUTTONUP:#if(_MSC_VER >= 1600)        // Don't process message generated by Windows Touch        if (m_bSupportTouch && (s_pfGetMessageExtraInfoFunction() & MOUSEEVENTF_FROMTOUCH) == MOUSEEVENTF_FROMTOUCH) break;#endif /* #if(_MSC_VER >= 1600) */        if (m_bCaptured)        {            POINT point = {(short)LOWORD(lParam), (short)HIWORD(lParam)};            CCPoint pt(point.x, point.y);            int id = 0;            pt.x /= m_fFrameZoomFactor;            pt.y /= m_fFrameZoomFactor;            handleTouchesEnd(1, &id, &pt.x, &pt.y);            ReleaseCapture();            m_bCaptured = false;        }        break;#if(_MSC_VER >= 1600)    case WM_TOUCH:{            BOOL bHandled = FALSE;            UINT cInputs = LOWORD(wParam);            PTOUCHINPUT pInputs = new TOUCHINPUT[cInputs];            if (pInputs)            {                if (s_pfGetTouchInputInfoFunction((HTOUCHINPUT)lParam, cInputs, pInputs, sizeof(TOUCHINPUT)))                {                    for (UINT i=0; i < cInputs; i++)                    {                        TOUCHINPUT ti = pInputs[i];                        POINT input;                        input.x = TOUCH_COORD_TO_PIXEL(ti.x);                        input.y = TOUCH_COORD_TO_PIXEL(ti.y);                        ScreenToClient(m_hWnd, &input);                        CCPoint pt(input.x, input.y);                        CCPoint tmp = ccp(pt.x, m_obScreenSize.height - pt.y);                        if (m_obViewPortRect.equals(CCRectZero) || m_obViewPortRect.containsPoint(tmp))                        {                            pt.x /= m_fFrameZoomFactor;                            pt.y /= m_fFrameZoomFactor;                            if (ti.dwFlags & TOUCHEVENTF_DOWN)                                handleTouchesBegin(1, reinterpret_cast<int*>(&ti.dwID), &pt.x, &pt.y);                            else if (ti.dwFlags & TOUCHEVENTF_MOVE)                                handleTouchesMove(1, reinterpret_cast<int*>(&ti.dwID), &pt.x, &pt.y);                            else if (ti.dwFlags & TOUCHEVENTF_UP)                                handleTouchesEnd(1, reinterpret_cast<int*>(&ti.dwID), &pt.x, &pt.y);                         }                     }                     bHandled = TRUE;                 }                 delete [] pInputs;             }             if (bHandled)             {                 s_pfCloseTouchInputHandleFunction((HTOUCHINPUT)lParam);             }}      break;#endif /* #if(_MSC_VER >= 1600) */    case WM_SIZE:        switch (wParam)        {        case SIZE_RESTORED:            CCApplication::sharedApplication()->applicationWillEnterForeground();            break;        case SIZE_MINIMIZED:            CCApplication::sharedApplication()->applicationDidEnterBackground();            break;        }        break;    case WM_KEYDOWN:        if (wParam == VK_F1 || wParam == VK_F2)        {            CCDirector* pDirector = CCDirector::sharedDirector();            if (GetKeyState(VK_LSHIFT) < 0 ||  GetKeyState(VK_RSHIFT) < 0 || GetKeyState(VK_SHIFT) < 0)                pDirector->getKeypadDispatcher()->dispatchKeypadMSG(wParam == VK_F1 ? kTypeBackClicked : kTypeMenuClicked);        }        if ( m_lpfnAccelerometerKeyHook!=NULL )        {            (*m_lpfnAccelerometerKeyHook)( message,wParam,lParam );        }        break;    case WM_KEYUP:        if ( m_lpfnAccelerometerKeyHook!=NULL )        {            (*m_lpfnAccelerometerKeyHook)( message,wParam,lParam );        }        break;    case WM_CHAR:        {            if (wParam < 0x20)            {                if (VK_BACK == wParam)                {                    CCIMEDispatcher::sharedDispatcher()->dispatchDeleteBackward();                }                else if (VK_RETURN == wParam)                {                    CCIMEDispatcher::sharedDispatcher()->dispatchInsertText("\n", 1);                }                else if (VK_TAB == wParam)                {                    // tab input                }                else if (VK_ESCAPE == wParam)                {                    // ESC input                    //CCDirector::sharedDirector()->end();                }            }            else if (wParam < 128)            {                // ascii char                CCIMEDispatcher::sharedDispatcher()->dispatchInsertText((const char *)&wParam, 1);            }            else            {                char szUtf8[8] = {0};                int nLen = WideCharToMultiByte(CP_UTF8, 0, (LPCWSTR)&wParam, 1, szUtf8, sizeof(szUtf8), NULL, NULL);                CCIMEDispatcher::sharedDispatcher()->dispatchInsertText(szUtf8, nLen);            }            if ( m_lpfnAccelerometerKeyHook!=NULL )            {                (*m_lpfnAccelerometerKeyHook)( message,wParam,lParam );            }        }        break;    case WM_PAINT:        PAINTSTRUCT ps;        BeginPaint(m_hWnd, &ps);        EndPaint(m_hWnd, &ps);        break;    case WM_CLOSE:        CCDirector::sharedDirector()->end();        break;    case WM_DESTROY:        destroyGL();        PostQuitMessage(0);        break;    default:        if (m_wndproc)        {                        m_wndproc(message, wParam, lParam, &bProcessed);            if (bProcessed) break;        }        return DefWindowProc(m_hWnd, message, wParam, lParam);    }    if (m_wndproc && !bProcessed)    {        m_wndproc(message, wParam, lParam, &bProcessed);    }    return 0;}

三,CCDirector类,导演类,逻辑处理类

1,逻辑循环代码段:

void CCDisplayLinkDirector::mainLoop(void){    if (m_bPurgeDirecotorInNextLoop)    {        m_bPurgeDirecotorInNextLoop = false;        purgeDirector();    }    else if (! m_bInvalid)     {         drawScene();              // release the objects         CCPoolManager::sharedPoolManager()->pop();             }}

2,图形逻辑代码段

// Draw the Scenevoid CCDirector::drawScene(void){    // calculate "global" dt    calculateDeltaTime();    //tick before glClear: issue #533    if (! m_bPaused)    {        m_pScheduler->update(m_fDeltaTime);    }    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);//glClear(GL_COLOR_BUFFER_BIT);#if 1    /* to avoid flickr, nextScene MUST be here: after tick and before draw.     XXX: Which bug is this one. It seems that it can't be reproduced with v0.9 */    if (m_pNextScene)    {        setNextScene();     }    kmGLPushMatrix();    // draw the scene    if (m_pRunningScene)    {        m_pRunningScene->visit();    }    // draw the notifications node    if (m_pNotificationNode)    {        m_pNotificationNode->visit();    }        if (m_bDisplayStats)    {        showStats();    }    kmGLPopMatrix();#endif    m_uTotalFrames++;    // swap buffers    if (m_pobOpenGLView)    {        m_pobOpenGLView->swapBuffers();    }        if (m_bDisplayStats)    {        calculateMPF();    }}