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();
}
#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方法。