您的位置:首页 > 移动开发 > Cocos引擎

解析cocos2d test项目(二)

2017-01-07 13:48 281 查看
我好厉害呀,都写到二了。啊,装疯卖傻一波,明明就几句话,还要换一章,mdzz。

废话有点多,我这个人。我们继续说AppDelegate 分为.h和.cpp两个文件。(我一直搞不懂,为啥要多个头文件,


定义声明写在一起感觉可能更有爱一点,应该也有我所不了解的细节吧)

我们这里先看AppDelegate.h

#ifndef  _APP_DELEGATE_H_
#define  _APP_DELEGATE_H_

#include "cocos2d.h"

class TestController;
/**
@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 cocos2d::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();

private:
TestController* _testController;
};

#endif // _APP_DELEGATE_H_


首先public的这几个我都还眼熟,我毕竟不是第一次看cocos2d,我还是打过那个HelloWorld的练习。

AppDelegate();

virtual ~AppDelegate();

首先是构造函数和析构函数,析构函数声明为虚函数由于:

在实现多态时,当用基类操作派生类,在析构时防止只析构基类而不析构派生类的状况发生。

virtual void initGLContextAttrs();//这是个什么玩意?去搜索了一下,说是设置OpenGL属性什么的,我记得cocos2d的一个优点就是隐藏了OpenGL什么的,说是我们不需要懂OpenGL就可以做游戏。反正我又不想做什么动画渲染啥的,应该不用了解,以后再说。

接下来这三个就很重要了

virtual bool applicationDidFinishLaunching();//这是程序启动过程中的第一个回调函数,我们在这里做一些初始化操作,我搜索关于该函数的调用

//我发现在int Application::run()中有这样代码,所以我们又多了一个疑问,Application是个啥玩意。

int Application::run()

{

\\前省略

if (!applicationDidFinishLaunching())

    {

        return 1;

    }

\\后省略

}

virtual void applicationDidEnterBackground();//说明:当程序被推送到后台的时候调用。所以要设置后台继续运行,则在这个函数里面设置即可,搜索调用出现了

void GLViewImpl::onGLFWWindowIconifyCallback(GLFWwindow* window, int iconified)

{

    if (iconified == GL_TRUE)

    {

        Application::getInstance()->applicationDidEnterBackground();

    }

    else

    {

        Application::getInstance()->applicationWillEnterForeground();

    }

}

My god ,问题越来越多了,这又是个啥,GLViewImpl应该和OpenGL有什么关系吧,同OpenGL一同暂时搁置吧,大概只要了解这是把程序推到后台就好了,暂时。

virtual void applicationWillEnterForeground();//说明:当程序从后台将要重新回到前台时候调用,这个刚好跟上面的那个方法相反。

搜索调用时同样出现了些坑爹的东西,还是GLViewImpl我就不贴出来了,看着闹心。

此外这三个函数全部在一个ApplicationProtocol里被声明为纯虚函数,我们应该可以猜想,AppDelegate的继承链上应该有这个类。

此外,这三个之外还有一个private:TestController* _testController;

关于这个应该是test项目特有的定义,我记得我在HelloWorld中没有看到过这个,应该不在API。

研究完AppDelegate.h总结一下:

一、我们知道的,应该背下来的:

这玩意继承了Application,我们暂时还不知道那是什么,虽然通过英文可以有一些大致的猜测

这玩意有构造函数和析构函数,AppDelegate();virtual ~AppDelegate();我们在main函数中调用了构造函数,但析构函数啥时候调用还不太清楚。

那三个最重要的函数(就是那几个名字差不多的函数)作用,applicationDidFinshLaunching() applicationDIdEnterBackground() applicationWillEnterForeground()

二、接下来要探索的:

1Application是个啥

2ApplicationProtocol是个啥

3TestController是个啥

4析构函数啥时候调用

三、暂时搁置:

1initGLContextAttrs()

2三个函数的具体调用GLViewImpl作用

class TestController;

class  AppDelegate : private cocos2d::Application\\Application是什么我们需要探究
{
public:
AppDelegate();
virtual ~AppDelegate(); \\构造函数和析构函数的具体细节我们需探索.cpp文件

virtual void initGLContextAttrs();//暂记为设置OpenGL属性,至于OpenGL是啥,暂时搁置

/**
@brief    Implement Director and cocos2d::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();//同上,当程序从后台推到前台是调用

private:
TestController* _testController;//需探究controller文件,是test项目自己定义的类,不属于cocos2d api
};

#endif // _APP_DELEGATE_H_


额,接下来看AppDelegate.cpp文件,啊等等,我还没吃午饭,那待会儿再说。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: