AppDelegate.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "AppDelegate.h"
  2. #include "BeginScene.h"
  3. // #define USE_AUDIO_ENGINE 1
  4. #define USE_SIMPLE_AUDIO_ENGINE 1
  5. #if USE_AUDIO_ENGINE && USE_SIMPLE_AUDIO_ENGINE
  6. #error "Don't use AudioEngine and SimpleAudioEngine at the same time. Please just select one in your game!"
  7. #endif
  8. #if USE_AUDIO_ENGINE
  9. #include "audio/include/AudioEngine.h"
  10. using namespace cocos2d::experimental;
  11. #elif USE_SIMPLE_AUDIO_ENGINE
  12. #include "audio/include/SimpleAudioEngine.h"
  13. using namespace CocosDenshion;
  14. #endif
  15. USING_NS_CC;
  16. static cocos2d::Size designResolutionSize = cocos2d::Size(1080/2, 1920/2);
  17. AppDelegate::AppDelegate() {
  18. }
  19. AppDelegate::~AppDelegate() {
  20. #if USE_AUDIO_ENGINE
  21. AudioEngine::end();
  22. #elif USE_SIMPLE_AUDIO_ENGINE
  23. SimpleAudioEngine::end();
  24. #endif
  25. }
  26. // if you want a different context, modify the value of glContextAttrs
  27. // it will affect all platforms
  28. void AppDelegate::initGLContextAttrs() {
  29. // set OpenGL context attributes: red,green,blue,alpha,depth,stencil
  30. GLContextAttrs glContextAttrs = {8, 8, 8, 8, 24, 8};
  31. GLView::setGLContextAttrs(glContextAttrs);
  32. }
  33. // if you want to use the package manager to install more packages,
  34. // don't modify or remove this function
  35. static int register_all_packages() {
  36. return 0; //flag for packages manager
  37. }
  38. bool AppDelegate::applicationDidFinishLaunching() {
  39. // initialize director
  40. auto director = Director::getInstance();
  41. auto glview = director->getOpenGLView();
  42. if(!glview) {
  43. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) || (CC_TARGET_PLATFORM == CC_PLATFORM_MAC) || (CC_TARGET_PLATFORM == CC_PLATFORM_LINUX)
  44. director->setContentScaleFactor(2);
  45. glview = GLViewImpl::createWithRect("RedCore", cocos2d::Rect(0, 0, designResolutionSize.width, designResolutionSize.height));
  46. #else
  47. glview = GLViewImpl::create("RedCore");
  48. #endif
  49. director->setOpenGLView(glview);
  50. }
  51. // turn on display FPS
  52. director->setDisplayStats(true);
  53. // set FPS. the default value is 1.0/60 if you don't call this
  54. director->setAnimationInterval(1.0f / 60);
  55. // Set the design resolution
  56. glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER);
  57. auto frameSize = glview->getFrameSize();
  58. // if the frame's height is larger than the height of medium size.
  59. // if (frameSize.height > mediumResolutionSize.height)
  60. // {
  61. // director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width));
  62. // }
  63. // // if the frame's height is larger than the height of small size.
  64. // else if (frameSize.height > smallResolutionSize.height)
  65. // {
  66. // director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width));
  67. // }
  68. // // if the frame's height is smaller than the height of medium size.
  69. // else
  70. // {
  71. // director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width));
  72. // }
  73. // if (!UserDefault::getInstance()->isXMLFileExist()) {
  74. // //CCLOG(UserDefault::getInstance()->getXMLFilePath());
  75. // }
  76. register_all_packages();
  77. // create a scene. it's an autorelease object
  78. // auto scene = BeginScene::createScene();
  79. //
  80. // // run
  81. // director->runWithScene(scene);
  82. Scene* scene = BeginScene::createScene();
  83. director->runWithScene(scene);
  84. return true;
  85. }
  86. // This function will be called when the app is inactive. Note, when receiving a phone call it is invoked.
  87. void AppDelegate::applicationDidEnterBackground() {
  88. Director::getInstance()->stopAnimation();
  89. #if USE_AUDIO_ENGINE
  90. AudioEngine::pauseAll();
  91. #elif USE_SIMPLE_AUDIO_ENGINE
  92. SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
  93. SimpleAudioEngine::getInstance()->pauseAllEffects();
  94. #endif
  95. }
  96. // this function will be called when the app is active again
  97. void AppDelegate::applicationWillEnterForeground() {
  98. Director::getInstance()->startAnimation();
  99. #if USE_AUDIO_ENGINE
  100. AudioEngine::resumeAll();
  101. #elif USE_SIMPLE_AUDIO_ENGINE
  102. SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
  103. SimpleAudioEngine::getInstance()->resumeAllEffects();
  104. #endif
  105. }