码迷,mamicode.com
首页 > 编程语言 > 详细

cocos游戏的真正入口,用C++实现的demo版本

时间:2014-10-27 17:05:59      阅读:274      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   color   os   ar   sp   文件   

1.cocos游戏的出发点

在main函数中有一句:   return CCApplication::sharedApplication()->run();

2.经过层层深入发现,真正的入口:  bool AppDelegate::applicationDidFinishLaunching()

3.类继承关系图:

bubuko.com,布布扣

4.代码实现:

CCApplicationProtocol的头文件如下,.cpp文件为空

#pragma once
class CCApplicationProtocol
{
public:
    /*定义了一个虚接口,用于游戏的真正入口*/
    virtual bool applicationDidFinishLaunching() = 0;
};

CCApplication.h

#pragma once
#include "CCApplicationProtocol.h"

#include <iostream>
using namespace std;

class CCApplication :public CCApplicationProtocol
{
public:
    CCApplication();

    /*run方法*/
    virtual int run();
    /*返回静态指针*/
    static CCApplication* sharedApplication();

protected:
    /*静态指针,用于实现单例*/
    static CCApplication * sm_pSharedApplication;
};

CCApplication.cpp

#include "CCApplication.h"

/*cpp文件中初始化静态成员*/
CCApplication * CCApplication::sm_pSharedApplication = NULL;

CCApplication::CCApplication()
{
    /*将子类对象赋值给父类的静态指针*/
    sm_pSharedApplication = this;
}

CCApplication* CCApplication::sharedApplication()
{ 
    /*实现单例*/
    if (sm_pSharedApplication != NULL)
    return sm_pSharedApplication;
}

int CCApplication::run()
{
    /*调用父类的方法,实现游戏的启动*/
    applicationDidFinishLaunching();
    return 0;
}

AppDelegate.h

#pragma once
#include "CCApplication.h"
class AppDelegate :private CCApplication
{
public:
    /*实现了继承来的祖父类方法*/
    virtual bool applicationDidFinishLaunching();
};

AppDelegate.cpp

#include "AppDelegate.h"

/*实现了继承来的祖父类方法*/
bool AppDelegate::applicationDidFinishLaunching()
{
    /*do somethint to iniialize the game*/

    cout << "game start" << endl;
    return true;
}

测试代码:

#include "AppDelegate.h"

int main()
{
    AppDelegate app;
    CCApplication::sharedApplication()->run();

    system("pause");
    return 0;
}

 

cocos游戏的真正入口,用C++实现的demo版本

标签:style   blog   http   io   color   os   ar   sp   文件   

原文地址:http://www.cnblogs.com/ttss/p/4054378.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!