Golias Engine
A C++ 20 'game engine' built with SDL3 with wide platform support.
rendering_canvas.h
Go to the documentation of this file.
1 #pragma once
2 
6 #include <spdlog/spdlog.h>
7 
8 
10 public:
11  explicit RenderingCanvas(RenderingDevice* device);
13 
14  bool initialize(int window_width, int window_height);
15  void shutdown();
16 
17  void begin(const Color& color = Color::BLACK);
18  void end();
19 
20  void set_camera(const glm::mat4& view_projection);
21  void reset_camera();
22 
23  void set_viewport_size(int width, int height);
24  void set_scale_mode(ScaleMode mode);
25 
26 
27  void draw_rect(float x, float y, float width, float height, const Color& color = Color::WHITE, float rotation = 0.0f);
28 
29  void draw_rect_outlined(float x, float y, float width, float height, const Color& color = Color::WHITE, float thickness = 1.0f);
30 
31  void draw_circle(float x, float y, float radius, const Color& color = Color::WHITE, int segments = 32);
32 
33  void draw_circle_outlined(float x, float y, float radius, const Color& color = Color::WHITE, float thickness = 1.0f, int segments = 32);
34 
35  void draw_arc(float x, float y, float radius, float start_angle, float end_angle, const Color& color = Color::WHITE, int segments = 32);
36 
37  void draw_line(float x1, float y1, float x2, float y2, const Color& color = Color::WHITE, float thickness = 1.0f);
38 
39  void draw_triangle(float x1, float y1, float x2, float y2, float x3, float y3, const Color& color = Color::WHITE);
40 
41  void draw_polygon(const Vector<glm::vec2>& points, const Color& color = Color::WHITE);
42 
43  void draw_texture(RID texture, float x, float y, float width = 0, float height = 0, const Color& tint = Color::WHITE,
44  float rotation = 0.0f);
45 
47  void draw_custom(RID shader, float x, float y, float width, float height, RID texture = INVALID_RID, const Color& color = Color::WHITE);
48 
49  void draw_texture_ex(float x, float y, float width, float height, RID texture = INVALID_RID, const Rect& source = {0, 0, 0, 0},
50  const Color& color = Color::WHITE, float rotation = 0.0f, bool flip_h = false, bool flip_v = false,
51  const CanvasMaterial* material = nullptr);
52 
53  template <typename... Args>
54  void draw_text(Font font, float x, float y, const Color& color, const char* format_str, Args&&... args);
55 
56  void draw_text(Font font, float x, float y, const Color& color, const String& text);
57 
58  void draw_text(float x, float y, const Color& color, const String& text);
59 
60  Font load_font_from_file(const char* filepath, int size);
61 
62  RID load_texture(const char* filepath);
63  RID load_texture(const char* filepath, const TextureDescription& desc);
64  Texture load_texture_from_file(const char* filepath);
65  Texture load_texture_from_file(const char* filepath, const TextureDescription& desc);
66  void get_texture_size(RID texture, uint32_t& out_width, uint32_t& out_height);
67 
68  RID load_texture_from_memory(void* data, int width, int height, int channels = 4);
69  void unload_texture(RID texture);
70 
71  RID load_shader_from_source(const char* vertex_src, const char* fragment_src);
72  RID load_shader_from_file(const char* filepath);
73  RID load_shader_from_source(const char* source);
74  void destroy_shader(RID shader);
75 
76  void set_blend_mode(BlendMode mode);
77  void set_line_width(float width);
78 
79  void present();
80 
82 
83 private:
84  struct Vertex {
85  glm::vec3 position;
86  glm::vec4 color;
87  glm::vec2 texcoord;
88  };
89 
90  struct DrawCommand {
91  RID texture;
92  uint32_t index_start;
93  uint32_t index_count;
94  bool use_texture;
95  };
96 
97 
98  RID shader;
99  RID pipeline;
100  RID vertex_buffer;
101  RID index_buffer;
102  RID white_texture;
103  RID default_sampler;
104 
105  std::unordered_map<RID, RID> texture_samplers;
106 
107  Vector<Vertex> vertices;
108  Vector<uint16_t> indices;
109  Vector<DrawCommand> draw_commands;
110 
111  glm::mat4 projection;
112  glm::mat4 view;
113  Vector<glm::mat4> transform_stack;
114  BlendMode current_blend_mode;
115  ScaleMode scale_mode;
116  float line_width;
117  Color clear_color;
118 
119  int window_width, window_height;
120  int viewport_width, viewport_height;
121  bool is_drawing;
122 
123  HashMap<RID, RID> custom_shader_pipelines;
124 
125  struct TextStorage {
126  Vector<RID> textures;
127  HashMap<String, Font> loaded_fonts;
128  Font default_font = {};
129  Font emoji_font = {};
130  } text_storage;
131 
132  RenderingDevice* rd;
133 
134  void flush();
135  void add_quad(const glm::mat4& transform, const Color& color, RID texture, bool use_texture);
136  glm::mat4 get_current_transform() const;
137  void calculate_viewport(int window_w, int window_h, int& out_x, int& out_y, int& out_w, int& out_h);
138  void setup_pipeline_for_blend_mode(BlendMode mode);
139  RID get_or_create_custom_pipeline(RID shader);
140 };
141 
142 
143 template <typename... Args>
144 void RenderingCanvas::draw_text(Font font, float x, float y, const Color& color, const char* format_str, Args&&... args) {
145  if (!font.get_native_handle()) {
146  spdlog::warn("DrawText called with null font!");
147  return;
148  }
149 
150  std::string formatted_text;
151  if constexpr (sizeof...(args) > 0) {
152  formatted_text = std::vformat(format_str, std::make_format_args(args...));
153  } else {
154  formatted_text = format_str;
155  }
156 
157  draw_text(font, x, y, color, formatted_text);
158 }
Definition: shader_material.h:8
Definition: rendering_canvas.h:9
void set_camera(const glm::mat4 &view_projection)
Definition: rendering_canvas.cpp:453
void set_blend_mode(BlendMode mode)
Definition: rendering_canvas.cpp:352
RID load_texture(const char *filepath)
Definition: rendering_canvas.cpp:534
void end()
Definition: rendering_canvas.cpp:159
void get_texture_size(RID texture, uint32_t &out_width, uint32_t &out_height)
Definition: rendering_canvas.cpp:318
~RenderingCanvas()
Definition: rendering_canvas.cpp:12
void draw_arc(float x, float y, float radius, float start_angle, float end_angle, const Color &color=Color::WHITE, int segments=32)
Definition: rendering_canvas.cpp:280
void draw_circle_outlined(float x, float y, float radius, const Color &color=Color::WHITE, float thickness=1.0f, int segments=32)
Definition: rendering_canvas.cpp:260
void set_scale_mode(ScaleMode mode)
Definition: rendering_canvas.cpp:468
RenderingDevice * get_rendering_device() const
Definition: rendering_canvas.cpp:362
void draw_custom(RID shader, float x, float y, float width, float height, RID texture=INVALID_RID, const Color &color=Color::WHITE)
Definition: rendering_canvas.cpp:778
void draw_text(Font font, float x, float y, const Color &color, const char *format_str, Args &&... args)
Definition: rendering_canvas.h:144
void draw_circle(float x, float y, float radius, const Color &color=Color::WHITE, int segments=32)
Definition: rendering_canvas.cpp:249
void draw_triangle(float x1, float y1, float x2, float y2, float x3, float y3, const Color &color=Color::WHITE)
Definition: rendering_canvas.cpp:383
RID load_shader_from_source(const char *vertex_src, const char *fragment_src)
Definition: rendering_canvas.cpp:702
Texture load_texture_from_file(const char *filepath)
Definition: rendering_canvas.cpp:603
void reset_camera()
Definition: rendering_canvas.cpp:458
void draw_rect_outlined(float x, float y, float width, float height, const Color &color=Color::WHITE, float thickness=1.0f)
Definition: rendering_canvas.cpp:238
void destroy_shader(RID shader)
Definition: rendering_canvas.cpp:741
void draw_texture(RID texture, float x, float y, float width=0, float height=0, const Color &tint=Color::WHITE, float rotation=0.0f)
Definition: rendering_canvas.cpp:410
void present()
Definition: rendering_canvas.cpp:358
RID load_texture_from_memory(void *data, int width, int height, int channels=4)
Definition: rendering_canvas.cpp:624
void draw_rect(float x, float y, float width, float height, const Color &color=Color::WHITE, float rotation=0.0f)
Definition: rendering_canvas.cpp:226
void unload_texture(RID texture)
Definition: rendering_canvas.cpp:635
Font load_font_from_file(const char *filepath, int size)
Definition: rendering_canvas.cpp:326
bool initialize(int window_width, int window_height)
Definition: rendering_canvas.cpp:16
void draw_line(float x1, float y1, float x2, float y2, const Color &color=Color::WHITE, float thickness=1.0f)
Definition: rendering_canvas.cpp:369
RID load_shader_from_file(const char *filepath)
Definition: rendering_canvas.cpp:706
void shutdown()
Definition: rendering_canvas.cpp:96
void begin(const Color &color=Color::BLACK)
Definition: rendering_canvas.cpp:139
void set_viewport_size(int width, int height)
Definition: rendering_canvas.cpp:463
void set_line_width(float width)
Definition: rendering_canvas.cpp:355
RenderingCanvas(RenderingDevice *device)
Definition: rendering_canvas.cpp:6
void draw_texture_ex(float x, float y, float width, float height, RID texture=INVALID_RID, const Rect &source={0, 0, 0, 0}, const Color &color=Color::WHITE, float rotation=0.0f, bool flip_h=false, bool flip_v=false, const CanvasMaterial *material=nullptr)
Definition: rendering_canvas.cpp:852
void draw_polygon(const Vector< glm::vec2 > &points, const Color &color=Color::WHITE)
Definition: rendering_canvas.cpp:387
Abstract base class for rendering devices.
Definition: rendering_device.h:326
Definition: str.h:8
Definition: vector.h:10
BlendMode
Definition: rendering_device.h:273
ScaleMode
Definition: rendering_device.h:275
constexpr RID INVALID_RID
Definition: rendering_device.h:13
uint32_t RID
Definition: rendering_device.h:12
Definition: rendering_device.h:55
static const Color WHITE
Definition: rendering_device.h:65
static const Color BLACK
Definition: rendering_device.h:66
Definition: rendering_device.h:20
TTF_Font * get_native_handle() const
Definition: rendering_device.h:26
Definition: rendering_device.h:76
Definition: rendering_device.h:281
Definition: rendering_device.h:301
std::unordered_map< K, V > HashMap
Definition: type_alias.h:20