2013-02-19 15:38:30 +08:00
|
|
|
#include "AppDelegate.h"
|
|
|
|
#include "HelloWorldScene.h"
|
|
|
|
|
2017-03-22 11:19:25 +08:00
|
|
|
// #define USE_AUDIO_ENGINE 1
|
|
|
|
// #define USE_SIMPLE_AUDIO_ENGINE 1
|
|
|
|
|
|
|
|
#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
|
|
|
|
|
2013-02-19 15:38:30 +08:00
|
|
|
USING_NS_CC;
|
|
|
|
|
2015-06-05 09:39:15 +08:00
|
|
|
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);
|
|
|
|
|
2016-05-25 22:18:57 +08:00
|
|
|
AppDelegate::AppDelegate()
|
|
|
|
{
|
2013-02-19 15:38:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
AppDelegate::~AppDelegate()
|
|
|
|
{
|
2017-03-22 11:19:25 +08:00
|
|
|
#if USE_AUDIO_ENGINE
|
|
|
|
AudioEngine::end();
|
|
|
|
#elif USE_SIMPLE_AUDIO_ENGINE
|
|
|
|
SimpleAudioEngine::end();
|
|
|
|
#endif
|
2013-02-19 15:38:30 +08:00
|
|
|
}
|
|
|
|
|
2016-05-25 22:18:57 +08:00
|
|
|
// if you want a different context, modify the value of glContextAttrs
|
|
|
|
// it will affect all platforms
|
2014-08-22 16:22:16 +08:00
|
|
|
void AppDelegate::initGLContextAttrs()
|
|
|
|
{
|
2018-01-19 09:28:22 +08:00
|
|
|
// set OpenGL context attributes: red,green,blue,alpha,depth,stencil,multisamplesCount
|
|
|
|
GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8, 0};
|
2014-08-22 16:22:16 +08:00
|
|
|
|
|
|
|
GLView::setGLContextAttrs(glContextAttrs);
|
|
|
|
}
|
|
|
|
|
2016-05-25 22:18:57 +08:00
|
|
|
// if you want to use the package manager to install more packages,
|
2015-02-25 00:25:40 +08:00
|
|
|
// don't modify or remove this function
|
|
|
|
static int register_all_packages()
|
|
|
|
{
|
|
|
|
return 0; //flag for packages manager
|
|
|
|
}
|
|
|
|
|
2013-02-19 15:38:30 +08:00
|
|
|
bool AppDelegate::applicationDidFinishLaunching() {
|
|
|
|
// initialize director
|
2013-09-19 07:50:26 +08:00
|
|
|
auto director = Director::getInstance();
|
2014-01-31 13:17:55 +08:00
|
|
|
auto glview = director->getOpenGLView();
|
|
|
|
if(!glview) {
|
2015-07-14 15:18:06 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
|
2016-03-04 11:21:30 +08:00
|
|
|
glview = GLViewImpl::createWithRect("HelloCpp", cocos2d::Rect(0, 0, designResolutionSize.width, designResolutionSize.height));
|
2015-07-14 15:18:06 +08:00
|
|
|
#else
|
|
|
|
glview = GLViewImpl::create("HelloCpp");
|
|
|
|
#endif
|
2014-01-31 13:17:55 +08:00
|
|
|
director->setOpenGLView(glview);
|
|
|
|
}
|
2013-02-19 15:38:30 +08:00
|
|
|
|
|
|
|
// turn on display FPS
|
2013-07-28 00:16:16 +08:00
|
|
|
director->setDisplayStats(true);
|
2013-02-19 15:38:30 +08:00
|
|
|
|
|
|
|
// set FPS. the default value is 1.0/60 if you don't call this
|
2016-04-20 03:01:06 +08:00
|
|
|
director->setAnimationInterval(1.0f / 60);
|
2013-02-19 15:38:30 +08:00
|
|
|
|
2015-06-05 09:39:15 +08:00
|
|
|
// Set the design resolution
|
|
|
|
glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER);
|
2016-03-04 11:21:30 +08:00
|
|
|
auto frameSize = glview->getFrameSize();
|
2015-06-05 09:39:15 +08:00
|
|
|
// 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));
|
|
|
|
}
|
|
|
|
|
2015-02-25 00:25:40 +08:00
|
|
|
register_all_packages();
|
|
|
|
|
2013-02-19 15:38:30 +08:00
|
|
|
// create a scene. it's an autorelease object
|
2013-09-19 07:50:26 +08:00
|
|
|
auto scene = HelloWorld::createScene();
|
2013-02-19 15:38:30 +08:00
|
|
|
|
|
|
|
// run
|
2013-07-28 00:16:16 +08:00
|
|
|
director->runWithScene(scene);
|
2013-02-19 15:38:30 +08:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2016-05-25 22:18:57 +08:00
|
|
|
// This function will be called when the app is inactive. Note, when receiving a phone call it is invoked.
|
2013-02-19 15:38:30 +08:00
|
|
|
void AppDelegate::applicationDidEnterBackground() {
|
2013-07-12 11:50:36 +08:00
|
|
|
Director::getInstance()->stopAnimation();
|
2013-02-19 15:38:30 +08:00
|
|
|
|
2017-03-22 11:19:25 +08:00
|
|
|
#if USE_AUDIO_ENGINE
|
|
|
|
AudioEngine::pauseAll();
|
|
|
|
#elif USE_SIMPLE_AUDIO_ENGINE
|
|
|
|
SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
|
|
|
|
SimpleAudioEngine::getInstance()->pauseAllEffects();
|
|
|
|
#endif
|
2013-02-19 15:38:30 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// this function will be called when the app is active again
|
|
|
|
void AppDelegate::applicationWillEnterForeground() {
|
2013-07-12 11:50:36 +08:00
|
|
|
Director::getInstance()->startAnimation();
|
2013-02-19 15:38:30 +08:00
|
|
|
|
2017-03-22 11:19:25 +08:00
|
|
|
#if USE_AUDIO_ENGINE
|
|
|
|
AudioEngine::resumeAll();
|
|
|
|
#elif USE_SIMPLE_AUDIO_ENGINE
|
|
|
|
SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
|
|
|
|
SimpleAudioEngine::getInstance()->resumeAllEffects();
|
|
|
|
#endif
|
2013-02-19 15:38:30 +08:00
|
|
|
}
|