Ember
A C++ 20 'game engine' built with SDL3 with wide platform support.
ember_gl.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "core/ember_core.h"
4 
5 // TODO: get this based on device?
6 constexpr int MAX_QUADS = 10000;
7 constexpr int MAX_VERTICES = MAX_QUADS * 4;
8 constexpr int MAX_INDICES = MAX_QUADS * 6;
9 
18 constexpr int MAX_TEXTURE_SLOTS = 16;
19 
25 class OpenglRenderer final : public Renderer {
26 public:
28  ~OpenglRenderer() override;
29 
30  void setup_shaders(Shader* default_shader, Shader* framebuffer_shader);
31 
32  void initialize() override;
33 
34  void resize_viewport(int view_width, int view_height) override;
35 
36  void set_context(const void* ctx) override;
37 
38  void* get_context() override;
39 
40  void destroy() override;
41 
42  std::shared_ptr<Texture> load_texture(const std::string& file_path) override;
43 
44  std::shared_ptr<Texture> get_texture(const std::string& path) override;
45 
46  bool load_font(const std::string& file_path, const std::string& font_alias, int font_size) override;
47 
48  void set_default_font(const std::string& font_name) override;
49 
50  void unload_font(const Font& font) override;
51 
52  void unload_texture(Uint32 id) override;
53 
54  void draw_texture(const Texture* texture, const Rect2& dest_rect, float rotation, const glm::vec4& color,
55  const Rect2& src_rect, int z_index,
56  bool flip_h, bool flip_v, const UberShader& uber_shader) override;
57 
58  void draw_rect(Rect2 rect, float rotation, const glm::vec4& color, bool filled, int z_index) override;
59 
60  void draw_text(const std::string& text, float x, float y, float rotation, float scale, const glm::vec4& color,
61  const std::string& font_alias, int z_index, const UberShader& uber_shader, int ft_size) override;
62 
63  void draw_line(float x1, float y1, float x2, float y2, float width, float rotation, const glm::vec4& color, int z_index) override;
64 
65  void draw_triangle(float x1, float y1, float x2, float y2, float x3, float y3, float rotation, const glm::vec4& color, bool filled,
66  int z_index) override;
67 
68  void draw_circle(float center_x, float center_y, float rotation, float radius, const glm::vec4& color, bool filled, int segments,
69  int z_index) override;
70 
71  void draw_polygon(const std::vector<glm::vec2>& points, float rotation, const glm::vec4& color, bool filled, int z_index) override;
72 
73  void flush() override;
74 
75  void present() override;
76 
77  void clear(glm::vec4 color) override;
78 
79  Uint32 get_framebuffer_texture() const override {
80  return _fbo_texture;
81  }
82 private:
83  OpenglShader* _default_shader = nullptr;
84  OpenglShader* _fbo_shader = nullptr;
85 
86  // DEFAULT shader (text/texture/primitives)
87  GLuint vao, vbo, ebo;
88  GLuint shader_program;
89 
90  // DEFAULT FBO shader
91  GLuint _fbo_vao,_fbo_vbo,_frame_buffer_object, _fbo_texture;
92 
93  SDL_GLContext context = nullptr;
94 
95  void render_fbo() override;
96 
97  void set_effect_uniforms(const UberShader& uber_shader, const glm::vec2& texture_size = glm::vec2(1, 1)) override;
98 
99  [[nodiscard]] glm::vec2 get_texture_size(Uint32 texture_id) const override;
100 
101 };
Opengl Renderer implementation.
Definition: ember_gl.h:25
void setup_shaders(Shader *default_shader, Shader *framebuffer_shader)
Definition: ember_gl.cpp:234
void flush() override
Submit all batched draw calls.
Definition: ember_gl.cpp:599
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, bool flip_v, const UberShader &uber_shader) override
Draw a textured quad.
Definition: ember_gl.cpp:559
std::shared_ptr< Texture > get_texture(const std::string &path) override
Get a previously loaded texture.
Definition: ember_gl.cpp:11
void draw_circle(float center_x, float center_y, float rotation, float radius, const glm::vec4 &color, bool filled, int segments, int z_index) override
Draw a circle.
Definition: ember_gl.cpp:148
void unload_font(const Font &font) override
Unload a loaded font.
Definition: ember_gl.cpp:750
void clear(glm::vec4 color) override
Clear the screen to the given color.
Definition: ember_gl.cpp:704
void unload_texture(Uint32 id) override
Unload a texture by its ID.
Definition: ember_gl.cpp:764
void draw_triangle(float x1, float y1, float x2, float y2, float x3, float y3, float rotation, const glm::vec4 &color, bool filled, int z_index) override
Draw a triangle.
Definition: ember_gl.cpp:124
OpenglRenderer()
Definition: ember_gl.cpp:5
void destroy() override
Destroy rendering resources.
Definition: ember_gl.cpp:374
~OpenglRenderer() override
void draw_polygon(const std::vector< glm::vec2 > &points, float rotation, const glm::vec4 &color, bool filled, int z_index) override
Draw a polygon.
Definition: ember_gl.cpp:193
bool load_font(const std::string &file_path, const std::string &font_alias, int font_size) override
Load a font.
Definition: ember_gl.cpp:481
Uint32 get_framebuffer_texture() const override
Definition: ember_gl.h:79
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, const UberShader &uber_shader, int ft_size) override
Draw text to screen.
Definition: ember_gl.cpp:36
void draw_rect(Rect2 rect, float rotation, const glm::vec4 &color, bool filled, int z_index) override
Draw a rectangle (filled or outlined).
Definition: ember_gl.cpp:29
std::shared_ptr< Texture > load_texture(const std::string &file_path) override
Load a texture from disk.
Definition: ember_gl.cpp:410
void initialize() override
Initialize the renderer and its resources.
Definition: ember_gl.cpp:242
void resize_viewport(int view_width, int view_height) override
Resize the rendering context.
Definition: ember_gl.cpp:333
void present() override
Swap buffers to present the rendered frame.
Definition: ember_gl.cpp:673
void draw_line(float x1, float y1, float x2, float y2, float width, float rotation, const glm::vec4 &color, int z_index) override
Draw a line.
Definition: ember_gl.cpp:96
void set_default_font(const std::string &font_name) override
Set the current font to use for rendering.
Definition: ember_gl.cpp:21
void * get_context() override
Get the platform-specific rendering context.
Definition: ember_gl.cpp:370
void set_context(const void *ctx) override
Set the platform-specific rendering context.
Definition: ember_gl.cpp:366
Opengl Shader implementation.
Definition: shader_gl.h:18
Base class for all renderers.
Definition: ember_core.h:109
Shader Abstract class.
Definition: shader.h:67
constexpr int MAX_INDICES
Definition: ember_gl.h:8
constexpr int MAX_QUADS
Definition: ember_gl.h:6
constexpr int MAX_TEXTURE_SLOTS
sampler2D (array) supported by platform DESKTOP - 32 WEBGL - 16 ANDROID - 32 IOS - 32
Definition: ember_gl.h:18
constexpr int MAX_VERTICES
Definition: ember_gl.h:7
Font struct.
Definition: engine_structs.h:137
Float rectangle struct.
Definition: engine_structs.h:33
Texture base class.
Definition: engine_structs.h:14
UberShader shader effects for rendering texts and sprites.
Definition: shader.h:8