-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAnimation.cpp
90 lines (73 loc) · 1.58 KB
/
Animation.cpp
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
#include "Animation.h"
Animation::Animation()
{
animationFrameCount = 0;
currentFrame = 0;
animClips = NULL;
xAnimPos = 0;
yAnimPos = 0;
}
Animation::~Animation()
{
animationFrameCount = 0;
currentFrame = 0;
animClips = NULL;
xAnimPos = 0;
yAnimPos = 0;
spriteSheetTexture.Free();
}
void Animation::LoadTexture(SDL_Renderer* renderer, const char* path, int frameWidth, int frameHeight, int rows, int columns, int totalSprites)
{
animationFrameCount = totalSprites == 0 ? rows * columns : totalSprites;
animClips = new SDL_Rect[animationFrameCount];
if (!spriteSheetTexture.LoadFromFile(renderer, path))
{
printf("Failed to load spritesheet : %s", path);
}
else
{
for (int i = 0; i < rows; i++)
{
for (int j = 0; j < columns; j++)
{
if (j + i * columns >= animationFrameCount)
{
break;
}
animClips[j + i * columns].x = frameWidth * j;
animClips[j + i * columns].y = frameHeight * i;
animClips[j + i * columns].w = frameWidth;
animClips[j + i * columns].h = frameHeight;
}
}
}
}
void Animation::Animate(int cyclesPerFrame)
{
// Render Current Frame
SDL_Rect* currentClip = &animClips[currentFrame / cyclesPerFrame];
spriteSheetTexture.Render(xAnimPos, yAnimPos, currentClip);
++currentFrame;
if (currentFrame / cyclesPerFrame >= animationFrameCount)
{
currentFrame = 0;
}
}
void Animation::SetAnimPosition(int x, int y)
{
xAnimPos = x;
yAnimPos = y;
}
void Animation::SetAnimScale(float value)
{
spriteSheetTexture.SetScale(value);
}
void Animation::Start()
{
}
void Animation::Update()
{
}
void Animation::Destroy()
{
}