CCGLViewImpl-ios.mm 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. NS_CC_BEGIN
  29. void* GLViewImpl::_pixelFormat = kEAGLColorFormatRGB565;
  30. int GLViewImpl::_depthFormat = GL_DEPTH_COMPONENT16;
  31. GLViewImpl* GLViewImpl::createWithEAGLView(void *eaglview)
  32. {
  33. auto ret = new (std::nothrow) GLViewImpl;
  34. if(ret && ret->initWithEAGLView(eaglview)) {
  35. ret->autorelease();
  36. return ret;
  37. }
  38. CC_SAFE_DELETE(ret);
  39. return nullptr;
  40. }
  41. GLViewImpl* GLViewImpl::create(const std::string& viewName)
  42. {
  43. auto ret = new (std::nothrow) GLViewImpl;
  44. if(ret && ret->initWithFullScreen(viewName)) {
  45. ret->autorelease();
  46. return ret;
  47. }
  48. CC_SAFE_DELETE(ret);
  49. return nullptr;
  50. }
  51. GLViewImpl* GLViewImpl::createWithRect(const std::string& viewName, const Rect& rect, float frameZoomFactor)
  52. {
  53. auto ret = new (std::nothrow) GLViewImpl;
  54. if(ret && ret->initWithRect(viewName, rect, frameZoomFactor)) {
  55. ret->autorelease();
  56. return ret;
  57. }
  58. CC_SAFE_DELETE(ret);
  59. return nullptr;
  60. }
  61. GLViewImpl* GLViewImpl::createWithFullScreen(const std::string& viewName)
  62. {
  63. auto ret = new (std::nothrow) GLViewImpl();
  64. if(ret && ret->initWithFullScreen(viewName)) {
  65. ret->autorelease();
  66. return ret;
  67. }
  68. CC_SAFE_DELETE(ret);
  69. return nullptr;
  70. }
  71. void GLViewImpl::convertAttrs()
  72. {
  73. if(_glContextAttrs.redBits==8 && _glContextAttrs.greenBits==8 && _glContextAttrs.blueBits==8 && _glContextAttrs.alphaBits==8)
  74. {
  75. _pixelFormat = kEAGLColorFormatRGBA8;
  76. } else if (_glContextAttrs.redBits==5 && _glContextAttrs.greenBits==6 && _glContextAttrs.blueBits==5 && _glContextAttrs.alphaBits==0)
  77. {
  78. _pixelFormat = kEAGLColorFormatRGB565;
  79. } else
  80. {
  81. CCASSERT(0, "Unsupported render buffer pixel format. Using default");
  82. }
  83. if(_glContextAttrs.depthBits==24 && _glContextAttrs.stencilBits==8)
  84. {
  85. _depthFormat = GL_DEPTH24_STENCIL8_OES;
  86. } else if (_glContextAttrs.depthBits==0 && _glContextAttrs.stencilBits==0)
  87. {
  88. _depthFormat = 0;
  89. } else
  90. {
  91. CCASSERT(0, "Unsupported format for depth and stencil buffers. Using default");
  92. }
  93. }
  94. GLViewImpl::GLViewImpl()
  95. {
  96. }
  97. GLViewImpl::~GLViewImpl()
  98. {
  99. //CCEAGLView *glview = (CCEAGLView*) _eaglview;
  100. //[glview release];
  101. }
  102. bool GLViewImpl::initWithEAGLView(void *eaglview)
  103. {
  104. _eaglview = eaglview;
  105. CCEAGLView *glview = (CCEAGLView*) _eaglview;
  106. _screenSize.width = _designResolutionSize.width = [glview getWidth];
  107. _screenSize.height = _designResolutionSize.height = [glview getHeight];
  108. // _scaleX = _scaleY = [glview contentScaleFactor];
  109. return true;
  110. }
  111. bool GLViewImpl::initWithRect(const std::string& viewName, const Rect& rect, float frameZoomFactor)
  112. {
  113. CGRect r = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
  114. convertAttrs();
  115. CCEAGLView *eaglview = [CCEAGLView viewWithFrame: r
  116. pixelFormat: (NSString*)_pixelFormat
  117. depthFormat: _depthFormat
  118. preserveBackbuffer: NO
  119. sharegroup: nil
  120. multiSampling: NO
  121. numberOfSamples: 0];
  122. // Not available on tvOS
  123. #if !defined(CC_TARGET_OS_TVOS)
  124. [eaglview setMultipleTouchEnabled:YES];
  125. #endif
  126. _screenSize.width = _designResolutionSize.width = [eaglview getWidth];
  127. _screenSize.height = _designResolutionSize.height = [eaglview getHeight];
  128. // _scaleX = _scaleY = [eaglview contentScaleFactor];
  129. _eaglview = eaglview;
  130. return true;
  131. }
  132. bool GLViewImpl::initWithFullScreen(const std::string& viewName)
  133. {
  134. CGRect rect = [[UIScreen mainScreen] bounds];
  135. Rect r;
  136. r.origin.x = rect.origin.x;
  137. r.origin.y = rect.origin.y;
  138. r.size.width = rect.size.width;
  139. r.size.height = rect.size.height;
  140. return initWithRect(viewName, r, 1);
  141. }
  142. bool GLViewImpl::isOpenGLReady()
  143. {
  144. return _eaglview != nullptr;
  145. }
  146. bool GLViewImpl::setContentScaleFactor(float contentScaleFactor)
  147. {
  148. CC_ASSERT(_resolutionPolicy == ResolutionPolicy::UNKNOWN); // cannot enable retina mode
  149. _scaleX = _scaleY = contentScaleFactor;
  150. CCEAGLView *eaglview = (CCEAGLView*) _eaglview;
  151. [eaglview setNeedsLayout];
  152. return true;
  153. }
  154. float GLViewImpl::getContentScaleFactor() const
  155. {
  156. CCEAGLView *eaglview = (CCEAGLView*) _eaglview;
  157. float scaleFactor = [eaglview contentScaleFactor];
  158. // CCASSERT(scaleFactor == _scaleX == _scaleY, "Logic error in GLView::getContentScaleFactor");
  159. return scaleFactor;
  160. }
  161. void GLViewImpl::end()
  162. {
  163. [CCDirectorCaller destroy];
  164. // destroy EAGLView
  165. CCEAGLView *eaglview = (CCEAGLView*) _eaglview;
  166. [eaglview removeFromSuperview];
  167. //[eaglview release];
  168. release();
  169. }
  170. void GLViewImpl::swapBuffers()
  171. {
  172. CCEAGLView *eaglview = (CCEAGLView*) _eaglview;
  173. [eaglview swapBuffers];
  174. }
  175. void GLViewImpl::setIMEKeyboardState(bool open)
  176. {
  177. CCEAGLView *eaglview = (CCEAGLView*) _eaglview;
  178. if (open)
  179. {
  180. [eaglview becomeFirstResponder];
  181. }
  182. else
  183. {
  184. [eaglview resignFirstResponder];
  185. }
  186. }
  187. NS_CC_END
  188. #endif // CC_PLATFOR_IOS