PageRenderTime 26ms CodeModel.GetById 38ms RepoModel.GetById 1ms app.codeStats 0ms

/cocos/2d/CCMotionStreak.cpp

https://bitbucket.org/nesdavid/cocos2dx
C++ | 359 lines | 260 code | 61 blank | 38 comment | 29 complexity | b57e40c2d927fcf32bdc5661b6aa02d6 MD5 | raw file
  1. /****************************************************************************
  2. Copyright (c) 2010-2012 cocos2d-x.org
  3. Copyright (c) 2011 ForzeField Studios S.L.
  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 false 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 "CCMotionStreak.h"
  22. #include "CCTextureCache.h"
  23. #include "ccGLStateCache.h"
  24. #include "CCGLProgram.h"
  25. #include "CCShaderCache.h"
  26. #include "ccMacros.h"
  27. #include "CCDirector.h"
  28. #include "CCVertex.h"
  29. NS_CC_BEGIN
  30. MotionStreak::MotionStreak()
  31. : _fastMode(false)
  32. , _startingPositionInitialized(false)
  33. , _texture(NULL)
  34. , _blendFunc(BlendFunc::ALPHA_NON_PREMULTIPLIED)
  35. , _positionR(Point::ZERO)
  36. , _stroke(0.0f)
  37. , _fadeDelta(0.0f)
  38. , _minSeg(0.0f)
  39. , _maxPoints(0)
  40. , _nuPoints(0)
  41. , _previousNuPoints(0)
  42. , _pointVertexes(NULL)
  43. , _pointState(NULL)
  44. , _vertices(NULL)
  45. , _colorPointer(NULL)
  46. , _texCoords(NULL)
  47. {
  48. }
  49. MotionStreak::~MotionStreak()
  50. {
  51. CC_SAFE_RELEASE(_texture);
  52. CC_SAFE_FREE(_pointState);
  53. CC_SAFE_FREE(_pointVertexes);
  54. CC_SAFE_FREE(_vertices);
  55. CC_SAFE_FREE(_colorPointer);
  56. CC_SAFE_FREE(_texCoords);
  57. }
  58. MotionStreak* MotionStreak::create(float fade, float minSeg, float stroke, const Color3B& color, const char* path)
  59. {
  60. MotionStreak *pRet = new MotionStreak();
  61. if (pRet && pRet->initWithFade(fade, minSeg, stroke, color, path))
  62. {
  63. pRet->autorelease();
  64. return pRet;
  65. }
  66. CC_SAFE_DELETE(pRet);
  67. return NULL;
  68. }
  69. MotionStreak* MotionStreak::create(float fade, float minSeg, float stroke, const Color3B& color, Texture2D* texture)
  70. {
  71. MotionStreak *pRet = new MotionStreak();
  72. if (pRet && pRet->initWithFade(fade, minSeg, stroke, color, texture))
  73. {
  74. pRet->autorelease();
  75. return pRet;
  76. }
  77. CC_SAFE_DELETE(pRet);
  78. return NULL;
  79. }
  80. bool MotionStreak::initWithFade(float fade, float minSeg, float stroke, const Color3B& color, const char* path)
  81. {
  82. CCASSERT(path != NULL, "Invalid filename");
  83. Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(path);
  84. return initWithFade(fade, minSeg, stroke, color, texture);
  85. }
  86. bool MotionStreak::initWithFade(float fade, float minSeg, float stroke, const Color3B& color, Texture2D* texture)
  87. {
  88. Node::setPosition(Point::ZERO);
  89. setAnchorPoint(Point::ZERO);
  90. ignoreAnchorPointForPosition(true);
  91. _startingPositionInitialized = false;
  92. _positionR = Point::ZERO;
  93. _fastMode = true;
  94. _minSeg = (minSeg == -1.0f) ? stroke/5.0f : minSeg;
  95. _minSeg *= _minSeg;
  96. _stroke = stroke;
  97. _fadeDelta = 1.0f/fade;
  98. _maxPoints = (int)(fade*60.0f)+2;
  99. _nuPoints = 0;
  100. _pointState = (float *)malloc(sizeof(float) * _maxPoints);
  101. _pointVertexes = (Point*)malloc(sizeof(Point) * _maxPoints);
  102. _vertices = (Vertex2F*)malloc(sizeof(Vertex2F) * _maxPoints * 2);
  103. _texCoords = (Tex2F*)malloc(sizeof(Tex2F) * _maxPoints * 2);
  104. _colorPointer = (GLubyte*)malloc(sizeof(GLubyte) * _maxPoints * 2 * 4);
  105. // Set blend mode
  106. _blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
  107. // shader program
  108. setShaderProgram(ShaderCache::getInstance()->getProgram(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
  109. setTexture(texture);
  110. setColor(color);
  111. scheduleUpdate();
  112. return true;
  113. }
  114. void MotionStreak::setPosition(const Point& position)
  115. {
  116. _startingPositionInitialized = true;
  117. _positionR = position;
  118. }
  119. void MotionStreak::tintWithColor(const Color3B& colors)
  120. {
  121. setColor(colors);
  122. // Fast assignation
  123. for(unsigned int i = 0; i<_nuPoints*2; i++)
  124. {
  125. *((Color3B*) (_colorPointer+i*4)) = colors;
  126. }
  127. }
  128. Texture2D* MotionStreak::getTexture(void) const
  129. {
  130. return _texture;
  131. }
  132. void MotionStreak::setTexture(Texture2D *texture)
  133. {
  134. if (_texture != texture)
  135. {
  136. CC_SAFE_RETAIN(texture);
  137. CC_SAFE_RELEASE(_texture);
  138. _texture = texture;
  139. }
  140. }
  141. void MotionStreak::setBlendFunc(const BlendFunc &blendFunc)
  142. {
  143. _blendFunc = blendFunc;
  144. }
  145. const BlendFunc& MotionStreak::getBlendFunc(void) const
  146. {
  147. return _blendFunc;
  148. }
  149. void MotionStreak::setOpacity(GLubyte opacity)
  150. {
  151. CCASSERT(false, "Set opacity no supported");
  152. }
  153. GLubyte MotionStreak::getOpacity(void) const
  154. {
  155. CCASSERT(false, "Opacity no supported");
  156. return 0;
  157. }
  158. void MotionStreak::setOpacityModifyRGB(bool bValue)
  159. {
  160. CC_UNUSED_PARAM(bValue);
  161. }
  162. bool MotionStreak::isOpacityModifyRGB(void) const
  163. {
  164. return false;
  165. }
  166. void MotionStreak::update(float delta)
  167. {
  168. if (!_startingPositionInitialized)
  169. {
  170. return;
  171. }
  172. delta *= _fadeDelta;
  173. unsigned int newIdx, newIdx2, i, i2;
  174. unsigned int mov = 0;
  175. // Update current points
  176. for(i = 0; i<_nuPoints; i++)
  177. {
  178. _pointState[i]-=delta;
  179. if(_pointState[i] <= 0)
  180. mov++;
  181. else
  182. {
  183. newIdx = i-mov;
  184. if(mov>0)
  185. {
  186. // Move data
  187. _pointState[newIdx] = _pointState[i];
  188. // Move point
  189. _pointVertexes[newIdx] = _pointVertexes[i];
  190. // Move vertices
  191. i2 = i*2;
  192. newIdx2 = newIdx*2;
  193. _vertices[newIdx2] = _vertices[i2];
  194. _vertices[newIdx2+1] = _vertices[i2+1];
  195. // Move color
  196. i2 *= 4;
  197. newIdx2 *= 4;
  198. _colorPointer[newIdx2+0] = _colorPointer[i2+0];
  199. _colorPointer[newIdx2+1] = _colorPointer[i2+1];
  200. _colorPointer[newIdx2+2] = _colorPointer[i2+2];
  201. _colorPointer[newIdx2+4] = _colorPointer[i2+4];
  202. _colorPointer[newIdx2+5] = _colorPointer[i2+5];
  203. _colorPointer[newIdx2+6] = _colorPointer[i2+6];
  204. }else
  205. newIdx2 = newIdx*8;
  206. const GLubyte op = (GLubyte)(_pointState[newIdx] * 255.0f);
  207. _colorPointer[newIdx2+3] = op;
  208. _colorPointer[newIdx2+7] = op;
  209. }
  210. }
  211. _nuPoints-=mov;
  212. // Append new point
  213. bool appendNewPoint = true;
  214. if(_nuPoints >= _maxPoints)
  215. {
  216. appendNewPoint = false;
  217. }
  218. else if(_nuPoints>0)
  219. {
  220. bool a1 = _pointVertexes[_nuPoints-1].getDistanceSq(_positionR) < _minSeg;
  221. bool a2 = (_nuPoints == 1) ? false : (_pointVertexes[_nuPoints-2].getDistanceSq(_positionR)< (_minSeg * 2.0f));
  222. if(a1 || a2)
  223. {
  224. appendNewPoint = false;
  225. }
  226. }
  227. if(appendNewPoint)
  228. {
  229. _pointVertexes[_nuPoints] = _positionR;
  230. _pointState[_nuPoints] = 1.0f;
  231. // Color assignment
  232. const unsigned int offset = _nuPoints*8;
  233. *((Color3B*)(_colorPointer + offset)) = _displayedColor;
  234. *((Color3B*)(_colorPointer + offset+4)) = _displayedColor;
  235. // Opacity
  236. _colorPointer[offset+3] = 255;
  237. _colorPointer[offset+7] = 255;
  238. // Generate polygon
  239. if(_nuPoints > 0 && _fastMode )
  240. {
  241. if(_nuPoints > 1)
  242. {
  243. ccVertexLineToPolygon(_pointVertexes, _stroke, _vertices, _nuPoints, 1);
  244. }
  245. else
  246. {
  247. ccVertexLineToPolygon(_pointVertexes, _stroke, _vertices, 0, 2);
  248. }
  249. }
  250. _nuPoints ++;
  251. }
  252. if( ! _fastMode )
  253. {
  254. ccVertexLineToPolygon(_pointVertexes, _stroke, _vertices, 0, _nuPoints);
  255. }
  256. // Updated Tex Coords only if they are different than previous step
  257. if( _nuPoints && _previousNuPoints != _nuPoints ) {
  258. float texDelta = 1.0f / _nuPoints;
  259. for( i=0; i < _nuPoints; i++ ) {
  260. _texCoords[i*2] = Tex2F(0, texDelta*i);
  261. _texCoords[i*2+1] = Tex2F(1, texDelta*i);
  262. }
  263. _previousNuPoints = _nuPoints;
  264. }
  265. }
  266. void MotionStreak::reset()
  267. {
  268. _nuPoints = 0;
  269. }
  270. void MotionStreak::draw()
  271. {
  272. if(_nuPoints <= 1)
  273. return;
  274. CC_NODE_DRAW_SETUP();
  275. GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX );
  276. GL::blendFunc( _blendFunc.src, _blendFunc.dst );
  277. GL::bindTexture2D( _texture->getName() );
  278. #ifdef EMSCRIPTEN
  279. // Size calculations from ::initWithFade
  280. setGLBufferData(_vertices, (sizeof(Vertex2F) * _maxPoints * 2), 0);
  281. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, 0);
  282. setGLBufferData(_texCoords, (sizeof(Tex2F) * _maxPoints * 2), 1);
  283. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, 0);
  284. setGLBufferData(_colorPointer, (sizeof(GLubyte) * _maxPoints * 2 * 4), 2);
  285. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, 0);
  286. #else
  287. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, _vertices);
  288. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORDS, 2, GL_FLOAT, GL_FALSE, 0, _texCoords);
  289. glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, _colorPointer);
  290. #endif // EMSCRIPTEN
  291. glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)_nuPoints*2);
  292. CC_INCREMENT_GL_DRAWS(1);
  293. }
  294. NS_CC_END