MiddlewareManager.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /****************************************************************************
  2. Copyright (c) 2018 Xiamen Yaji Software Co., Ltd.
  3. http://www.cocos2d-x.org
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in
  11. all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  18. THE SOFTWARE.
  19. ****************************************************************************/
  20. #include "MiddlewareManager.h"
  21. #include "base/CCGLUtils.h"
  22. #include "scripting/js-bindings/jswrapper/SeApi.h"
  23. #include <algorithm>
  24. MIDDLEWARE_BEGIN
  25. MiddlewareManager* MiddlewareManager::_instance = nullptr;
  26. MiddlewareManager::MiddlewareManager()
  27. {
  28. }
  29. MiddlewareManager::~MiddlewareManager()
  30. {
  31. for (auto it : _mbMap)
  32. {
  33. auto buffer = it.second;
  34. if (buffer)
  35. {
  36. delete buffer;
  37. }
  38. }
  39. _mbMap.clear();
  40. }
  41. MeshBuffer* MiddlewareManager::getMeshBuffer(int format)
  42. {
  43. MeshBuffer* mb = _mbMap[format];
  44. if (!mb)
  45. {
  46. mb = new MeshBuffer(format);
  47. _mbMap[format] = mb;
  48. }
  49. return mb;
  50. }
  51. void MiddlewareManager::_clearRemoveList()
  52. {
  53. for (std::size_t i = 0; i < _removeList.size(); i++)
  54. {
  55. auto editor = _removeList[i];
  56. auto it = std::find(_updateList.begin(), _updateList.end(), editor);
  57. if (it != _updateList.end())
  58. {
  59. _updateList.erase(it);
  60. }
  61. }
  62. _removeList.clear();
  63. }
  64. void MiddlewareManager::update(float dt)
  65. {
  66. isUpdating = true;
  67. for (std::size_t i = 0, n = _updateList.size(); i < n; i++)
  68. {
  69. auto editor = _updateList[i];
  70. if (_removeList.size() > 0)
  71. {
  72. auto removeIt = std::find(_removeList.begin(), _removeList.end(), editor);
  73. if (removeIt == _removeList.end())
  74. {
  75. editor->update(dt);
  76. }
  77. }
  78. else
  79. {
  80. editor->update(dt);
  81. }
  82. }
  83. isUpdating = false;
  84. _clearRemoveList();
  85. }
  86. void MiddlewareManager::render(float dt)
  87. {
  88. for (auto it : _mbMap)
  89. {
  90. auto buffer = it.second;
  91. if (buffer)
  92. {
  93. buffer->reset();
  94. }
  95. }
  96. isRendering = true;
  97. auto isOrderDirty = false;
  98. uint32_t maxRenderOrder = 0;
  99. for (std::size_t i = 0, n = _updateList.size(); i < n; i++)
  100. {
  101. auto editor = _updateList[i];
  102. uint32_t renderOrder = maxRenderOrder;
  103. if (_removeList.size() > 0)
  104. {
  105. auto removeIt = std::find(_removeList.begin(), _removeList.end(), editor);
  106. if (removeIt == _removeList.end())
  107. {
  108. editor->render(dt);
  109. renderOrder = editor->getRenderOrder();
  110. }
  111. }
  112. else
  113. {
  114. editor->render(dt);
  115. renderOrder = editor->getRenderOrder();
  116. }
  117. if (maxRenderOrder > renderOrder)
  118. {
  119. isOrderDirty = true;
  120. }
  121. else
  122. {
  123. maxRenderOrder = renderOrder;
  124. }
  125. }
  126. isRendering = false;
  127. for (auto it : _mbMap)
  128. {
  129. auto buffer = it.second;
  130. if (buffer)
  131. {
  132. buffer->uploadIB();
  133. buffer->uploadVB();
  134. }
  135. }
  136. _clearRemoveList();
  137. if (isOrderDirty)
  138. {
  139. std::sort(_updateList.begin(), _updateList.end(), [](IMiddleware* it1, IMiddleware* it2)
  140. {
  141. return it1->getRenderOrder() < it2->getRenderOrder();
  142. });
  143. }
  144. }
  145. void MiddlewareManager::addTimer(IMiddleware* editor)
  146. {
  147. auto it0 = std::find(_updateList.begin(), _updateList.end(), editor);
  148. if (it0 != _updateList.end()) {
  149. return;
  150. }
  151. auto it1 = std::find(_removeList.begin(), _removeList.end(), editor);
  152. if (it1 != _removeList.end())
  153. {
  154. _removeList.erase(it1);
  155. }
  156. _updateList.push_back(editor);
  157. }
  158. void MiddlewareManager::removeTimer(IMiddleware* editor)
  159. {
  160. if (isUpdating || isRendering)
  161. {
  162. _removeList.push_back(editor);
  163. }
  164. else
  165. {
  166. auto it = std::find(_updateList.begin(), _updateList.end(), editor);
  167. if (it != _updateList.end())
  168. {
  169. _updateList.erase(it);
  170. }
  171. }
  172. }
  173. MIDDLEWARE_END