ST_engine  0.3-ALPHA
Classes | Functions
ST::renderer_sdl Namespace Reference

The renderer for the engine. More...

Classes

class  texture
 

Functions

void set_draw_color (uint8_t, uint8_t, uint8_t, uint8_t)
 
void clear_screen (SDL_Color color)
 
void clear_screen ()
 
void present ()
 
void draw_background (uint16_t arg)
 
void draw_background_parallax (uint16_t arg, uint16_t offset)
 
void draw_overlay (uint16_t arg, uint8_t sprite, uint8_t sprite_num)
 
void draw_texture (uint16_t arg, int32_t x, int32_t y)
 
void draw_texture_scaled (uint16_t arg, int32_t x, int32_t y, float scale_x, float scale_y)
 
void draw_rectangle (int32_t x, int32_t y, int32_t w, int32_t h, SDL_Color color)
 
void draw_rectangle_filled (int32_t x, int32_t y, int32_t w, int32_t h, SDL_Color color)
 
void draw_sprite (uint16_t arg, int32_t x, int32_t y, uint8_t sprite, uint8_t animation, uint8_t animation_num, uint8_t sprite_num)
 
void draw_sprite_scaled (uint16_t arg, int32_t x, int32_t y, uint8_t sprite, uint8_t animation, uint8_t animation_num, uint8_t sprite_num, float scale_x, float scale_y)
 
uint16_t draw_text_cached_glyphs (uint16_t font, const std::string &text, int x, int y, SDL_Color color_font)
 
uint16_t draw_text_lru_cached (uint16_t font, const std::string &arg2, int x, int y, SDL_Color color_font)
 
void upload_surfaces (ska::bytell_hash_map< uint16_t, SDL_Surface * > *surfaces)
 
void upload_fonts (ska::bytell_hash_map< uint16_t, TTF_Font * > *fonts)
 
void vsync_on ()
 
void vsync_off ()
 
int8_t initialize (SDL_Window *win, int16_t width, int16_t height)
 
void close ()
 
void set_resolution (int16_t r_width, int16_t r_height)
 
void cache_font (TTF_Font *Font, uint16_t font_and_size)
 
void process_surfaces (std::vector< std::pair< uint16_t, SDL_Surface * >> &surfaces_pairs)
 

Detailed Description

The renderer for the engine.

A LRU Cache that caches rendered strings, it is used in the drawTextNormal() method of the renderer class.

This renderer is based entirely on the SDL2 renderer and uses it, along with some helper methods and rendering techniques to draw textures, text and primitives to the screen.

Caches textures and the string, font+size used to render them.

Function Documentation

◆ cache_font()

void ST::renderer_sdl::cache_font ( TTF_Font *  Font,
uint16_t  font_and_size 
)

Caches all glyphs of a font at a given size. Works with the draw_text_cached method. Do not confuse this method with the font_cache class, they have nothing in common, this caches fonts to be used with ASCII strings, font_cache is a LRU cache that works with any string.

Parameters
FontThe Font to render with.
font_and_sizeThe name+size of the font.

Definition at line 361 of file renderer_sdl.cpp.

◆ clear_screen() [1/2]

void ST::renderer_sdl::clear_screen ( )

Clears the screen with the color set by set_draw_color

Definition at line 616 of file renderer_sdl.cpp.

◆ clear_screen() [2/2]

void ST::renderer_sdl::clear_screen ( SDL_Color  color)

Clears the screen given a color

Definition at line 607 of file renderer_sdl.cpp.

◆ close()

void ST::renderer_sdl::close ( )

Closes the renderer. Deletes all textures and fonts.

Definition at line 95 of file renderer_sdl.cpp.

◆ draw_background()

void ST::renderer_sdl::draw_background ( uint16_t  arg)

Draws a texture that fills the entire screen (Background).

Parameters
argThe hash of the texture name.

Definition at line 502 of file renderer_sdl.cpp.

◆ draw_background_parallax()

void ST::renderer_sdl::draw_background_parallax ( uint16_t  arg,
uint16_t  offset 
)
Parameters
argThe hash of the name of the texture.
offsetThe offset in the texture for the parallax effect

Definition at line 515 of file renderer_sdl.cpp.

◆ draw_overlay()

void ST::renderer_sdl::draw_overlay ( uint16_t  arg,
uint8_t  sprite,
uint8_t  sprite_num 
)

Draws an animated overlay. Works similarly to draw_sprite, except only one animation is supported.

Parameters
argThe hash of the texture name.
spriteThe number of the sprite to use.
sprite_numThe total number of frames this spritesheet has.

Definition at line 594 of file renderer_sdl.cpp.

◆ draw_rectangle()

void ST::renderer_sdl::draw_rectangle ( int32_t  x,
int32_t  y,
int32_t  w,
int32_t  h,
SDL_Color  color 
)

Draws a rectangle on the screen.

Parameters
xThe X position to draw at.
yThe Y position to draw at.
wThe width of the rectangle.
hThe height of the rectangle.
colorThe color of the rectangle.

Definition at line 491 of file renderer_sdl.cpp.

◆ draw_rectangle_filled()

void ST::renderer_sdl::draw_rectangle_filled ( int32_t  x,
int32_t  y,
int32_t  w,
int32_t  h,
SDL_Color  color 
)

Draws a filled rectangle on the screen.

Parameters
xThe X position to draw at.
yThe Y position to draw at.
wThe width of the rectangle.
hThe height of the rectangle.
colorThe color of the rectangle.

Definition at line 476 of file renderer_sdl.cpp.

◆ draw_sprite()

void ST::renderer_sdl::draw_sprite ( uint16_t  arg,
int32_t  x,
int32_t  y,
uint8_t  sprite,
uint8_t  animation,
uint8_t  animation_num,
uint8_t  sprite_num 
)

Draws a texture that is a spritesheet.

Parameters
argThe hash of the name of the spritesheet.
xThe X position to render at.
yThe Y position to render at.
spriteThe number of the sprite in the texture. (Column in the spritesheet).
animationThe number of the animation in the texture (Row in the spritesheet).
animation_numThe total number of animations in a spritesheet (Rows in the spritesheet).
sprite_numThe total number of sprites in a spritesheet. (Columns in a spritesheet).

Definition at line 544 of file renderer_sdl.cpp.

◆ draw_sprite_scaled()

void ST::renderer_sdl::draw_sprite_scaled ( uint16_t  arg,
int32_t  x,
int32_t  y,
uint8_t  sprite,
uint8_t  animation,
uint8_t  animation_num,
uint8_t  sprite_num,
float  scale_x,
float  scale_y 
)

Draws a texture that is a spritesheet.

Parameters
argThe hash of the name of the spritesheet.
xThe X position to render at.
yThe Y position to render at.
spriteThe number of the sprite in the texture. (Column in the spritesheet).
animationThe number of the animation in the texture (Row in the spritesheet).
animation_numThe total number of animations in a spritesheet (Rows in the spritesheet).
sprite_numThe total number of sprites in a spritesheet. (Columns in a spritesheet).

Definition at line 569 of file renderer_sdl.cpp.

◆ draw_text_cached_glyphs()

uint16_t ST::renderer_sdl::draw_text_cached_glyphs ( uint16_t  font,
const std::string &  text,
int  x,
int  y,
SDL_Color  color_font 
)

This will draw text using cached glyphs - the fastest way possible, works only with ASCII and relatively simple fonts (any complex cursive fonts won't be rendered properly).

Parameters
argThe font to render with.
textThe text to render.
xThe x position to render at.
yThe y position to render at.
color_fontThe color to render with.
sizeThe size of the font to render at.
Returns
The width of the rendered string in pixels

Note that the font must previously be loaded at the selected size.

Definition at line 175 of file renderer_sdl.cpp.

◆ draw_text_lru_cached()

uint16_t ST::renderer_sdl::draw_text_lru_cached ( uint16_t  font,
const std::string &  arg2,
int  x,
int  y,
SDL_Color  color_font 
)

Text rendering method for non-ASCII text – works with cyrillic, expanded Latin (Spanish, German, etc..) and I guess all of UTF8

Parameters
argThe font to render with.
arg2The text to render.
xThe x position to render at.
yThe y position to render at.
color_fontThe color to render with.
sizeThe size of the font to render at.
Returns
The width of the rendered string in pixels

Note that the font must previously be loaded at the selected size.

Definition at line 136 of file renderer_sdl.cpp.

◆ draw_texture()

void ST::renderer_sdl::draw_texture ( uint16_t  arg,
int32_t  x,
int32_t  y 
)

Draw a texture at a given position.

Parameters
argThe hash of the texture name.
xThe X position to render at.
yThe Y position to render at.

Definition at line 438 of file renderer_sdl.cpp.

◆ draw_texture_scaled()

void ST::renderer_sdl::draw_texture_scaled ( uint16_t  arg,
int32_t  x,
int32_t  y,
float  scale_x,
float  scale_y 
)

Draw a texture at a given position and scale it.

Parameters
argThe hash of the texture name.
xThe X position to render at.
yThe Y position to render at.

Definition at line 454 of file renderer_sdl.cpp.

◆ initialize()

int8_t ST::renderer_sdl::initialize ( SDL_Window *  r_window,
int16_t  r_width,
int16_t  r_height 
)

Initializes the renderer.

Parameters
windowThe window to bind this renderer to.
widthThe virtual width of the window.
heightThe virtual height of the window.
Returns
Always 0.

Definition at line 66 of file renderer_sdl.cpp.

◆ present()

void ST::renderer_sdl::present ( )

Presents the framebuffer to the window.

Definition at line 634 of file renderer_sdl.cpp.

◆ process_surfaces()

void ST::renderer_sdl::process_surfaces ( std::vector< std::pair< uint16_t, SDL_Surface * >> &  surfaces_pairs)

Iterates over all surfaces, adding them to texture atlases if they have dimensions that are a power of 2. If a surfaces how non POW2 dimensions, a separate texture is created for it. If the atlas becomes full, this function is called recursively with the remaining textures.

Parameters
surfaces_pairsSurfaces to process.

Definition at line 236 of file renderer_sdl.cpp.

◆ set_draw_color()

void ST::renderer_sdl::set_draw_color ( uint8_t  r,
uint8_t  g,
uint8_t  b,
uint8_t  a 
)

Sets a draw color.

Parameters
rRed value.
gGreen value.
bBlue value.
aAlpha value.

Definition at line 627 of file renderer_sdl.cpp.

◆ set_resolution()

void ST::renderer_sdl::set_resolution ( int16_t  r_width,
int16_t  r_height 
)

Sets the internal rendering resolution

Parameters
r_widthWidth
r_heightHeight

Definition at line 643 of file renderer_sdl.cpp.

◆ upload_fonts()

void ST::renderer_sdl::upload_fonts ( ska::bytell_hash_map< uint16_t, TTF_Font * > *  fonts_t)

Upload fonts to the GPU. (save and cache their glyphs).

Definition at line 324 of file renderer_sdl.cpp.

◆ upload_surfaces()

void ST::renderer_sdl::upload_surfaces ( ska::bytell_hash_map< uint16_t, SDL_Surface * > *  surfaces)

Upload all surface to the GPU. (Create textures from them).

Parameters
surfacesThe surfaces to upload.

Definition at line 202 of file renderer_sdl.cpp.

◆ vsync_off()

void ST::renderer_sdl::vsync_off ( )

Turns off vsync.

Definition at line 418 of file renderer_sdl.cpp.

◆ vsync_on()

void ST::renderer_sdl::vsync_on ( )

Turns on vsync.

Definition at line 403 of file renderer_sdl.cpp.