Skip to content

Instantly share code, notes, and snippets.

@OscarLeif
Created December 29, 2015 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OscarLeif/507232e0d4dbb486e8e3 to your computer and use it in GitHub Desktop.
Save OscarLeif/507232e0d4dbb486e8e3 to your computer and use it in GitHub Desktop.
MotionStreak with follow option for Cocos2d-x version 3.9
/****************************************************************************
Copyright (c) 2011 ForzeField Studios S.L.
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN false EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "2d/CCMotionStreak.h"
#include "math/CCVertex.h"
#include "base/CCDirector.h"
#include "renderer/CCTextureCache.h"
#include "renderer/ccGLStateCache.h"
#include "renderer/CCTexture2D.h"
#include "renderer/CCRenderer.h"
#include "renderer/CCGLProgramState.h"
NS_CC_BEGIN
MotionStreak::MotionStreak()
: _fastMode(false)
, _startingPositionInitialized(false)
, _texture(nullptr)
, _blendFunc(BlendFunc::ALPHA_NON_PREMULTIPLIED)
, _stroke(0.0f)
, _fadeDelta(0.0f)
, _minSeg(0.0f)
, _maxPoints(0)
, _nuPoints(0)
, _previousNuPoints(0)
, _pointVertexes(nullptr)
, _pointState(nullptr)
, _vertices(nullptr)
, _colorPointer(nullptr)
, _texCoords(nullptr)
{
}
MotionStreak::~MotionStreak()
{
CC_SAFE_RELEASE(_texture);
CC_SAFE_FREE(_pointState);
CC_SAFE_FREE(_pointVertexes);
CC_SAFE_FREE(_vertices);
CC_SAFE_FREE(_colorPointer);
CC_SAFE_FREE(_texCoords);
}
MotionStreak* MotionStreak::create(float fade, float minSeg, float stroke, const Color3B& color, const std::string& path)
{
MotionStreak *ret = new (std::nothrow) MotionStreak();
if (ret && ret->initWithFade(fade, minSeg, stroke, color, path))
{
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
MotionStreak* MotionStreak::create(float fade, float minSeg, float stroke, const Color3B& color, Texture2D* texture)
{
MotionStreak *ret = new (std::nothrow) MotionStreak();
if (ret && ret->initWithFade(fade, minSeg, stroke, color, texture))
{
ret->autorelease();
return ret;
}
CC_SAFE_DELETE(ret);
return nullptr;
}
bool MotionStreak::initWithFade(float fade, float minSeg, float stroke, const Color3B& color, const std::string& path)
{
CCASSERT(!path.empty(), "Invalid filename");
Texture2D *texture = Director::getInstance()->getTextureCache()->addImage(path);
return initWithFade(fade, minSeg, stroke, color, texture);
}
bool MotionStreak::initWithFade(float fade, float minSeg, float stroke, const Color3B& color, Texture2D* texture)
{
Node::setPosition(Vec2::ZERO);
setAnchorPoint(Vec2::ZERO);
ignoreAnchorPointForPosition(true);
_startingPositionInitialized = false;
_positionR.setZero();
_fastMode = true;
_minSeg = (minSeg == -1.0f) ? stroke/5.0f : minSeg;
_minSeg *= _minSeg;
_stroke = stroke;
_fadeDelta = 1.0f/fade;
_maxPoints = (int)(fade*60.0f)+2;
_nuPoints = 0;
_pointState = (float *)malloc(sizeof(float) * _maxPoints);
_pointVertexes = (Vec2*)malloc(sizeof(Vec2) * _maxPoints);
_vertices = (Vec2*)malloc(sizeof(Vec2) * _maxPoints * 2);
_texCoords = (Tex2F*)malloc(sizeof(Tex2F) * _maxPoints * 2);
_colorPointer = (GLubyte*)malloc(sizeof(GLubyte) * _maxPoints * 2 * 4);
// Set blend mode
_blendFunc = BlendFunc::ALPHA_NON_PREMULTIPLIED;
// shader state
setGLProgramState(GLProgramState::getOrCreateWithGLProgramName(GLProgram::SHADER_NAME_POSITION_TEXTURE_COLOR));
setTexture(texture);
setColor(color);
scheduleUpdate();
return true;
}
void MotionStreak::setPosition(const Vec2& position)
{
if (!_startingPositionInitialized) {
_startingPositionInitialized = true;
}
_positionR = position;
}
void MotionStreak::setPosition(float x, float y)
{
if (!_startingPositionInitialized) {
_startingPositionInitialized = true;
}
_positionR.x = x;
_positionR.y = y;
}
const Vec2& MotionStreak::getPosition() const
{
return _positionR;
}
void MotionStreak::getPosition(float* x, float* y) const
{
*x = _positionR.x;
*y = _positionR.y;
}
float MotionStreak::getPositionX() const
{
return _positionR.x;
}
Vec3 MotionStreak::getPosition3D() const
{
return Vec3(_positionR.x, _positionR.y, getPositionZ());
}
void MotionStreak::setPositionX(float x)
{
if (!_startingPositionInitialized) {
_startingPositionInitialized = true;
}
_positionR.x = x;
}
float MotionStreak::getPositionY() const
{
return _positionR.y;
}
void MotionStreak::setPositionY(float y)
{
if (!_startingPositionInitialized) {
_startingPositionInitialized = true;
}
_positionR.y = y;
}
void MotionStreak::tintWithColor(const Color3B& colors)
{
setColor(colors);
// Fast assignation
for(unsigned int i = 0; i<_nuPoints*2; i++)
{
*((Color3B*) (_colorPointer+i*4)) = colors;
}
}
Texture2D* MotionStreak::getTexture(void) const
{
return _texture;
}
void MotionStreak::setTexture(Texture2D *texture)
{
if (_texture != texture)
{
CC_SAFE_RETAIN(texture);
CC_SAFE_RELEASE(_texture);
_texture = texture;
}
}
void MotionStreak::setBlendFunc(const BlendFunc &blendFunc)
{
_blendFunc = blendFunc;
}
const BlendFunc& MotionStreak::getBlendFunc(void) const
{
return _blendFunc;
}
void MotionStreak::setOpacity(GLubyte opacity)
{
CCASSERT(false, "Set opacity no supported");
}
GLubyte MotionStreak::getOpacity(void) const
{
CCASSERT(false, "Opacity no supported");
return 0;
}
void MotionStreak::setOpacityModifyRGB(bool bValue)
{
CC_UNUSED_PARAM(bValue);
}
bool MotionStreak::isOpacityModifyRGB(void) const
{
return false;
}
void MotionStreak::update(float delta)
{
if (!_startingPositionInitialized)
{
return;
}
delta *= _fadeDelta;
unsigned int newIdx, newIdx2, i, i2;
unsigned int mov = 0;
// Update current points
for(i = 0; i<_nuPoints; i++)
{
_pointState[i]-=delta;
if(_pointState[i] <= 0)
mov++;
else
{
newIdx = i-mov;
if(mov>0)
{
// Move data
_pointState[newIdx] = _pointState[i];
// Move point
_pointVertexes[newIdx] = _pointVertexes[i];
// Move vertices
i2 = i*2;
newIdx2 = newIdx*2;
_vertices[newIdx2] = _vertices[i2];
_vertices[newIdx2+1] = _vertices[i2+1];
// Move color
i2 *= 4;
newIdx2 *= 4;
_colorPointer[newIdx2+0] = _colorPointer[i2+0];
_colorPointer[newIdx2+1] = _colorPointer[i2+1];
_colorPointer[newIdx2+2] = _colorPointer[i2+2];
_colorPointer[newIdx2+4] = _colorPointer[i2+4];
_colorPointer[newIdx2+5] = _colorPointer[i2+5];
_colorPointer[newIdx2+6] = _colorPointer[i2+6];
}else
newIdx2 = newIdx*8;
const GLubyte op = (GLubyte)(_pointState[newIdx] * 255.0f);
_colorPointer[newIdx2+3] = op;
_colorPointer[newIdx2+7] = op;
}
}
_nuPoints-=mov;
// Append new point
bool appendNewPoint = true;
if(_nuPoints >= _maxPoints)
{
appendNewPoint = false;
}
else if(_nuPoints>0)
{
bool a1 = _pointVertexes[_nuPoints-1].getDistanceSq(_positionR) < _minSeg;
bool a2 = (_nuPoints == 1) ? false : (_pointVertexes[_nuPoints-2].getDistanceSq(_positionR)< (_minSeg * 2.0f));
if(a1 || a2)
{
appendNewPoint = false;
}
}
if(appendNewPoint)
{
_pointVertexes[_nuPoints] = _positionR;
_pointState[_nuPoints] = 1.0f;
// Color assignment
const unsigned int offset = _nuPoints*8;
*((Color3B*)(_colorPointer + offset)) = _displayedColor;
*((Color3B*)(_colorPointer + offset+4)) = _displayedColor;
// Opacity
_colorPointer[offset+3] = 255;
_colorPointer[offset+7] = 255;
// Generate polygon
if(_nuPoints > 0 && _fastMode )
{
if(_nuPoints > 1)
{
ccVertexLineToPolygon(_pointVertexes, _stroke, _vertices, _nuPoints, 1);
}
else
{
ccVertexLineToPolygon(_pointVertexes, _stroke, _vertices, 0, 2);
}
}
_nuPoints ++;
}
if( ! _fastMode )
{
ccVertexLineToPolygon(_pointVertexes, _stroke, _vertices, 0, _nuPoints);
}
// Updated Tex Coords only if they are different than previous step
if( _nuPoints && _previousNuPoints != _nuPoints ) {
float texDelta = 1.0f / _nuPoints;
for( i=0; i < _nuPoints; i++ ) {
_texCoords[i*2] = Tex2F(0, texDelta*i);
_texCoords[i*2+1] = Tex2F(1, texDelta*i);
}
_previousNuPoints = _nuPoints;
}
}
void MotionStreak::reset()
{
_nuPoints = 0;
}
void MotionStreak::onDraw(const Mat4 &transform, uint32_t flags)
{
getGLProgram()->use();
getGLProgram()->setUniformsForBuiltins(transform);
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX );
GL::blendFunc( _blendFunc.src, _blendFunc.dst );
GL::bindTexture2D( _texture->getName() );
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 2, GL_FLOAT, GL_FALSE, 0, _vertices);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, 0, _texCoords);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, 0, _colorPointer);
glDrawArrays(GL_TRIANGLE_STRIP, 0, (GLsizei)_nuPoints*2);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, _nuPoints*2);
}
void MotionStreak::draw(Renderer *renderer, const Mat4 &transform, uint32_t flags)
{
if(_nuPoints <= 1)
return;
_customCommand.init(_globalZOrder, transform, flags);
_customCommand.func = CC_CALLBACK_0(MotionStreak::onDraw, this, transform, flags);
renderer->addCommand(&_customCommand);
}
NS_CC_END
/****************************************************************************
Copyright (c) 2011 ForzeField Studios S.L.
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CCMOTION_STREAK_H__
#define __CCMOTION_STREAK_H__
#include "base/CCProtocols.h"
#include "2d/CCNode.h"
#include "renderer/CCCustomCommand.h"
NS_CC_BEGIN
class Texture2D;
/**
* @addtogroup _2d
* @{
*/
/** @class MotionStreak.
* @brief Creates a trailing path.
*/
class CC_DLL MotionStreak : public Node, public TextureProtocol
{
public:
/** Creates and initializes a motion streak with fade in seconds, minimum segments, stroke's width, color, texture filename.
*
* @param fade The fade time, in seconds.
* @param minSeg The minimum segments.
* @param stroke The width of stroke.
* @param color The color of stroke.
* @param path The texture file name of stoke.
* @return An autoreleased MotionStreak object.
*/
static MotionStreak* create(float fade, float minSeg, float stroke, const Color3B& color, const std::string& path);
/** Creates and initializes a motion streak with fade in seconds, minimum segments, stroke's width, color, texture.
*
* @param fade The fade time, in seconds.
* @param minSeg The minimum segments.
* @param stroke The width of stroke.
* @param color The color of stroke.
* @param texture The texture name of stoke.
* @return An autoreleased MotionStreak object.
*/
static MotionStreak* create(float fade, float minSeg, float stroke, const Color3B& color, Texture2D* texture);
/** Color used for the tint.
*
* @param colors The color used for the tint.
*/
void tintWithColor(const Color3B& colors);
/** Remove all living segments of the ribbon.
*/
void reset();
/** When fast mode is enabled, new points are added faster but with lower precision.
*
* @return True if fast mode is enabled.
*/
inline bool isFastMode() const { return _fastMode; }
/** Sets fast mode or not.
*
* @param bFastMode True if enabled fast mode.
*/
inline void setFastMode(bool bFastMode) { _fastMode = bFastMode; }
/** Get stroke.
*
* @return float stroke.
*/
inline float getStroke() const { return _stroke; }
/** Set stroke.
*
* @param stroke The width of stroke.
*/
inline void setStroke(float stroke) { _stroke = stroke; }
/** Is the starting position initialized or not.
*
* @return True if the starting position is initialized.
*/
inline bool isStartingPositionInitialized() const { return _startingPositionInitialized; }
/** Sets the starting position initialized or not.
*
* @param bStartingPositionInitialized True if initialized the starting position.
*/
inline void setStartingPositionInitialized(bool bStartingPositionInitialized)
{
_startingPositionInitialized = bStartingPositionInitialized;
}
// Overrides
virtual void setPosition(const Vec2& position) override;
virtual void setPosition(float x, float y) override;
virtual const Vec2& getPosition() const override;
virtual void getPosition(float* x, float* y) const override;
virtual void setPositionX(float x) override;
virtual void setPositionY(float y) override;
virtual float getPositionX(void) const override;
virtual float getPositionY(void) const override;
virtual Vec3 getPosition3D() const override;
/**
* @js NA
* @lua NA
*/
virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
/**
* @lua NA
*/
virtual void update(float delta) override;
virtual Texture2D* getTexture() const override;
virtual void setTexture(Texture2D *texture) override;
/**
* @js NA
* @lua NA
*/
virtual void setBlendFunc(const BlendFunc &blendFunc) override;
/**
* @js NA
* @lua NA
*/
virtual const BlendFunc& getBlendFunc() const override;
virtual GLubyte getOpacity() const override;
virtual void setOpacity(GLubyte opacity) override;
virtual void setOpacityModifyRGB(bool value) override;
virtual bool isOpacityModifyRGB() const override;
CC_CONSTRUCTOR_ACCESS:
MotionStreak();
virtual ~MotionStreak();
/** initializes a motion streak with fade in seconds, minimum segments, stroke's width, color and texture filename */
bool initWithFade(float fade, float minSeg, float stroke, const Color3B& color, const std::string& path);
/** initializes a motion streak with fade in seconds, minimum segments, stroke's width, color and texture */
bool initWithFade(float fade, float minSeg, float stroke, const Color3B& color, Texture2D* texture);
protected:
//renderer callback
void onDraw(const Mat4 &transform, uint32_t flags);
bool _fastMode;
bool _startingPositionInitialized;
/** texture used for the motion streak */
Texture2D* _texture;
BlendFunc _blendFunc;
Vec2 _positionR;
float _stroke;
float _fadeDelta;
float _minSeg;
unsigned int _maxPoints;
unsigned int _nuPoints;
unsigned int _previousNuPoints;
/** Pointers */
Vec2* _pointVertexes;
float* _pointState;
// Opengl
Vec2* _vertices;
GLubyte* _colorPointer;
Tex2F* _texCoords;
CustomCommand _customCommand;
private:
CC_DISALLOW_COPY_AND_ASSIGN(MotionStreak);
};
// end of _2d group
/// @}
NS_CC_END
#endif //__CCMOTION_STREAK_H__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment