commit 8f9fea8

uint  ·  2026-07-21 14:35:49 +0000 UTC
parent eeb9766
add initialisation, main.c: create window with gl context
3 files changed,  +89, -3
+15, -3
 1@@ -1,9 +1,21 @@
 2-#include <stdio.h>
 3 #include <stdlib.h>
 4 
 5+#include "platform/platform.h"
 6+#include "platform/util.h"
 7+
 8 int main(void)
 9 {
10-	puts("Hello Magnolia!");
11-	return EXIT_SUCCESS;
12+	MGContext ctx = {
13+		NULL,       /* window */
14+		{NULL},     /* video-be */
15+		"Magnolia", /* title */
16+		0, 0,       /* x, y */
17+		1024, 768,  /* w, h */
18+	};
19+
20+	mg_log(stdout, "creating window");
21+	platform_window_create(&ctx);
22+
23+	return MG_ERR_EXIT_SUCCESS;
24 }
25 
+33, -0
 1@@ -0,0 +1,33 @@
 2+#include <SDL2/SDL.h>
 3+#include <SDL2/SDL_video.h>
 4+
 5+#include "platform.h"
 6+#include "util.h"
 7+
 8+void platform_init(MGContext* ctx)
 9+{
10+	(void) ctx;
11+	SDL_Init(SDL_INIT_VIDEO);
12+
13+	/* TODO: OpenGL specifis */
14+	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
15+	SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
16+	SDL_GL_SetAttribute( SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
17+	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
18+	SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
19+}
20+
21+void platform_window_create(MGContext* ctx)
22+{
23+	ctx->win = (MGWindow*) SDL_CreateWindow(
24+		ctx->title, ctx->x, ctx->y, ctx->width, ctx->height,
25+		SDL_WINDOW_RESIZABLE|SDL_WINDOW_ALLOW_HIGHDPI
26+	);
27+
28+	ctx->vbe.gl = SDL_GL_CreateContext(ctx->win);
29+	if (!ctx->vbe.gl)
30+		mg_die(MG_ERR_GL_CTX_CREATE, "Failed to initialise GL Context");
31+	SDL_GL_MakeCurrent(ctx->win, ctx->vbe.gl);
32+	SDL_GL_SetSwapInterval(1);
33+}
34+
+41, -0
 1@@ -0,0 +1,41 @@
 2+#ifndef PLATFORM_H
 3+#define PLATFORM_H
 4+
 5+#include <stdint.h>
 6+
 7+#include <SDL2/SDL.h>
 8+
 9+typedef int8_t   i8;
10+typedef int16_t  i16;
11+typedef int32_t  i32;
12+typedef int64_t  i64;
13+typedef uint8_t  u8;
14+typedef uint16_t u16;
15+typedef uint32_t u32;
16+typedef uint64_t u64;
17+
18+typedef SDL_Window MGWindow;
19+
20+/* video backend !only OpenGL for now */
21+typedef struct {
22+	SDL_GLContext gl;
23+} MGVidBE;
24+
25+typedef struct {
26+	MGWindow*   win;
27+	MGVidBE     vbe;
28+	const char* title;
29+	i32         x;
30+	i32         y;
31+	i32         width;
32+	i32         height;
33+} MGContext;
34+
35+/* initialise resources */
36+void platform_init(MGContext* ctx);
37+
38+/* create window */
39+void platform_window_create(MGContext* ctx);
40+
41+#endif /* PLATFORM_H */
42+