Sprite (component)#
Component used for handling Entity’s sprites (textures and animations). Although
texture loading and their memory management are now completely handled by the
Game::assets
(class of type
Game::AssetManager
), this
component will keep pointers to the textures involved. Some manual handling is
still possible with the use of the :cpp:member`Sprite::add_animation`,
:cpp:member`Sprite::set_animation`, and :cpp:member`Sprite::set_texture` functions.
This can be useful in the future.
Function Sprite::maybe_update_collider
will handle updating the
Collider
component’s shape automatically if it is enabled. It is
called in the setter functions defined here. That is why the destination rectangle
(dst_rect
) and the scale variables (scale_x, scale_y
) are private members.
Note
In the future, it would be useful to be able to render more than one animation at once. This would allow better control of some animations.
-
class Sprite : public Component#
- #include <Sprite.hpp>
NOTE: dst_rect’s position is SET in the update method, according to the Transform object. To change it, one must change the Transform’s position instead of trying to change it here.
Public Functions
-
Sprite(std::string texture_id)#
-
Sprite(TexTup texture_tuple, bool is_animated = false)#
This constructor is kept for the TileMap, for which multiple tile textures are contained in one single image file and thus texture object. This allows manual control over the sprite.
-
virtual void init() override#
Defined in Sprite.cpp file (long)
Defining destination_rect position from transform: Note that the transform will always have already been defined because it MUST be updated before the sprite, so there is no reason to check if it exists here.
-
virtual void update() override#
Defined in Sprite.cpp file (long)
-
virtual void render() override#
Defined in Sprite.cpp file (long)
-
void add_animation(Animation animation, std::string animation_name)#
Although dealing with animations is mostly automatized now, this manual control could be useful if we would like to manually tune some things, especially if we want to have animated tiles, for example. Should be avoided, though.
-
void set_animation(std::string animation_name)#
Changes current animation (reaches into the animation_map member).
-
void set_animation(int an_index)#
Overloading.
-
void set_texture(std::string texture_id)#
Overriding the current texture and its width/height information. This is more for showing off the different player models and is not supposed to remain later.
-
void maybe_update_collider()#
Checks if set_collider is true and updates the collider’s shape if so.
Why not check for a member of collider? Like getComponent<Collider>().dynamic_shape ? Because this function is called in Sprite’s initialization, before Sprite’s “entity” member is initialized. So we can’t access other components yet… It is easier to initialize set_collider to false and have this called.
-
inline void set_dst_width(int w)#
Sets actual width on the screen.
-
inline void set_dst_height(int h)#
Sets actual height on the screen.
-
inline int get_dst_width()#
Gets actual width on the screen.
-
inline int get_dst_height()#
Gets actual height on the screen.
-
inline void set_xscale(float sclx)#
-
inline float get_xscale()#
-
inline void set_yscale(float scly)#
-
inline float get_yscale()#
-
inline void set_scale(float scale)#
Public Members
-
int image_width#
-
int image_height#
Image shape (source).
-
std::string m_texture_id#
The texture ID.
-
SDL_Texture *texture#
(
scale_x = dst_rect.w/src_rect.w
andscale_y = dst_rect.h/src_rect.h
).Pointer to the Texture used.
-
SDL_Rect src_rect#
Rendering rectangle (source).
-
SDL_RendererFlip sprite_flip = SDL_FLIP_NONE#
Whether to flip the texture. Will be dealt with by the KeyboardPlayer Component, when the player turns around, for example.
-
vec shift = vec(0, 0)#
A possible shift to add to the position of the transform for rendering the object on screen.
-
bool animated#
Whether the sprite is is animated.
-
int frames#
How many frames in the current animation.
-
int animation_period#
The period of the current animation being played (ms/frame).
-
int nb_animations#
Total number of animations.
-
std::map<std::string, Animation> animation_map#
Map translating animation names to animation objects.
-
std::string current_animation#
Current animation being rendered.
-
int current_frame#
The frame of the animation we are at (starts at 0).
-
Uint64 m_reference_time#
Reference time for calculating which frame to show.
-
Sprite(std::string texture_id)#