WOWCube Docs logo
WOWCube Docs
Mission Control
Section Shortcuts
APIExamplesSourceWOWConnectChangelog
Filters
SDK and language defaults persist in cookies.
SDK version
Navigation Tree
Collapsed by default, focused on the active path.
Made byMcKay Seamons
GitHub
  1. Home
  2. Docs
  3. Source
  4. AnimatedSprite.cpp
Mission NodeSDK 6.3C++gfx

AnimatedSprite.cpp

SDK Source File: AnimatedSprite.cpp

Source / SDK 6.3 / C++ / gfx

AnimatedSprite.cpp

AnimatedSprite.cpp
CPP
1/* Copyright Statement:
2 *
3 * (C) 2021-2025 Cubios Inc. All rights reserved.
4 */
5#include "AnimatedSprite.h"
6#include "Screen.h"
7
8namespace Cubios
9{
10namespace Gfx
11{
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);
17
18 std::vector<uint32_t>* ids = atlas->ElementsInsertionOrder();
19
20 for(size_t i=0;i<this->numFrames;i++)
21 {
22 this->resourceIds.push_back(ids->at(i));
23 }
24}
25
26AnimatedSprite::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);
32
33 std::vector<uint32_t>* ids = atlas->ElementsInsertionOrder();
34
35 for(size_t i=0;i<this->numFrames;i++)
36 {
37 this->resourceIds.push_back(ids->at(i));
38 }
39}
40
41AnimatedSprite::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);
45
46 std::vector<uint32_t>* ids = atlas->ElementsInsertionOrder();
47
48 for(size_t i=0;i<this->numFrames;i++)
49 {
50 this->resourceIds.push_back(ids->at(i));
51 }
52}
53
54AnimatedSprite::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);
58
59 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}
64
65AnimatedSprite::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);
70
71 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}
76
77AnimatedSprite::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);
83
84 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}
89
90AnimatedSprite::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;
94
95 this->Transform = t;
96 this->numFrames = 1+lastFrame-firstFrame;
97 this->Color.Set(255,0,255);
98
99 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;
110
111 this->Transform.Position.X = x;
112 this->Transform.Position.Y = y;
113 this->numFrames = 1+lastFrame-firstFrame;
114 this->Color.Set(255,0,255);
115
116 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}
123
124AnimatedSprite::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;
128
129 this->numFrames = 1+lastFrame-firstFrame;
130 this->Color.Set(255,0,255);
131
132 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}
139
140AnimatedSprite::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;
147
148 this->atlas = sprite.atlas;
149
150 this->Transform = sprite.Transform;
151 this->Color = sprite.Color;
152
153 for(size_t i=0;i<sprite.resourceIds.size();i++)
154 {
155 this->resourceIds.push_back(sprite.resourceIds.at(i));
156 }
157}
158
159AnimatedSprite::~AnimatedSprite() { }
160
161void AnimatedSprite::Render()
162{
163 if(this->currentFrame==-1) return;
164
165 Math::Vec2 pos = this->ScreenPosition();
166
167 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 else
172 {
173 Cubios::Gfx::SpriteAtlasElement* el = this->atlas->Get(this->resourceIds.at(this->currentFrame));
174 Cubios::Math::Rect2* rc = el->Rect();
175
176 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}
184
185Cubios::SceneObject* AnimatedSprite::SetFrame(int16_t frameNumber)
186{
187 if(frameNumber<0 || frameNumber>this->numFrames-1) frameNumber = 0;
188
189 if(this->currentFrame==-1) { this->startFrame = frameNumber; if(this->playbackMode==Set) this->currentFrame = frameNumber; return this;}
190
191 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 }
202
203 this->prevTime = this->lastDt;
204
205 return this;
206}
207
208Cubios::SceneObject* AnimatedSprite::SetPlaybackSpeed(int16_t speed)
209{
210 if(speed<1) this->playbackSpeed = 1;
211 this->playbackSpeed = speed;
212
213 return this;
214}
215
216Cubios::SceneObject* AnimatedSprite::SetPlaybackMode(playbackMode_t mode)
217{
218 this->playbackMode = mode;
219
220 return this;
221}
222
223int16_t AnimatedSprite::Tick(uint32_t dt)
224{
225 this->lastDt = dt;
226 if(!this->Visible) return this->currentFrame;
227
228 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}
237
238int16_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}
246
247void AnimatedSprite::doBounce(uint32_t dt)
248{
249 if(this->currentFrame==-1)
250 {
251 this->currentFrame = this->startFrame;
252 this->prevTime = dt;
253 }
254 else
255 {
256 int32_t curTime = this->prevTime+dt;
257 int32_t nf = curTime/playbackSpeed;
258
259 if(nf>0)
260 {
261 if(!this->reverse)
262 {
263 int32_t nextFrame = this->currentFrame+nf;
264 this->prevTime = curTime%playbackSpeed;
265
266 if(nextFrame<(this->numFrames-1))
267 {
268 this->currentFrame = nextFrame;
269 }
270 else
271 {
272 this->currentFrame = this->numFrames-1;
273 this->reverse = true;
274 }
275 }
276 else
277 {
278 int32_t nextFrame = this->currentFrame-nf;
279 this->prevTime = curTime%playbackSpeed;
280
281 if(nextFrame>0)
282 {
283 this->currentFrame = nextFrame;
284 }
285 else
286 {
287 this->currentFrame = 0;
288 this->reverse = false;
289 }
290 }
291 }
292 else
293 {
294 this->prevTime = curTime;
295 }
296 }
297}
298
299void AnimatedSprite::doLoop(uint32_t dt)
300{
301 if(this->currentFrame==-1)
302 {
303 this->currentFrame = this->startFrame;
304 this->prevTime = dt;
305 }
306 else
307 {
308 int32_t curTime = this->prevTime+dt;
309 int32_t nf = curTime/playbackSpeed;
310
311 if(nf>0)
312 {
313 int32_t nextFrame = this->currentFrame+nf;
314 this->prevTime = curTime%playbackSpeed;
315
316 if(nextFrame<(this->numFrames-1))
317 {
318 this->currentFrame = nextFrame;
319 }
320 else
321 {
322 this->currentFrame = nextFrame%this->numFrames;
323 }
324 }
325 else
326 {
327 this->prevTime = curTime;
328 }
329 }
330}
331
332}
333}
334
335
Wrapped for easier reading. Turn wrap off to inspect exact line lengths.
Context Rail

Related nodes

AnimatedSprite.h
Source / SDK 6.3 / C++ / gfx
Background.cpp
Source / SDK 6.3 / C++ / gfx
Background.h
Source / SDK 6.3 / C++ / gfx
Colors.h
Source / SDK 6.3 / C++ / gfx
Previous Node
Gfx.h
Source / SDK 6.3 / C++ / Core
Next Node
AnimatedSprite.h
Source / SDK 6.3 / C++ / gfx