cocos2dx-cpptest的結構

畫的不是太清楚,這裏詳細描述下:
AppDelegate是一個單例類。表示該應用的。入口爲:main.cpp
# include   "main.h"
# include   "AppDelegate.h"
USING_NS_CC;    //#define USING_NS_CC   using namespace cocos2d  
int  APIENTRY _tWinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPTSTR    lpCmdLine,
                        int        nCmdShow)
{
    //UNREFERENCED_PARAMETER用於在VC編譯器下告知編譯器,沒必要檢測改變量是否使用的警告。
    UNREFERENCED_PARAMETER(hPrevInstance); 
    UNREFERENCED_PARAMETER(lpCmdLine);
     //建立一個cocos2dx單例類
    AppDelegate app;
     return  Application : : getInstance() - > run();
}

當run方法執行後,通過一系列的處理,系統會調用AppDelegate的applicationDidFinishLaunching方法。這裏有必要講下AppDelegate中的3個方法:
applicationDidFinishLaunching應用啓動會執行該方法。
applicationDidEnterBackground 應用暫停會執行該方法.
applicationWillEnterForeground當應用恢復運行會執行該方法。

進入applicationDidFinishLaunching,看下這裏作了什麼初始化的操做
   
   
   
   
   

    // As an example, load config file    // XXX: This should be loaded before the Director is initialized,    // XXX: but at this point, the director is already initialized    Configuration::getInstance()->loadConfigFile("configs/config-example.plist");    // initialize director    auto director = Director::getInstance();    auto glview = director->getOpenGLView();    if(!glview) {        glview = GLView::create("Cpp Tests");        director->setOpenGLView(glview);    }    director->setDisplayStats(true);    director->setAnimationInterval(1.0 / 60);    auto screenSize = glview->getFrameSize();    auto designSize = Size(480, 320);    auto fileUtils = FileUtils::getInstance();    std::vector<std::string> searchPaths;        if (screenSize.height > 320)    {        auto resourceSize = Size(960, 640);        searchPaths.push_back("hd");        searchPaths.push_back("ccs-res/hd");        searchPaths.push_back("ccs-res/hd/scenetest");        searchPaths.push_back("ccs-res/hd/scenetest/ArmatureComponentTest");        searchPaths.push_back("ccs-res/hd/scenetest/AttributeComponentTest");        searchPaths.push_back("ccs-res/hd/scenetest/BackgroundComponentTest");        searchPaths.push_back("ccs-res/hd/scenetest/EffectComponentTest");        searchPaths.push_back("ccs-res/hd/scenetest/LoadSceneEdtiorFileTest");        searchPaths.push_back("ccs-res/hd/scenetest/ParticleComponentTest");        searchPaths.push_back("ccs-res/hd/scenetest/SpriteComponentTest");        searchPaths.push_back("ccs-res/hd/scenetest/TmxMapComponentTest");        searchPaths.push_back("ccs-res/hd/scenetest/UIComponentTest");        searchPaths.push_back("ccs-res/hd/scenetest/TriggerTest");        searchPaths.push_back("ccs-res");        director->setContentScaleFactor(resourceSize.height/designSize.height);    }    else    {        searchPaths.push_back("ccs-res");        searchPaths.push_back("ccs-res/scenetest/ArmatureComponentTest");        searchPaths.push_back("ccs-res/scenetest/AttributeComponentTest");        searchPaths.push_back("ccs-res/scenetest/BackgroundComponentTest");        searchPaths.push_back("ccs-res/scenetest/EffectComponentTest");        searchPaths.push_back("ccs-res/scenetest/LoadSceneEdtiorFileTest");        searchPaths.push_back("ccs-res/scenetest/ParticleComponentTest");        searchPaths.push_back("ccs-res/scenetest/SpriteComponentTest");        searchPaths.push_back("ccs-res/scenetest/TmxMapComponentTest");        searchPaths.push_back("ccs-res/scenetest/UIComponentTest");        searchPaths.push_back("ccs-res/scenetest/TriggerTest");    }        fileUtils->setSearchPaths(searchPaths);#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)    // a bug in DirectX 11 level9-x on the device prevents ResolutionPolicy::NO_BORDER from working correctly    glview->setDesignResolutionSize(designSize.width, designSize.height, ResolutionPolicy::SHOW_ALL);#else    glview->setDesignResolutionSize(designSize.width, designSize.height, ResolutionPolicy::NO_BORDER);#endif    auto scene = Scene::create();    auto layer = new TestController();#if (CC_TARGET_PLATFORM != CC_PLATFORM_WP8) && (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)    layer->addConsoleAutoTest();#endif    layer->autorelease();    layer->addConsoleAutoTest();    scene->addChild(layer);    director->runWithScene(scene);    // Enable Remote Console#if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)    auto console = director->getConsole();    console->listenOnTCP(5678);    Configuration *conf = Configuration::getInstance();    bool isAutoRun = conf->getValue("cocos2d.x.testcpp.autorun", Value(false)).asBool();    if(isAutoRun)    {        layer->startAutoRun();    }#endif        return true;
這裏作了那些操做:
1:初始化Configuration配置對象,並加載配置文件。這必定要在Director對象初始化以前完成
2:初始化Director對象 ,並初始化OpenGLView對象,設置到Director裏面。
3:作屏幕適配。 resourceSize經過setContentScaleFactor適配到designSize,若是隻有一套資源,那麼resouceSize和designSize相同,
designSize經過setDesignResolutionSize適配到screenSize。咱們後面使用的position都是相對designSize而言的。
4:而後就是初始化layer,scene,而後運行這個scene. 注意   director->runWithScene(scene);會使得scene和layer的引用計數加1。 layer->autorelease();
5:剩下的是Console,沒仔細瞭解。

如今程序進入TestController::TestController(),在這裏初始化menu. 學習了c++11的function和匿名函數的用法。還有事件處理的方法。看下菜單項的事件處理:
   
   
   
   
   
void TestController::menuCallback(Ref * sender){ Director::getInstance()->purgeCachedData();    // get the userdata, it's the index of the menu item clicked    auto menuItem = static_cast<MenuItem *>(sender);    int idx = menuItem->getLocalZOrder() - 10000;    // create the test scene and run it    auto scene = g_aTestNames[idx].callback();    if (scene)    {        scene->runThisTest();        scene->release();    }}
Director :: getInstance ()-> purgeCachedData (); 清除緩存的數據。而後執行這個Scene的方法runThisTest()方法。看下ActionsTestScene的runThisScene方法把:
    
    
    
    
    
void ActionsTestScene::runThisTest() { sceneIdx = -1; addChild(nextAction()); Director::getInstance()->replaceScene(this); }
replaceScene以後要執行這個scene對象的release方法。


相關文章
相關標籤/搜索