AppDelegate.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. #if DEBUG
  53. director->setDisplayStats(true);
  54. #endif
  55. // set FPS. the default value is 1.0/60 if you don't call this
  56. director->setAnimationInterval(1.0f / 60);
  57. // Set the design resolution
  58. glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER);
  59. auto frameSize = glview->getFrameSize();
  60. // if the frame's height is larger than the height of medium size.
  61. // if (frameSize.height > mediumResolutionSize.height)
  62. // {
  63. // director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height, largeResolutionSize.width/designResolutionSize.width));
  64. // }
  65. // // if the frame's height is larger than the height of small size.
  66. // else if (frameSize.height > smallResolutionSize.height)
  67. // {
  68. // director->setContentScaleFactor(MIN(mediumResolutionSize.height/designResolutionSize.height, mediumResolutionSize.width/designResolutionSize.width));
  69. // }
  70. // // if the frame's height is smaller than the height of medium size.
  71. // else
  72. // {
  73. // director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height, smallResolutionSize.width/designResolutionSize.width));
  74. // }
  75. register_all_packages();
  76. Scene* scene = BeginScene::createScene();
  77. director->runWithScene(scene);
  78. return true;
  79. }
  80. // This function will be called when the app is inactive. Note, when receiving a phone call it is invoked.
  81. void AppDelegate::applicationDidEnterBackground() {
  82. Director::getInstance()->stopAnimation();
  83. #if USE_AUDIO_ENGINE
  84. AudioEngine::pauseAll();
  85. #elif USE_SIMPLE_AUDIO_ENGINE
  86. SimpleAudioEngine::getInstance()->pauseBackgroundMusic();
  87. SimpleAudioEngine::getInstance()->pauseAllEffects();
  88. #endif
  89. }
  90. // this function will be called when the app is active again
  91. void AppDelegate::applicationWillEnterForeground() {
  92. Director::getInstance()->startAnimation();
  93. #if USE_AUDIO_ENGINE
  94. AudioEngine::resumeAll();
  95. #elif USE_SIMPLE_AUDIO_ENGINE
  96. SimpleAudioEngine::getInstance()->resumeBackgroundMusic();
  97. SimpleAudioEngine::getInstance()->resumeAllEffects();
  98. #endif
  99. }