The Table Engine
animation.cpp
Go to the documentation of this file.
1
#include "
animation.h
"
2
3
Animation::Animation
() :
Texture
() {
4
setName
(
"animation"
);
5
m_startTime
= SDL_GetTicks();
6
m_isPlaying
=
true
;
7
}
8
9
Animation::Animation
(SDL_Texture* texture) :
Texture
(texture) {
10
setName
(
"animation"
);
11
m_startTime
= SDL_GetTicks();
12
m_isPlaying
=
true
;
13
}
14
15
int
Animation::getRows
() {
16
return
m_rows
;
17
}
18
19
int
Animation::getCols
() {
20
return
m_cols
;
21
}
22
23
int
Animation::getNumFrames
() {
24
return
m_numFrames
;
25
}
26
27
void
Animation::setAutoPlay
(
bool
autoplay) {
28
m_isPlaying
= autoplay;
29
if
(autoplay) {
30
m_startTime
= SDL_GetTicks();
31
}
32
}
33
34
void
Animation::setRowsColsInSpriteMap
(
int
rows,
int
cols) {
35
m_rows
= rows;
36
m_cols
= cols;
37
m_numFrames
=
m_rows
*
m_cols
;
38
m_msPerFrame
=
m_time
* 1000 /
m_numFrames
;
39
update
();
40
}
41
42
void
Animation::setAnimationTime
(
float
time) {
43
m_time
= time;
44
m_msPerFrame
=
m_time
* 1000 /
m_numFrames
;
45
}
46
47
void
Animation::play
() {
48
if
(!
m_isPlaying
) {
49
m_isPlaying
=
true
;
50
m_startTime
= SDL_GetTicks();
51
}
52
}
53
54
void
Animation::pause
() {
55
if
(
m_isPlaying
) {
56
m_isPlaying
=
false
;
57
m_pausedTime
= SDL_GetTicks();
58
}
59
}
60
61
void
Animation::setFrame
(
int
frame) {
62
m_currentFrame
= frame %
m_numFrames
;
63
64
m_spriteClip
->w =
m_spriteBox
->w /
m_cols
;
65
m_spriteClip
->h =
m_spriteBox
->h /
m_rows
;
66
m_spriteClip
->x = (
m_currentFrame
%
m_cols
) *
m_spriteClip
->w +
m_spriteBox
->x;
67
m_spriteClip
->y = (
m_currentFrame
/
m_cols
) *
m_spriteClip
->h +
m_spriteBox
->y;
68
}
69
70
void
Animation::update
() {
71
if
(
m_numFrames
== 1) {
72
m_spriteClip
->x =
m_spriteBox
->x;
73
m_spriteClip
->y =
m_spriteBox
->y;
74
m_spriteClip
->w =
m_spriteBox
->w;
75
m_spriteClip
->h =
m_spriteBox
->h;
76
return
;
77
}
78
79
if
(
m_isPlaying
) {
80
int
elapsedTime = SDL_GetTicks() -
m_startTime
;
81
m_currentFrame
= (elapsedTime /
m_msPerFrame
) %
m_numFrames
;
82
}
83
84
m_spriteClip
->w =
m_spriteBox
->w /
m_cols
;
85
m_spriteClip
->h =
m_spriteBox
->h /
m_rows
;
86
m_spriteClip
->x = (
m_currentFrame
%
m_cols
) *
m_spriteClip
->w +
m_spriteBox
->x;
87
m_spriteClip
->y = (
m_currentFrame
/
m_cols
) *
m_spriteClip
->h +
m_spriteBox
->y;
88
}
animation.h
Defines the Animation class for handling sprite animations.
Animation::setFrame
void setFrame(int frame)
Sets a specific frame of animation.
Definition:
animation.cpp:61
Animation::getNumFrames
int getNumFrames()
Get the total number of frames.
Definition:
animation.cpp:23
Animation::m_rows
int m_rows
Number of rows in the sprite sheet.
Definition:
animation.h:23
Animation::setRowsColsInSpriteMap
void setRowsColsInSpriteMap(int rows, int cols)
Sets the number of rows and columns in the sprite map.
Definition:
animation.cpp:34
Animation::pause
void pause()
Pauses the animation at current frame.
Definition:
animation.cpp:54
Animation::getCols
int getCols()
Get the number of columns.
Definition:
animation.cpp:19
Animation::m_pausedTime
int m_pausedTime
Time when animation was paused.
Definition:
animation.h:32
Animation::setAutoPlay
void setAutoPlay(bool autoplay)
Sets whether the animation should start playing automatically.
Definition:
animation.cpp:27
Animation::m_cols
int m_cols
Number of columns in the sprite sheet.
Definition:
animation.h:24
Animation::m_msPerFrame
int m_msPerFrame
Milliseconds per frame.
Definition:
animation.h:27
Animation::getRows
int getRows()
Get the number of rows.
Definition:
animation.cpp:15
Animation::Animation
Animation()
Default constructor.
Definition:
animation.cpp:3
Animation::m_currentFrame
int m_currentFrame
Current frame of animation.
Definition:
animation.h:30
Animation::m_isPlaying
bool m_isPlaying
Whether the animation is currently playing.
Definition:
animation.h:29
Animation::play
void play()
Starts or resumes the animation.
Definition:
animation.cpp:47
Animation::update
void update() override
Updates the animation state.
Definition:
animation.cpp:70
Animation::m_time
float m_time
Total animation time in seconds.
Definition:
animation.h:25
Animation::m_startTime
int m_startTime
Time when animation started/resumed.
Definition:
animation.h:31
Animation::m_numFrames
int m_numFrames
Total number of frames.
Definition:
animation.h:26
Animation::setAnimationTime
void setAnimationTime(float time)
Sets the total time for the animation cycle.
Definition:
animation.cpp:42
Component::setName
void setName(std::string name)
Sets the name of the component.
Definition:
component.cpp:19
Texture
Component for handling textures in the game engine.
Definition:
texture.h:20
Texture::m_spriteClip
SDL_Rect * m_spriteClip
Source rectangle in the sprite sheet for slicing the texture.
Definition:
texture.h:25
Texture::m_spriteBox
SDL_Rect * m_spriteBox
Destination rectangle on the screen for rendering the texture.
Definition:
texture.h:24
Engine
src
components
animation.cpp
Generated by
1.9.1