CCApplication.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /****************************************************************************
  2. Copyright (c) 2013-2016 Chukong Technologies Inc.
  3. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  4. http://www.cocos2d-x.org
  5. Permission is hereby granted, free of charge, to any person obtaining a copy
  6. of this software and associated documentation files (the "Software"), to deal
  7. in the Software without restriction, including without limitation the rights
  8. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  9. copies of the Software, and to permit persons to whom the Software is
  10. furnished to do so, subject to the following conditions:
  11. The above copyright notice and this permission notice shall be included in
  12. all copies or substantial portions of the Software.
  13. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. THE SOFTWARE.
  20. ****************************************************************************/
  21. #pragma once
  22. #include <string>
  23. #include <memory>
  24. #include "base/ccMacros.h"
  25. #include "platform/CCPlatformConfig.h"
  26. #include "platform/CCPlatformDefine.h"
  27. #include "base/CCRenderTexture.h"
  28. NS_CC_BEGIN
  29. class Scheduler;
  30. /**
  31. * @addtogroup platform
  32. * @{
  33. */
  34. class CC_DLL Application
  35. {
  36. public:
  37. /** Since WINDOWS and ANDROID are defined as macros, we could not just use these keywords in enumeration(Platform).
  38. * Therefore, 'OS_' prefix is added to avoid conflicts with the definitions of system macros.
  39. */
  40. enum class Platform
  41. {
  42. WINDOWS, /**< Windows */
  43. LINUX, /**< Linux */
  44. MAC, /**< Mac OS X*/
  45. ANDROIDOS, /**< Android, because ANDROID is a macro, so use ANDROIDOS instead */
  46. IPHONE, /**< iPhone */
  47. IPAD, /**< iPad */
  48. };
  49. enum class LanguageType
  50. {
  51. ENGLISH = 0,
  52. CHINESE,
  53. FRENCH,
  54. ITALIAN,
  55. GERMAN,
  56. SPANISH,
  57. DUTCH,
  58. RUSSIAN,
  59. KOREAN,
  60. JAPANESE,
  61. HUNGARIAN,
  62. PORTUGUESE,
  63. ARABIC,
  64. NORWEGIAN,
  65. POLISH,
  66. TURKISH,
  67. UKRAINIAN,
  68. ROMANIAN,
  69. BULGARIAN
  70. };
  71. enum class PixelFormat
  72. {
  73. RGB8,
  74. RGB565,
  75. RGBA8
  76. };
  77. enum class DepthFormat
  78. {
  79. NONE, // no depth and no stencil
  80. DEPTH_COMPONENT16,
  81. DEPTH_COMPONENT24,
  82. DEPTH_COMPONENT32F,
  83. DEPTH24_STENCIL8,
  84. DEPTH32F_STENCIL8,
  85. STENCIL_INDEX8
  86. };
  87. // This class is useful for internal usage.
  88. static Application* getInstance() { return _instance; }
  89. Application(const std::string& name, int width, int height);
  90. virtual ~Application();
  91. virtual bool applicationDidFinishLaunching();
  92. virtual void onPause();
  93. virtual void onResume();
  94. inline void* getView() const { return _view; }
  95. inline std::shared_ptr<Scheduler> getScheduler() const { return _scheduler; }
  96. inline RenderTexture* getRenderTexture() const { return _renderTexture; }
  97. void runOnMainThread();
  98. void start();
  99. void restart();
  100. void end();
  101. /**
  102. * @brief Sets the preferred frame rate for main loop callback.
  103. * @param fps The preferred frame rate for main loop callback.
  104. */
  105. void setPreferredFramesPerSecond(int fps);
  106. void setMultitouch(bool value);
  107. /**
  108. @brief Get current language config.
  109. @return Current language config.
  110. */
  111. LanguageType getCurrentLanguage() const;
  112. /**
  113. @brief Get current language iso 639-1 code.
  114. @return Current language iso 639-1 code.
  115. */
  116. std::string getCurrentLanguageCode() const;
  117. const cocos2d::Vec2& getViewSize() const;
  118. void updateViewSize(int width, int height);
  119. /**
  120. @brief Get current display stats.
  121. @return bool, is displaying stats or not.
  122. */
  123. bool isDisplayStats();
  124. /**
  125. @brief set display stats information.
  126. */
  127. void setDisplayStats(bool isShow);
  128. void setDevicePixelRatio(uint8_t ratio)
  129. {
  130. if (ratio <= 1)
  131. return;
  132. _devicePixelRatio = ratio;
  133. _isDownsampleEnabled = true;
  134. _renderTexture->init(ratio);
  135. }
  136. inline uint8_t getDevicePixelRatio() const { return _devicePixelRatio; }
  137. inline bool isDownsampleEnabled() const { return _isDownsampleEnabled; }
  138. /** The value is (framebuffer size) / (window size), but on iOS, it is special, its value is 1.
  139. */
  140. float getScreenScale() const;
  141. GLint getMainFBO() const;
  142. /**
  143. @brief Get target platform.
  144. */
  145. Platform getPlatform() const;
  146. /**
  147. @brief Open url in default browser.
  148. @param String with url to open.
  149. @return True if the resource located by the URL was successfully opened; otherwise false.
  150. */
  151. bool openURL(const std::string &url);
  152. void copyTextToClipboard(const std::string &text);
  153. std::string getSystemVersion();
  154. protected:
  155. virtual void onCreateView(PixelFormat& pixelformat, DepthFormat& depthFormat, int& multisamplingCount);
  156. private:
  157. void createView(const std::string& name, int width, int height);
  158. static Application* _instance;
  159. static std::shared_ptr<Scheduler> _scheduler;
  160. void* _view = nullptr;
  161. void* _delegate = nullptr;
  162. RenderTexture* _renderTexture = nullptr;
  163. int _fps = 60;
  164. GLint _mainFBO = 0;
  165. // The ratio to downsample, for example, if its value is 2,
  166. // then the rendering size of render texture is device_resolution/2.
  167. uint8_t _devicePixelRatio = 1;
  168. bool _multiTouch = false;
  169. bool _isStarted = false;
  170. bool _isDownsampleEnabled = false;
  171. cocos2d::Vec2 _viewSize;
  172. };
  173. // end of platform group
  174. /** @} */
  175. NS_CC_END