#include "AppDelegate.h"
#include "HelloWorldScene.h"
//定义使用音频引擎或简单音频引擎
// #define USE_AUDIO_ENGINE 1
// #define USE_SIMPLE_AUDIO_ENGINE 1
//如果同时使用AudioEngin 和SimpleAudioEngine则报错
#if USE_AUDIO_ENGINE && USE_SIMPLE_AUDIO_ENGINE
#error "Don't use AudioEngine and SimpleAudioEngine at the same time. Please just select one in your game!"
#endif
#if USE_AUDIO_ENGINE
#include "audio/include/AudioEngine.h"
using namespace cocos2d::experimental;
#elif USE_SIMPLE_AUDIO_ENGINE
#include "audio/include/SimpleAudioEngine.h"
using namespace CocosDenshion;
#endif
USING_NS_CC;
//定义设计分辨率和不同分辨率下的屏幕尺寸
static cocos2d::Size designResolutionSize = cocos2d::Size(800, 800);
static cocos2d::Size smallResolutionSize = cocos2d::Size(600, 400);
static cocos2d::Size mediumResolutionSize = cocos2d::Size(1024, 768);
static cocos2d::Size largeResolutionSize = cocos2d::Size(2048, 1536);
//构造函数
AppDelegate::AppDelegate()
{
}
//析构函数
AppDelegate::~AppDelegate()
{
#if USE_AUDIO_ENGINE
AudioEngine::end();
#elif USE_SIMPLE_AUDIO_ENGINE
SimpleAudioEngine::end();
#endif
}
//初始化OpenGL上下文属性
// if you want a different context, modify the value of glContextAttrs
// it will affect all platforms
void AppDelegate::initGLContextAttrs()
{
// set OpenGL context attributes: red,green,blue,alpha,depth,stencil,multisamplesCount
GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8, 0};
GLView::setGLContextAttrs(glContextAttrs);
}
//注册所有的扩展包
// if you want to use the package manager to install more packages,
// don't modify or remove this function
static int register_all_packages()
{
return 0; //flag for packages manager
}
//应用程序启动时调用
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director初始化导演
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
//如果OpenGLView不存在,则创建一个
if(!glview) {
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
glview = GLViewImpl::createWithRect("2020152093helloworld", cocos2d::Rect(0, 0, designResolutionSize.width, designResolutionSize.height));//设置窗口信息
#else
glview = GLViewImpl::create("HelloWorld");
#endif
director->setOpenGLView(glview);
}
// turn on display FPS显示fps
director->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this设置fps
director->setAnimationInterval(1.0f / 60);
// Set the design resolution设置设计分辨率和屏幕适配
glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER);
auto 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();
// create a scene. it's an autorelease object创建helloworld场景
auto scene = HelloWorld::createScene();
// run运行场景
director->runWithScene(scene);
return true;
}
//当应用程序进入后台时调用
// This function will be called when the app is inactive. Note, when receiving a phone call it is invoked.
void AppDelegate::applicationDidEnterBackground() {
Director::getInstance()->stopAnimation();
#if USE_AUDIO_ENGINE
AudioEngine::pauseAll();
#elif USE_SIMPLE_AUDIO_ENGINE
SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
SimpleAudioEngine::getInstance()->pauseAllEffects();
#endif
}
//当应用程序恢复到前台时调用
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground() {
Director::getInstance()->startAnimation();
#if USE_AUDIO_ENGINE
AudioEngine::resumeAll();
#elif USE_SIMPLE_AUDIO_ENGINE
SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
SimpleAudioEngine::getInstance()->resumeAllEffects();
#endif
}
cocos2dx中的AppDelegate.cpp
于 2023-03-12 16:33:21 首次发布