Ember
A C++ 20 'game engine' built with SDL3 with wide platform support.
ember_mtl.h
Go to the documentation of this file.
1 #pragma once
2 #include "core/ember_core.h"
3 // #include "core/renderer/shader.h"
4 
5 #if defined(__APPLE__)
11 class MetalRenderer final : public Renderer {
12 public:
13  MetalRenderer();
14  ~MetalRenderer() override;
15 
16  void initialize() override;
17 
18  void clear(glm::vec4 color) override;
19 
20  void present() override;
21 
22  void resize_viewport(int view_width, int view_height) override;
23 
24  void set_context(const void* ctx) override;
25 
26  void* get_context() override;
27 
28  void destroy() override;
29 
30  bool load_font(const std::string&, const std::string&, int) override;
31 
32 
33  std::shared_ptr<Texture> load_texture(const std::string& path) override;
34 
35  std::shared_ptr<Texture> get_texture(const std::string& path) override;
36 
37  void unload_font(const Font& font) override;
38 
39  void unload_texture(Uint32 id) override;
40 
41  void draw_texture(const Texture* texture, const Rect2& dest_rect, float rotation, const glm::vec4& color, const Rect2& src_rect,
42  int z_index, bool flip_h, bool flip_v, const UberShader& uber_shader) override;
43 
44  void draw_rect(Rect2 rect, float rotation, const glm::vec4& color, bool filled, int z_index) override;
45 
46  void draw_text(const std::string& text, float x, float y, float rotation, float scale, const glm::vec4& color,
47  const std::string& font_alias, int z_index, const UberShader& uber_shader, int ft_size) override;
48 
49  void draw_line(float x1, float y1, float x2, float y2, float width, float rotation, const glm::vec4& color, int z_index) override;
50 
51  void draw_triangle(float x1, float y1, float x2, float y2, float x3, float y3, float rotation, const glm::vec4& color, bool filled,
52  int z_index) override;
53 
54  void draw_circle(float cx, float cy, float rotation, float radius, const glm::vec4& color, bool filled, int segments,
55  int z_index) override;
56 
57  void draw_polygon(const std::vector<glm::vec2>& points, float rotation, const glm::vec4& color, bool filled, int z_index) override;
58 
59  void flush() override;
60 
61 protected:
62  void set_default_font(const std::string& font_name) override;
63  void render_fbo() override;
64  void set_effect_uniforms(const UberShader& uber_shader, const glm::vec2& texture_size) override;
65  glm::vec2 get_texture_size(Uint32 texture_id) const override;
66 
67 private:
68  void* device_ = nullptr; // id<MTLDevice>
69  void* queue_ = nullptr; // id<MTLCommandQueue>
70  void* pipeline_ = nullptr; // id<MTLRenderPipelineState>
71  void* metal_layer_ = nullptr; // CAMetalLayer*
72  void* sampler_ = nullptr;
73 
74  SDL_MetalView metal_view_ = nullptr; // SDL_MetalView
75  glm::vec4 clear_color_{0, 0, 0, 1};
76 };
77 #else
78 
79 class MetalRenderer final : public Renderer {
80 public:
82  }
83  ~MetalRenderer() override {
84  }
85 
86  void initialize() override {
87  }
88  void clear(glm::vec4) override {
89  }
90  void present() override {
91  }
92  void resize_viewport(int, int) override {
93  }
94  void set_context(const void*) override {
95  }
96  void* get_context() override {
97  return nullptr;
98  }
99  void destroy() override {
100  }
101  bool load_font(const std::string&, const std::string&, int) override {
102  return false;
103  }
104  std::shared_ptr<Texture> load_texture(const std::string&) override {
105  return nullptr;
106  }
107  std::shared_ptr<Texture> get_texture(const std::string&) override {
108  return nullptr;
109  }
110  void unload_font(const Font&) override {
111  }
112  void unload_texture(Uint32) override {
113  }
114  void draw_texture(const Texture* texture, const Rect2& dest_rect, float rotation, const glm::vec4& color, const Rect2& src_rect,
115  int z_index, bool flip_h, bool flip_v, const UberShader& uber_shader) override {
116  }
117 
118  void draw_rect(Rect2, float, const glm::vec4&, bool, int) override {
119  }
120  void draw_text(const std::string&, float, float, float, float, const glm::vec4&, const std::string&, int, const UberShader&,
121  int) override {
122  }
123  void draw_line(float, float, float, float, float, float, const glm::vec4&, int) override {
124  }
125  void draw_triangle(float, float, float, float, float, float, float, const glm::vec4&, bool, int) override {
126  }
127  void draw_circle(float, float, float, float, const glm::vec4&, bool, int, int) override {
128  }
129  void draw_polygon(const std::vector<glm::vec2>&, float, const glm::vec4&, bool, int) override {
130  }
131  void flush() override {
132  }
133 
134 protected:
135  void set_default_font(const std::string&) override {
136  }
137  void render_fbo() override {
138  }
139  void set_effect_uniforms(const UberShader&, const glm::vec2&) override {
140  }
141  glm::vec2 get_texture_size(Uint32) const override {
142  return {0, 0};
143  }
144 };
145 
146 #endif
Definition: ember_mtl.h:79
void * get_context() override
Get the platform-specific rendering context.
Definition: ember_mtl.h:96
std::shared_ptr< Texture > get_texture(const std::string &) override
Get a previously loaded texture.
Definition: ember_mtl.h:107
void set_default_font(const std::string &) override
Set the current font to use for rendering.
Definition: ember_mtl.h:135
void draw_text(const std::string &, float, float, float, float, const glm::vec4 &, const std::string &, int, const UberShader &, int) override
Draw text to screen.
Definition: ember_mtl.h:120
~MetalRenderer() override
Definition: ember_mtl.h:83
void unload_texture(Uint32) override
Unload a texture by its ID.
Definition: ember_mtl.h:112
void destroy() override
Destroy rendering resources.
Definition: ember_mtl.h:99
void set_effect_uniforms(const UberShader &, const glm::vec2 &) override
Definition: ember_mtl.h:139
void render_fbo() override
Render the current frame buffer.
Definition: ember_mtl.h:137
void draw_line(float, float, float, float, float, float, const glm::vec4 &, int) override
Draw a line.
Definition: ember_mtl.h:123
glm::vec2 get_texture_size(Uint32) const override
Definition: ember_mtl.h:141
void draw_triangle(float, float, float, float, float, float, float, const glm::vec4 &, bool, int) override
Draw a triangle.
Definition: ember_mtl.h:125
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_mtl.h:114
void resize_viewport(int, int) override
Resize the rendering context.
Definition: ember_mtl.h:92
void initialize() override
Initialize the renderer and its resources.
Definition: ember_mtl.h:86
MetalRenderer()
Definition: ember_mtl.h:81
void unload_font(const Font &) override
Unload a loaded font.
Definition: ember_mtl.h:110
void draw_rect(Rect2, float, const glm::vec4 &, bool, int) override
Draw a rectangle (filled or outlined).
Definition: ember_mtl.h:118
bool load_font(const std::string &, const std::string &, int) override
Load a font.
Definition: ember_mtl.h:101
void clear(glm::vec4) override
Clear the screen to the given color.
Definition: ember_mtl.h:88
std::shared_ptr< Texture > load_texture(const std::string &) override
Load a texture from disk.
Definition: ember_mtl.h:104
void flush() override
Submit all batched draw calls.
Definition: ember_mtl.h:131
void set_context(const void *) override
Set the platform-specific rendering context.
Definition: ember_mtl.h:94
void draw_polygon(const std::vector< glm::vec2 > &, float, const glm::vec4 &, bool, int) override
Draw a polygon.
Definition: ember_mtl.h:129
void present() override
Swap buffers to present the rendered frame.
Definition: ember_mtl.h:90
void draw_circle(float, float, float, float, const glm::vec4 &, bool, int, int) override
Draw a circle.
Definition: ember_mtl.h:127
Base class for all renderers.
Definition: ember_core.h:109
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