AnimatedSprite.cpp
AnimatedSprite.cpp
CPP1/* Copyright Statement:2 *3 * (C) 2021-2025 Cubios Inc. All rights reserved.4 */5#include "AnimatedSprite.h"6#include "Screen.h"78namespace Cubios9{10namespace Gfx11{12AnimatedSprite::AnimatedSprite(Cubios::Gfx::SpriteAtlas<Cubios::Gfx::SpriteAtlasElement>* atlas, const Cubios::Math::Transform& t):playbackSpeed(50),currentFrame(-1),prevTime(0),playbackMode(Loop),reverse(false),startFrame(0),atlas(atlas)13{14 this->Transform = t;15 this->numFrames = atlas->Count();16 this->Color.Set(255,0,255);1718 std::vector<uint32_t>* ids = atlas->ElementsInsertionOrder();1920 for(size_t i=0;i<this->numFrames;i++)21 {22 this->resourceIds.push_back(ids->at(i));23 }24}2526AnimatedSprite::AnimatedSprite(Cubios::Gfx::SpriteAtlas<Cubios::Gfx::SpriteAtlasElement>* atlas, float x, float y):playbackSpeed(50),currentFrame(-1),prevTime(0),playbackMode(Loop),reverse(false),startFrame(0),atlas(atlas)27{28 this->Transform.Position.X = x;29 this->Transform.Position.Y = y;30 this->numFrames = atlas->Count();31 this->Color.Set(255,0,255);3233 std::vector<uint32_t>* ids = atlas->ElementsInsertionOrder();3435 for(size_t i=0;i<this->numFrames;i++)36 {37 this->resourceIds.push_back(ids->at(i));38 }39}4041AnimatedSprite::AnimatedSprite(Cubios::Gfx::SpriteAtlas<Cubios::Gfx::SpriteAtlasElement>* atlas):playbackSpeed(50),currentFrame(-1),prevTime(0),playbackMode(Loop),reverse(false),startFrame(0),atlas(atlas)42{43 this->numFrames = atlas->Count();44 this->Color.Set(255,0,255);4546 std::vector<uint32_t>* ids = atlas->ElementsInsertionOrder();4748 for(size_t i=0;i<this->numFrames;i++)49 {50 this->resourceIds.push_back(ids->at(i));51 }52}5354AnimatedSprite::AnimatedSprite(std::vector<std::string>& names):playbackSpeed(50),currentFrame(-1),prevTime(0),playbackMode(Loop),reverse(false),startFrame(0),atlas(nullptr)55{56 this->numFrames = names.size();57 this->Color.Set(255,0,255);5859 for(size_t i=0;i<this->numFrames;i++)60 {61 this->resourceIds.push_back(Cubios::GFX_getAssetId(names.at(i).c_str()));62 }63}6465AnimatedSprite::AnimatedSprite(std::vector<std::string>& names, const Cubios::Math::Transform& t):playbackSpeed(50),currentFrame(-1),prevTime(0),playbackMode(Loop),reverse(false),startFrame(0),atlas(nullptr)66{67 this->Transform = t;68 this->numFrames = names.size();69 this->Color.Set(255,0,255);7071 for(size_t i=0;i<this->numFrames;i++)72 {73 this->resourceIds.push_back(Cubios::GFX_getAssetId(names.at(i).c_str()));74 }75}7677AnimatedSprite::AnimatedSprite(std::vector<std::string>& names, float x, float y):playbackSpeed(50),currentFrame(-1),prevTime(0),playbackMode(Loop),reverse(false),startFrame(0),atlas(nullptr)78{79 this->Transform.Position.X = x;80 this->Transform.Position.Y = y;81 this->numFrames = names.size();82 this->Color.Set(255,0,255);8384 for(size_t i=0;i<this->numFrames;i++)85 {86 this->resourceIds.push_back(Cubios::GFX_getAssetId(names.at(i).c_str()));87 }88}8990AnimatedSprite::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),atlas(nullptr)91{92 if(firstFrame<1) firstFrame = 1;93 if(lastFrame<firstFrame) lastFrame = firstFrame;9495 this->Transform = t;96 this->numFrames = 1+lastFrame-firstFrame;97 this->Color.Set(255,0,255);9899 std::string nn;100 for(int16_t i=firstFrame;i<=lastFrame;i++)101 {102 nn = name+std::to_string(i)+"."+extension;103 this->resourceIds.push_back(Cubios::GFX_getAssetId(nn.c_str()));104 }105}106AnimatedSprite::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),atlas(nullptr)107{108 if(firstFrame<1) firstFrame = 1;109 if(lastFrame<firstFrame) lastFrame = firstFrame;110111 this->Transform.Position.X = x;112 this->Transform.Position.Y = y;113 this->numFrames = 1+lastFrame-firstFrame;114 this->Color.Set(255,0,255);115116 std::string nn;117 for(int16_t i=firstFrame;i<=lastFrame;i++)118 {119 nn = name+std::to_string(i)+"."+extension;120 this->resourceIds.push_back(Cubios::GFX_getAssetId(nn.c_str()));121 }122}123124AnimatedSprite::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),atlas(nullptr)125{126 if(firstFrame<1) firstFrame = 1;127 if(lastFrame<firstFrame) lastFrame = firstFrame;128129 this->numFrames = 1+lastFrame-firstFrame;130 this->Color.Set(255,0,255);131132 std::string nn;133 for(int16_t i=firstFrame;i<=lastFrame;i++)134 {135 nn = name+std::to_string(i)+"."+extension;136 this->resourceIds.push_back(Cubios::GFX_getAssetId(nn.c_str()));137 }138}139140AnimatedSprite::AnimatedSprite(const AnimatedSprite& sprite):currentFrame(-1),prevTime(0),startFrame(0)141{142 this->numFrames = sprite.numFrames;143 this->playbackSpeed = sprite.playbackSpeed;144 this->playbackMode = sprite.playbackMode;145 this->startFrame = sprite.startFrame;146 this->reverse = sprite.reverse;147148 this->atlas = sprite.atlas;149150 this->Transform = sprite.Transform;151 this->Color = sprite.Color;152153 for(size_t i=0;i<sprite.resourceIds.size();i++)154 {155 this->resourceIds.push_back(sprite.resourceIds.at(i));156 }157}158159AnimatedSprite::~AnimatedSprite() { }160161void AnimatedSprite::Render()162{163 if(this->currentFrame==-1) return;164165 Math::Vec2 pos = this->ScreenPosition();166167 if(this->atlas==nullptr)168 {169 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);170 }171 else172 {173 Cubios::Gfx::SpriteAtlasElement* el = this->atlas->Get(this->resourceIds.at(this->currentFrame));174 Cubios::Math::Rect2* rc = el->Rect();175176 Cubios::GFX_drawSubImage(this->atlas->resourceId, pos.X,pos.Y,177 rc->v0.X,rc->v0.Y,rc->W,rc->H,178 Color.A(),179 Color.Value(),180 Transform.ScaleX, Transform.ScaleY,181 Transform.SafeRotation()+ScreenAngle, Transform.Mirroring);182 }183}184185Cubios::SceneObject* AnimatedSprite::SetFrame(int16_t frameNumber)186{187 if(frameNumber<0 || frameNumber>this->numFrames-1) frameNumber = 0;188189 if(this->currentFrame==-1) { this->startFrame = frameNumber; if(this->playbackMode==Set) this->currentFrame = frameNumber; return this;}190191 switch(this->playbackMode)192 {193 case Loop: break;194 case Bounce: break;195 case Set:196 {197 this->currentFrame = frameNumber;198 }199 break;200 default: break;201 }202203 this->prevTime = this->lastDt;204205 return this;206}207208Cubios::SceneObject* AnimatedSprite::SetPlaybackSpeed(int16_t speed)209{210 if(speed<1) this->playbackSpeed = 1;211 this->playbackSpeed = speed;212213 return this;214}215216Cubios::SceneObject* AnimatedSprite::SetPlaybackMode(playbackMode_t mode)217{218 this->playbackMode = mode;219220 return this;221}222223int16_t AnimatedSprite::Tick(uint32_t dt)224{225 this->lastDt = dt;226 if(!this->Visible) return this->currentFrame;227228 switch(this->playbackMode)229 {230 case Loop: this->doLoop(dt); break;231 case Bounce: this->doBounce(dt); break;232 case Set: this->doSet(dt); break;233 default: break;234 }235 return this->currentFrame;236}237238int16_t AnimatedSprite::PrevFrame()239{240 if(this->currentFrame>0) return this->currentFrame-1; else return this->numFrames-1;241}242int16_t AnimatedSprite::NextFrame()243{244 if(this->currentFrame<this->numFrames-1) return this->currentFrame+1; else return 0;245}246247void AnimatedSprite::doBounce(uint32_t dt)248{249 if(this->currentFrame==-1)250 {251 this->currentFrame = this->startFrame;252 this->prevTime = dt;253 }254 else255 {256 int32_t curTime = this->prevTime+dt;257 int32_t nf = curTime/playbackSpeed;258259 if(nf>0)260 {261 if(!this->reverse)262 {263 int32_t nextFrame = this->currentFrame+nf;264 this->prevTime = curTime%playbackSpeed;265266 if(nextFrame<(this->numFrames-1))267 {268 this->currentFrame = nextFrame;269 }270 else271 {272 this->currentFrame = this->numFrames-1;273 this->reverse = true;274 }275 }276 else277 {278 int32_t nextFrame = this->currentFrame-nf;279 this->prevTime = curTime%playbackSpeed;280281 if(nextFrame>0)282 {283 this->currentFrame = nextFrame;284 }285 else286 {287 this->currentFrame = 0;288 this->reverse = false;289 }290 }291 }292 else293 {294 this->prevTime = curTime;295 }296 }297}298299void AnimatedSprite::doLoop(uint32_t dt)300{301 if(this->currentFrame==-1)302 {303 this->currentFrame = this->startFrame;304 this->prevTime = dt;305 }306 else307 {308 int32_t curTime = this->prevTime+dt;309 int32_t nf = curTime/playbackSpeed;310311 if(nf>0)312 {313 int32_t nextFrame = this->currentFrame+nf;314 this->prevTime = curTime%playbackSpeed;315316 if(nextFrame<(this->numFrames-1))317 {318 this->currentFrame = nextFrame;319 }320 else321 {322 this->currentFrame = nextFrame%this->numFrames;323 }324 }325 else326 {327 this->prevTime = curTime;328 }329 }330}331332}333}334335
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.