AppDelegate.cpp 4.2 KB

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