zoukankan      html  css  js  c++  java
  • 学习cocos 空程序

    今天开始学习cocos代码,首先研究源码中的空程序。

    在这个程序中,在main函数中,创建了一个Application:
    1. int APIENTRY _tWinMain(HINSTANCE hInstance,
    2. HINSTANCE hPrevInstance,
    3. LPTSTR lpCmdLine,
    4. int nCmdShow)
    5. {
    6. UNREFERENCED_PARAMETER(hPrevInstance);
    7. UNREFERENCED_PARAMETER(lpCmdLine);
    8. // create the application instance
    9. AppDelegate app;
    10. return Application::getInstance()->run();
    11. }
    其中AppDelegate继承自Application。这里明显使用了单例模式,AppDelegate构造时,Application内部的静态成员指针被赋值:
    1. Application::Application()
    2. : _instance(nullptr)
    3. , _accelTable(nullptr)
    4. {
    5. _instance = GetModuleHandle(nullptr);
    6. _animationInterval.QuadPart = 0;
    7. CC_ASSERT(! sm_pSharedApplication);
    8. sm_pSharedApplication = this;
    9. }
    其中sm_pSharedApplication即为静态成员。之后getInstance函数,便是返回了这个成员:
    1. Application* Application::getInstance()
    2. {
    3. CC_ASSERT(sm_pSharedApplication);
    4. return sm_pSharedApplication;
    5. }
    在main函数调用run之后,AppDelegate::applicationDidFinishLaunching会被调用,这个函数是一个虚函数,需要由用户指定实现。
    在这个函数中,用户需要创建自己的场景。具体做法是,获取一个Director指针(Director也是单例模式),最后调用Director::runWithScene函数,把用户自定义的Scene加入:
    1. bool AppDelegate::applicationDidFinishLaunching() {
    2. // initialize director
    3. auto director = Director::getInstance();
    4. auto glview = director->getOpenGLView();
    5. if(!glview) {
    6. glview = GLViewImpl::create("Cpp Empty Test");
    7. director->setOpenGLView(glview);
    8. }
    9. director->setOpenGLView(glview);

    10. // turn on display FPS
    11. director->setDisplayStats(true);
    12. // set FPS. the default value is 1.0/60 if you don't call this
    13. director->setAnimationInterval(1.0 / 60);
    14. // create a scene. it's an autorelease object
    15. auto scene = HelloWorld::scene();
    16. // run
    17. director->runWithScene(scene);
    18. return true;
    19. }
    上面代码中的HelloWorld是用户定义的类:
    1. class HelloWorld : public cocos2d::Layer
    2. {
    3. public:
    4. // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
    5. virtual bool init();
    6. // there's no 'id' in cpp, so we recommend returning the class instance pointer
    7. static cocos2d::Scene* scene();
    8. // a selector callback
    9. void menuCloseCallback(Ref* sender);
    10. // implement the "static node()" method manually
    11. CREATE_FUNC(HelloWorld);
    12. };
    实际上HelloWorld本身是一个Layer,调用scene函数的时候,会创建一个Scene的实例,调用scene的addChild函数把自身的一个实例加进去:
    1. Scene* HelloWorld::scene()
    2. {
    3. // 'scene' is an autorelease object
    4. auto scene = Scene::create();
    5. // 'layer' is an autorelease object
    6. HelloWorld *layer = HelloWorld::create();
    7. // add layer as a child to scene
    8. scene->addChild(layer);
    9. // return the scene
    10. return scene;
    11. }
    在create函数当中,会调用HelloWorld的init函数,这个函数是一个虚函数,在这个函数中,可以完成用户自身的初始化工作,例如加入菜单项,创建各种组成场景的元素。

    总结一下,总的调用顺序是:
    main调用Application::run
    AppDelegate::applicationDidFinishLaunching被调用
    创建Layer,Layer的init函数被调用
    创建Scene,把Layer加进scene
    获得Director指针(只有一个director),把Scene加进Director










  • 相关阅读:
    SYSBIOS学习笔记---线程(Threads)
    spring mvc: 生成RSS源
    spring mvc: Hibernate验证器(字段不能为空,在1-150自己)
    解决At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JAR
    spring mvc: 多解析器映射(资源绑定视图解析器 + 内部资源[普通模式/]视图解析器)
    spring mvc: 资源绑定视图解析器(不推荐)
    spring mvc:内部资源视图解析器2(注解实现)@Controller/@RequestMapping
    centos双机热备份
    spring mvc:内部资源视图解析器(注解实现)@Controller/@RequestMapping
    spring mvc: 可参数化的视图控制器(在配置中指定jsp文件)MultiActionController/SimpleUrlHandlerMapping/ParameterizableViewController
  • 原文地址:https://www.cnblogs.com/dydx/p/4295941.html
Copyright © 2011-2022 走看看