COCOS2D-X源码分析之初始化游戏

#include "cocos2d.h"
/**
 CCEGLView  自适应模块业务
**/
#include "CCEGLView.h"
#include "AppDelegate.h"
#include "HelloWorldScene.h"
#include "SimpleAudioEngine.h"
/**
@see 委托类
**/
using namespace CocosDenshion;
// #define USING_NS_CC                     using namespace cocos2d 创建一个声明用于创建场景类
USING_NS_CC;


AppDelegate::AppDelegate()
{
}


AppDelegate::~AppDelegate()
{
/**
@see 设置游戏的音效
**/
    SimpleAudioEngine::end();
}
/**


(1)初始化CCEGLView,在不同平台下,CCEGLView是不同的
(PS:cocos2d-x中有很多这样的用法,即同一个头文件,在不同平台上实现不同)


win32平台下的CCEGLView是一个windows窗口,具体情况CCEGLView::Create()


(PS:CCEGLView还用来处理鼠标点击模拟器触摸,按键等事件的处理,如果要自定义,请参看CCEGLView::WindowProc(),这个函数是Windows程序接收消息回调)


(2)游戏引擎的主循环
在完成CCEGLView初始化之后,将开启游戏渲染的主循环,即调用CCDirector::sharedDirector()->mainLoop()。这样,程序就又交给CCDirector类来处理,CCDirector主要处理两件事情
a. 通过引用计数实现对内存的管理
b. 游戏内元素的渲染
**/
bool AppDelegate::applicationDidFinishLaunching()
{
    //初始化导演类
    CCDirector *pDirector = CCDirector::sharedDirector();
//如果stop了动画,则调用此函数
    pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
    //设置OpenGL的默认值为true
    pDirector->setDisplayStats(true);


    //设置FPS。默认值是1.0/60,30FPS是一般录像的常用帧数,60是一般游戏的常用帧数
    pDirector->setAnimationInterval(1.0 / 60);


    // 创建一个场景。它是一个自动释放的对象
    CCScene *pScene = HelloWorld::scene();


    //运行场景
    pDirector->runWithScene(pScene);
    return true;
}


/**
游戏中,突然间来了一个接收到电话事件,这时候,调用该函数到后台,暂停游戏
**/
void AppDelegate::applicationDidEnterBackground()
{
//停止游戏脚本
    CCDirector::sharedDirector()->stopAnimation();
//暂停音乐
    SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
}


//游戏恢复
void AppDelegate::applicationWillEnterForeground()
{
//
    CCDirector::sharedDirector()->startAnimation();
//系统恢复
    SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值