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,看下这里做了什么初始化的操作

  1.     // As an example, load config file
  2.     // XXX: This should be loaded before the Director is initialized,
  3.     // XXX: but at this point, the director is already initialized
  4.     Configuration::getInstance()->loadConfigFile("configs/config-example.plist");
  1.     // initialize director
  2.     auto director = Director::getInstance();
  3.     auto glview = director->getOpenGLView();
  4.     if(!glview) {
  5.         glview = GLView::create("Cpp Tests");
  6.         director->setOpenGLView(glview);
  7.     }
  8.     director->setDisplayStats(true);
  9.     director->setAnimationInterval(1.0 / 60);
  10.     auto screenSize = glview->getFrameSize();
  11.     auto designSize = Size(480, 320);
  12.     auto fileUtils = FileUtils::getInstance();
  13.     std::vector<std::string> searchPaths;
  14.     
  15.     if (screenSize.height > 320)
  16.     {
  17.         auto resourceSize = Size(960, 640);
  18.         searchPaths.push_back("hd");
  19.         searchPaths.push_back("ccs-res/hd");
  20.         searchPaths.push_back("ccs-res/hd/scenetest");
  21.         searchPaths.push_back("ccs-res/hd/scenetest/ArmatureComponentTest");
  22.         searchPaths.push_back("ccs-res/hd/scenetest/AttributeComponentTest");
  23.         searchPaths.push_back("ccs-res/hd/scenetest/BackgroundComponentTest");
  24.         searchPaths.push_back("ccs-res/hd/scenetest/EffectComponentTest");
  25.         searchPaths.push_back("ccs-res/hd/scenetest/LoadSceneEdtiorFileTest");
  26.         searchPaths.push_back("ccs-res/hd/scenetest/ParticleComponentTest");
  27.         searchPaths.push_back("ccs-res/hd/scenetest/SpriteComponentTest");
  28.         searchPaths.push_back("ccs-res/hd/scenetest/TmxMapComponentTest");
  29.         searchPaths.push_back("ccs-res/hd/scenetest/UIComponentTest");
  30.         searchPaths.push_back("ccs-res/hd/scenetest/TriggerTest");
  31.         searchPaths.push_back("ccs-res");
  32.         director->setContentScaleFactor(resourceSize.height/designSize.height);
  33.     }
  34.     else
  35.     {
  36.         searchPaths.push_back("ccs-res");
  37.         searchPaths.push_back("ccs-res/scenetest/ArmatureComponentTest");
  38.         searchPaths.push_back("ccs-res/scenetest/AttributeComponentTest");
  39.         searchPaths.push_back("ccs-res/scenetest/BackgroundComponentTest");
  40.         searchPaths.push_back("ccs-res/scenetest/EffectComponentTest");
  41.         searchPaths.push_back("ccs-res/scenetest/LoadSceneEdtiorFileTest");
  42.         searchPaths.push_back("ccs-res/scenetest/ParticleComponentTest");
  43.         searchPaths.push_back("ccs-res/scenetest/SpriteComponentTest");
  44.         searchPaths.push_back("ccs-res/scenetest/TmxMapComponentTest");
  45.         searchPaths.push_back("ccs-res/scenetest/UIComponentTest");
  46.         searchPaths.push_back("ccs-res/scenetest/TriggerTest");
  47.     }
  48.     
  49.     fileUtils->setSearchPaths(searchPaths);
  50. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
  51.     // a bug in DirectX 11 level9-x on the device prevents ResolutionPolicy::NO_BORDER from working correctly
  52.     glview->setDesignResolutionSize(designSize.width, designSize.height, ResolutionPolicy::SHOW_ALL);
  53. #else
  54.     glview->setDesignResolutionSize(designSize.width, designSize.height, ResolutionPolicy::NO_BORDER);
  55. #endif
  56.     auto scene = Scene::create();
  57.     auto layer = new TestController();
  58. #if (CC_TARGET_PLATFORM != CC_PLATFORM_WP8) && (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
  59.     layer->addConsoleAutoTest();
  60. #endif
  61.     layer->autorelease();
  62.     layer->addConsoleAutoTest();
  63.     scene->addChild(layer);
  64.     director->runWithScene(scene);
  65.     // Enable Remote Console
  66. #if (CC_TARGET_PLATFORM != CC_PLATFORM_WINRT)
  67.     auto console = director->getConsole();
  68.     console->listenOnTCP(5678);
  69.     Configuration *conf = Configuration::getInstance();
  70.     bool isAutoRun = conf->getValue("cocos2d.x.testcpp.autorun", Value(false)).asBool();
  71.     if(isAutoRun)
  72.     {
  73.         layer->startAutoRun();
  74.     }
  75. #endif
  76.     
  77.     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和匿名函数的用法。还有事件处理的方法。看下菜单项的事件处理:
  1. void TestController::menuCallback(Ref * sender)
  2. {
  3. Director::getInstance()->purgeCachedData();
  4.     // get the userdata, it's the index of the menu item clicked
  5.     auto menuItem = static_cast<MenuItem *>(sender);
  6.     int idx = menuItem->getLocalZOrder() - 10000;
  7.     // create the test scene and run it
  8.     auto scene = g_aTestNames[idx].callback();
  9.     if (scene)
  10.     {
  11.         scene->runThisTest();
  12.         scene->release();
  13.     }
  14. }
Director::getInstance()->purgeCachedData();清除缓存的数据。然后执行这个Scene的方法runThisTest()方法。看下ActionsTestScene的runThisScene方法把:
  1. void ActionsTestScene::runThisTest()
  2. {
  3. sceneIdx = -1;
  4. addChild(nextAction());
  5. Director::getInstance()->replaceScene(this);
  6. }
replaceScene之后要执行这个scene对象的release方法。

posted @ 2015-08-08 09:20  Anzhongliu  阅读(349)  评论(0编辑  收藏  举报