AppDelegate.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /******************************************************************************
  2. * Spine Runtimes License Agreement
  3. * Last updated April 5, 2025. Replaces all prior versions.
  4. *
  5. * Copyright (c) 2013-2025, Esoteric Software LLC
  6. *
  7. * Integration of the Spine Runtimes into software or otherwise creating
  8. * derivative works of the Spine Runtimes is permitted under the terms and
  9. * conditions of Section 2 of the Spine Editor License Agreement:
  10. * http://esotericsoftware.com/spine-editor-license
  11. *
  12. * Otherwise, it is permitted to integrate the Spine Runtimes into software
  13. * or otherwise create derivative works of the Spine Runtimes (collectively,
  14. * "Products"), provided that each user of the Products must obtain their own
  15. * Spine Editor license and redistribution of the Products in any form must
  16. * include this license and copyright notice.
  17. *
  18. * THE SPINE RUNTIMES ARE PROVIDED BY ESOTERIC SOFTWARE LLC "AS IS" AND ANY
  19. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL ESOTERIC SOFTWARE LLC BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES,
  24. * BUSINESS INTERRUPTION, OR LOSS OF USE, DATA, OR PROFITS) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THE SPINE RUNTIMES, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *****************************************************************************/
  29. #include "AppDelegate.h"
  30. #include <string>
  31. #include <vector>
  32. #include "AppMacros.h"
  33. #include "PhysicsExample.h"
  34. #include <spine/Debug.h>
  35. #include <spine/spine-cocos2dx.h>
  36. USING_NS_CC;
  37. using namespace std;
  38. using namespace spine;
  39. DebugExtension debugExtension(SpineExtension::getInstance());
  40. AppDelegate::AppDelegate() {
  41. }
  42. AppDelegate::~AppDelegate() {
  43. SkeletonBatch::destroyInstance();
  44. SkeletonTwoColorBatch::destroyInstance();
  45. debugExtension.reportLeaks();
  46. }
  47. bool AppDelegate::applicationDidFinishLaunching() {
  48. // initialize director
  49. auto director = Director::getInstance();
  50. auto glview = director->getOpenGLView();
  51. if (!glview) {
  52. GLContextAttrs attrs = {8, 8, 8, 8, 0, 0};
  53. GLView::setGLContextAttrs(attrs);
  54. glview = GLViewImpl::create("Spine Example");
  55. director->setOpenGLView(glview);
  56. }
  57. // Set the design resolution
  58. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8)
  59. // a bug in DirectX 11 level9-x on the device prevents ResolutionPolicy::NO_BORDER from working correctly
  60. glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::SHOW_ALL);
  61. #else
  62. glview->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, ResolutionPolicy::NO_BORDER);
  63. #endif
  64. cocos2d::Size frameSize = glview->getFrameSize();
  65. vector<string> searchPath;
  66. // In this demo, we select resource according to the frame's height.
  67. // If the resource size is different from design resolution size, you need to set contentScaleFactor.
  68. // We use the ratio of resource's height to the height of design resolution,
  69. // this can make sure that the resource's height could fit for the height of design resolution.
  70. if (frameSize.height > mediumResource.size.height) {
  71. // if the frame's height is larger than the height of medium resource size, select large resource.
  72. searchPath.push_back(largeResource.directory);
  73. director->setContentScaleFactor(MIN(largeResource.size.height / designResolutionSize.height, largeResource.size.width / designResolutionSize.width));
  74. } else if (frameSize.height > smallResource.size.height) {
  75. // if the frame's height is larger than the height of small resource size, select medium resource.
  76. searchPath.push_back(mediumResource.directory);
  77. director->setContentScaleFactor(MIN(mediumResource.size.height / designResolutionSize.height, mediumResource.size.width / designResolutionSize.width));
  78. } else {
  79. // if the frame's height is smaller than the height of medium resource size, select small resource.
  80. searchPath.push_back(smallResource.directory);
  81. director->setContentScaleFactor(MIN(smallResource.size.height / designResolutionSize.height, smallResource.size.width / designResolutionSize.width));
  82. }
  83. searchPath.push_back("common");
  84. // set search path
  85. FileUtils::getInstance()->setSearchPaths(searchPath);
  86. // turn on display FPS
  87. director->setDisplayStats(true);
  88. // set FPS. the default value is 1.0/60 if you don't call this
  89. director->setAnimationInterval(1.0f / 60);
  90. // Set the Debug wrapper extension so we know about memory leaks.
  91. SpineExtension::setInstance(&debugExtension);
  92. // create a scene. it's an autorelease object
  93. //auto scene = RaptorExample::scene();
  94. auto scene = PhysicsExample::scene();
  95. // run
  96. director->runWithScene(scene);
  97. return true;
  98. }
  99. // This function will be called when the app is inactive. When comes a phone call,it's be invoked too
  100. void AppDelegate::applicationDidEnterBackground() {
  101. Director::getInstance()->stopAnimation();
  102. // if you use SimpleAudioEngine, it must be paused
  103. // SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
  104. }
  105. // this function will be called when the app is active again
  106. void AppDelegate::applicationWillEnterForeground() {
  107. Director::getInstance()->startAnimation();
  108. // if you use SimpleAudioEngine, it must resume here
  109. // SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
  110. }