-
Notifications
You must be signed in to change notification settings - Fork 0
/
Entity.cpp
85 lines (64 loc) · 1.84 KB
/
Entity.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
#include "Entity.h"
#include <SDL_image.h>
Entity::Entity(SDL_Renderer* rrenderer)
{
renderer = rrenderer;
}
int Entity::Move(SDL_Point movew)
{
move.x += movew.x;
move.y += movew.y;
return 0;
}
Uint32 Update_Animation(Uint32 interval, void* param)
{
// Adds 1 to the frame and if it equals the maximum frames, resets it to 0
Entity* entity = static_cast<Entity*>(param);
entity->CURRENT_FRAME = (entity->CURRENT_FRAME + 1) % entity->FRAME_MAXIMUM;
return interval;
}
int Entity::Update()
{
Position.x += move.x;
Position.y += move.y;
if (hasAnimation)
{
// If entity has an animator then update the animator and render the entity
static Uint32 timer_id = SDL_AddTimer(100 / animationSpeed, Update_Animation, this);
SDL_Rect srcRect = { CURRENT_FRAME * SPRITE_WIDTH, CURRENT_ROW * SPRITE_HEIGHT, SPRITE_WIDTH, SPRITE_HEIGHT};
SDL_Rect destRect = { Position.x, Position.y, SPRITE_WIDTH, SPRITE_HEIGHT };
SDL_RenderCopy(renderer, sprite_sheet, &srcRect, &destRect);
}
else
{
// Else, only render a white square
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_Rect body = {Position.x, Position.y, 50, 50};
SDL_RenderFillRect(renderer, &body);
}
move.x = 0;
move.y = 0;
return 0;
}
int Entity::SetAnimator(std::string path, int total_frame, int row_total, float speed)
{
SDL_Surface* surface = IMG_Load(&path[0]);
sprite_sheet = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
FRAME_MAXIMUM = total_frame;
hasAnimation = true;
ROW_MAXIMUM = row_total;
animationSpeed = speed;
return 0;
}
int Entity::SetAnimationSpeed(float speed)
{
animationSpeed = speed;
return 0;
}
int Entity::SwitchRow(int row)
{
if (row <= ROW_MAXIMUM)
CURRENT_ROW = row;
return 0;
}