//AppDelegate.h
ifndef _APP_DELEGATE_H_
#define _APP_DELEGATE_H_
#include "cocos2d.h"
/**
@brief The cocos2d Application.
The reason for implement as private inheritance is to hide some interface call by Director.
*/
class AppDelegate : private cocos2d::Application
{
public:
AppDelegate();
virtual ~AppDelegate();
virtual void initGLContextAttrs();
/**
@brief Implement Director and Scene init code here.
@return true Initialize success, app continue.
@return false Initialize failed, app terminate.
*/
virtual bool applicationDidFinishLaunching();
/**
@brief The function be called when the application enter background
@param the pointer of the application
*/
virtual void applicationDidEnterBackground();
/**
@brief The function be called when the application enter foreground
@param the pointer of the application
*/
virtual void applicationWillEnterForeground();
};
#endif // _APP_DELEGATE_H_
//AppDelegate.cpp
#include "HNGame.h"
#include "Game/Game/GameManagerBase.h"
USING_NS_CC;
AppDelegate::AppDelegate() {
}
AppDelegate::~AppDelegate()
{
}
//if you want a different context,just modify the value of glContextAttrs
//it will takes effect on all platforms
void AppDelegate::initGLContextAttrs()
{
//set OpenGL context attributions,now can only set six attributions:
//red,green,blue,alpha,depth,stencil
GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
GLView::setGLContextAttrs(glContextAttrs);
}
// If you want to use packages manager to install more packages,
// don‘t modify or remove this function
static int register_all_packages()
{
return 0; //flag for packages manager
}
//当应用程序启动时执行,游戏程序启动入口
//在这里我们启动了第一个scene(场景)
//在具体游戏中通常在这里启动loading界面
//你的游戏从这里开始!
bool AppDelegate::applicationDidFinishLaunching() {
// 初始化 director
auto director = Director::getInstance();//初始化Director
auto glview = director->getOpenGLView(); //获得GLView,也就是游戏窗口
if(!glview) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
//如果当前是以上平台,就创建一个width为960,height为540的窗口
glview = GLViewImpl::createWithRect("GameBase", Rect(0, 0, 960,540));
#else
//其他平台,则使用默认设置(在ios上是全屏窗口,其他平台不太清楚,也有可能是默认设置了一个窗口大小)
glview = GLViewImpl::create(("GameBase");
#endif
director->setOpenGLView(glview);//director获得当前新建的窗口
}
// 在屏幕上显示FPS数
// 开发阶段建议开启这个设置,可以通过这个对自己游戏性能有个大体了解
// 等游戏正式发布时关闭这个设置
director->setDisplayStats(true); //显示帧率信息
// 设置 FPS数 默认值为 1.0/60
director->setAnimationInterval(1.0 / 60); //设置动画帧率,也就是界面刷新帧率咯
register_all_packages(); //使用包管理器
// 创建一个HelloWorld的scene.这个是自动回收的对象
auto scene = HelloWorld::createScene();
//Scene *pScene = HNGame::scene();
// 告诉director运行HelloWorld的scene
director->runWithScene(scene);
//director->runWithScene(pScene);
return true;
}
//static GLViewImpl* createWithRect(const std::string& viewName, Rect size, float frameZoomFactor = 1.0f);//窗口的名字,大小,和缩放比例
// This function will be called when the app is inactive. When comes a phone call,it‘s be invoked too
void AppDelegate::applicationDidEnterBackground() {
Director::getInstance()->stopAnimation();
if (GameManagerBase::pInstanceBase())
{
GameManagerBase::InstanceBase().applicationDidEnterBackground();
}
// if you use SimpleAudioEngine, it must be pause
// SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
}
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
Director::getInstance()->startAnimation();
if (GameManagerBase::pInstanceBase())
{
GameManagerBase::InstanceBase().applicationWillEnterForeground();
}
// if you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}