Skip to content

cocos2d x 3.3rc2 001 hello world 之 cpp empty test

cheyiliu edited this page Jan 31, 2015 · 1 revision

工程结构

cpp-empty-test
	Classes  // 公共代码, 或平台无关代码
	proj.android // proj.xxx 是支持的各平台入口代码
	proj.ios
	proj.linux
	proj.mac
	proj.win8.1-universal
	proj.win32
	proj-wp8-xaml
	Resources  // 资源
	CMakeLists.txt // cmake配置文件

  • class AppDelegate : private cocos2d::Application

    • 负责app启动后游戏场景的加载
    • 负责app前后切换的相关业务逻辑, 如停止后台音乐
  • class HelloWorld : public cocos2d::Layer

    • 游戏场景的某层-layer,包括一个label, sprite和menu
    • 这个类中用到了一个宏, CREATE_FUNC(HelloWorld);

定义在cocos2d-x-3.3rc2/cocos/platform/CCPlatformMacros.h 看了定义就很明白了, 两段构造 /**

  • define a create function for a specific type, such as Layer
  • @param _TYPE_ class type to add create(), such as Layer / #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;
    }
    }
    
    
  • AppMacros.h
    • 相关宏定义

流程, 以proj.linux为例

  1. int main(int argc, char **argv)

  2. 生成AppDelegate app;

  3. 调用Application::getInstance()->run();//也即是上一步的app对象的run方法, 单例模式。 最终调用到CCApplication-linux.cpp(为何是这里见cocos2d.h)的run方法, 该方法做的工作如下

    1. 调用AppDelegate::applicationDidFinishLaunching(), 加载场景
    2. 开始事件循环
    while (!glview->windowShouldClose())
    {
      lastTime = getCurrentMillSecond();
    
      director->mainLoop();
      glview->pollEvents();
    
      curTime = getCurrentMillSecond();
      if (curTime - lastTime < _animationInterval)
      {
          usleep((_animationInterval - curTime + lastTime)*1000);
      }
    }
    
  4. 各种事件回调

拓展

  • cpp-tests工程本质和cpp-empty-test没区别, 只不过多了很多场景用于各种test。 提供了接口(class BaseTest : public cocos2d::Layer class TestScene : public Scene )用于统一协调各种测试场景

  • proj.android, cocos作了不少封装, 待分析TODO

  • 如何加log, 按照这个guide, 打开debug宏, 添加log, 重新编译apk, 验证成功, http://www.cocos2d-x.org/wiki/How_to_use_CCLOG

D/cocos2d-x debug info(31281): AppDelegate::applicationDidFinishLaunching(): 28 D/cocos2d-x debug info(31281): HelloWorld::scene(): 9 D/cocos2d-x debug info(31281): HelloWorld::init(): 26 D/cocos2d-x debug info(31281): cocos2d: fullPathForFilename: No file found at Arial. Possible missing file. D/cocos2d-x debug info(31281): AppDelegate::applicationDidEnterBackground(): 98 D/cocos2d-x debug info(31281): AppDelegate::applicationWillEnterForeground(): 107 D/cocos2d-x debug info(31281): HelloWorld::menuCloseCallback(): 83 D/Process (31281): org.cocos2dx.lib.Cocos2dxHelper.terminateProcess(Cocos2dxHelper.java:298) D/Process (31281): org.cocos2dx.lib.Cocos2dxRenderer.nativeRender(Native Method) D/Process (31281): org.cocos2dx.lib.Cocos2dxRenderer.onDrawFrame(Cocos2dxRenderer.java:91) I/ActivityManager( 281): Process org.cocos2dx.cpp_empty_test (pid 31281) has died.

* 加log也可以直接用log方法```log("%p", this); ```

# 小结
* 对coco工程的各部分有大概了解
* Application为各大平台的程序入口提供抽象
* Scene,场景,容器,It is a good practice to use a Scene as the parent of all your nodes.
* Layer,涂层,Layer is a subclass of Node that implements the TouchEventsDelegate protocol.
* Director,总导演,Class that creates and handles the main Window and manages how
and when to execute the Scenes. The Director is also responsible for openGL相关设置。
Clone this wiki locally