AnimatedSprite.cpp
AnimatedSprite.cpp
CPP1/* Copyright Statement:2 *3 * (C) 2021-2024 Cubios Inc. All rights reserved.4 */5#include "AnimatedSprite.h"6#include "Screen.h"78namespace Cubios9{10namespace Gfx11{12AnimatedSprite::AnimatedSprite(std::vector<std::string>& names):playbackSpeed(50),currentFrame(-1),prevTime(0),playbackMode(Loop),reverse(false),startFrame(0)13{14 this->numFrames = names.size();15 this->Color.Set(255,0,255);1617 for(size_t i=0;i<this->numFrames;i++)18 {19 this->resourceIds.push_back(Cubios::GFX_getAssetId(names.at(i).c_str()));20 }21}2223AnimatedSprite::AnimatedSprite(std::vector<std::string>& names, const Cubios::Math::Transform& t):playbackSpeed(50),currentFrame(-1),prevTime(0),playbackMode(Loop),reverse(false),startFrame(0)24{25 this->Transform = t;26 this->numFrames = names.size();27 this->Color.Set(255,0,255);2829 for(size_t i=0;i<this->numFrames;i++)30 {31 this->resourceIds.push_back(Cubios::GFX_getAssetId(names.at(i).c_str()));32 }33}3435AnimatedSprite::AnimatedSprite(std::vector<std::string>& names, float x, float y):playbackSpeed(50),currentFrame(-1),prevTime(0),playbackMode(Loop),reverse(false),startFrame(0)36{37 this->Transform.Position.X = x;38 this->Transform.Position.Y = y;39 this->numFrames = names.size();40 this->Color.Set(255,0,255);4142 for(size_t i=0;i<this->numFrames;i++)43 {44 this->resourceIds.push_back(Cubios::GFX_getAssetId(names.at(i).c_str()));45 }46}4748AnimatedSprite::AnimatedSprite(std::string name,std::string extension, int16_t firstFrame, int16_t lastFrame, const Cubios::Math::Transform& t):playbackSpeed(50),currentFrame(-1),prevTime(0),playbackMode(Loop),reverse(false),startFrame(0)49{50 if(firstFrame<1) firstFrame = 1;51 if(lastFrame<firstFrame) lastFrame = firstFrame;5253 this->Transform = t;54 this->numFrames = 1+lastFrame-firstFrame;55 this->Color.Set(255,0,255);5657 std::string nn;58 for(int16_t i=firstFrame;i<=lastFrame;i++)59 {60 nn = name+std::to_string(i)+"."+extension;61 this->resourceIds.push_back(Cubios::GFX_getAssetId(nn.c_str()));62 }63}64AnimatedSprite::AnimatedSprite(std::string name,std::string extension, int16_t firstFrame, int16_t lastFrame, float x, float y):playbackSpeed(50),currentFrame(-1),prevTime(0),playbackMode(Loop),reverse(false),startFrame(0)65{66 if(firstFrame<1) firstFrame = 1;67 if(lastFrame<firstFrame) lastFrame = firstFrame;6869 this->Transform.Position.X = x;70 this->Transform.Position.Y = y;71 this->numFrames = 1+lastFrame-firstFrame;72 this->Color.Set(255,0,255);7374 std::string nn;75 for(int16_t i=firstFrame;i<=lastFrame;i++)76 {77 nn = name+std::to_string(i)+"."+extension;78 this->resourceIds.push_back(Cubios::GFX_getAssetId(nn.c_str()));79 }80}8182AnimatedSprite::AnimatedSprite(std::string name,std::string extension, int16_t firstFrame, int16_t lastFrame):playbackSpeed(50),currentFrame(-1),prevTime(0),playbackMode(Loop),reverse(false),startFrame(0)83{84 if(firstFrame<1) firstFrame = 1;85 if(lastFrame<firstFrame) lastFrame = firstFrame;8687 this->numFrames = 1+lastFrame-firstFrame;88 this->Color.Set(255,0,255);8990 std::string nn;91 for(int16_t i=firstFrame;i<=lastFrame;i++)92 {93 nn = name+std::to_string(i)+"."+extension;94 this->resourceIds.push_back(Cubios::GFX_getAssetId(nn.c_str()));95 }96}9798AnimatedSprite::AnimatedSprite(const AnimatedSprite& sprite):currentFrame(-1),prevTime(0),startFrame(0)99{100 this->numFrames = sprite.numFrames;101 this->playbackSpeed = sprite.playbackSpeed;102 this->playbackMode = sprite.playbackMode;103 this->startFrame = sprite.startFrame;104 this->reverse = sprite.reverse;105106 this->Transform = sprite.Transform;107 this->Color = sprite.Color;108109 for(size_t i=0;i<sprite.resourceIds.size();i++)110 {111 this->resourceIds.push_back(sprite.resourceIds.at(i));112 }113}114115AnimatedSprite::~AnimatedSprite() { }116117void AnimatedSprite::Render()118{119 if(this->currentFrame==-1) return;120121 Math::Vec2 pos = this->ScreenPosition();122 Cubios::GFX_drawImage(this->resourceIds.at(this->currentFrame), pos.X, pos.Y, Color.A(), Color.Value(), Transform.ScaleX, Transform.ScaleY, Transform.SafeRotation()+ScreenAngle, Transform.Mirroring);123}124125Cubios::SceneObject* AnimatedSprite::SetFrame(int16_t frameNumber)126{127 if(frameNumber<0 || frameNumber>this->numFrames-1) frameNumber = 0;128129 if(this->currentFrame==-1) { this->startFrame = frameNumber; if(this->playbackMode==Set) this->currentFrame = frameNumber; return this;}130131 switch(this->playbackMode)132 {133 case Loop: break;134 case Bounce: break;135 case Set:136 {137 this->currentFrame = frameNumber;138 }139 break;140 default: break;141 }142143 this->prevTime = this->lastDt;144145 return this;146}147148Cubios::SceneObject* AnimatedSprite::SetPlaybackSpeed(int16_t speed)149{150 if(speed<1) this->playbackSpeed = 1;151 this->playbackSpeed = speed;152153 return this;154}155156Cubios::SceneObject* AnimatedSprite::SetPlaybackMode(playbackMode_t mode)157{158 this->playbackMode = mode;159160 return this;161}162163int16_t AnimatedSprite::Tick(uint32_t dt)164{165 this->lastDt = dt;166 if(!this->Visible) return this->currentFrame;167168 switch(this->playbackMode)169 {170 case Loop: this->doLoop(dt); break;171 case Bounce: this->doBounce(dt); break;172 case Set: this->doSet(dt); break;173 default: break;174 }175 return this->currentFrame;176}177178int16_t AnimatedSprite::PrevFrame()179{180 if(this->currentFrame>0) return this->currentFrame-1; else return this->numFrames-1;181}182int16_t AnimatedSprite::NextFrame()183{184 if(this->currentFrame<this->numFrames-1) return this->currentFrame+1; else return 0;185}186187void AnimatedSprite::doBounce(uint32_t dt)188{189 if(this->currentFrame==-1)190 {191 this->currentFrame = this->startFrame;192 this->prevTime = dt;193 }194 else195 {196 int32_t curTime = this->prevTime+dt;197 int32_t nf = curTime/playbackSpeed;198199 if(nf>0)200 {201 if(!this->reverse)202 {203 int32_t nextFrame = this->currentFrame+nf;204 this->prevTime = curTime%playbackSpeed;205206 if(nextFrame<(this->numFrames-1))207 {208 this->currentFrame = nextFrame;209 }210 else211 {212 this->currentFrame = this->numFrames-1;213 this->reverse = true;214 }215 }216 else217 {218 int32_t nextFrame = this->currentFrame-nf;219 this->prevTime = curTime%playbackSpeed;220221 if(nextFrame>0)222 {223 this->currentFrame = nextFrame;224 }225 else226 {227 this->currentFrame = 0;228 this->reverse = false;229 }230 }231 }232 else233 {234 this->prevTime = curTime;235 }236 }237}238239void AnimatedSprite::doLoop(uint32_t dt)240{241 if(this->currentFrame==-1)242 {243 this->currentFrame = this->startFrame;244 this->prevTime = dt;245 }246 else247 {248 int32_t curTime = this->prevTime+dt;249 int32_t nf = curTime/playbackSpeed;250251 if(nf>0)252 {253 int32_t nextFrame = this->currentFrame+nf;254 this->prevTime = curTime%playbackSpeed;255256 if(nextFrame<(this->numFrames-1))257 {258 this->currentFrame = nextFrame;259 }260 else261 {262 this->currentFrame = nextFrame%this->numFrames;263 }264 }265 else266 {267 this->prevTime = curTime;268 }269 }270}271272}273}274275
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.