cocos2d-x学习之路(二)——分析AppDelegate和HelloWorldScene文件

时间:2023-03-10 01:59:14
cocos2d-x学习之路(二)——分析AppDelegate和HelloWorldScene文件

这里我们来看一下cocos自动给我们生成的工程里有些什么东西,并且分析一下这些代码的用途,来为我们以后编写cocos程序铺下基础。

这里我建议看我这份随笔的看官先看看cocos官网的快速入门手册,不然可能会比较迷糊(因为待会要分析一些代码,如果以前没见过的话会比较昏)。传送门在这里

其中一些基本不需要程序员干涉的代码我可能会不予分析。你也可以查看官方API手册。传送门在这里

下面的代码分析,如果是非常有用的东西我会在分析中用蓝色标出。


首先我们进入相关的系统(你的如果是mac就打开proj.ios_mac文件夹下的工程,是windows就打开proj.win32文件夹下的工程,以此类推)的工程。我这里是mac,运行一下cocos给我们生成的代码,结果如下:

cocos2d-x学习之路(二)——分析AppDelegate和HelloWorldScene文件

既然是自己的demo嘛,当然要给自己打个广告啦(那个居中的图标显然是cocos的logo)

这个界面里面有一些我们可以直接看出来的东西:

cocos2d-x学习之路(二)——分析AppDelegate和HelloWorldScene文件

这里所有的元素都是我根据以往用过的引擎猜测的,实际上我们还是要看一下代码。不过我们目前知道大概有这么些东西了,待会可以针对着看一下。

然后我们可以看看这些资源在哪里,我通过XCODE可以直接看到:

cocos2d-x学习之路(二)——分析AppDelegate和HelloWorldScene文件

这里logo是HelloWorld.png,右下角的开关机图标是CloseNormal.png,而那个CloseSelected.png是按下按钮的图片。


分析AppDelegate

好的,到我们的分析阶段了。AppDelegate分为头文件和实现文件。我们当然是先看头文件啦。这个文件在Classes文件夹下。

 #ifndef  _APP_DELEGATE_H_
#define _APP_DELEGATE_H_ #include "cocos2d.h" /**
@brief The cocos2d Application. Private inheritance here hides part of interface from Director.
*/
class AppDelegate : private cocos2d::Application
{
public:
AppDelegate();  //构造函数
virtual ~AppDelegate();  //析构函数 virtual void initGLContextAttrs();  //这个暂时不知道是干什么的 /**
@brief Implement Director and Scene init code here.
@return true Initialize success, app continue.
@return false Initialize failed, app terminate.
*/
virtual bool applicationDidFinishLaunching(); /**
@brief Called when the application moves to the background
@param the pointer of the application
*/
virtual void applicationDidEnterBackground(); /**
@brief Called when the application reenters the foreground
@param the pointer of the application
*/
virtual void applicationWillEnterForeground();
}; #endif // _APP_DELEGATE_H_

第4行包含了cocos2d的头文件。

第11行定义了AppDelegate类,继承自cocos2d的Application类。

这里堆AppDelegate类的几个虚函数在注释上都有一定的说明了:

第24行的applicationDidFinishLaunching()是在程序初始化的时候自动调用的函数。在这里面我们可以初始化导演(Director)和场景(Scence)。如果程序初始化成果会返回True,否则返回False。

第30行的applicationDidEnterBackground()是在程序失去焦点的时候调用。这里面一般是加入用来停止程序的代码。

第36行的applicationWillEnterForeground()是在程序获得焦点的时候调用,可以在里面加入继续游戏的代码。

接下来看看实现文件:

 #include "AppDelegate.h"
#include "HelloWorldScene.h" // #define USE_AUDIO_ENGINE 1
// #define USE_SIMPLE_AUDIO_ENGINE 1 #if USE_AUDIO_ENGINE && USE_SIMPLE_AUDIO_ENGINE
#error "Don't use AudioEngine and SimpleAudioEngine at the same time. Please just select one in your game!"
#endif #if USE_AUDIO_ENGINE
#include "audio/include/AudioEngine.h"
using namespace cocos2d::experimental;
#elif USE_SIMPLE_AUDIO_ENGINE
#include "audio/include/SimpleAudioEngine.h"
using namespace CocosDenshion;
#endif USING_NS_CC; static cocos2d::Size designResolutionSize = cocos2d::Size(, );
static cocos2d::Size smallResolutionSize = cocos2d::Size(, );
static cocos2d::Size mediumResolutionSize = cocos2d::Size(, );
static cocos2d::Size largeResolutionSize = cocos2d::Size(, ); AppDelegate::AppDelegate()
{
} AppDelegate::~AppDelegate()
{
#if USE_AUDIO_ENGINE
AudioEngine::end();
#elif USE_SIMPLE_AUDIO_ENGINE
SimpleAudioEngine::end();
#endif
} // if you want a different context, modify the value of glContextAttrs
// it will affect all platforms
void AppDelegate::initGLContextAttrs()    //这个真心没看懂是个啥
{
// set OpenGL context attributes: red,green,blue,alpha,depth,stencil,multisamplesCount
GLContextAttrs glContextAttrs = {, , , , , , }; GLView::setGLContextAttrs(glContextAttrs);
} // if you want to use the package manager to install more packages,
// don't modify or remove this function
static int register_all_packages()
{
return ; //flag for packages manager
} bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
glview = GLViewImpl::createWithRect("TestGame", cocos2d::Rect(, , designResolutionSize.width, designResolutionSize.height));
#else
glview = GLViewImpl::create("TestGame");
#endif
director->setOpenGLView(glview);
} // turn on display FPS
director->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this
director->setAnimationInterval(1.0f / ); // Set the design resolution
glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER);  //不知道是干啥的
auto frameSize = glview->getFrameSize();
// if the frame's height is larger than the height of medium size.
if (frameSize.height > mediumResolutionSize.height)
{
director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width));
}
// if the frame's height is larger than the height of small size.
else if (frameSize.height > smallResolutionSize.height)
{
director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width));
}
// if the frame's height is smaller than the height of medium size.
else
{
director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width));
} register_all_packages(); // create a scene. it's an autorelease object
auto scene = HelloWorld::createScene(); // run
director->runWithScene(scene); return true;
} // This function will be called when the app is inactive. Note, when receiving a phone call it is invoked.
void AppDelegate::applicationDidEnterBackground() {
Director::getInstance()->stopAnimation(); #if USE_AUDIO_ENGINE
AudioEngine::pauseAll();
#elif USE_SIMPLE_AUDIO_ENGINE
SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
SimpleAudioEngine::getInstance()->pauseAllEffects();
#endif
} // this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
Director::getInstance()->startAnimation(); #if USE_AUDIO_ENGINE
AudioEngine::resumeAll();
#elif USE_SIMPLE_AUDIO_ENGINE
SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
SimpleAudioEngine::getInstance()->resumeAllEffects();
#endif
}

第18行以上的乱七八糟的东西我们就先不看了,无非就是包含头文件啊,使用命名空间什么的。这些暂时不管。

第19行是一个宏,可以进入到里面看看(这个宏后面用到的还比较多):

#define USING_NS_CC                     using namespace cocos2d

可以看到就是使用cocos2d的命名空间。

  • 第21~24行定义了四种大小(从cocos2d::Size可以看出是大小的定义),按照变量名字分别是设计时大小,最小化大小,通常化大小和最大化大小。这里我还是要重复说一下:现在这些只是我们的猜测,具体的还是要看到相关代码才行。

那么我们可以想,我是不是更改一下这些玩意就可以改变窗口大小了呢?嗯……可以尝试一下,就先从designResolutionSize下手吧。

我这里将designResolutionSize参数改成1024,480,果然窗口的大小改变了:

cocos2d-x学习之路(二)——分析AppDelegate和HelloWorldScene文件

不过改变了其他三个尺寸之后窗口没什么变化。暂时先不管吧,反正现在知道designResolutionSize变量存储的是当前窗口的大小就行了

  • 30~37行是析构函数的实现。可以看出是释放了AudioEngine和SimpleAudioEngine两个模块。
  • 51行的函数用于管理包。当你想要使用包管理器(Package Manager)来安装多个包的时候,就不要修改或者删除这里的代码。(你问我包是啥?对不起我也不晓得。你说那你怎么知道这个是包管理器函数?看注释啊