The Table Engine
animation.cpp
Go to the documentation of this file.
1 #include "animation.h"
2 
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 
16  return m_rows;
17 }
18 
20  return m_cols;
21 }
22 
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;
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 
48  if (!m_isPlaying) {
49  m_isPlaying = true;
50  m_startTime = SDL_GetTicks();
51  }
52 }
53 
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 
68 }
69 
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 
88 }
Defines the Animation class for handling sprite animations.
void setFrame(int frame)
Sets a specific frame of animation.
Definition: animation.cpp:61
int getNumFrames()
Get the total number of frames.
Definition: animation.cpp:23
int m_rows
Number of rows in the sprite sheet.
Definition: animation.h:23
void setRowsColsInSpriteMap(int rows, int cols)
Sets the number of rows and columns in the sprite map.
Definition: animation.cpp:34
void pause()
Pauses the animation at current frame.
Definition: animation.cpp:54
int getCols()
Get the number of columns.
Definition: animation.cpp:19
int m_pausedTime
Time when animation was paused.
Definition: animation.h:32
void setAutoPlay(bool autoplay)
Sets whether the animation should start playing automatically.
Definition: animation.cpp:27
int m_cols
Number of columns in the sprite sheet.
Definition: animation.h:24
int m_msPerFrame
Milliseconds per frame.
Definition: animation.h:27
int getRows()
Get the number of rows.
Definition: animation.cpp:15
Animation()
Default constructor.
Definition: animation.cpp:3
int m_currentFrame
Current frame of animation.
Definition: animation.h:30
bool m_isPlaying
Whether the animation is currently playing.
Definition: animation.h:29
void play()
Starts or resumes the animation.
Definition: animation.cpp:47
void update() override
Updates the animation state.
Definition: animation.cpp:70
float m_time
Total animation time in seconds.
Definition: animation.h:25
int m_startTime
Time when animation started/resumed.
Definition: animation.h:31
int m_numFrames
Total number of frames.
Definition: animation.h:26
void setAnimationTime(float time)
Sets the total time for the animation cycle.
Definition: animation.cpp:42
void setName(std::string name)
Sets the name of the component.
Definition: component.cpp:19
Component for handling textures in the game engine.
Definition: texture.h:20
SDL_Rect * m_spriteClip
Source rectangle in the sprite sheet for slicing the texture.
Definition: texture.h:25
SDL_Rect * m_spriteBox
Destination rectangle on the screen for rendering the texture.
Definition: texture.h:24