Cocos2d-x HelloWorld 之源码分析

Cocos2d-x HelloWorld 之源码分析,第1张

概述在《HelloWorld 之目录结构》中我们已经了解了怎么样新建一个HelloWorld工程,下面我们来看看源代码是什么样子的 main.cpp -- 程序的主入口 在《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/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 层} 总要知识点 Layer 需要调用super 的init() 函数
                                            if!Layerinit) //c++ 使用类名来指定调用哪个父类的方法
// 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 版 glvIEw->setFrameSize(500,500); //指定显示的大小
GLFW_ICON ICON "res\\custom_game.ico" //在win32 的game.rc 文件中修改
http://www.ico.la/这个网站弄的
修改init 方法 //定制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
总结

以上是内存溢出为你收集整理的Cocos2d-x HelloWorld 之源码分析全部内容,希望文章能够帮你解决Cocos2d-x HelloWorld 之源码分析所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1063118.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存