#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();
}
/**
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();
}