UILabel (component)#

This component renders text on screen. It depends on the Transform component of the Entity. The Transform component must update before this, so it must be added to the Entity first.

Font sizes are dealt with inside of this component, with the font_size, used in the set_text function. In SDL2, the font size is stored inside of the TTF_Font object, meaning that everytime this function is called this pointer’s information is updated. This isn’t a problem, since set_text is only called when updating the text, which is not done all the time.

struct UILabel : public Component#

Public Functions

inline UILabel(float x_shift, float y_shift, int width, int height, std::string mText, std::string mFont_id, SDL_Color &mColour, int mFont_size = 24)#

Constructor

Parameters:
  • x_shift – How much to shift the text box in the x-axis, in pixels (with respect to the Entity’s Transform::position).

  • y_shift – Analogously, in the y-axis (pixels).

  • width – The width of the destination rectangle (pixels).

  • height – The height if the destination rectangle (pixels).

  • mText – The text to be displayed.

  • mFont_id – The font_id (see member).

  • mColour – The text color.

  • mFont_size – The font size (24) by default.

inline ~UILabel()#

The destructor (destroys the texture pointer).

inline virtual void init() override#

Initialization function: it sets the transform member and initializes the destination rectangle position according to the shift. It appears that trying to set the transform member in the constructor doesn’t work (probably something related to not having access to the entity member or something like that).

void set_text(std::string mText, std::string mFont_id)#

Sets the text to be displayed. Called in the constructor. TODO: separate changing the font ID and colour and size in other functions.

virtual void update() override#

Updates dst_rect’s position according to the transform’s (defined in UILabel.cpp).

virtual void render() override#

Update function (called every game tick).

inline void set_transform(Transform *t)#

Function for hooking up this component to another Entity’s transform. This can be useful if we want to have multiple text boxes following a same Entity.

Public Members

vec shift#

How much to shift the text box from the Transform’s position.

std::string text#

The actual text to display.

std::string font_id#

The ID of the font to be used (will be loaded through Game::assets::get_font).

SDL_Color colour#

The color of the object.

int font_size#

Size of the font.

SDL_Rect dst_rect#

The destination rectangle to be rendered.

SDL_Texture *texture#

The texture object to be rendered (managed by the component, and destroyed by the destructor).

Transform *transform#

The Entity’s Transform component for the position. Here we use a pointer, so that we don’t have to explicitly initialize it in the constructor.