The Table Engine
animationsmanager.cpp
Go to the documentation of this file.
1 // animationsmanager.cpp
2 #include "animationsmanager.h"
3 
5  setName("animationsmanager");
6 }
7 
9  for (Animation* anim : m_animations) {
10  delete anim;
11  }
12  m_animations.clear();
13 }
14 
15 void AnimationsManager::setTexture(SDL_Texture* texture) {
16  Texture::setTexture(texture);
17  // Propagate texture to all animations
18  for (Animation* anim : m_animations) {
19  anim->setTexture(texture);
20  }
21 }
22 
25  for (Animation* anim : m_animations) {
26  anim->setSizeInSpriteMap(w, h);
27  }
28 }
29 
32  for (Animation* anim : m_animations) {
33  anim->setPositionInSpriteMap(x, y);
34  }
35 }
36 
39  for (Animation* anim : m_animations) {
40  anim->setFlipHorizontal(flip);
41  }
42 }
43 
46  for (Animation* anim : m_animations) {
47  anim->setFlipVertical(flip);
48  }
49 }
50 
51 void AnimationsManager::setAngle(float angle) {
52  Texture::setAngle(angle);
53  for (Animation* anim : m_animations) {
54  anim->setAngle(angle);
55  }
56 }
57 
59  // Set the owner GameObject to the animation
60  animation->setOwner(m_owner);
61  m_animations.push_back(animation);
62  return static_cast<int>(m_animations.size() - 1);
63 }
64 
65 int AnimationsManager::createAnimation(SDL_Texture* texture, int rows, int cols, float time) {
66  Animation* animation = new Animation(texture);
67  animation->setRowsColsInSpriteMap(rows, cols);
68  animation->setAnimationTime(time);
69  return addAnimation(animation);
70 }
71 
73  return m_isPlaying;
74 }
75 
77  return m_currentAnimation;
78 }
79 
81  if (index < m_animations.size()) {
82  m_currentAnimation = index;
83  }
84 }
85 
87  if (m_animations.empty() || m_currentAnimation >= m_animations.size()) {
88  return nullptr;
89  }
91 }
92 
94  m_isPlaying = true;
95  if (Animation* current = getCurrentAnimationObject()) {
96  current->play();
97  }
98 }
99 
101  m_isPlaying = false;
102  if (Animation* current = getCurrentAnimationObject()) {
103  current->pause();
104  }
105 }
106 
108  if (Animation* current = getCurrentAnimationObject()) {
109  current->setFrame(frame);
110  }
111 }
112 
114  if (m_animations.empty() || m_currentAnimation >= m_animations.size()) {
115  return 0;
116  }
117  return m_animations[m_currentAnimation]->getCurrentFrame();
118 }
119 
121  if (Animation* current = getCurrentAnimationObject()) {
122  current->update();
123  }
124 }
125 
127  if (Animation* current = getCurrentAnimationObject()) {
128  current->setOwner(m_owner); // Ensure owner is set before rendering
129  current->render();
130  }
131 }
Component to handle animation of game objects. Derived from Texture.
Definition: animation.h:21
void setRowsColsInSpriteMap(int rows, int cols)
Sets the number of rows and columns in the sprite map.
Definition: animation.cpp:34
void setAnimationTime(float time)
Sets the total time for the animation cycle.
Definition: animation.cpp:42
void setPositionInSpriteMap(int x, int y) override
Sets the position of the sprite in the sprite map.
bool m_isPlaying
Flag to check if the animation is currently playing.
size_t m_currentAnimation
Index of the currently active animation.
int createAnimation(SDL_Texture *texture, int rows, int cols, float time)
void setCurrentAnimation(size_t index)
void setFlipVertical(bool flip) override
Sets vertical flip status.
void render() override
void setTexture(SDL_Texture *texture) override
Sets the SDL_Texture object.
size_t getCurrentAnimation() const
std::vector< Animation * > m_animations
Stores pointers to Animation objects.
void update() override
void setSizeInSpriteMap(int w, int h) override
Sets the size of the sprite in the sprite map.
void setFrame(int frame)
void setFlipHorizontal(bool flip) override
Sets horizontal flip status.
int addAnimation(Animation *animation)
void setAngle(float angle) override
Sets the rotation angle of the texture.
Animation * getCurrentAnimationObject()
GameObject * m_owner
Pointer to the GameObject that owns this component.
Definition: component.h:28
void setName(std::string name)
Sets the name of the component.
Definition: component.cpp:19
void setOwner(GameObject *owner)
Sets the GameObject owner of this component.
Definition: component.cpp:23
Component for handling textures in the game engine.
Definition: texture.h:20
virtual void setTexture(SDL_Texture *texture)
Sets the SDL_Texture object.
Definition: texture.cpp:21
virtual void setAngle(float angle)
Sets the rotation angle of the texture.
Definition: texture.cpp:51
virtual void setPositionInSpriteMap(int x, int y)
Sets the position of the sprite in the sprite map.
Definition: texture.cpp:37
virtual void setFlipHorizontal(bool flip)
Sets horizontal flip status.
Definition: texture.cpp:43
virtual void setFlipVertical(bool flip)
Sets vertical flip status.
Definition: texture.cpp:47
virtual void setSizeInSpriteMap(int w, int h)
Sets the size of the sprite in the sprite map.
Definition: texture.cpp:31