cocos2dx-3.0(2)------cpp-empty-test

         对于3.0以前的版本都是helloworld,在3.0里面改成cpp-empty-test

我们先来看看main.cpp,作为c、c++程序员,程序入口就是main.cpp所以他很重要,看下面代码:

#include "main.h"
#include "../Classes/AppDelegate.h"

USING_NS_CC;

int APIENTRY _tWinMain(HINSTANCE hInstance,
                       HINSTANCE hPrevInstance,
                       LPTSTR    lpCmdLine,
                       int       nCmdShow)
{
    UNREFERENCED_PARAMETER(hPrevInstance);
    UNREFERENCED_PARAMETER(lpCmdLine);

    // create the application instance
    AppDelegate app;
    return Application::getInstance()->run();
}
对于我,上面无关重要的(我自己觉得)我不管

int APIENTRY _tWinMain
这货根我们常见的不同,但是对我们重要吗?管他了,他就是一吊丝,偷笑还有下面这货

UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
进去一看,没用!!


根我们有关,程序真正运行起来是这两行代码

AppDelegate app;
return Application::getInstance()->run();
上面代码涉及到两个类,我们来看看他们的关系



我们从根开始聊聊:

1.先看看ApplicationProtocol代码

class CC_DLL ApplicationProtocol
{
public:
    /**
     * @js NA
     * @lua NA
     */
    virtual ~ApplicationProtocol() {}

    /**
    @brief    Implement Director and Scene init code here.
    @return true    Initialize success, app continue.
    @return false   Initialize failed, app terminate.
    * @js NA
    * @lua NA
    */
    virtual bool applicationDidFinishLaunching() = 0;

    /**
    @brief  This function will be called when the application enters background.
    * @js NA
    * @lua NA
    */
    virtual void applicationDidEnterBackground() = 0;

    /**
    @brief  This function will be called when the application enters foreground.
    * @js NA
    * @lua NA
    */
    virtual void applicationWillEnterForeground() = 0;

    /**
    @brief    Callback by Director for limit FPS.
    @param interval The time, expressed in seconds, between current frame and next.
    * @js NA
    * @lua NA
    */
    virtual void setAnimationInterval(double interval) = 0;

    /**
    @brief Get current language config
    @return Current language config
    * @js NA
    * @lua NA
    */
    virtual LanguageType getCurrentLanguage() = 0;
    
    /**
     @brief Get current language iso 639-1 code
     @return Current language iso 639-1 code
     * @js NA
     * @lua NA
     */
    virtual const char * getCurrentLanguageCode() = 0;
    
    /**
     @brief Get target platform
     * @js NA
     * @lua NA
     */
    virtual Platform getTargetPlatform() = 0;
};
看到没,全是纯虚函数,好好根着学学吧,骚年!!(其实我就是那骚年)这样设计函数接口,大家知道含有纯虚函数的类为抽象类,不能实例化。

2.在来看看Application类

class CC_DLL Application : public ApplicationProtocol
{
public:
    /**
     * @js ctor
     */
    Application();
    /**
     * @js NA
     * @lua NA
     */
    virtual ~Application();

    /**
    @brief    Run the message loop.
    */
    int run();

    /**
    @brief    Get current applicaiton instance.
    @return Current application instance pointer.
    */
    static Application* getInstance();
    
protected:


    static Application * sm_pSharedApplication;
};
virtual bool applicationDidFinishLaunching() = 0;
 
virtual void applicationDidEnterBackground() = 0;
virtual void applicationWillEnterForeground() = 0;

这是我简化后的,因为对于我们来说,只有这几个函数对于我们有用。需要注意一点Application继承于ApplicationProtocol但是他没有实现所有的纯虚函数,还有三个函数没有实现

所以Application也是抽象类

2.1 Application的构造函数:

Application::Application()
: _instance(nullptr)
, _accelTable(nullptr)
{
    _instance    = GetModuleHandle(nullptr);
    _animationInterval.QuadPart = 0;
    CC_ASSERT(! sm_pSharedApplication);
    sm_pSharedApplication = this;
}
主要关注sm_pSharedApplication=this, 他保存了当前自己,他是个static Application的指针,他在下面这个函数接口中有用

Application* Application::getInstance()
{
    CC_ASSERT(sm_pSharedApplication);
    return sm_pSharedApplication;
}
他代替了3.0以前的

static Application* sharedApplication();
前面我们看到main.cpp中Application::getInstance()->run();前面有AppDelegate app;大家也许发现,只是声明了一个AppDelegate的对象,却不用他,能不能把他去掉了?

答案是不能的,并且他写这里并不是无关紧要的,大家知道c++中有多态,当父类的函数中某个函数为虚函数时,他会主动调用子类的实现。我们知道AppDelegate中有个三个函数没有实现,他们是在AppDelegate中实现,大家调试一下就知道,这样一来,main.cpp分析完了

3.AppDelegate类

bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();
    if(!glview) {
<span style="white-space:pre">	</span>//修改主窗体名字
<span style="white-space:pre">	</span>glview = GLView::create("test");


<span style="white-space:pre">	</span>//重置窗体大小
<span style="white-space:pre">	</span>glview->setFrameSize(640, 480);
 
        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.0 / 60);

    // create a scene. it's an autorelease object
    auto scene = HelloWorld::createScene();

    // run
    director->runWithScene(scene);

    return true;
}
此函数是用来初始化程序的,这个函数和3.0以前也不一样了。3.0把GLView的一个对象放到导演类(Director)中。GLView是window框架设计的开始

运行程序时,大家发现左下角有几行数据,里面还有一些数字时不时改变一下,自己还不能控制是不是觉得很蛋疼,你终于不用蛋疼了,只需要把下面代码改一改

    // turn on display FPS
    director->setDisplayStats(true);
改成false,从此你的世界就清静了。

大家想说,我们怎么加内容在框架里面了?

================================================================华丽的分割线

                                                                 恩,这集结束了,请看下集吧~~~~~~








  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值