Ember
A C++ 20 'game engine' built with SDL3 with wide platform support.
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
Renderer Class Referenceabstract

Base class for all renderers. More...

#include <ember_core.h>

Inheritance diagram for Renderer:
MetalRenderer OpenglRenderer

Public Member Functions

virtual ~Renderer ()=default
 
virtual void initialize ()=0
 Initialize the renderer and its resources. More...
 
virtual bool load_font (const std::string &font_path, const std::string &font_alias, int font_size=48)=0
 Load a font. More...
 
virtual std::shared_ptr< Textureload_texture (const std::string &path)=0
 Load a texture from disk. More...
 
virtual std::shared_ptr< Textureget_texture (const std::string &path)=0
 Get a previously loaded texture. More...
 
virtual void draw_texture (const Texture *texture, const Rect2 &dest_rect, float rotation, const glm::vec4 &color, const Rect2 &src_rect, int z_index, bool flip_h=false, bool flip_v=false, const UberShader &uber_shader={})=0
 Draw a textured quad. More...
 
virtual void draw_rect (Rect2 rect, float rotation, const glm::vec4 &color, bool filled=true, int z_index=0)=0
 Draw a rectangle (filled or outlined). More...
 
virtual void draw_text (const std::string &text, float x, float y, float rotation, float scale, const glm::vec4 &color, const std::string &font_alias="", int z_index=0, const UberShader &uber_shader=UberShader::none(), int ft_size=0)=0
 Draw text to screen. More...
 
virtual void draw_line (float x1, float y1, float x2, float y2, float thickness, float rotation, const glm::vec4 &color, int z_index=0)=0
 Draw a line. More...
 
virtual void draw_triangle (float x1, float y1, float x2, float y2, float x3, float y3, float rotation, const glm::vec4 &color, bool filled=true, int z_index=0)=0
 Draw a triangle. More...
 
virtual void draw_circle (float center_x, float center_y, float rotation, float radius, const glm::vec4 &color, bool filled=true, int segments=32, int z_index=0)=0
 Draw a circle. More...
 
virtual void draw_polygon (const std::vector< glm::vec2 > &points, float rotation, const glm::vec4 &color, bool filled=true, int z_index=0)=0
 Draw a polygon. More...
 
virtual void flush ()=0
 Submit all batched draw calls. More...
 
virtual void present ()=0
 Swap buffers to present the rendered frame. More...
 
virtual void clear (glm::vec4 color=glm::vec4(0.0f, 0.0f, 0.0f, 0.0f))=0
 Clear the screen to the given color. More...
 
virtual void resize_viewport (int view_width, int view_height)=0
 Resize the rendering context. More...
 
virtual void set_context (const void *ctx)=0
 Set the platform-specific rendering context. More...
 
virtual void * get_context ()=0
 Get the platform-specific rendering context. More...
 
virtual void destroy ()=0
 Destroy rendering resources. More...
 
virtual void unload_font (const Font &font)=0
 Unload a loaded font. More...
 
virtual void unload_texture (Uint32 id)=0
 Unload a texture by its ID. More...
 
void set_view_matrix (const glm::mat4 &view_matrix=glm::mat4(1.f))
 
glm::mat4 get_view_matrix () const
 
virtual Uint32 get_framebuffer_texture () const
 
virtual HashMap< std::string, std::shared_ptr< Texture > > & get_loaded_textures ()
 
virtual std::vector< std::string > & get_loaded_fonts_name ()
 

Public Attributes

int Viewport [2] = {800, 600}
 Viewport size. More...
 
SDL_Window * Window = nullptr
 SDL Window. More...
 
Backend Type = Backend::GL_COMPATIBILITY
 Default Type of renderer. More...
 

Protected Member Functions

virtual void set_default_font (const std::string &font_name)=0
 Set the current font to use for rendering. More...
 
glm::vec2 rotate_point (const glm::vec2 &point, const glm::vec2 &center, float radians)
 Rotate a point around a center point. More...
 
Recti calc_display ()
 Calculate the display rectangle for rendering based on the viewport and window size. More...
 
virtual void render_fbo ()=0
 Render the current frame buffer. More...
 
void submit (const BatchKey &key, float x, float y, float w, float h, float u0, float v0, float u1, float v1, const glm::vec4 &color, float rotation=0.0f, bool is_filled=true)
 Add a quad (textured or untextured) to the appropriate batch. More...
 
virtual void set_effect_uniforms (const UberShader &uber_shader, const glm::vec2 &texture_size=glm::vec2(1, 1))=0
 
virtual glm::vec2 get_texture_size (Uint32 texture_id) const =0
 

Protected Attributes

HashMap< BatchKey, Batch_batches
 All batches by key. More...
 
FT_Library _ft = {}
 FreeType library instance. More...
 
HashMap< std::string, Fontfonts
 Loaded fonts. More...
 
std::string current_font_name
 Currently selected font alias. More...
 
glm::mat4 _projection = glm::mat4(1.f)
 Projection matrix. More...
 
glm::mat4 _view = glm::mat4(1.f)
 View matrix. More...
 
HashMap< std::string, std::shared_ptr< Texture > > _textures
 Cached textures. More...
 
HashMap< Uint32, glm::vec2 > _texture_sizes
 HACK_FIX: Cached texture sizes by ID. More...
 

Detailed Description

Base class for all renderers.

Constructor & Destructor Documentation

◆ ~Renderer()

virtual Renderer::~Renderer ( )
virtualdefault

Member Function Documentation

◆ calc_display()

Recti Renderer::calc_display ( )
protected

Calculate the display rectangle for rendering based on the viewport and window size.

This function calculates the rectangle to draw the viewport in a way that maintains the aspect ratio (letterbox or pillarbox).

Version
1.0.0

◆ clear()

virtual void Renderer::clear ( glm::vec4  color = glm::vec4(0.0f, 0.0f, 0.0f, 0.0f))
pure virtual

Clear the screen to the given color.

Parameters
colorColor to clear the screen with (default is black).

Implemented in MetalRenderer, and OpenglRenderer.

◆ destroy()

virtual void Renderer::destroy ( )
pure virtual

Destroy rendering resources.

Implemented in OpenglRenderer, and MetalRenderer.

◆ draw_circle()

virtual void Renderer::draw_circle ( float  center_x,
float  center_y,
float  rotation,
float  radius,
const glm::vec4 &  color,
bool  filled = true,
int  segments = 32,
int  z_index = 0 
)
pure virtual

Draw a circle.

Implemented in MetalRenderer, and OpenglRenderer.

◆ draw_line()

virtual void Renderer::draw_line ( float  x1,
float  y1,
float  x2,
float  y2,
float  thickness,
float  rotation,
const glm::vec4 &  color,
int  z_index = 0 
)
pure virtual

Draw a line.

Implemented in MetalRenderer, and OpenglRenderer.

◆ draw_polygon()

virtual void Renderer::draw_polygon ( const std::vector< glm::vec2 > &  points,
float  rotation,
const glm::vec4 &  color,
bool  filled = true,
int  z_index = 0 
)
pure virtual

Draw a polygon.

Implemented in OpenglRenderer, and MetalRenderer.

◆ draw_rect()

virtual void Renderer::draw_rect ( Rect2  rect,
float  rotation,
const glm::vec4 &  color,
bool  filled = true,
int  z_index = 0 
)
pure virtual

Draw a rectangle (filled or outlined).

Implemented in MetalRenderer, and OpenglRenderer.

◆ draw_text()

virtual void Renderer::draw_text ( const std::string &  text,
float  x,
float  y,
float  rotation,
float  scale,
const glm::vec4 &  color,
const std::string &  font_alias = "",
int  z_index = 0,
const UberShader uber_shader = UberShader::none(),
int  ft_size = 0 
)
pure virtual

Draw text to screen.

Implemented in OpenglRenderer, and MetalRenderer.

◆ draw_texture()

virtual void Renderer::draw_texture ( const Texture texture,
const Rect2 dest_rect,
float  rotation,
const glm::vec4 &  color,
const Rect2 src_rect,
int  z_index,
bool  flip_h = false,
bool  flip_v = false,
const UberShader uber_shader = {} 
)
pure virtual

Draw a textured quad.

Implemented in OpenglRenderer, and MetalRenderer.

◆ draw_triangle()

virtual void Renderer::draw_triangle ( float  x1,
float  y1,
float  x2,
float  y2,
float  x3,
float  y3,
float  rotation,
const glm::vec4 &  color,
bool  filled = true,
int  z_index = 0 
)
pure virtual

Draw a triangle.

Implemented in MetalRenderer, and OpenglRenderer.

◆ flush()

virtual void Renderer::flush ( )
pure virtual

Submit all batched draw calls.

Implemented in OpenglRenderer, and MetalRenderer.

◆ get_context()

virtual void* Renderer::get_context ( )
pure virtual

Get the platform-specific rendering context.

Implemented in OpenglRenderer, and MetalRenderer.

◆ get_framebuffer_texture()

virtual Uint32 Renderer::get_framebuffer_texture ( ) const
inlinevirtual

Reimplemented in OpenglRenderer.

◆ get_loaded_fonts_name()

virtual std::vector<std::string>& Renderer::get_loaded_fonts_name ( )
inlinevirtual

◆ get_loaded_textures()

virtual HashMap<std::string, std::shared_ptr<Texture> >& Renderer::get_loaded_textures ( )
inlinevirtual

◆ get_texture()

virtual std::shared_ptr<Texture> Renderer::get_texture ( const std::string &  path)
pure virtual

Get a previously loaded texture.

Parameters
pathPath or alias of the texture.
Returns
Reference to the Texture.

Implemented in OpenglRenderer, and MetalRenderer.

◆ get_texture_size()

virtual glm::vec2 Renderer::get_texture_size ( Uint32  texture_id) const
protectedpure virtual

Implemented in MetalRenderer.

◆ get_view_matrix()

glm::mat4 Renderer::get_view_matrix ( ) const

◆ initialize()

virtual void Renderer::initialize ( )
pure virtual

Initialize the renderer and its resources.

Implemented in OpenglRenderer, and MetalRenderer.

◆ load_font()

virtual bool Renderer::load_font ( const std::string &  font_path,
const std::string &  font_alias,
int  font_size = 48 
)
pure virtual

Load a font.

Parameters
font_pathPath to TTF file.
font_aliasAlias name to refer to this font.
font_sizeSize of font in pixels.
Returns
true if loaded successfully.

Implemented in OpenglRenderer, and MetalRenderer.

◆ load_texture()

virtual std::shared_ptr<Texture> Renderer::load_texture ( const std::string &  path)
pure virtual

Load a texture from disk.

Parameters
pathPath to image file.
Returns
Reference to the loaded Texture.

Implemented in OpenglRenderer, and MetalRenderer.

◆ present()

virtual void Renderer::present ( )
pure virtual

Swap buffers to present the rendered frame.

This function is called to display the rendered content on the screen.

Implemented in OpenglRenderer, and MetalRenderer.

◆ render_fbo()

virtual void Renderer::render_fbo ( )
protectedpure virtual

Render the current frame buffer.

Implemented in MetalRenderer.

◆ resize_viewport()

virtual void Renderer::resize_viewport ( int  view_width,
int  view_height 
)
pure virtual

Resize the rendering context.

Parameters
view_widthWidth of the view.
view_heightHeight of the view.

Implemented in MetalRenderer, and OpenglRenderer.

◆ rotate_point()

glm::vec2 Renderer::rotate_point ( const glm::vec2 &  point,
const glm::vec2 &  center,
float  radians 
)
protected

Rotate a point around a center point.

◆ set_context()

virtual void Renderer::set_context ( const void *  ctx)
pure virtual

Set the platform-specific rendering context.

Implemented in OpenglRenderer, and MetalRenderer.

◆ set_default_font()

virtual void Renderer::set_default_font ( const std::string &  font_name)
protectedpure virtual

Set the current font to use for rendering.

Parameters
font_nameAlias of the font to use.

Implemented in OpenglRenderer, and MetalRenderer.

◆ set_effect_uniforms()

virtual void Renderer::set_effect_uniforms ( const UberShader uber_shader,
const glm::vec2 &  texture_size = glm::vec2(1, 1) 
)
protectedpure virtual

Implemented in MetalRenderer.

◆ set_view_matrix()

void Renderer::set_view_matrix ( const glm::mat4 &  view_matrix = glm::mat4(1.f))

◆ submit()

void Renderer::submit ( const BatchKey key,
float  x,
float  y,
float  w,
float  h,
float  u0,
float  v0,
float  u1,
float  v1,
const glm::vec4 &  color,
float  rotation = 0.0f,
bool  is_filled = true 
)
protected

Add a quad (textured or untextured) to the appropriate batch.

◆ unload_font()

virtual void Renderer::unload_font ( const Font font)
pure virtual

Unload a loaded font.

Implemented in OpenglRenderer, and MetalRenderer.

◆ unload_texture()

virtual void Renderer::unload_texture ( Uint32  id)
pure virtual

Unload a texture by its ID.

Implemented in MetalRenderer, and OpenglRenderer.

Member Data Documentation

◆ _batches

HashMap<BatchKey, Batch> Renderer::_batches
protected

All batches by key.

◆ _ft

FT_Library Renderer::_ft = {}
protected

FreeType library instance.

◆ _projection

glm::mat4 Renderer::_projection = glm::mat4(1.f)
protected

Projection matrix.

◆ _texture_sizes

HashMap<Uint32, glm::vec2> Renderer::_texture_sizes
protected

HACK_FIX: Cached texture sizes by ID.

◆ _textures

HashMap<std::string, std::shared_ptr<Texture> > Renderer::_textures
protected

Cached textures.

◆ _view

glm::mat4 Renderer::_view = glm::mat4(1.f)
protected

View matrix.

◆ current_font_name

std::string Renderer::current_font_name
protected

Currently selected font alias.

◆ fonts

HashMap<std::string, Font> Renderer::fonts
protected

Loaded fonts.

◆ Type

Default Type of renderer.

◆ Viewport

int Renderer::Viewport[2] = {800, 600}

Viewport size.

◆ Window

SDL_Window* Renderer::Window = nullptr

SDL Window.


The documentation for this class was generated from the following files: