CCGLViewImpl-ios.mm 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2017 Chukong Technologies Inc.
  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. #include "platform/CCPlatformConfig.h"
  22. #if CC_TARGET_PLATFORM == CC_PLATFORM_IOS
  23. #import <UIKit/UIKit.h>
  24. #include "platform/ios/CCEAGLView-ios.h"
  25. #include "platform/ios/CCDirectorCaller-ios.h"
  26. #include "platform/ios/CCGLViewImpl-ios.h"
  27. #include "base/CCTouch.h"
  28. #include "base/CCDirector.h"
  29. NS_CC_BEGIN
  30. void* GLViewImpl::_pixelFormat = kEAGLColorFormatRGB565;
  31. int GLViewImpl::_depthFormat = GL_DEPTH_COMPONENT16;
  32. int GLViewImpl::_multisamplingCount = 0;
  33. GLViewImpl* GLViewImpl::createWithEAGLView(void *eaglview)
  34. {
  35. auto ret = new (std::nothrow) GLViewImpl;
  36. if(ret && ret->initWithEAGLView(eaglview)) {
  37. ret->autorelease();
  38. return ret;
  39. }
  40. CC_SAFE_DELETE(ret);
  41. return nullptr;
  42. }
  43. GLViewImpl* GLViewImpl::create(const std::string& viewName)
  44. {
  45. auto ret = new (std::nothrow) GLViewImpl;
  46. if(ret && ret->initWithFullScreen(viewName)) {
  47. ret->autorelease();
  48. return ret;
  49. }
  50. CC_SAFE_DELETE(ret);
  51. return nullptr;
  52. }
  53. GLViewImpl* GLViewImpl::createWithRect(const std::string& viewName, const Rect& rect, float frameZoomFactor)
  54. {
  55. auto ret = new (std::nothrow) GLViewImpl;
  56. if(ret && ret->initWithRect(viewName, rect, frameZoomFactor)) {
  57. ret->autorelease();
  58. return ret;
  59. }
  60. CC_SAFE_DELETE(ret);
  61. return nullptr;
  62. }
  63. GLViewImpl* GLViewImpl::createWithFullScreen(const std::string& viewName)
  64. {
  65. auto ret = new (std::nothrow) GLViewImpl();
  66. if(ret && ret->initWithFullScreen(viewName)) {
  67. ret->autorelease();
  68. return ret;
  69. }
  70. CC_SAFE_DELETE(ret);
  71. return nullptr;
  72. }
  73. void GLViewImpl::convertAttrs()
  74. {
  75. if(_glContextAttrs.redBits==8 && _glContextAttrs.greenBits==8 && _glContextAttrs.blueBits==8 && _glContextAttrs.alphaBits==8)
  76. {
  77. _pixelFormat = kEAGLColorFormatRGBA8;
  78. } else if (_glContextAttrs.redBits==5 && _glContextAttrs.greenBits==6 && _glContextAttrs.blueBits==5 && _glContextAttrs.alphaBits==0)
  79. {
  80. _pixelFormat = kEAGLColorFormatRGB565;
  81. } else
  82. {
  83. CCASSERT(0, "Unsupported render buffer pixel format. Using default");
  84. }
  85. if(_glContextAttrs.depthBits==24 && _glContextAttrs.stencilBits==8)
  86. {
  87. _depthFormat = GL_DEPTH24_STENCIL8_OES;
  88. } else if (_glContextAttrs.depthBits==0 && _glContextAttrs.stencilBits==0)
  89. {
  90. _depthFormat = 0;
  91. } else
  92. {
  93. CCASSERT(0, "Unsupported format for depth and stencil buffers. Using default");
  94. }
  95. _multisamplingCount = _glContextAttrs.multisamplingCount;
  96. }
  97. GLViewImpl::GLViewImpl()
  98. {
  99. }
  100. GLViewImpl::~GLViewImpl()
  101. {
  102. //CCEAGLView *glview = (CCEAGLView*) _eaglview;
  103. //[glview release];
  104. }
  105. bool GLViewImpl::initWithEAGLView(void *eaglview)
  106. {
  107. _eaglview = eaglview;
  108. CCEAGLView *glview = (CCEAGLView*) _eaglview;
  109. _screenSize.width = _designResolutionSize.width = [glview getWidth];
  110. _screenSize.height = _designResolutionSize.height = [glview getHeight];
  111. // _scaleX = _scaleY = [glview contentScaleFactor];
  112. return true;
  113. }
  114. bool GLViewImpl::initWithRect(const std::string& viewName, const Rect& rect, float frameZoomFactor)
  115. {
  116. CGRect r = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
  117. convertAttrs();
  118. CCEAGLView *eaglview = [CCEAGLView viewWithFrame: r
  119. pixelFormat: (NSString*)_pixelFormat
  120. depthFormat: _depthFormat
  121. preserveBackbuffer: NO
  122. sharegroup: nil
  123. multiSampling: NO
  124. numberOfSamples: 0];
  125. // Not available on tvOS
  126. #if !defined(CC_TARGET_OS_TVOS)
  127. [eaglview setMultipleTouchEnabled:YES];
  128. #endif
  129. _screenSize.width = _designResolutionSize.width = [eaglview getWidth];
  130. _screenSize.height = _designResolutionSize.height = [eaglview getHeight];
  131. // _scaleX = _scaleY = [eaglview contentScaleFactor];
  132. _eaglview = eaglview;
  133. return true;
  134. }
  135. bool GLViewImpl::initWithFullScreen(const std::string& viewName)
  136. {
  137. CGRect rect = [[UIScreen mainScreen] bounds];
  138. Rect r;
  139. r.origin.x = rect.origin.x;
  140. r.origin.y = rect.origin.y;
  141. r.size.width = rect.size.width;
  142. r.size.height = rect.size.height;
  143. return initWithRect(viewName, r, 1);
  144. }
  145. bool GLViewImpl::isOpenGLReady()
  146. {
  147. return _eaglview != nullptr;
  148. }
  149. bool GLViewImpl::setContentScaleFactor(float contentScaleFactor)
  150. {
  151. CC_ASSERT(_resolutionPolicy == ResolutionPolicy::UNKNOWN); // cannot enable retina mode
  152. _scaleX = _scaleY = contentScaleFactor;
  153. CCEAGLView *eaglview = (CCEAGLView*) _eaglview;
  154. [eaglview setNeedsLayout];
  155. return true;
  156. }
  157. float GLViewImpl::getContentScaleFactor() const
  158. {
  159. CCEAGLView *eaglview = (CCEAGLView*) _eaglview;
  160. float scaleFactor = [eaglview contentScaleFactor];
  161. // CCASSERT(scaleFactor == _scaleX == _scaleY, "Logic error in GLView::getContentScaleFactor");
  162. return scaleFactor;
  163. }
  164. void GLViewImpl::end()
  165. {
  166. [CCDirectorCaller destroy];
  167. // destroy EAGLView
  168. CCEAGLView *eaglview = (CCEAGLView*) _eaglview;
  169. [eaglview removeFromSuperview];
  170. //[eaglview release];
  171. release();
  172. }
  173. void GLViewImpl::swapBuffers()
  174. {
  175. CCEAGLView *eaglview = (CCEAGLView*) _eaglview;
  176. [eaglview swapBuffers];
  177. }
  178. void GLViewImpl::setIMEKeyboardState(bool open)
  179. {
  180. CCEAGLView *eaglview = (CCEAGLView*) _eaglview;
  181. if (open)
  182. {
  183. [eaglview becomeFirstResponder];
  184. }
  185. else
  186. {
  187. [eaglview resignFirstResponder];
  188. }
  189. }
  190. Rect GLViewImpl::getSafeAreaRect() const
  191. {
  192. CCEAGLView *eaglview = (CCEAGLView*) _eaglview;
  193. #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 110000
  194. float version = [[UIDevice currentDevice].systemVersion floatValue];
  195. if (version >= 11.0f)
  196. {
  197. #pragma clang diagnostic push
  198. #pragma clang diagnostic ignored "-Wpartial-availability"
  199. UIEdgeInsets safeAreaInsets = eaglview.safeAreaInsets;
  200. #pragma clang diagnostic pop
  201. // Multiply contentScaleFactor since safeAreaInsets return points.
  202. safeAreaInsets.left *= eaglview.contentScaleFactor;
  203. safeAreaInsets.right *= eaglview.contentScaleFactor;
  204. safeAreaInsets.top *= eaglview.contentScaleFactor;
  205. safeAreaInsets.bottom *= eaglview.contentScaleFactor;
  206. // Get leftBottom and rightTop point in UI coordinates
  207. Vec2 leftBottom = Vec2(safeAreaInsets.left, _screenSize.height - safeAreaInsets.bottom);
  208. Vec2 rightTop = Vec2(_screenSize.width - safeAreaInsets.right, safeAreaInsets.top);
  209. // Convert a point from UI coordinates to which in design resolution coordinate.
  210. leftBottom.x = (leftBottom.x - _viewPortRect.origin.x) / _scaleX,
  211. leftBottom.y = (leftBottom.y - _viewPortRect.origin.y) / _scaleY;
  212. rightTop.x = (rightTop.x - _viewPortRect.origin.x) / _scaleX,
  213. rightTop.y = (rightTop.y - _viewPortRect.origin.y) / _scaleY;
  214. // Adjust points to make them inside design resolution
  215. leftBottom.x = MAX(leftBottom.x, 0);
  216. leftBottom.y = MIN(leftBottom.y, _designResolutionSize.height);
  217. rightTop.x = MIN(rightTop.x, _designResolutionSize.width);
  218. rightTop.y = MAX(rightTop.y, 0);
  219. // Convert to GL coordinates
  220. leftBottom = Director::getInstance()->convertToGL(leftBottom);
  221. rightTop = Director::getInstance()->convertToGL(rightTop);
  222. return Rect(leftBottom.x, leftBottom.y, rightTop.x - leftBottom.x, rightTop.y - leftBottom.y);
  223. }
  224. #endif
  225. // If running on iOS devices lower than 11.0, return visiable rect instead.
  226. return GLView::getSafeAreaRect();
  227. }
  228. NS_CC_END
  229. #endif // CC_PLATFORM_IOS