cocos2dx实现全局游戏公告

最刚开始分平台调用原生的提示,但是由于项目之后要显示的内容变的复杂,只好研究一下全局的游戏公告该怎么显示了。

直接addChild的是行不通的,或者处理起来比较繁琐,因为每次切换scene都会清空。

研究了一下cocos2dx的渲染机制。

入口:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. CCApplication::sharedApplication()->run()  
CCDirecor会周期执行一个mainLoop

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. int CCApplication::run()  
  2. {  
  3.     PVRFrameEnableControlWindow(false);  
  4.   
  5.     // Main message loop:  
  6.     MSG msg;  
  7.     LARGE_INTEGER nFreq;  
  8.     LARGE_INTEGER nLast;  
  9.     LARGE_INTEGER nNow;  
  10.   
  11.     QueryPerformanceFrequency(&nFreq);  
  12.     QueryPerformanceCounter(&nLast);  
  13.   
  14.     // Initialize instance and cocos2d.  
  15.     if (!applicationDidFinishLaunching())  
  16.     {  
  17.         return 0;  
  18.     }  
  19.   
  20.     CCEGLView* pMainWnd = CCEGLView::sharedOpenGLView();  
  21.     pMainWnd->centerWindow();  
  22.     ShowWindow(pMainWnd->getHWnd(), SW_SHOW);  
  23.   
  24.     while (1)  
  25.     {  
  26.         if (! PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))  
  27.         {  
  28.             // Get current time tick.  
  29.             QueryPerformanceCounter(&nNow);  
  30.   
  31.             // If it's the time to draw next frame, draw it, else sleep a while.  
  32.             if (nNow.QuadPart - nLast.QuadPart > m_nAnimationInterval.QuadPart)  
  33.             {  
  34.                 nLast.QuadPart = nNow.QuadPart;  
  35.                 CCDirector::sharedDirector()->mainLoop();  
  36.             }  
  37.             else  
  38.             {  
  39.                 Sleep(0);  
  40.             }  
  41.             continue;  
  42.         }  
  43.   
  44.         if (WM_QUIT == msg.message)  
  45.         {  
  46.             // Quit message loop.  
  47.             break;  
  48.         }  
  49.   
  50.         // Deal with windows message.  
  51.         if (! m_hAccelTable || ! TranslateAccelerator(msg.hwnd, m_hAccelTable, &msg))  
  52.         {  
  53.             TranslateMessage(&msg);  
  54.             DispatchMessage(&msg);  
  55.         }  
  56.     }  
  57.   
  58.     return (int) msg.wParam;  
  59. }  
mainLoop里面又会draw当前的scene:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void CCDisplayLinkDirector::mainLoop(void)  
  2. {  
  3.     if (m_bPurgeDirecotorInNextLoop)  
  4.     {  
  5.         m_bPurgeDirecotorInNextLoop = false;  
  6.         purgeDirector();  
  7.     }  
  8.     else if (! m_bInvalid)  
  9.      {  
  10.          drawScene();  
  11.        
  12.          // release the objects  
  13.          CCPoolManager::sharedPoolManager()->pop();          
  14.      }  
  15. }  
重点是在drawScene里面有一个m_pNotificationNode:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // Draw the Scene  
  2. void CCDirector::drawScene(void)  
  3. {  
  4.     // calculate "global" dt  
  5.     calculateDeltaTime();  
  6.   
  7.     //tick before glClear: issue #533  
  8.     if (! m_bPaused)  
  9.     {  
  10.         m_pScheduler->update(m_fDeltaTime);  
  11.     }  
  12.   
  13.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
  14.   
  15.     /* to avoid flickr, nextScene MUST be here: after tick and before draw. 
  16.      XXX: Which bug is this one. It seems that it can't be reproduced with v0.9 */  
  17.     if (m_pNextScene)  
  18.     {  
  19.         setNextScene();  
  20.     }  
  21.   
  22.     kmGLPushMatrix();  
  23.   
  24.     // draw the scene  
  25.     if (m_pRunningScene)  
  26.     {  
  27.         m_pRunningScene->visit();  
  28.     }  
  29.   
  30.     // draw the notifications node  
  31.     if (m_pNotificationNode)  
  32.     {  
  33.         m_pNotificationNode->visit();  
  34.     }  
  35.       
  36.     if (m_bDisplayStats)  
  37.     {  
  38.         showStats();  
  39.     }  
  40.       
  41.     kmGLPopMatrix();  
  42.   
  43.     m_uTotalFrames++;  
  44.   
  45.     // swap buffers  
  46.     if (m_pobOpenGLView)  
  47.     {  
  48.         m_pobOpenGLView->swapBuffers();  
  49.     }  
  50.       
  51.     if (m_bDisplayStats)  
  52.     {  
  53.         calculateMPF();  
  54.     }  
  55. }  
查看m_pNotificationNode定义

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /* This object will be visited after the scene. Useful to hook a notification node */  
  2.    CCNode *m_pNotificationNode;  
所以创建一个游戏公告层:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. NoticeBoard* noticeBoard = NoticeBoard::create();  
  2.     pDirector->setNotificationNode(noticeBoard);  
这样两行代码就可以实现全局的游戏公告,不受切换场景的影响了。

最刚开始分平台调用原生的提示,但是由于项目之后要显示的内容变的复杂,只好研究一下全局的游戏公告该怎么显示了。

直接addChild的是行不通的,或者处理起来比较繁琐,因为每次切换scene都会清空。

研究了一下cocos2dx的渲染机制。

入口:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. CCApplication::sharedApplication()->run()  
CCDirecor会周期执行一个mainLoop

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. int CCApplication::run()  
  2. {  
  3.     PVRFrameEnableControlWindow(false);  
  4.   
  5.     // Main message loop:  
  6.     MSG msg;  
  7.     LARGE_INTEGER nFreq;  
  8.     LARGE_INTEGER nLast;  
  9.     LARGE_INTEGER nNow;  
  10.   
  11.     QueryPerformanceFrequency(&nFreq);  
  12.     QueryPerformanceCounter(&nLast);  
  13.   
  14.     // Initialize instance and cocos2d.  
  15.     if (!applicationDidFinishLaunching())  
  16.     {  
  17.         return 0;  
  18.     }  
  19.   
  20.     CCEGLView* pMainWnd = CCEGLView::sharedOpenGLView();  
  21.     pMainWnd->centerWindow();  
  22.     ShowWindow(pMainWnd->getHWnd(), SW_SHOW);  
  23.   
  24.     while (1)  
  25.     {  
  26.         if (! PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))  
  27.         {  
  28.             // Get current time tick.  
  29.             QueryPerformanceCounter(&nNow);  
  30.   
  31.             // If it's the time to draw next frame, draw it, else sleep a while.  
  32.             if (nNow.QuadPart - nLast.QuadPart > m_nAnimationInterval.QuadPart)  
  33.             {  
  34.                 nLast.QuadPart = nNow.QuadPart;  
  35.                 CCDirector::sharedDirector()->mainLoop();  
  36.             }  
  37.             else  
  38.             {  
  39.                 Sleep(0);  
  40.             }  
  41.             continue;  
  42.         }  
  43.   
  44.         if (WM_QUIT == msg.message)  
  45.         {  
  46.             // Quit message loop.  
  47.             break;  
  48.         }  
  49.   
  50.         // Deal with windows message.  
  51.         if (! m_hAccelTable || ! TranslateAccelerator(msg.hwnd, m_hAccelTable, &msg))  
  52.         {  
  53.             TranslateMessage(&msg);  
  54.             DispatchMessage(&msg);  
  55.         }  
  56.     }  
  57.   
  58.     return (int) msg.wParam;  
  59. }  
mainLoop里面又会draw当前的scene:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. void CCDisplayLinkDirector::mainLoop(void)  
  2. {  
  3.     if (m_bPurgeDirecotorInNextLoop)  
  4.     {  
  5.         m_bPurgeDirecotorInNextLoop = false;  
  6.         purgeDirector();  
  7.     }  
  8.     else if (! m_bInvalid)  
  9.      {  
  10.          drawScene();  
  11.        
  12.          // release the objects  
  13.          CCPoolManager::sharedPoolManager()->pop();          
  14.      }  
  15. }  
重点是在drawScene里面有一个m_pNotificationNode:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. // Draw the Scene  
  2. void CCDirector::drawScene(void)  
  3. {  
  4.     // calculate "global" dt  
  5.     calculateDeltaTime();  
  6.   
  7.     //tick before glClear: issue #533  
  8.     if (! m_bPaused)  
  9.     {  
  10.         m_pScheduler->update(m_fDeltaTime);  
  11.     }  
  12.   
  13.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  
  14.   
  15.     /* to avoid flickr, nextScene MUST be here: after tick and before draw. 
  16.      XXX: Which bug is this one. It seems that it can't be reproduced with v0.9 */  
  17.     if (m_pNextScene)  
  18.     {  
  19.         setNextScene();  
  20.     }  
  21.   
  22.     kmGLPushMatrix();  
  23.   
  24.     // draw the scene  
  25.     if (m_pRunningScene)  
  26.     {  
  27.         m_pRunningScene->visit();  
  28.     }  
  29.   
  30.     // draw the notifications node  
  31.     if (m_pNotificationNode)  
  32.     {  
  33.         m_pNotificationNode->visit();  
  34.     }  
  35.       
  36.     if (m_bDisplayStats)  
  37.     {  
  38.         showStats();  
  39.     }  
  40.       
  41.     kmGLPopMatrix();  
  42.   
  43.     m_uTotalFrames++;  
  44.   
  45.     // swap buffers  
  46.     if (m_pobOpenGLView)  
  47.     {  
  48.         m_pobOpenGLView->swapBuffers();  
  49.     }  
  50.       
  51.     if (m_bDisplayStats)  
  52.     {  
  53.         calculateMPF();  
  54.     }  
  55. }  
查看m_pNotificationNode定义

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. /* This object will be visited after the scene. Useful to hook a notification node */  
  2.    CCNode *m_pNotificationNode;  
所以创建一个游戏公告层:

[cpp]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. NoticeBoard* noticeBoard = NoticeBoard::create();  
  2.     pDirector->setNotificationNode(noticeBoard);  
这样两行代码就可以实现全局的游戏公告,不受切换场景的影响了。

猜你喜欢

转载自blog.csdn.net/qq_28180261/article/details/54955268