-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnimation.cpp
More file actions
58 lines (50 loc) · 1.28 KB
/
Animation.cpp
File metadata and controls
58 lines (50 loc) · 1.28 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
#include "Animation.h"
Animation::Animation(SDL_Texture* spritesheet, int startframe, int framecount, int framewidth, int frameheight, bool loop)
{
Animation::spritesheet = spritesheet;
Animation::frame = 0;
Animation::startframe = startframe;
Animation::framecount = framecount;
Animation::loop = loop;
Animation::frametime = 0.0f;
Animation::framewidth = framewidth;
Animation::frameheight = frameheight;
create_frames();
}
Animation::~Animation()
{
Animation::spritesheet = nullptr;
for (int i = 0;i < framecount;i++)
{
delete (frames[i]);
}
}
void Animation::create_frames()
{
for (int i = 0;i < framecount;i++)
{
SDL_FRect* f = new SDL_FRect();
f->w = (float)framewidth;
f->h = (float)frameheight;
f->x = (float)((startframe + i) * framewidth);
f->y = 0.0f;
frames.push_back(f);
}
}
void Animation::update(double &deltatime)
{
Animation::frametime += deltatime;
if (frametime > 0.1f)
{
Animation::frame++;
if (Animation::frame >= Animation::framecount)
{
frame = Animation::loop ? 0 : Animation::framecount - 1;
}
Animation::frametime = 0;
}
}
void Animation::render(SDL_Renderer* renderer, SDL_FRect* destination)
{
SDL_RenderTexture(renderer, spritesheet, frames[frame], destination);
}