SPARK  0.1.0
A general purpose game engine written in C++.
Loading...
Searching...
No Matches
spark::core::Renderer2D< Backend > Class Template Reference

An object that can render 2D graphics on a surface. More...

#include <Renderer2D.h>

Public Types

using backend_type = Backend
 
using device_type = typename backend_type::device_type
 
using buffer_type = typename device_type::buffer_type
 
using command_buffer_type = typename device_type::command_buffer_type
 
using descriptor_set_type = typename command_buffer_type::descriptor_set_type
 
using adapter_type = typename device_type::adapter_type
 
using surface_type = typename backend_type::surface_type
 
using render_pass_type = typename backend_type::render_pass_type
 
using render_pipeline_type = typename backend_type::render_pipeline_type
 
using pipeline_layout_type = typename backend_type::pipeline_layout_type
 
using shader_program_type = typename backend_type::shader_program_type
 
using shader_module_type = typename shader_program_type::shader_module_type
 
using input_assembler_type = typename backend_type::input_assembler_type
 
using rasterizer_type = typename backend_type::rasterizer_type
 
using factory_type = typename device_type::factory_type
 
using vertex_buffer_layout_type = typename factory_type::vertex_buffer_layout_type
 
using vertex_buffer_type = typename factory_type::vertex_buffer_type
 
using index_buffer_layout_type = typename factory_type::index_buffer_layout_type
 
using index_buffer_type = typename factory_type::index_buffer_type
 

Public Member Functions

 Renderer2D (const math::Vector2< unsigned > &render_area, std::function< typename surface_type::handle_type(const typename backend_type::handle_type &)> surface_factory, std::span< std::string > required_extensions)
 Creates a new 2D renderer.
 
 Renderer2D (const Renderer2D &other)=delete
 
 Renderer2D (Renderer2D &&other) noexcept=delete
 
Renderer2Doperator= (const Renderer2D &other)=delete
 
Renderer2Doperator= (Renderer2D &&other) noexcept=delete
 
void recreateSwapChain (const math::Vector2< unsigned > &new_size)
 Recreates the swap chain with frame buffers of the new size.
 
void render ()
 Draws the current frame.
 
void drawQuad (const glm::mat4 &transform_matrix, const spark::math::Vector4< float > &color={1.f, 1.f, 1.f, 1.f})
 Draws a 1x1 quad with the given transformation_matrix.
 
void drawCircle (const glm::mat4 &transform_matrix, float radius, const spark::math::Vector4< float > &color={1.f, 1.f, 1.f, 1.f})
 Draws a circle with the given transformation_matrix and radius.
 

Friends

class Window
 

Detailed Description

template<typename Backend>
class spark::core::Renderer2D< Backend >

An object that can render 2D graphics on a surface.

Template Parameters
BackendThe backend to use for rendering. Must be a subclass of render::RenderBackend.

Constructor & Destructor Documentation

◆ Renderer2D()

template<typename Backend >
spark::core::Renderer2D< Backend >::Renderer2D ( const math::Vector2< unsigned > & render_area,
std::function< typename surface_type::handle_type(const typename backend_type::handle_type &)> surface_factory,
std::span< std::string > required_extensions )
explicit

Creates a new 2D renderer.

Parameters
render_areaThe size of the area to render to.
surface_factoryA factory function that creates a raw surface handle from a raw device handle.
required_extensionsThe list of extensions that the renderer backend requires.

Member Function Documentation

◆ drawCircle()

template<typename Backend >
void spark::core::Renderer2D< Backend >::drawCircle ( const glm::mat4 & transform_matrix,
float radius,
const spark::math::Vector4< float > & color = {1.f, 1.f, 1.f, 1.f} )

Draws a circle with the given transformation_matrix and radius.

Parameters
transform_matrixThe 4x4 matrix describing the transformation of the circle into the final world space.
radiusThe desired radius of the circle.
colorThe color of the circle. Defaults to white.

Internally, the circle is drawn as a 1x1 quad with the given transformation matrix (in the same batch as the quads). Then, the radius is passed to the fragment shader, which uses it to calculate the distance of each fragment to the center of the circle. It discards the fragment if the distance is greater than the radius, effectively clipping the quad into a circle.

◆ drawQuad()

template<typename Backend >
void spark::core::Renderer2D< Backend >::drawQuad ( const glm::mat4 & transform_matrix,
const spark::math::Vector4< float > & color = {1.f, 1.f, 1.f, 1.f} )

Draws a 1x1 quad with the given transformation_matrix.

Parameters
transform_matrixThe 4x4 matrix describing the transformation of the 1x1 quad into the final world space.
colorThe color of the quad. Defaults to white.

◆ recreateSwapChain()

template<typename Backend >
void spark::core::Renderer2D< Backend >::recreateSwapChain ( const math::Vector2< unsigned > & new_size)

Recreates the swap chain with frame buffers of the new size.

Parameters
new_sizeThe new size of the render area.