cocos2d-x的Helloworld程序运行流程(win32)

前端之家收集整理的这篇文章主要介绍了cocos2d-x的Helloworld程序运行流程(win32)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

自学cocos2d难免有错误的理解,作为参考吧

创建好Helloworld工程后,进入VS2012可以看到main主函数

首先程序从主函数运行,代码如下:

  1. #include "main.h"
  2. #include "AppDelegate.h"
  3. #include "cocos2d.h"
  4.  
  5. USING_NS_CC;
  6.  
  7. @H_301_17@int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine,@H_301_17@int nCmdShow)
  8. {
  9. UNREFERENCED_PARAMETER(hPrevInstance);
  10. UNREFERENCED_PARAMETER(lpCmdLine);
  11.  
  12. // create the application instance
  13. AppDelegate app; //(第一步)创建对象
  14. @H_301_17@return Application::getInstance()->run();
  15. }

创建完对象后的下一步才是最关键的。
(第二步)调用Application类里的getInstance()方法,这步我看了下源码基本上就是用一个指针指向app对象,然后通过该指针(第三步)调用run()方法
进入run()方法

  1. int Application::run()
  2. {
  3. PVRFrameEnableControlWindow(false);
  4.  
  5. // Main message loop:
  6. LARGE_INTEGER nFreq;
  7. LARGE_INTEGER nLast;
  8. LARGE_INTEGER nNow;
  9.  
  10. QueryPerformanceFrequency(&nFreq);
  11. QueryPerformanceCounter(&nLast);
  12.  
  13. initGLContextAttrs();
  14.  
  15. // Initialize instance and cocos2d.
  16. @H_301_17@if (!applicationDidFinishLaunching())//(第四步)从这里进入AppDelegate类中初始化操作
  17. {
  18. @H_301_17@return 0;
  19. }
  20.  
  21. auto director = Director::getInstance();
  22. auto glview = director->getOpenGLView();
  23.  
  24. // Retain glview to avoid glview being released in the while loop
  25. glview->retain();
  26. //(第十五步)游戏循环,如果没有触发停止操作会一直循环
  27. @H_301_17@while(!glview->windowShouldClose())
  28. {
  29. QueryPerformanceCounter(&nNow);
  30. @H_301_17@if (nNow.QuadPart - nLast.QuadPart > _animationInterval.QuadPart)
  31. {
  32. nLast.QuadPart = nNow.QuadPart - (nNow.QuadPart % _animationInterval.QuadPart);
  33.  
  34. director->mainLoop();
  35. glview->pollEvents();
  36. }
  37. @H_301_17@else
  38. {
  39. Sleep(1);
  40. }
  41. }
  42.  
  43. // Director should still do a cleanup if the window was closed manually.
  44. @H_301_17@if (glview->isOpenGLReady())
  45. {
  46. director->end();
  47. director->mainLoop();
  48. director = nullptr;
  49. }
  50. glview->release();
  51. @H_301_17@return true;
  52. }

在AppDelegate.cpp中查看applicationDidFinishLaunching()

  1. bool AppDelegate::applicationDidFinishLaunching() {
  2. // 初始化导演
  3. auto director = Director::getInstance();//(第五步)进入系统默认的init()初始化
  4. auto glview = director->getOpenGLView();//(第八步)创建OpenGL的对象
  5. @H_301_17@if(!glview) {
  6. glview = GLViewImpl::create("My Game");
  7. director->setOpenGLView(glview);
  8. }
  9.  
  10. // (第九步)设置FPS的显示与否
  11. director->setDisplayStats(true);
  12.  
  13. // (第十步)设置FPS值
  14. director->setAnimationInterval(1.0 / 60);
  15.  
  16. // (第十一步)创建一个背景,这里面有用户自己写的init()方法初始化
  17. auto scene = HelloWorld::createScene();
  18.  
  19. // 运行
  20. director->runWithScene(scene);
  21.  
  22. @H_301_17@return true;
  23. }

第一句是创建导演对象以及初始化他,但是这个初始化init()不是Helloworld.cpp里面的init(),可以查看源码

  1. Director* Director::getInstance()
  2. {
  3. @H_301_17@if (!s_SharedDirector)
  4. {
  5. s_SharedDirector = new (std::nothrow) DisplayLinkDirector();
  6. CCASSERT(s_SharedDirector,"FATAL: Not enough memory");
  7. s_SharedDirector->init();//(第六步)在这里做初始化操作
  8. }
  9.  
  10. @H_301_17@return s_SharedDirector;
  11. }

查看auto scene = HelloWorld::createScene();所用方法代码,可以看见

  1. Scene* HelloWorld::createScene()
  2. {
  3. // 'scene' is an autorelease object
  4. auto scene = Scene::create(); //(第十二步)这里是调用Scene的init()方法
  5.  
  6. // 'layer' is an autorelease object
  7. auto layer = HelloWorld::create(); //(第十三步)这里是调用了Helloworld的init()方法
  8.  
  9. //(第十四步)将图层加到背景上
  10. scene->addChild(layer);
  11.  
  12. // return the scene
  13. @H_301_17@return scene;
  14. }

调用create()方法后程序会调用各自的init()方法

注意:第一次写博客语言表达肯能不太清晰,编写工具运用不太熟悉,谅解。

猜你在找的Cocos2d-x相关文章