The Table Engine
texture.cpp
Go to the documentation of this file.
1 #include "texture.h"
2 #include "gameobject.h"
3 #include "resourcemanager.h"
4 
6  setName("texture");
8  m_texture = NULL;
9  m_spriteBox = new SDL_Rect();
10  m_spriteClip = new SDL_Rect();
11 }
12 
13 Texture::Texture(SDL_Texture* texture) : Texture() {
14  setTexture(texture);
15 }
16 
17 SDL_Texture* Texture::getTexture() {
18  return m_texture;
19 }
20 
21 void Texture::setTexture(SDL_Texture* texture) {
22  m_texture = texture;
23  SDL_QueryTexture(m_texture, NULL, NULL, &m_spriteBox->w, &m_spriteBox->h);
24  m_spriteBox->x = 0;
25  m_spriteBox->y = 0;
26 
27  // Initialize sprite clip to full texture
29 }
30 
31 void Texture::setSizeInSpriteMap(int w, int h) {
32  m_spriteBox->w = w;
33  m_spriteBox->h = h;
35 }
36 
37 void Texture::setPositionInSpriteMap(int x, int y) {
38  m_spriteBox->x = x;
39  m_spriteBox->y = y;
41 }
42 
43 void Texture::setFlipHorizontal(bool flip) {
44  m_flipHorizontal = flip;
45 }
46 
47 void Texture::setFlipVertical(bool flip) {
48  m_flipVertical = flip;
49 }
50 
51 void Texture::setAngle(float angle) {
52  m_angle = angle;
53 }
54 
56  // Base texture doesn't need updating
57 }
58 
60  SDL_Rect* rect = m_owner->getComponent<Transform>()->getScreenRect();
61  if (rect->x + rect->w < 0 || rect->x > 640 || rect->y + rect->h < 0 || rect->y > 480)
62  return;
63 
64  int hFlip = (m_flipHorizontal ? SDL_FLIP_HORIZONTAL : SDL_FLIP_NONE);
65  int vFlip = (m_flipVertical ? SDL_FLIP_VERTICAL : SDL_FLIP_NONE);
66  SDL_RendererFlip flip = (SDL_RendererFlip)(hFlip | vFlip);
67 
68  SDL_RenderCopyEx(m_renderer, m_texture, m_spriteClip, rect, m_angle, NULL, flip);
69 }
70 
71 void Texture::setColorMod(const SDL_Color& color) {
72  SDL_SetTextureColorMod(m_texture, color.r, color.g, color.b);
73 }
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
T * getComponent()
Gets a component of the specified type.
Definition: gameobject.h:123
SDL_Renderer * getRenderer()
Gets the current renderer.
static ResourceManager & getInstance()
Retrieves the singleton instance of ResourceManager.
Component for handling textures in the game engine.
Definition: texture.h:20
virtual void update() override
Updates texture properties, used mainly for animations or transformations.
Definition: texture.cpp:55
bool m_flipHorizontal
Flag for horizontal flipping of the texture.
Definition: texture.h:27
virtual void setTexture(SDL_Texture *texture)
Sets the SDL_Texture object.
Definition: texture.cpp:21
float m_angle
Rotation angle of the texture in degrees.
Definition: texture.h:29
SDL_Rect * m_spriteClip
Source rectangle in the sprite sheet for slicing the texture.
Definition: texture.h:25
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
SDL_Texture * m_texture
Pointer to the SDL_Texture object.
Definition: texture.h:22
Texture()
Default constructor for the Texture class, initializes an empty texture.
Definition: texture.cpp:5
virtual void setFlipHorizontal(bool flip)
Sets horizontal flip status.
Definition: texture.cpp:43
SDL_Texture * getTexture()
Retrieves the SDL_Texture object.
Definition: texture.cpp:17
void setColorMod(const SDL_Color &color)
Sets the color modulation for the texture.
Definition: texture.cpp:71
virtual void render() override
Renders the texture to the screen based on current properties.
Definition: texture.cpp:59
SDL_Rect * m_spriteBox
Destination rectangle on the screen for rendering the texture.
Definition: texture.h:24
virtual void setFlipVertical(bool flip)
Sets vertical flip status.
Definition: texture.cpp:47
bool m_flipVertical
Flag for vertical flipping of the texture.
Definition: texture.h:28
SDL_Renderer * m_renderer
Pointer to the SDL_Renderer associated with this texture.
Definition: texture.h:23
virtual void setSizeInSpriteMap(int w, int h)
Sets the size of the sprite in the sprite map.
Definition: texture.cpp:31
Component for handling the transformation of game objects.
Definition: transform.h:23
Manages resources such as textures, fonts, and potentially sounds for a game engine.
Defines the Texture component for managing textures within the Game Engine, encapsulating SDL texture...