HelloWorld空项目的大概流程理解(仅是简单认识,初学者)
主函数_tWinMain入口
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// create the application instance
AppDelegate app;
CCEGLView* eglView = CCEGLView::sharedOpenGLView();
eglView->setViewName("HelloWorld");
eglView->setFrameSize(480, 320);
return CCApplication::sharedApplication()->run();
}
函数体前两行跟windows平台相关的代码,
大概流程:
1、创建这个实例 app,主要执行了AppDelegate 父类 CCApplication 的构造函数,看似是只跟windows平台相关代码,可能初始化跟窗口有关工作,暂时不讨论。
2、程序启动自动调用:AppDelegate::applicationDidFinishLaunching()
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
CCDirector* pDirector = CCDirector::sharedDirector();
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
//......忽略中间处理
// create a scene. it's an autorelease object
CCScene *pScene = HelloWorld::scene();
// run
pDirector->runWithScene(pScene);
return true;
}
2、1在这个函数applicationDidFinishLaunching()中分别创建了CCDirector实例pDirector、、CCEGLView实例pEGLView(都是通过静态方法创建,紧接创建一个CCScene实例: CCScene *pScene = HelloWorld::scene(); 然后通过pDirector->runWithScene(pScene); 再将这个实例pScene加入pDirector,
3、 在HelloWorld::scene()中创建一个场景CCScene的实例场景scene, 通过HelloWorld::create()创建一个HelloWorld的实例层layer,然后再将这个层layer加入到这个场景scene中。
CCScene* HelloWorld::scene()
{
// 'scene' is an autorelease object
CCScene *scene = CCScene::create();
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
4、HelloWorld::create()中方法create是一个宏定义,同样也在这个函数中调用了HelloWorld::init()
#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; \
} \
}
note: 查看这个方法可以在HelloWorld::create()点击转到定义--> CREATE_FUNC(HelloWorld)->#define CREATE_FUNC(__TYPE__)
5、 回到主函数,创建一 CCEGLView实例OpenGL视图eglView,并设置其视图名称和大小
调用CCApplication::sharedApplication()->run(),使整个程序run起来,可以发现这是一个死循环,让程序一直run.
int CCApplication::run()
{
//......
while (1)
{
//...
}
return (int) msg.wParam;
}
小结一下大概流程:
AppDelegate app -> CCApplication() -> AppDelegate::applicationDidFinishLaunching() - > HelloWorld::scene() -> HelloWorld::create() -> HelloWorld::init()
-> CCApplication::sharedApplication()->run()
如果我们自定义类,一般是写场景Scene,继承CCLayer / CCScene,实现游戏场景渲染。
那么在这个流程中一般只需要修改 (我们自定义的类):scene()
HelloWorld详细jie'shao: http://www.cnblogs.com/hll2008/p/4232426.html
理解宏USING_NS_CC:http://blog.csdn.net/dionysos_lai/article/details/19401777
函数调用(主要是思路):http://blog.csdn.net/ironyoung/article/details/39546009
cocos2d-x 元素大概了解:
导演,场景,层,精灵模型
1、CCDirector导演
单例类,通过CCDirector::sharedDirector函数去获取导演对象
CCDirector* d = new CCDirector() // error,单例对象不应该自己去创建
1)导演负责初始化环境
2)导演负责推动游戏进程 runWithScene, replaceScene, pushScene, popScene
3) 导演负责坐标变换
2、CCScene 场景:一幕戏
从cocos2dx数据结构来收,它是一个渲染树的根节点
任一时刻,只有一个Scene在运行
场景是一个容器
3、CCLayer 层:
从逻辑上划分游戏元素的容器,
某些移动功能要求,必须将一些元素组合在一个层中
人机交互接口的响应都是由CCLayer来完成(由虚函数实现)
一般从CCLayer派生出自己的Layer类,来增加代码,实现游戏元素的渲染
4、CCSprite 精灵
主要是用来体现游戏元素的对象
------在游戏设计中-----
一般游戏由多个场景组成