画的不是太清楚,这里详细描述下:
AppDelegate是一个单例类。表示该应用的。入口为:main.cpp
#
include
"main.h"
# include "AppDelegate.h"
USING_NS_CC; //#defineUSING_NS_CC usingnamespacecocos2d
int APIENTRY_tWinMain(HINSTANCEhInstance,
HINSTANCEhPrevInstance,
LPTSTRlpCmdLine,
int nCmdShow)
{
//UNREFERENCED_PARAMETER用于在VC编译器下告知编译器,不必检测改变量是否使用的警告。
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
//创建一个cocos2dx单例类
AppDelegateapp;
return Application : : getInstance() - > run();
}
# include "AppDelegate.h"
USING_NS_CC; //#defineUSING_NS_CC usingnamespacecocos2d
int APIENTRY_tWinMain(HINSTANCEhInstance,
HINSTANCEhPrevInstance,
LPTSTRlpCmdLine,
int nCmdShow)
{
//UNREFERENCED_PARAMETER用于在VC编译器下告知编译器,不必检测改变量是否使用的警告。
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
//创建一个cocos2dx单例类
AppDelegateapp;
return Application : : getInstance() - > run();
}
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,204)">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, 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 Consoleif (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,没仔细了解。