CCGLView.cpp 14 KB

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