Cocos2d-x HelloWorld 之源码分析

前端之家收集整理的这篇文章主要介绍了Cocos2d-x HelloWorld 之源码分析前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在《HelloWorld 之目录结构》中我们已经了解了怎么样新建一个HelloWorld工程,下面我们来看看源代码是什么样子的

main.cpp-- 程序的主入口
   
   
#include "main.h""AppDelegate.h""cocos2d.h"USING_NS_CC;int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance LPTSTR lpCmdLine int nCmdShow){ UNREFERENCED_PARAMETERhPrevInstance);lpCmdLine // create the application instance AppDelegate app; //创建这个应用程序实例 return Application::getInstance()->run(); //运行它}
问题:AppDelegate app; 的时候创建应用程序对象,但是为什么可以使用Application::getInstance()->run(); 来运行呢。为什么不是app.run(); 呢?
AppDelegate.cpp-- 应用程序类,相当于Android 里面自定义Application
classAppDelegate:privatecocos2d::Application
classCC_DLLApplication:publicApplicationProtocol
  • CCApplicationProtocol.h 没有对应的cpp 文件代码量很少
  • CCApplication.h这个类在每一个不同的平台都会有对应的实现类
  • 代码位置为:F:\software\cocos2d\cocos2d-x-3.6\cocos\platform
Application 的单例实现方案
win32
Application *sm_pSharedApplication = 0; //0 就是null,C++比Java灵活多了Application::Application() //构造函数: _instance(nullptr _accelTable _instance GetModuleHandle _animationInterval.QuadPart; CC_ASSERT(! sm_pSharedApplication sm_pSharedApplication this;}Application::~Application() //析构函数this==} Application() //拿到实例函数sm_pSharedApplication}
Android
;Application() CCAssert"" NULL}
可以看出win32 和Android版的基本相同,在整个Cocos2d-x中AppDelegate就是一个单例模式。
解上面的问题:单例模式和Java里面的有点不同,不是类本身实例化自己,还是要靠外部来实例化,例如在main.cpp 里面的 AppDelegate app; 创建了实例,同时赋值给静态变量 sm_pSharedApplication,以后可以通过getInstance() 方法拿到这个实例(C++里面是叫实例吗?好纠结这些问题哦),为了满足单例模式拿到实例的一贯使用getInstance() 方法(习惯了Java里面叫方法,好像C++里面叫函数,以后我会改的)拿到实例。所以不用app->run();明白了吗?

回到AppDelegale.cpp/h
相关的函数
AppDelegate();virtual~AppDelegatevoid initGLContextAttrs(); //初始化OpenGL Context 的6个属性,red,green,blue,alpha,depth,stencilbool applicationDidFinishLaunching(); //当应用程序启动的时候执行,这里是游戏的开始,例如启动了loading 界面,开始加载游戏 applicationDidEnterBackground(); //当游戏进入后台时会执行,例如电话来了 applicationWillEnterForeground(); //当游戏恢复到前台运行时候执行,例如拒接电话
实现的函数
英文不好也要看英文呀
这里又有一个问题,Context 是不是和Android 的Context 同一个概念呢?但是我连Android 的Context 的不理解,就算相同我也不知道是怎么样的一个东西。
  
  
//if you want a different context,just modify the value of glContextAttrs//it will takes effect on all platforms AppDelegateinitGLContextAttrs//set OpenGL context attributions,now can only set six attributions://red,stencil GLContextAttrs glContextAttrs {824}; GLViewsetGLContextAttrsglContextAttrs);//是为GLView 设置的,感觉这个GLView 非同一般}
说明几点
  • auto c++ 11 的特征,相当于PHP 的var。类型自动识别
  • Director::getInstance(); 导演的单例模式
  • if(!glview){} 两部理解,我们的需求是,当个头OpenGLView() 返回空的时候我们需要创建这个glview
    • 1,if(glview) 当glview == null 的时候,但发现null == 0,这样直接为假,跳出去了
    • 2,if(!glview) 我加一个!不就完事了吗。
    • 3,背下来就ok了。
  • director有getOpenGLView() &setOpenGLView(glview); 说明director 有一个属性GLView,猜想这个就是用来显示场景的,在拍戏的时候导演都是坐在一个电视机前面叫开始的,这个电视机就是GLView 了。
applicationDidFinishLaunching()// initialize directorauto director Director();//导演隆重登场,这里也是用了单例模式,整个游戏只能有一个导演,不知道有没有副导演呢? glview director->getOpenGLView//GLView 再次出现,还是导演亲自请来的ifglview){ //0(null) 是假,非0 是真,那当什么时候!glview 为真呢?就是当glview == null 的时候 //当glview 为null的时候glview GLViewImplcreate("My Game"setOpenGLView}// turn on display FPSsetDisplayStatstrue); //显示FPS// set FPS. the default value is 1.0/60 if you don't call thissetAnimationInterval(1.0/@H_403_524@60); //刷新屏幕的间隔时间,就是等待多久换一次帧数据 register_all_packages// create a scene. it's an autorelease object scene HelloWorldcreateScene// runrunWithScenescene}
HelloWorld 的超简单实现了另外两个函数
// This function will be called when the app is inactive. When comes a phone call,it's be invoked tooapplicationDidEnterBackgroundstopAnimation// if you use SimpleAudioEngine,it must be pause// SimpleAudioEngine::getInstance()->pauseBackgroundMusic();}// this function will be called when the app is active againapplicationWillEnterForegroundstartAnimation// SimpleAudioEngine::getInstance()->resumeBackgroundMusic();}
从分析上面的代码看出,显示的第一个场景为HelloWorld::createScene();
HelloWorldScene.cpp-- 运行的场景,相当于Android的Activity
相关函数
  • Ref 类:Ref is used for reference count manangement. If a class inherits from Ref,then it is easy to be shared in different places.
    • cocos2dx中所有对象都继承于Ref,或者继承于 Ref和Clonable,Ref中就是维护了一个计数器,用于判断该继承于Ref的对象是否应该delete,意思就是我们可以放心的使用这些实例,不需要担心实例什么时候需要delete
  • CREATE_FUNC(HelloWorld)
    • 定义create() 方法,里面做了
      • 创建对象
      • 调用对象的init() 方法
      • 把对象加入到引用计数器的对象池中
      • 返回这个对象
    • 想必普通的对象都使用了这个宏了
    • 以后都使用这个函数来实例化对象
//there's no 'id' in cpp,so we recommend returning the class instance pointer
  • Here's a difference. Method 'init' in cocos2d-x returns bool,instead of returning '' in cocos2d-iphone
  • static cocos2d::Scene createScene(); //创建场景实例,返回的是一个指针// init(); //看到这个注释我也是醉了,一开始看到在cpp 里不能返回id,我就是看不明白,现在明白了,是不是iPhone 里面是可以返回id 的?猜应该是,Cocos2d-x 是由Cocos2d-iphone 版本发展过来的 menuCloseCallbackcocos2d::Ref pSender); //一个回调函数CREATE_FUNC(HelloWorld);
  • 函数实现
    Scene Scene(); //创建一个场景 layer (); //HelloWorld是一个层,MD,才发现HelloWorld 是一个层,看文件名字,我还以为是场景呢 sceneaddChildlayer); //直接显示HelloWorld 层}
    总要知识点
    // on "init" you need to initialize your instance//////////////////////////////// 1. super init firstfalse Size visibleSize getVisibleSize();//显示大小 Vec2 origin getVisibleOrigin(); //起点位置/////////////////////////////// 2. add a menu item with "X" image,which is clicked to quit the program// you may modify it.// add a "close" icon to exit the progress. it's an autorelease object closeItem MenuItemImage"CloseNormal.png""CloseSelected.png" CC_CALLBACK_1menuCloseCallback)); closeItemsetPosition(Vec2origin.x + visibleSizewidth -getContentSize().width/2 originy height2// create menu,it's an autorelease object menu MenucloseItem menuZEROmenu1); //添加菜单// 3. add your codes below...// add a label shows "Hello World"// create and initialize a label label LabelcreateWithTTF"Hello World""fonts/Marker Felt.ttf"// position the label on the center of the screen labelheight // add the label as a child to this layerlabel); //添加label// add "HelloWorld" splash screen" sprite Sprite"HelloWorld.png"// position the sprite on the center of the screen spritevisibleSizexy// add the sprite as a child to this layersprite);//最后加入一个精灵当做背景}
    回调函数,直接退出程序
    (Refend();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS) exit);#endif}

    定制HelloWorld -- win32 版
    //定制HelloWorld//弄了背景图 background "custom_hello_world.jpg" backgroundbackground//显示个Label 标题,显示在背景图的上面 labelTitle "Custom Hello World" labelTitlelabelTitle//使用Label 做点击菜单 labelMenu "Click Me,Click Me" closeMenu MenuItemLabellabelMenu closeMenu labelMenusetCallbackCC_CALLBACK_1)); //设置回调函数closeMenuNULL}
    运行结果,nice

    原文链接:https://www.f2er.com/cocos2dx/342380.html

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