CCGLView.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2013-2016 Chukong Technologies Inc.
  4. Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
  5. http://www.cocos2d-x.org
  6. Permission is hereby granted, free of charge, to any person obtaining a copy
  7. of this software and associated documentation files (the "Software"), to deal
  8. in the Software without restriction, including without limitation the rights
  9. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. copies of the Software, and to permit persons to whom the Software is
  11. furnished to do so, subject to the following conditions:
  12. The above copyright notice and this permission notice shall be included in
  13. all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. THE SOFTWARE.
  21. ****************************************************************************/
  22. #include "platform/CCGLView.h"
  23. #include "base/CCTouch.h"
  24. #include "base/CCDirector.h"
  25. #include "base/CCEventDispatcher.h"
  26. #include "2d/CCCamera.h"
  27. #include "2d/CCScene.h"
  28. #include "renderer/CCRenderer.h"
  29. #include "vr/CCVRProtocol.h"
  30. #include "vr/CCVRGenericRenderer.h"
  31. NS_CC_BEGIN
  32. namespace {
  33. static Touch* g_touches[EventTouch::MAX_TOUCHES] = { nullptr };
  34. static unsigned int g_indexBitsUsed = 0;
  35. // System touch pointer ID (It may not be ascending order number) <-> Ascending order number from 0
  36. static std::map<intptr_t, int> g_touchIdReorderMap;
  37. static int getUnUsedIndex()
  38. {
  39. int i;
  40. int temp = g_indexBitsUsed;
  41. for (i = 0; i < EventTouch::MAX_TOUCHES; i++) {
  42. if (! (temp & 0x00000001)) {
  43. g_indexBitsUsed |= (1 << i);
  44. return i;
  45. }
  46. temp >>= 1;
  47. }
  48. // all bits are used
  49. return -1;
  50. }
  51. static std::vector<Touch*> getAllTouchesVector()
  52. {
  53. std::vector<Touch*> ret;
  54. int i;
  55. int temp = g_indexBitsUsed;
  56. for (i = 0; i < EventTouch::MAX_TOUCHES; i++) {
  57. if ( temp & 0x00000001) {
  58. ret.push_back(g_touches[i]);
  59. }
  60. temp >>= 1;
  61. }
  62. return ret;
  63. }
  64. static void removeUsedIndexBit(int index)
  65. {
  66. if (index < 0 || index >= EventTouch::MAX_TOUCHES)
  67. {
  68. return;
  69. }
  70. unsigned int temp = 1 << index;
  71. temp = ~temp;
  72. g_indexBitsUsed &= temp;
  73. }
  74. }
  75. //default context attributions are set as follows
  76. GLContextAttrs GLView::_glContextAttrs = {8, 8, 8, 8, 24, 8, 0};
  77. void GLView::setGLContextAttrs(GLContextAttrs& glContextAttrs)
  78. {
  79. _glContextAttrs = glContextAttrs;
  80. }
  81. GLContextAttrs GLView::getGLContextAttrs()
  82. {
  83. return _glContextAttrs;
  84. }
  85. GLView::GLView()
  86. : _scaleX(1.0f)
  87. , _scaleY(1.0f)
  88. , _resolutionPolicy(ResolutionPolicy::UNKNOWN)
  89. , _vrImpl(nullptr)
  90. , _designResolutionSize(0,0)
  91. , _screenSize(0,0)
  92. {
  93. }
  94. GLView::~GLView()
  95. {
  96. }
  97. void GLView::pollInputEvents()
  98. {
  99. pollEvents();
  100. }
  101. void GLView::pollEvents()
  102. {
  103. }
  104. void GLView::updateDesignResolutionSize()
  105. {
  106. if (_screenSize.width > 0 && _screenSize.height > 0
  107. && _designResolutionSize.width > 0 && _designResolutionSize.height > 0)
  108. {
  109. _scaleX = (float)_screenSize.width / _designResolutionSize.width;
  110. _scaleY = (float)_screenSize.height / _designResolutionSize.height;
  111. if (_resolutionPolicy == ResolutionPolicy::NO_BORDER)
  112. {
  113. _scaleX = _scaleY = MAX(_scaleX, _scaleY);
  114. }
  115. else if (_resolutionPolicy == ResolutionPolicy::SHOW_ALL)
  116. {
  117. _scaleX = _scaleY = MIN(_scaleX, _scaleY);
  118. }
  119. else if ( _resolutionPolicy == ResolutionPolicy::FIXED_HEIGHT) {
  120. _scaleX = _scaleY;
  121. _designResolutionSize.width = ceilf(_screenSize.width/_scaleX);
  122. }
  123. else if ( _resolutionPolicy == ResolutionPolicy::FIXED_WIDTH) {
  124. _scaleY = _scaleX;
  125. _designResolutionSize.height = ceilf(_screenSize.height/_scaleY);
  126. }
  127. // calculate the rect of viewport
  128. float viewPortW = _designResolutionSize.width * _scaleX;
  129. float viewPortH = _designResolutionSize.height * _scaleY;
  130. _viewPortRect.setRect((_screenSize.width - viewPortW) / 2, (_screenSize.height - viewPortH) / 2, viewPortW, viewPortH);
  131. // reset director's member variables to fit visible rect
  132. auto director = Director::getInstance();
  133. director->_winSizeInPoints = getDesignResolutionSize();
  134. director->_isStatusLabelUpdated = true;
  135. director->setProjection(director->getProjection());
  136. // Github issue #16139
  137. // A default viewport is needed in order to display the FPS,
  138. // since the FPS are rendered in the Director, and there is no viewport there.
  139. // Everything, including the FPS should renderer in the Scene.
  140. glViewport(0, 0, _screenSize.width, _screenSize.height);
  141. }
  142. }
  143. void GLView::setDesignResolutionSize(float width, float height, ResolutionPolicy resolutionPolicy)
  144. {
  145. CCASSERT(resolutionPolicy != ResolutionPolicy::UNKNOWN, "should set resolutionPolicy");
  146. if (width == 0.0f || height == 0.0f)
  147. {
  148. return;
  149. }
  150. _designResolutionSize.setSize(width, height);
  151. _resolutionPolicy = resolutionPolicy;
  152. updateDesignResolutionSize();
  153. }
  154. const Size& GLView::getDesignResolutionSize() const
  155. {
  156. return _designResolutionSize;
  157. }
  158. Size GLView::getFrameSize() const
  159. {
  160. return _screenSize;
  161. }
  162. void GLView::setFrameSize(float width, float height)
  163. {
  164. _screenSize = Size(width, height);
  165. // Github issue #16003 and #16485
  166. // only update the designResolution if it wasn't previously set
  167. if (_designResolutionSize.equals(Size::ZERO))
  168. _designResolutionSize = _screenSize;
  169. }
  170. Rect GLView::getVisibleRect() const
  171. {
  172. Rect ret;
  173. ret.size = getVisibleSize();
  174. ret.origin = getVisibleOrigin();
  175. return ret;
  176. }
  177. Rect GLView::getSafeAreaRect() const
  178. {
  179. return getVisibleRect();
  180. }
  181. Size GLView::getVisibleSize() const
  182. {
  183. if (_resolutionPolicy == ResolutionPolicy::NO_BORDER)
  184. {
  185. return Size(_screenSize.width/_scaleX, _screenSize.height/_scaleY);
  186. }
  187. else
  188. {
  189. return _designResolutionSize;
  190. }
  191. }
  192. Vec2 GLView::getVisibleOrigin() const
  193. {
  194. if (_resolutionPolicy == ResolutionPolicy::NO_BORDER)
  195. {
  196. return Vec2((_designResolutionSize.width - _screenSize.width/_scaleX)/2,
  197. (_designResolutionSize.height - _screenSize.height/_scaleY)/2);
  198. }
  199. else
  200. {
  201. return Vec2::ZERO;
  202. }
  203. }
  204. void GLView::setViewPortInPoints(float x , float y , float w , float h)
  205. {
  206. experimental::Viewport vp((float)(x * _scaleX + _viewPortRect.origin.x),
  207. (float)(y * _scaleY + _viewPortRect.origin.y),
  208. (float)(w * _scaleX),
  209. (float)(h * _scaleY));
  210. Camera::setDefaultViewport(vp);
  211. }
  212. void GLView::setScissorInPoints(float x , float y , float w , float h)
  213. {
  214. glScissor((GLint)(x * _scaleX + _viewPortRect.origin.x),
  215. (GLint)(y * _scaleY + _viewPortRect.origin.y),
  216. (GLsizei)(w * _scaleX),
  217. (GLsizei)(h * _scaleY));
  218. }
  219. bool GLView::isScissorEnabled()
  220. {
  221. return (GL_FALSE == glIsEnabled(GL_SCISSOR_TEST)) ? false : true;
  222. }
  223. Rect GLView::getScissorRect() const
  224. {
  225. GLfloat params[4];
  226. glGetFloatv(GL_SCISSOR_BOX, params);
  227. float x = (params[0] - _viewPortRect.origin.x) / _scaleX;
  228. float y = (params[1] - _viewPortRect.origin.y) / _scaleY;
  229. float w = params[2] / _scaleX;
  230. float h = params[3] / _scaleY;
  231. return Rect(x, y, w, h);
  232. }
  233. void GLView::setViewName(const std::string& viewname )
  234. {
  235. _viewName = viewname;
  236. }
  237. const std::string& GLView::getViewName() const
  238. {
  239. return _viewName;
  240. }
  241. void GLView::handleTouchesBegin(int num, intptr_t ids[], float xs[], float ys[])
  242. {
  243. intptr_t id = 0;
  244. float x = 0.0f;
  245. float y = 0.0f;
  246. int unusedIndex = 0;
  247. EventTouch touchEvent;
  248. for (int i = 0; i < num; ++i)
  249. {
  250. id = ids[i];
  251. x = xs[i];
  252. y = ys[i];
  253. auto iter = g_touchIdReorderMap.find(id);
  254. // it is a new touch
  255. if (iter == g_touchIdReorderMap.end())
  256. {
  257. unusedIndex = getUnUsedIndex();
  258. // The touches is more than MAX_TOUCHES ?
  259. if (unusedIndex == -1) {
  260. CCLOG("The touches is more than MAX_TOUCHES, unusedIndex = %d", unusedIndex);
  261. continue;
  262. }
  263. Touch* touch = g_touches[unusedIndex] = new (std::nothrow) Touch();
  264. touch->setTouchInfo(unusedIndex, (x - _viewPortRect.origin.x) / _scaleX,
  265. (y - _viewPortRect.origin.y) / _scaleY);
  266. CCLOGINFO("x = %f y = %f", touch->getLocationInView().x, touch->getLocationInView().y);
  267. g_touchIdReorderMap.emplace(id, unusedIndex);
  268. touchEvent._touches.push_back(touch);
  269. }
  270. }
  271. if (touchEvent._touches.size() == 0)
  272. {
  273. CCLOG("touchesBegan: size = 0");
  274. return;
  275. }
  276. touchEvent._eventCode = EventTouch::EventCode::BEGAN;
  277. auto dispatcher = Director::getInstance()->getEventDispatcher();
  278. dispatcher->dispatchEvent(&touchEvent);
  279. }
  280. void GLView::handleTouchesMove(int num, intptr_t ids[], float xs[], float ys[])
  281. {
  282. handleTouchesMove(num, ids, xs, ys, nullptr, nullptr);
  283. }
  284. void GLView::handleTouchesMove(int num, intptr_t ids[], float xs[], float ys[], float fs[], float ms[])
  285. {
  286. intptr_t id = 0;
  287. float x = 0.0f;
  288. float y = 0.0f;
  289. float force = 0.0f;
  290. float maxForce = 0.0f;
  291. EventTouch touchEvent;
  292. for (int i = 0; i < num; ++i)
  293. {
  294. id = ids[i];
  295. x = xs[i];
  296. y = ys[i];
  297. force = fs ? fs[i] : 0.0f;
  298. maxForce = ms ? ms[i] : 0.0f;
  299. auto iter = g_touchIdReorderMap.find(id);
  300. if (iter == g_touchIdReorderMap.end())
  301. {
  302. CCLOG("if the index doesn't exist, it is an error");
  303. continue;
  304. }
  305. CCLOGINFO("Moving touches with id: %d, x=%f, y=%f, force=%f, maxFource=%f", (int)id, x, y, force, maxForce);
  306. Touch* touch = g_touches[iter->second];
  307. if (touch)
  308. {
  309. touch->setTouchInfo(iter->second, (x - _viewPortRect.origin.x) / _scaleX,
  310. (y - _viewPortRect.origin.y) / _scaleY, force, maxForce);
  311. touchEvent._touches.push_back(touch);
  312. }
  313. else
  314. {
  315. // It is error, should return.
  316. CCLOG("Moving touches with id: %ld error", (long int)id);
  317. return;
  318. }
  319. }
  320. if (touchEvent._touches.size() == 0)
  321. {
  322. CCLOG("touchesMoved: size = 0");
  323. return;
  324. }
  325. touchEvent._eventCode = EventTouch::EventCode::MOVED;
  326. auto dispatcher = Director::getInstance()->getEventDispatcher();
  327. dispatcher->dispatchEvent(&touchEvent);
  328. }
  329. void GLView::handleTouchesOfEndOrCancel(EventTouch::EventCode eventCode, int num, intptr_t ids[], float xs[], float ys[])
  330. {
  331. intptr_t id = 0;
  332. float x = 0.0f;
  333. float y = 0.0f;
  334. EventTouch touchEvent;
  335. for (int i = 0; i < num; ++i)
  336. {
  337. id = ids[i];
  338. x = xs[i];
  339. y = ys[i];
  340. auto iter = g_touchIdReorderMap.find(id);
  341. if (iter == g_touchIdReorderMap.end())
  342. {
  343. CCLOG("if the index doesn't exist, it is an error");
  344. continue;
  345. }
  346. /* Add to the set to send to the director */
  347. Touch* touch = g_touches[iter->second];
  348. if (touch)
  349. {
  350. CCLOGINFO("Ending touches with id: %d, x=%f, y=%f", (int)id, x, y);
  351. touch->setTouchInfo(iter->second, (x - _viewPortRect.origin.x) / _scaleX,
  352. (y - _viewPortRect.origin.y) / _scaleY);
  353. touchEvent._touches.push_back(touch);
  354. g_touches[iter->second] = nullptr;
  355. removeUsedIndexBit(iter->second);
  356. g_touchIdReorderMap.erase(id);
  357. }
  358. else
  359. {
  360. CCLOG("Ending touches with id: %ld error", static_cast<long>(id));
  361. return;
  362. }
  363. }
  364. if (touchEvent._touches.size() == 0)
  365. {
  366. CCLOG("touchesEnded or touchesCancel: size = 0");
  367. return;
  368. }
  369. touchEvent._eventCode = eventCode;
  370. auto dispatcher = Director::getInstance()->getEventDispatcher();
  371. dispatcher->dispatchEvent(&touchEvent);
  372. for (auto& touch : touchEvent._touches)
  373. {
  374. // release the touch object.
  375. touch->release();
  376. }
  377. }
  378. void GLView::handleTouchesEnd(int num, intptr_t ids[], float xs[], float ys[])
  379. {
  380. handleTouchesOfEndOrCancel(EventTouch::EventCode::ENDED, num, ids, xs, ys);
  381. }
  382. void GLView::handleTouchesCancel(int num, intptr_t ids[], float xs[], float ys[])
  383. {
  384. handleTouchesOfEndOrCancel(EventTouch::EventCode::CANCELLED, num, ids, xs, ys);
  385. }
  386. const Rect& GLView::getViewPortRect() const
  387. {
  388. return _viewPortRect;
  389. }
  390. std::vector<Touch*> GLView::getAllTouches() const
  391. {
  392. return getAllTouchesVector();
  393. }
  394. float GLView::getScaleX() const
  395. {
  396. return _scaleX;
  397. }
  398. float GLView::getScaleY() const
  399. {
  400. return _scaleY;
  401. }
  402. void GLView::renderScene(Scene* scene, Renderer* renderer)
  403. {
  404. CCASSERT(scene, "Invalid Scene");
  405. CCASSERT(renderer, "Invalid Renderer");
  406. if (_vrImpl)
  407. {
  408. _vrImpl->render(scene, renderer);
  409. }
  410. else
  411. {
  412. scene->render(renderer, Mat4::IDENTITY, nullptr);
  413. }
  414. }
  415. VRIRenderer* GLView::getVR() const
  416. {
  417. return _vrImpl;
  418. }
  419. void GLView::setVR(VRIRenderer* vrRenderer)
  420. {
  421. if (_vrImpl != vrRenderer)
  422. {
  423. if (_vrImpl) {
  424. _vrImpl->cleanup();
  425. delete _vrImpl;
  426. }
  427. if (vrRenderer)
  428. vrRenderer->setup(this);
  429. _vrImpl = vrRenderer;
  430. }
  431. }
  432. NS_CC_END