commit fe9b3a2
uint
·
2026-07-22 15:00:34 +0000 UTC
parent 9c6f92b
keep renderer state private
4 files changed,
+32,
-30
+4,
-10
1@@ -3,24 +3,18 @@
2
3 #include <platform/platform.h>
4
5-typedef struct {
6- MGContext* ctx;
7- i8 pass; /* basically a bool; in
8- render pass or not */
9-} MGRenderer;
10-
11 /* create renderer with chosen context
12 (just GL for now) */
13-void renderer_create(MGContext* ctx, MGRenderer* ren);
14+void renderer_create(MGContext* ctx);
15
16 /* start drawing to screen */
17-void renderer_begin(MGRenderer* ren);
18+void renderer_begin(void);
19
20 /* finish drawing to screen */
21-void renderer_end(MGRenderer* ren);
22+void renderer_end(void);
23
24 /* destroy renderer resources */
25-void renderer_destroy(MGRenderer* ren);
26+void renderer_destroy(void);
27
28 #endif /* RENDERER_H */
29
+4,
-5
1@@ -13,7 +13,6 @@ int main(void)
2 0, 0, /* x, y */
3 1024, 768, /* w, h */
4 };
5- MGRenderer ren;
6
7 mg_log(LOG_INF, "test log info");
8 mg_log(LOG_ERR, "test log error");
9@@ -21,19 +20,19 @@ int main(void)
10
11 platform_init(&ctx);
12 platform_window_create(&ctx);
13- renderer_create(&ctx, &ren);
14+ renderer_create(&ctx);
15
16 while (platform_poll(&ctx)) {
17- renderer_begin(&ren);
18+ renderer_begin();
19
20 /* ... */
21
22- renderer_end(&ren);
23+ renderer_end();
24 platform_present(&ctx);
25 SDL_Delay(1); /* TODO: replace with target fps */
26 }
27
28- renderer_destroy(&ren);
29+ renderer_destroy();
30 platform_shutdown(&ctx);
31 return MG_ERR_EXIT_SUCCESS;
32 }
+2,
-2
1@@ -15,8 +15,8 @@ void platform_init(MGContext* ctx)
2 mg_die(MG_ERR_SDL_INIT, "SDL_Init failed: %s", SDL_GetError());
3
4 /* TODO: OpenGL specifis */
5- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
6- SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
7+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
8+ SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
9 SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
10 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
11 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
+22,
-13
1@@ -2,11 +2,20 @@
2
3 #include "util.h"
4 #include "types.h"
5+#include "platform/platform.h"
6 #include "renderer/renderer.h"
7
8+typedef struct {
9+ MGContext* ctx;
10+ i8 pass; /* basically a bool; in
11+ render pass or not */
12+} RendererState;
13+
14 /* logging function for passing to sokol */
15 static void sokol_log(const char* tag, u32 level, u32 item, const char* msg, u32 ln, const char* file, void* user);
16
17+static RendererState ren;
18+
19 static void sokol_log(const char* tag, u32 level, u32 item, const char* msg, u32 ln, const char* file, void* user)
20 {
21 LogType type;
22@@ -24,12 +33,12 @@ static void sokol_log(const char* tag, u32 level, u32 item, const char* msg, u32
23 );
24 }
25
26-void renderer_create(MGContext* ctx, MGRenderer* ren)
27+void renderer_create(MGContext* ctx)
28 {
29 sg_desc desc = {0};
30
31- ren->ctx = ctx;
32- ren->pass = 0;
33+ ren.ctx = ctx;
34+ ren.pass = 0;
35
36 desc.environment.defaults.color_format = SG_PIXELFORMAT_RGBA8;
37 desc.environment.defaults.depth_format = SG_PIXELFORMAT_DEPTH;
38@@ -42,15 +51,15 @@ void renderer_create(MGContext* ctx, MGRenderer* ren)
39 mg_die(MG_ERR_REN_CREATE, "Failed to initialise renderer");
40 }
41
42-void renderer_begin(MGRenderer* ren)
43+void renderer_begin(void)
44 {
45 sg_pass pass = {0};
46 i32 width;
47 i32 height;
48
49- ren->pass = 0;
50+ ren.pass = 0;
51
52- platform_get_drawable_size(ren->ctx, &width, &height);
53+ platform_get_drawable_size(ren.ctx, &width, &height);
54 /* (minimized window?) no need to draw */
55 if (width <= 0 || height <= 0)
56 return;
57@@ -74,25 +83,25 @@ void renderer_begin(MGRenderer* ren)
58 pass.swapchain.gl.framebuffer = 0;
59
60 sg_begin_pass(&pass);
61- ren->pass = 1;
62+ ren.pass = 1;
63 }
64
65-void renderer_end(MGRenderer* ren)
66+void renderer_end(void)
67 {
68- if (!ren->pass)
69+ if (!ren.pass)
70 return;
71
72 sg_end_pass();
73 sg_commit();
74
75- ren->pass = 0;
76+ ren.pass = 0;
77 }
78
79-void renderer_destroy(MGRenderer* ren)
80+void renderer_destroy(void)
81 {
82 if (sg_isvalid())
83 sg_shutdown();
84- ren->ctx = NULL;
85- ren->pass = 0;
86+ ren.ctx = NULL;
87+ ren.pass = 0;
88 }
89