Repository navigation
Expand file tree
/
Copy pathAnimator.cpp
More file actions
743 lines (635 loc) · 14.7 KB
/
Copy pathAnimator.cpp
File metadata and controls
743 lines (635 loc) · 14.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
#include "Animator.h"
#include <cstdio>
Animator::Animator(const char* AnimatorName, const unsigned int NumOfFramesPerRow, const unsigned int NumOfRows, const unsigned int Speed, const bool bPlayInReverse, const bool bContinuous, const bool bLooping)
{
Name = AnimatorName;
Framerate = Speed == 0 ? 1 : Speed;
Columns = NumOfFramesPerRow;
Rows = NumOfRows == 0 ? 1 : NumOfRows;
bReverse = bPlayInReverse;
bCanLoop = bLooping;
this->bContinuous = bContinuous;
PlaybackPosition = 0;
DelayFramesCounter = 0;
CurrentFrame = 0;
CurrentRow = 0;
CurrentColumn = 0;
bFlipH = false;
bFlipV = false;
}
void Animator::AssignSprite(const Texture2D& Sprite)
{
this->Sprite = Sprite;
Restart();
}
void Animator::ChangeSprite(const Texture2D& NewSprite, const unsigned int NumOfFramesPerRow, const unsigned int NumOfRows, const unsigned int Speed, const float DelayInSeconds, const bool bPlayInReverse, const bool bContinuous, const bool bLooping)
{
DelayFramesCounter++;
if (GetFPS() >= 0)
{
if (DelayFramesCounter > DelayInSeconds * GetFPS())
{
Rows = NumOfRows == 0 ? 1 : NumOfRows;
Columns = NumOfFramesPerRow;
Framerate = Speed;
bCanLoop = bLooping;
this->bContinuous = bContinuous;
bReverse = bPlayInReverse;
PlaybackPosition = 0;
DelayFramesCounter = 0;
bIsAnimationFinished = false;
bHasStartedPlaying = !bPaused;
AssignSprite(NewSprite);
}
}
}
void Animator::FlipSprite(const bool bHorizontalFlip, const bool bVerticalFlip)
{
//FrameRec.width = fabsf(FrameRec.width) * (bHorizontalFlip ? -1 : 1);
//FrameRec.height = fabsf(FrameRec.height) * (bVerticalFlip ? -1 : 1);
bFlipH = bHorizontalFlip;
bFlipV = !bFlipV;
if (bHorizontalFlip && bVerticalFlip)
{
FrameRec.width *= -1;
FrameRec.height *= -1;
}
else if (bHorizontalFlip)
{
FrameRec.width *= -1;
}
else if (bVerticalFlip)
{
FrameRec.height *= -1;
}
printf("Width: %f, Height: %f\n", FrameRec.width, FrameRec.height);
}
void Animator::SetLooping(const bool bLooping)
{
bCanLoop = bLooping;
}
void Animator::SetContinuous(const bool bIsContinuous)
{
bContinuous = bIsContinuous;
}
void Animator::ResetFrameRec()
{
FrameRec.width = bFlipH ? -float(Sprite.width) / Columns : float(Sprite.width) / Columns;
FrameRec.height = bFlipV ? -float(Sprite.height) / Rows : float(Sprite.height) / Rows;
FrameWidth = FrameRec.width;
FrameHeight = FrameRec.height;
FrameRec.x = bReverse && bContinuous ? Sprite.width - FrameWidth : 0;
FrameRec.y = bReverse && bContinuous ? Sprite.height - FrameHeight : 0;
CurrentFrame = bReverse ? Columns - 1 : 0;
CurrentRow = bReverse ? Rows - 1 : 0;
CurrentColumn = bReverse ? Columns - 1 : 0;
}
void Animator::GoToRow(const unsigned int RowNumber)
{
if (RowNumber >= Rows)
{
FrameRec.y = (Rows - 1) * FrameHeight;
CurrentRow = Rows - 1;
TimeRemainingFramesCounter = float(GetTotalTimeInFrames());
}
else if (Rows >= 1)
{
FrameRec.y = RowNumber == 0 ? 0 : RowNumber * FrameHeight;
CurrentRow = RowNumber;
TimeRemainingFramesCounter = float(GetTotalTimeInFrames() - (RowNumber*Columns + Columns));
}
}
void Animator::GoToColumn(const unsigned int ColumnNumber)
{
if (ColumnNumber >= Columns)
{
FrameRec.x = (Columns - 1) * FrameWidth;
CurrentColumn = Columns - 1;
CurrentFrame = Columns - 1;
TimeRemainingFramesCounter = float(GetTotalTimeInFrames()) - CurrentRow*Columns;
}
else if (Columns >= 1)
{
FrameRec.x = ColumnNumber == 0 ? 0 : ColumnNumber * FrameWidth;
CurrentColumn = ColumnNumber;
CurrentFrame = ColumnNumber;
TimeRemainingFramesCounter = float(GetTotalTimeInFrames()) - CurrentRow*Columns - ColumnNumber;
}
}
void Animator::GoToFirstRow()
{
GoToRow(0);
// Update time remaining
TimeRemainingFramesCounter = float(GetTotalTimeInFrames()) - CurrentColumn;
}
void Animator::GoToFirstColumn()
{
if (!bIsAnimationFinished)
{
GoToColumn(0);
// Update time remaining
bContinuous ? TimeRemainingFramesCounter = float(GetTotalTimeInFrames()) - Columns*CurrentRow : TimeRemainingFramesCounter = float(Columns);
}
else
{
GoToColumn(0);
TimeRemainingFramesCounter = bContinuous ? float(GetTotalTimeInFrames()) - float(GetTotalTimeInFrames()) / Rows * CurrentRow : 0;
}
}
void Animator::GoToLastRow()
{
GoToRow(Rows - 1);
// Update time remaining
bContinuous ? TimeRemainingFramesCounter = float(GetTotalTimeInFrames()) - CurrentColumn - Columns*(Rows-1) : TimeRemainingFramesCounter = float(Columns - CurrentColumn);
}
void Animator::GoToLastColumn()
{
if (!bIsAnimationFinished)
{
GoToColumn(Columns - 1);
// Update time remaining
if (bContinuous)
{
if (!bReverse)
{
if (Columns*CurrentRow != 0)
TimeRemainingFramesCounter = float(GetTotalTimeInFrames()) - Columns*CurrentRow + Columns;
else
TimeRemainingFramesCounter = float(GetTotalTimeInFrames()) - Columns;
}
else
{
TimeRemainingFramesCounter = float(Columns*CurrentRow + Columns);
}
}
else
TimeRemainingFramesCounter = bIsAnimationFinished ? 0.0f : float(Columns);
}
else
{
GoToColumn(Columns - 1);
TimeRemainingFramesCounter = float(GetTotalTimeInFrames()) - CurrentRow * Columns - Columns;
}
}
void Animator::GoToFrame(const unsigned int FrameNumber)
{
// Does frame exist in sprite-sheet
if (FrameNumber < Columns * Rows)
{
GoToRow(FrameNumber / Columns);
GoToColumn(FrameNumber % Columns);
}
else
printf("ERROR from GoToFrame(): Frame %u does not exist! %s sprite has frames from %u to %u.\n", FrameNumber, Name, 0, Rows*Columns-1);
}
void Animator::GoToFirstFrame()
{
GoToColumn(0);
}
void Animator::GoToLastFrame()
{
GoToColumn(Columns - 1);
}
void Animator::GoToFirstFrameOfSpriteSheet()
{
GoToRow(0);
GoToColumn(0);
}
void Animator::GoToLastFrameOfSpriteSheet()
{
GoToRow(Rows - 1);
GoToColumn(Columns - 1);
}
void Animator::NextFrame()
{
// Only increment when animation is playing
if (!bIsAnimationFinished)
{
CurrentFrame++;
CurrentColumn++;
}
if (bCanLoop)
{
// Are we over the max columns
if (CurrentFrame > Columns - 1)
{
// If we are continuous, Go to the next row in the sprite-sheet
if (bContinuous)
{
NextRow();
GoToFirstColumn();
}
// Otherwise, Go back to the start
else
{
GoToFirstColumn();
}
}
}
else
{
// Are we over the max columns
if (CurrentFrame > Columns - 1)
{
// If we are continuous, Go to the next row in the sprite-sheet
if (bContinuous)
{
// Clamp values back down
CurrentFrame = Columns - 1;
CurrentColumn = Columns - 1;
// Go to next row if we are not at the last frame
if (!IsAtLastFrame())
{
NextRow();
GoToFirstColumn();
}
else
bIsAnimationFinished = true;
}
// Otherwise, Stay at the end
else
{
bIsAnimationFinished = true;
GoToLastColumn();
}
}
}
}
void Animator::PreviousFrame()
{
// Only decrement when animation is playing
if (!bIsAnimationFinished)
{
CurrentFrame--;
CurrentColumn--;
}
if (bCanLoop)
{
// Are we over the max columns OR equal to zero
if (CurrentFrame == 0 || CurrentFrame > Columns)
{
// If we are continuous, Go to the previous row in the sprite-sheet
if (bContinuous)
{
PreviousRow();
GoToLastColumn();
}
// Otherwise, Go back to the last column
else
{
GoToLastColumn();
}
}
}
else
{
// Are we over the max columns OR equal to zero
if (CurrentFrame == 0 || CurrentFrame > Columns)
{
// If we are continuous, Go to the previous row in the sprite-sheet
if (bContinuous)
{
// Clamp values back down
CurrentFrame = 0;
CurrentColumn = 0;
// Go to previous row if we are not at the first frame
if (!IsAtFirstFrame())
{
PreviousRow();
GoToLastColumn();
}
else
bIsAnimationFinished = true;
}
// Otherwise, Stay at the start
else
{
bIsAnimationFinished = true;
GoToFirstColumn();
}
}
}
}
float Animator::Lerp(const float Start, const float End, const float Alpha)
{
return (1.0f - Alpha) * Start + Alpha * End;
}
void Animator::NextRow()
{
FrameRec.y += FrameHeight;
if (FrameRec.y >= Sprite.height)
{
// Go to start
if (bCanLoop)
{
FrameRec.y = 0;
CurrentRow = 0;
}
// Stay at end
else
{
FrameRec.y = float(Sprite.height);
CurrentRow = Rows - 1;
}
ResetTimer();
}
else
CurrentRow++;
// Update the time remaining
TimeRemainingFramesCounter = GetTotalTimeInFrames() - (float(GetTotalTimeInFrames()) / Rows) * CurrentRow;
}
void Animator::PreviousRow()
{
FrameRec.y -= FrameHeight;
if (FrameRec.y < 0)
{
FrameRec.y = float(Sprite.height) - FrameHeight;
CurrentRow = Rows - 1;
ResetTimer();
}
else
CurrentRow--;
// Update the time remaining
if (!bReverse)
TimeRemainingFramesCounter = GetTotalTimeInFrames() - (float(GetTotalTimeInFrames()) / Rows) * CurrentRow;
}
void Animator::NextColumn()
{
FrameRec.x += FrameWidth;
if (FrameRec.x > Sprite.width)
{
FrameRec.x = 0;
CurrentColumn = 0;
}
else
CurrentColumn++;
// Update the time remaining
TimeRemainingFramesCounter -= 1;
}
void Animator::PreviousColumn()
{
FrameRec.x -= FrameWidth;
if (FrameRec.x < 0)
{
FrameRec.x = float(Sprite.width) - FrameWidth;
CurrentColumn = Columns - 1;
}
else
CurrentColumn--;
// Update the time remaining
TimeRemainingFramesCounter += 1;
}
void Animator::Forward()
{
if (bReverse)
bReverse = false;
}
void Animator::Reverse(const bool bToggle)
{
if (bToggle)
{
bReverse = !bReverse;
TimeRemainingFramesCounter += GetTotalTimeInFrames() - TimeRemainingFramesCounter*2;
bIsAnimationFinished = false;
}
else
{
bReverse = true;
TimeRemainingFramesCounter += GetTotalTimeInFrames() - TimeRemainingFramesCounter*2;
bIsAnimationFinished = false;
}
}
void Animator::Restart()
{
ResetFrameRec();
ResetTimer();
bHasStartedPlaying = true;
}
unsigned Animator::GetTotalFrames() const
{
return Rows * Columns;
}
unsigned Animator::GetTotalRows() const
{
return Rows;
}
unsigned Animator::GetTotalColumns() const
{
return Columns;
}
unsigned Animator::GetCurrentFrame() const
{
return CurrentRow*Columns + CurrentColumn;
}
unsigned Animator::GetCurrentRow() const
{
return CurrentRow;
}
unsigned Animator::GetCurrentColumn() const
{
return CurrentColumn;
}
unsigned int Animator::GetTotalTimeInFrames() const
{
return bContinuous ? Columns * Rows : Columns;
}
float Animator::GetTotalTimeInSeconds() const
{
return bContinuous ? float(Columns * Rows) / Framerate : float(Columns) / Framerate;
}
unsigned Animator::GetTimeRemainingInFrames() const
{
return unsigned(TimeRemainingFramesCounter);
}
float Animator::GetTimeRemainingInSeconds() const
{
return float(TimeRemainingFramesCounter) / Framerate;
}
const char* Animator::GetName() const
{
return Name;
}
void Animator::CountdownInFrames()
{
if (TimeRemainingFramesCounter != 0.0f)
TimeRemainingFramesCounter -= GetFrameTime() < 0.01f ? Framerate * GetFrameTime() : 0.0f;
if (TimeRemainingFramesCounter <= 0.0f)
TimeRemainingFramesCounter = 0.0f;
}
void Animator::Play()
{
if (!bPaused)
{
PlaybackPosition++;
// Update the time remaining
if (!bIsAnimationFinished)
CountdownInFrames();
// Has 'X' amount of frames passed?
if (PlaybackPosition > GetFPS() / Framerate)
{
// Reset playback position
PlaybackPosition = 0;
// Go to previous frame when reversing
if (bReverse)
PreviousFrame();
// Go to next frame if not reversing
else
NextFrame();
}
// Only go to next frame if animation has not finished playing
if (!bIsAnimationFinished)
FrameRec.x = float(CurrentFrame)*FrameWidth;
//printf("Row: %u, Column: %u\n", CurrentRow, CurrentColumn);
bHasStartedPlaying = false;
}
}
void Animator::LerpAnim(const float Speed, const bool bConstant)
{
PlaybackPosition++;
if (PlaybackPosition > GetFPS() / Framerate)
{
PlaybackPosition = 0;
if (bConstant)
FrameRec.x += Speed * GetFrameTime();
else
FrameRec.x = Lerp(FrameRec.x, Sprite.width, Speed * GetFrameTime());
}
}
void Animator::Start()
{
UnPause();
if (!bHasStartedPlaying)
bHasStartedPlaying = true;
}
void Animator::Stop()
{
PlaybackPosition = 0;
CurrentColumn = 0;
CurrentFrame = 0;
CurrentRow = 0;
bHasStartedPlaying = true;
bIsAnimationFinished = true;
ResetFrameRec();
ResetTimer();
Pause();
}
void Animator::UnPause()
{
bPaused = false;
bHasStartedPlaying = true;
}
void Animator::Pause(const bool bToggle)
{
if (bToggle)
{
bPaused = !bPaused;
bHasStartedPlaying = !bPaused;
}
else
{
bPaused = true;
bHasStartedPlaying = false;
}
}
void Animator::SetFramerate(const unsigned int NewFramerate)
{
Framerate = NewFramerate;
}
bool Animator::IsAtFrame(const unsigned int FrameNumber) const
{
// Does frame exist in sprite-sheet
if (FrameNumber < Columns * Rows)
{
const unsigned int RowFrameNumberIsOn = FrameNumber / Columns;
const unsigned int ColumnFrameNumberIsOn = FrameNumber % Columns;
return IsAtRow(RowFrameNumberIsOn) && IsAtColumn(ColumnFrameNumberIsOn);
}
printf("ERROR from IsAtFrame(): Frame %u does not exist! %s sprite has frames from %u to %u.\n", FrameNumber, Name, 0, Rows*Columns-1);
return false;
}
bool Animator::IsAtRow(const unsigned int RowNumber) const
{
if (RowNumber < Rows)
return RowNumber == CurrentRow;
printf("ERROR from IsAtRow(): Row does not exist!\n");
return false;
}
bool Animator::IsAtColumn(const unsigned int ColumnNumber) const
{
if (ColumnNumber < Columns)
return ColumnNumber == CurrentColumn;
printf("ERROR from IsAtColumn(): Column does not exist!\n");
return false;
}
bool Animator::IsAtFirstFrameOfSpriteSheet() const
{
return IsAtFirstRow() && IsAtFirstColumn();
}
bool Animator::IsAtLastFrameOfSpriteSheet() const
{
return IsAtLastRow() && IsAtLastColumn();
}
bool Animator::IsAtFirstRow() const
{
return CurrentRow == 0;
}
bool Animator::IsAtFirstColumn() const
{
return CurrentColumn == 0;
}
bool Animator::IsAtFirstFrame() const
{
return bContinuous ? IsAtFirstRow() && IsAtFirstColumn() : IsAtFirstColumn();
}
bool Animator::IsAtLastFrame() const
{
return bContinuous ? IsAtLastRow() && IsAtLastColumn() : IsAtLastColumn();
}
void Animator::ResetTimer()
{
TimeRemainingFramesCounter = float(GetTotalTimeInFrames());
}
Rectangle Animator::GetFrameRec() const
{
return FrameRec;
}
Texture2D Animator::GetSprite() const
{
return Sprite;
}
bool Animator::IsAtLastRow() const
{
return CurrentRow == Rows - 1;
}
bool Animator::IsAtLastColumn() const
{
return CurrentColumn == Columns - 1;
}
bool Animator::IsStartedPlaying()
{
if (IsAtFirstFrame())
{
ResetTimer();
return true;
}
return bHasStartedPlaying;
}
bool Animator::IsFinishedPlaying()
{
if (IsAtLastFrame())
{
ResetTimer();
return true;
}
if (!bCanLoop)
return bIsAnimationFinished;
return bIsAnimationFinished;
}
bool Animator::IsPlaying() const
{
if (bCanLoop)
return !bPaused;
if (!bCanLoop && bContinuous)
return !bIsAnimationFinished;
return !bIsAnimationFinished;
}