Cocos2dx global game announcement

Source: Internet
Author: User

Cocos2dx global game announcement

At the beginning, the sub-platform called the native prompt. However, due to the complexity of the content to be displayed after the project, I had to study how to display the global game announcement.

Directly addChild does not work, or it is complicated to handle, because it will be cleared every time you switch to scene.

The rendering mechanism of cocos2dx is studied.

Entry:

CCApplication::sharedApplication()->run()
CCDirecor periodically executes a mainLoop

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;}
In mainLoop, the current scene of draw is displayed again:

void CCDisplayLinkDirector::mainLoop(void){    if (m_bPurgeDirecotorInNextLoop)    {        m_bPurgeDirecotorInNextLoop = false;        purgeDirector();    }    else if (! m_bInvalid)     {         drawScene();              // release the objects         CCPoolManager::sharedPoolManager()->pop();             }}
The point is that there is a m_pNotificationNode in drawScene:

// 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);    /* 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();    m_uTotalFrames++;    // swap buffers    if (m_pobOpenGLView)    {        m_pobOpenGLView->swapBuffers();    }        if (m_bDisplayStats)    {        calculateMPF();    }}
View m_picationicationnode Definitions

 /* This object will be visited after the scene. Useful to hook a notification node */    CCNode *m_pNotificationNode;
Create a game announcement layer:

NoticeBoard* noticeBoard = NoticeBoard::create();pDirector->setNotificationNode(noticeBoard);
In this way, two lines of code can be used to implement global game announcements without the impact of switching scenarios.





Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.