cocos2d-x 3.3 RC 個人升級總結 Director的主線解析,cocos2d-xrc

來源:互聯網
上載者:User

cocos2d-x 3.3 RC 個人升級總結 Director的主線解析,cocos2d-xrc

 /**     *  Sets the Resource root path.     *  @deprecated Please use FileUtils::getInstance()->setSearchPaths() instead.     */    CC_DEPRECATED_ATTRIBUTE void setResourceRootPath(const std::string& rootResDir);    /**      *  Gets the Resource root path.     *  @deprecated Please use FileUtils::getInstance()->getSearchPaths() instead.      */    CC_DEPRECATED_ATTRIBUTE const std::string& getResourceRootPath(void);


1、越來越多的api變化,怎麼辦?鑒於此,以後每次升級引擎的時候,在全域中搜尋:CC_DEPRECATED_ATTRIBUTE,這樣子就可以看到那些api進行了修改。如果是在mac環境中,則可以繼續尋找結果中匹配的字元,比如某個api,直接搜尋看看說明。這樣子就能方便的切換新api,而不是經常跳轉。



2、我終於明白引擎的運行完整路線了。

首先,通過director和scene的create和add分別完成資源建立和渲染樹構造,之前一直漏掉了這句,現在已經補上了。可以去相關部落格重新閱讀。

接著,每次迴圈中執行cocos2d-x的引擎主線和輪詢OpenGL的事件

 <span style="white-space: pre;"></span>director->mainLoop();           <span style="white-space: pre;"></span>glview->pollEvents();

然後,在mainLoop中完成drawScene和清理當前垃圾(未處理的記憶體要求開發自己去管理),drawScene主要完成了:

 // calculate "global" dt  計算時間切片    calculateDeltaTime();        // skip one flame when _deltaTime equal to zero. 是否跳過當前幀的繪製    if(_deltaTime < FLT_EPSILON)    {        return;    }    if (_openGLView)  //輪詢OpenGL的回調事件處理,可以看到目前是個空實現    {        _openGLView->pollEvents();    }

進入平時主線相關的定時器重新整理、事件的派發。
//tick before glClear: issue #533    if (! _paused)    {        _scheduler->update(_deltaTime);        _eventDispatcher->dispatchEvent(_eventAfterUpdate);    }


完了原本的資料處理之後開始進入映像渲染的工作流程:

/* 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 (_nextScene)    {        setNextScene();    }

1、setNextScene的工作內容和代碼:根據是否需要切換情境完成情境切換相關:添加節點:執行完init()方法進入replaceScene後,在渲染樹中,新舊情境是同時存在的,1.1、先清理舊情境,執行舊情境的清場 1.2 執行新情境的入場和入場後動作。我再次瞄了一下3.3、3.2、2.2.3和是一樣的,我曾經因為聽別人說先進入新情境進場再執行舊情境的退場(這可能是之前某個版本的bug,現在不再去考究了)。大家記住,一定要自己看源碼,要不然就不選開源引擎啦。

void Director::setNextScene(){    bool runningIsTransition = dynamic_cast<TransitionScene*>(_runningScene) != nullptr;    bool newIsTransition = dynamic_cast<TransitionScene*>(_nextScene) != nullptr;    // If it is not a transition, call onExit/cleanup     if (! newIsTransition)     {         if (_runningScene)         {             _runningScene->onExitTransitionDidStart();             _runningScene->onExit();         }          // issue #709. the root node (scene) should receive the cleanup message too         // otherwise it might be leaked.         if (_sendCleanupToScene && _runningScene)         {             _runningScene->cleanup();         }     }    if (_runningScene)    {        _runningScene->release();    }    _runningScene = _nextScene;    _nextScene->retain();    _nextScene = nullptr;    if ((! runningIsTransition) && _runningScene)    {        _runningScene->onEnter();        _runningScene->onEnterTransitionDidFinish();    }}

設定完情境,即掛載了當前的渲染樹,就開始進入OpenGL的影像處理階段:

//入棧,設定OpenGL的狀態機器,跟2D、3D切換相關,悲劇的是我沒看懂,求大家一起來

pushMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW);


 // draw the scene    if (_runningScene)    {        //clear draw stats  3.3才有幾這句,3.2是沒有的        _renderer->clearDrawStats();                //render the scene        _runningScene->render(_renderer);                _eventDispatcher->dispatchEvent(_eventAfterVisit);    }
繪製未變化或剛剛設定掛載的渲染樹(在跟渲染相關時,我比較喜歡把scene成為渲染樹,因為它就是rootNode,在遊戲引擎為單視窗單線程中只有一個Scene)

//下面這段代碼是完成彈窗通知,因為其繪製永遠在主渲染樹之後,所以永遠會遮蓋當前的遊戲主映像// draw the notifications node    if (_notificationNode)    {        _notificationNode->visit(_renderer, Mat4::IDENTITY, false);    }//設定debug狀態下的OpenGL繪製資訊,不影響遊戲情節    if (_displayStats)    {        showStats();    }

接下來又是一句新代碼:

 _renderer->render();

待續!!!繪製命令的執行。



首先得說一句:在2.x中OpenGL的繪製是使用立即模式繪製,所以才有SpriteBatchNode這個產物

立即模式下是直接在引擎的每個渲染節點中使用OpenGL的繪製命令,所以會有這樣類似的代碼:kmGLPushMatrix、kmGLPopMatrix 管理OpenGL的狀態。

   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();

但是這個是v2版本的代碼。v3中使用的OpenGL 可以參考這個文章:點擊開啟連結

v3用的是顯示列表模式(不再需要我們自己去使用OpenGL的命令,因為已經有封裝好立即模式的DrawPrimitives了)


繪製完之後:

<span style="white-space:pre"></span>_eventDispatcher->dispatchEvent(_eventAfterDraw);//派發繪製結束的事件,在OpenGL繪製之後主線程回到cocos引擎中    <span style="white-space:pre"></span>popMatrix(MATRIX_STACK_TYPE::MATRIX_STACK_MODELVIEW); //回複原先的opengl狀態

雙緩衝,切換緩衝區,準備進入下一張畫布的繪製。

// swap buffers    if (_openGLView)    {        _openGLView->swapBuffers();    }    if (_displayStats)    {        calculateMPF();    }



誰可以幫忙解決在W7下vs2013搭建cocos2d-x33的問題,新手助

你如果英文好的話,你看下cocos2d-x-3.3主目錄下的README.md檔案,你基本也就知道怎麼做了。提前要安裝的軟體有NDK、ANT。如果要搞Android的話,還要JDK,SDK,ADT,eclipse。
然後,cmd視窗下,進去解壓後的cocos2d-x-3.3目錄,按README.md檔案所說,運行如下命令:
//下面三步初次運行時設定PATH變數用的
//中途如果檢測到你有一些環境變數沒設定的話,會讓你輸入
//如:COCOS_CONSOLE_ROOT(cocos2d-x-3.3/tools/cocos2d-console/bin目錄)
//還有ANT, NDK的安裝路徑等
//windows的話,第三條命令可以不用
$ cd cocos2d-x-3.3
$ ./setup.py
$ source FILE_TO_SAVE_SYSTEM_VARIABLE

//下面兩步在建立新工程的命令
$ cocos new MyGame -p com.your_company.mygame -l cpp -d NEW_PROJECTS_DIR
$ cd NEW_PROJECTS_DIR/MyGame

建好工程後,去工程目錄下,開啟proj.win32檔案夾中副檔名為sln的檔案就行了。
 
cocos2d-x 可以不可以實現情境的部分切換, 或者是 Director 可以同時調用多個情境

在情境中用一個CCLayer做背景什麼的
再用CCLayerMultiplex用作可切換層
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.