标签:
在前一节中已经搭建好了Cocos2d-x 3.2开发环境,现在就创建第一个项目:
cocos new HelloWorld -l cpp (-p com.ivae.hellocc -d .)
用VS打开项目,编译运行,运行成功!

1.在win32文件夹下找到Windows平台的入口函数main.cpp中的winMain函数
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
AppDelegate app;
return Application::getInstance()->run();
}
在WinMain函数中定义了一个AppDelegate对象,将先后调用其父类Application和AppDelegate的构造函数,在Application()函数中将程序实例的指针this,保存到静态成员变量sm_pSharedApplication中,以后可以通过调用其静态成员函数getInstance()来获得程序实例,这是单例模式的典型作法。
2.紧接着程序回到main()中运行
return Application::getInstance()->run()
将调用Application::run(),主要代码如下:
if (!applicationDidFinishLaunching())
{
return 0;
}
接着执行
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
glview = GLView::create("My Game");
director->setOpenGLView(glview);
}
// turn on display FPS
director->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don‘t call this
director->setAnimationInterval(1.0 / 60);
// create a scene. it‘s an autorelease object
auto scene = HelloWorld::createScene();
// run
director->runWithScene(scene);
return true;
}
在applicationDidFinishLaunching()中设置了一下基本参数,创建并运行了第一个场景。
3.来分析一下场景创建的代码
Scene* HelloWorld::createScene()
{
// ‘scene‘ is an autorelease object
auto scene = Scene::create();
// ‘layer‘ is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
在函数中创建了一个场景和一个HelloWorld层,在HelloWorld类中通过CREATE_FUNC(HelloWorld)宏,自动创建了一个create()函数,这样可以实现内存自动管理
#define CREATE_FUNC(__TYPE__) static __TYPE__* create() { __TYPE__ *pRet = new __TYPE__(); if (pRet && pRet->init()) { pRet->autorelease(); return pRet; } else { delete pRet; pRet = NULL; return NULL; } }
在create()函数中调用init()函数,所以init()是HelloWorld实际的初始化函数。
用一幅程序执行流程图,可以更好的理解程序的执行过程,希望本文能帮助你理清一点点思绪,我也是刚接触cocos2d-x,借着笔记整理下自己的所思所想,如有不正确的地方,还请指出。

Cocos2d-x 3.2入门之二:深入分析HelloWorld
标签:
原文地址:http://www.cnblogs.com/ivae/p/4265472.html