您的位置:首页 > 移动开发 > Cocos引擎

cocos2dx[3.8] ——入口类AppDelegate.cpp

2015-10-28 17:46 543 查看
AppDelegate.cpp,这是游戏程序的入口,主要用于游戏程序的逻辑初始化,并创建运行程序的入口界面(即第一个游戏界面场景)。
AppDelegate类似于android的Application的作用,提供一些应用程序级别的状态的回调,整个游戏应用程序由这个文件方法进行控制。

AppDelegate.cpp里面有三个方法:

initGLContextAttrs();//设置
OpenGL环境
applicationDidFinishLaunching();  //逻辑初始化
applicationDidEnterBackground();  //切换到后台
applicationWillEnterForeground(); //切换到前台

AppDelegate.cpp 代码详解:

<span style="font-size:18px;">#include "AppDelegate.h"
#include "HelloWorldScene.h"

//命名空间宏,using name space cocos2d;
USING_NS_CC;

//设置默认分辨率及三种尺寸分辨率,用于适配
static cocos2d::Size designResolutionSize = cocos2d::Size(480, 320);
static cocos2d::Size smallResolutionSize = cocos2d::Size(480, 320);
static cocos2d::Size mediumResolutionSize = cocos2d::Size(1024, 768);
static cocos2d::Size largeResolutionSize = cocos2d::Size(2048, 1536);

AppDelegate::AppDelegate() {

}

AppDelegate::~AppDelegate()
{
}

//设置 OpenGL环境,这个设置对所有平台都有效
void AppDelegate::initGLContextAttrs()
{
//设置OpenGL context 属性,目前只能设置6个属性
//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
bool AppDelegate::applicationDidFinishLaunching()
{
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
glview = GLViewImpl::createWithRect("Test", Rect(0, 0, designResolutionSize.width, designResolutionSize.height));
#else
glview = GLViewImpl::create("Test");
#endif
director->setOpenGLView(glview);
}

// 在屏幕上显示FPS数
// 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);

//分辨率适配
// Set the design resolution
glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER);
Size frameSize = glview->getFrameSize();
// if the frame's height is larger than the height of medium size.
if (frameSize.height > mediumResolutionSize.height)
{
director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width));
}
// if the frame's height is larger than the height of small size.
else if (frameSize.height > smallResolutionSize.height)
{
director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width));
}
// if the frame's height is smaller than the height of medium size.
else
{
director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width));
}

register_all_packages();

//创建一个HelloWorld的scene,程序运行的第一个场景
// create a scene. it's an autorelease object
auto scene = HelloWorld::createScene();

// run
director->runWithScene(scene);

return true;
}

// 切换到后台
// 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 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 you use SimpleAudioEngine, it must resume here
// SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
}
</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息