-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimation.h
91 lines (81 loc) · 1.68 KB
/
Animation.h
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
#pragma once
#ifndef __ANIMATION_H__
#define __ANIMATION_H__
#include "SDL_rect.h"
#define MAX_FRAMES 100
class Animation
{
public:
bool loop = true;
bool flow = false;
float speed = 1.0f;
SDL_Rect frames[MAX_FRAMES]; //array of sdl_rect to store each sprite of the animation
private:
float currentFrame = 0.0f;
int lastFrame = 0;
int loops = 0;
enum flow {
FORWARDS,
BACKWARDS
} direction = FORWARDS;
public:
Animation() {}
Animation(const Animation& anim) : loop(anim.loop), speed(anim.speed), lastFrame(anim.lastFrame) { // the colon is used to inherit or in this case to initialize variables before the constructor is called
SDL_memcpy(&frames, anim.frames, sizeof(frames));//copies the info in anim.frames to local variable frames
}
void PushBack(const SDL_Rect& rect) //stores the rect into frames array
{
frames[lastFrame++] = rect;
}
SDL_Rect& GetCurrentFrame()
{
switch (direction)
{
case flow::FORWARDS:
{
currentFrame += speed;
if (currentFrame >= lastFrame)
{
currentFrame = (loop || flow) ? 0.0f : lastFrame - 1;
direction = flow ? flow::BACKWARDS : flow::FORWARDS;
loops++;
}
}
break;
case flow::BACKWARDS:
{
currentFrame -= speed;
if (currentFrame <= 0.0f)
{
currentFrame = 0.0f;
direction = flow::FORWARDS;
loops++;
}
}
break;
}
return frames[(int)currentFrame];
}
SDL_Rect& GetFrame(int frameNumber) {
return frames[frameNumber];
}
bool Finished() const
{
return loops > 0;
}
void Update()
{
currentFrame += speed;
if (currentFrame >= lastFrame)
{
currentFrame = (loop) ? 0.0f : lastFrame - 1;
++loops;
}
}
void Reset()
{
loops = 0;
currentFrame = 0.0f;
}
};
#endif