游戏开发记录120811:cocos2d-x引擎学习---目录结构及主要类

1-Cocos2d-x目录结构

Cocos2d-x的目录结构如下:

游戏开发记录120811:cocos2d-x引擎学习---目录结构及主要类_第1张图片

游戏开发记录120811:cocos2d-x引擎学习---目录结构及主要类_第2张图片

目录的具体结构介绍如下:

Box2D:物理引擎Box2D的相关源文件

Chipmunk:物理引擎chipmunk的相关源文件

cocos2dx:cocos2d-x引擎的核心部分,存放了引擎的大部分源文件

CocosDenshion:声音模块相关源文件

Debug.win32:在Windows 上调试输出目录

Doxygen:生成doxygen项目文档时需要的配置文件

HelloLua:在游戏中使用lua的示例代码

HelloWorld:测试代码helloworld

Js:cocos2d-x js脚本支持源码目录

Licenses:许可文件存放目录

Lua:脚本语言lua支持的相关源文件

Template:包括编译iOS和Android等平台开发时需要的配置文件

Testjs:cocos2d-x引擎各平台js语言的api示例代码

Tests:cocos2d-x引擎所有api的示例代码

Tools:包括"tolua的配置文件"和“xcode4的模板生成工具”

build-win32.bat:编译cocos2d-x引擎的windwos项目脚本

cocos2d-win32.vc2008.sln:windows项目vs2008解决方案文件

cocos2d-win32.vc2010.sln:windows项目vs2010解决方案文件

create-android-project.bat :在windows上面创建android项目的脚本

create-android-project.sh:在linux上创建android的脚本

install-templates-msvc.bat:创建vs2010或vs2008的工程模板脚本

install-templates-xcode.sh:创建xcode工程模板的脚本

 

2-Cocos2d-x主要类

2.1 框架类(AppDelegate(CCApplication)(应用程序主流程)->CCEGLView(创建窗口)->CCDisplayLinkDirector(CCDirector)(主循环)->CCScene(场景)->推动力:事件(时间事件、帧事件、渲染事件)、CCScheduler、CCTimer、CCLayer、CCNode)

1).CCApplication与AppDelegate(单例)

CCApplication是AppDelegate的一个抽象基类。主要初始化OpenGL示例初始化,设置资源路径等。

- virtual bool applicationDidFinishLaunching() = 0;//在AppDelegate中实现,初始化CCDirector 和CCScene

-virtual void applicationDidEnterBackground() = 0;//在AppDelegate中实现,游戏退至后台时调用

-virtual void applicationWillEnterForeground() = 0;//在AppDelegate中实现,游戏恢复前台时调用

2).CCTimer与CCScheduler(单例)

CCScheduler是游戏的调度器,主要是根据CCTimer定时器触发the scheduled callbacks。我门应该避免使用CCTimer,而是此类。CCScheduler主要的触发事件有两种:

- update selector: the 'update' selector will be called every frame. You can customize the priority.//更新事件回调
- custom selector: A custom selector will be called every frame, or with a custom interval of time//自定义事件回调

游戏开发记录120811:cocos2d-x引擎学习---目录结构及主要类_第3张图片

游戏开发记录120811:cocos2d-x引擎学习---目录结构及主要类_第4张图片

(此图是来至Silverlight游戏调度器开发(刘振宁))

此类就是根据定时器,遍历自己维护的游戏对象队列,执行各游戏动画。比如可以通过 void setTimeScale(ccTime fTimeScale) { m_fTimeScale = fTimeScale; }修改时间倍数,使游戏对象加速或者减速实现快慢镜头。

3).CCDirector与CCDisplayLinkDirector(单例)

有四种Director:

    - kCCDirectorTypeNSTimer (default)
    - kCCDirectorTypeMainLoop
    - kCCDirectorTypeThreadMainLoop
    - kCCDirectorTypeDisplayLink

每种Director都有他自身的利弊。但是现在cocos2d-x仅支持kCCDirectorTypeDisplayLink

CCDirector类是一个抽象类(virtual void mainLoop(void) = 0;),主要是创建和处理主窗口和管理怎么样和什么时候执行场景,主要职责:

  - initializing the OpenGL context//初始化OpenGL上下文
  - setting the OpenGL pixel format (default on is RGB565)//设置OpenGL像素格式默认是RGB565
  - setting the OpenGL buffer depth (default one is 0-bit)//设置OpenGL缓冲深度默认是0bit
  - setting the projection (default one is 3D)//设置投影(默认值是3D)
  - setting the orientation (default one is Protrait)

由于CCDirector是一个单例,标准的使用方式:CCDirector::sharedDirector()->methodName();

CCDirector也设置了默认的OpenGL上下文:

  - GL_TEXTURE_2D is enabled
  - GL_VERTEX_ARRAY is enabled
  - GL_COLOR_ARRAY is enabled
  - GL_TEXTURE_COORD_ARRAY is enabled

CCDisplayLinkDirector实现了(virtual void mainLoop(void) = 0;),是一个以显示刷新速度同步定时器的Director,我们程序中使用的就是这个Director。主要特性和限制:

  - Scheduled timers & drawing are synchronizes with the refresh rate of the display//调度定时器和渲染和FBS同步
  - Only supports animation intervals of 1/60 1/30 & 1/15//当前仅支持60、30、15帧动画

游戏开发记录120811:cocos2d-x引擎学习---目录结构及主要类_第5张图片

4).CCEGLView

CCEGLView窗口类,主要用来创建窗口,接收窗口事件,调节窗口大小等。

5).CCScene、CCLayer、CCSprite与CCAction、CCActionManager

5.1 class CC_DLL CCLayer : public CCNode, public CCTouchDelegate, public CCAccelerometerDelegate, public CCKeypadDelegate

CCLayer是CCNode的子类同时实现了TouchEventsDelegate协议,其主要特性:

-接收iphone的Touch事件

-接收Accelerometer 输入


5.2 class CC_DLL CCScene : public CCNode

CCScene is a subclass of CCNode that is used only as an abstract concept.

CCScene an CCNode are almost identical with the difference that CCScene has it's
anchor point (by default) at the center of the screen.

For the moment CCScene has no other logic than that, but in future releases it might have
additional logic.

It is a good practice to use and CCScene as the parent of all your nodes.

5.3 class CC_DLL CCSprite : public CCNode, public CCTextureProtocol, public CCRGBAProtocol

* CCSprite can be created with an image, or with a sub-rectangle of an image.
*
* If the parent or any of its ancestors is a CCSpriteBatchNode then the following features/limitations are valid
*    - Features when the parent is a CCBatchNode:
*        - MUCH faster rendering, specially if the CCSpriteBatchNode has many children. All the children will be drawn in a single batch.
*
*    - Limitations
*        - Camera is not supported yet (eg: CCOrbitCamera action doesn't work)
*        - GridBase actions are not supported (eg: CCLens, CCRipple, CCTwirl)
*        - The Alias/Antialias property belongs to CCSpriteBatchNode, so you can't individually set the aliased property.
*        - The Blending function property belongs to CCSpriteBatchNode, so you can't individually set the blending function property.
*        - Parallax scroller is not supported, but can be simulated with a "proxy" sprite.
*
*  If the parent is an standard CCNode, then CCSprite behaves like any other CCNode:
*    - It supports blending functions
*    - It supports aliasing / antialiasing
*    - But the rendering will be slower: 1 draw per children.
*
* The default anchorPoint in CCSprite is (0.5, 0.5).

5.4 CCAction、CCActionManager

CCActionManager的初始化和清理,维护这一组Action列表信息。在初始化的时候,manager被添加进了CCSechedule的更新列表中。清除的时候,manager从CCSechedule中被清除。

游戏开发记录120811:cocos2d-x引擎学习---目录结构及主要类_第6张图片

游戏开发记录120811:cocos2d-x引擎学习---目录结构及主要类_第7张图片

游戏开发记录120811:cocos2d-x引擎学习---目录结构及主要类_第8张图片

6).CCNode

CCNode是一个主要元素,任何需要绘制和包涵子节点的对象都需要继承CCNode。常用的CCNode之类有:CCScene, CCLayer, CCSprite, CCMenu.

CCNode主要特性:

     - They can contain other CCNode nodes (addChild, getChildByTag, removeChild, etc)//子节点的增删改查等管理
     - They can schedule periodic callback (schedule, unschedule, etc)//定时调度回调
     - They can execute actions (runAction, stopAction, etc)//执行动画相关操作(运行,暂停,停止)

CCNode的子类常常需要:

     - overriding init to initialize resources and schedule callbacks//重写init函数用于初始化资源和scheule回调函数
     - create callbacks to handle the advancement of time//创建回调函数处理时间
     - overriding draw to render the node//重写draw函数 渲染节点

游戏开发记录120811:cocos2d-x引擎学习---目录结构及主要类_第9张图片

    void scheduleSelector(SEL_SCHEDULE pfnSelector, CCObject *pTarget, ccTime fInterval, bool bPaused);

     scheduleSelector method will be called every 'interval' seconds.
     If paused is YES, then it won't be called until it is resumed.
     If 'interval' is 0, it will be called every frame, but if so, it recommened to use 'scheduleUpdateForTarget:' instead.
     If the selector is already scheduled, then only the interval parameter will be updated without re-scheduling it again

   

游戏开发记录120811:cocos2d-x引擎学习---目录结构及主要类_第10张图片

     void scheduleUpdateForTarget(CCObject *pTarget, int nPriority, bool bPaused);

     Schedules the 'update' selector for a given target with a given priority.
     The 'update' selector will be called every frame.
     The lower the priority, the earlier it is called.

通过CCNode的scheduleUpdate和scheduleSelector将自己或者selector加入到CCScheduler维护的队列中。如下图:

 

游戏开发记录120811:cocos2d-x引擎学习---目录结构及主要类_第11张图片

各种selector:

游戏开发记录120811:cocos2d-x引擎学习---目录结构及主要类_第12张图片


    

现在总结下流程:

主流程:

游戏开发记录120811:cocos2d-x引擎学习---目录结构及主要类_第13张图片

CCApplication中的run()中执行CCDirector中的mainloop:

image

在mainloop的drawscene函数中,执行CCScheduler的tick();

游戏开发记录120811:cocos2d-x引擎学习---目录结构及主要类_第14张图片

在tick中,foreach遍历游戏对象列表或者selector列表、CCActionManger并更新update或者自定义的selector

游戏开发记录120811:cocos2d-x引擎学习---目录结构及主要类_第15张图片

 

在整个流程中,CCSchedule调度器可以说起到了心脏的作用,(定时器产生的时间事件,touch事件,windows事件等)为游戏的运行提供动力。

粗粗的学习了一边coco2d-x的框架,如有错误希望指点,共同进步……

参考文档地址:http://cn.cocos2d-x.org/document

你可能感兴趣的:(游戏开发记录120811:cocos2d-x引擎学习---目录结构及主要类)