commit 971a236

uint  ·  2026-08-04 14:09:08 +0000 UTC
parent 5e2d537
vendor libmaus as subproject, add and implement maus windowing backend option
11 files changed,  +309, -14
M TODO
+1, -0
1@@ -5,3 +5,4 @@ assets/resourcepacks/default
2 
3 subprojects/.wraplock
4 subprojects/libtls-bearssl/
5+subprojects/maus/
M TODO
+12, -5
 1@@ -1,13 +1,20 @@
 2 uint:
 3-[ ] Maus wayland
 4-[ ] Separate SDL from main.c
 5-
 6-[X] Wireframe
 7 [-] Face Hiding
 8 [-] Chunk meshing
 9 	[ ] Greedily
10-[ ] Render multiplayer
11+[-] Render multiplayer
12+	[ ] Players
13+[-] Multiplayer
14+	[ ] Death
15+	[ ] Physics
16+	[ ] Physics
17+[ ] UI
18+	[ ] Debug
19 [ ] Fonts
20 [ ] MGEvents abstraction
21 [ ] Check it works on Windows
22 
23+[X] Maus wayland
24+[X] Wireframe
25+[X] Separate SDL from main.c
26+
+87, -1
 1@@ -92,7 +92,93 @@ enum {
 2 	MGK_RGUI = SDLK_RGUI
 3 };
 4 
 5-#else /* libmaus */
 6+#elif MG_PLATFORM_MAUS
 7+#include <maus_input.h>
 8+typedef int MGKey;
 9+enum {
10+	MGK_UNKNOWN = MAUS_KEY_NONE,
11+
12+	MGK_A = MAUS_KEY_A, MGK_B = MAUS_KEY_B, MGK_C = MAUS_KEY_C,
13+	MGK_D = MAUS_KEY_D, MGK_E = MAUS_KEY_E, MGK_F = MAUS_KEY_F,
14+	MGK_G = MAUS_KEY_G, MGK_H = MAUS_KEY_H, MGK_I = MAUS_KEY_I,
15+	MGK_J = MAUS_KEY_J, MGK_K = MAUS_KEY_K, MGK_L = MAUS_KEY_L,
16+	MGK_M = MAUS_KEY_M, MGK_N = MAUS_KEY_N, MGK_O = MAUS_KEY_O,
17+	MGK_P = MAUS_KEY_P, MGK_Q = MAUS_KEY_Q, MGK_R = MAUS_KEY_R,
18+	MGK_S = MAUS_KEY_S, MGK_T = MAUS_KEY_T, MGK_U = MAUS_KEY_U,
19+	MGK_V = MAUS_KEY_V, MGK_W = MAUS_KEY_W, MGK_X = MAUS_KEY_X,
20+	MGK_Y = MAUS_KEY_Y, MGK_Z = MAUS_KEY_Z,
21+
22+	MGK_0 = MAUS_KEY_0, MGK_1 = MAUS_KEY_1, MGK_2 = MAUS_KEY_2,
23+	MGK_3 = MAUS_KEY_3, MGK_4 = MAUS_KEY_4, MGK_5 = MAUS_KEY_5,
24+	MGK_6 = MAUS_KEY_6, MGK_7 = MAUS_KEY_7, MGK_8 = MAUS_KEY_8,
25+	MGK_9 = MAUS_KEY_9,
26+
27+	MGK_RETURN = MAUS_KEY_ENTER,
28+	MGK_ESCAPE = MAUS_KEY_ESCAPE,
29+	MGK_BACKSPACE = MAUS_KEY_BACKSPACE,
30+	MGK_TAB = MAUS_KEY_TAB,
31+	MGK_SPACE = MAUS_KEY_SPACE,
32+
33+	MGK_MINUS = MAUS_KEY_MINUS,
34+	MGK_EQUALS = MAUS_KEY_EQUAL,
35+	MGK_LEFTBRACKET = MAUS_KEY_LEFT_BRACKET,
36+	MGK_RIGHTBRACKET = MAUS_KEY_RIGHT_BRACKET,
37+	MGK_BACKSLASH = MAUS_KEY_BACKSLASH,
38+	MGK_SEMICOLON = MAUS_KEY_SEMICOLON,
39+	MGK_APOSTROPHE = MAUS_KEY_APOSTROPHE,
40+	MGK_GRAVE = MAUS_KEY_GRAVE,
41+	MGK_COMMA = MAUS_KEY_COMMA,
42+	MGK_PERIOD = MAUS_KEY_PERIOD,
43+	MGK_SLASH = MAUS_KEY_SLASH,
44+
45+	MGK_CAPSLOCK = MAUS_KEY_CAPS_LOCK,
46+
47+	MGK_F1 = MAUS_KEY_F1, MGK_F2 = MAUS_KEY_F2, MGK_F3 = MAUS_KEY_F3,
48+	MGK_F4 = MAUS_KEY_F4, MGK_F5 = MAUS_KEY_F5, MGK_F6 = MAUS_KEY_F6,
49+	MGK_F7 = MAUS_KEY_F7, MGK_F8 = MAUS_KEY_F8, MGK_F9 = MAUS_KEY_F9,
50+	MGK_F10 = MAUS_KEY_F10, MGK_F11 = MAUS_KEY_F11, MGK_F12 = MAUS_KEY_F12,
51+
52+	MGK_PRINTSCREEN = MAUS_KEY_PRINT_SCREEN,
53+	MGK_SCROLLLOCK = MAUS_KEY_SCROLL_LOCK,
54+	MGK_PAUSE = MAUS_KEY_PAUSE,
55+	MGK_INSERT = MAUS_KEY_INSERT,
56+	MGK_HOME = MAUS_KEY_HOME,
57+	MGK_PAGEUP = MAUS_KEY_PAGE_UP,
58+	MGK_DELETE = MAUS_KEY_DELETE,
59+	MGK_END = MAUS_KEY_END,
60+	MGK_PAGEDOWN = MAUS_KEY_PAGE_DOWN,
61+	MGK_RIGHT = MAUS_KEY_RIGHT,
62+	MGK_LEFT = MAUS_KEY_LEFT,
63+	MGK_DOWN = MAUS_KEY_DOWN,
64+	MGK_UP = MAUS_KEY_UP,
65+
66+	MGK_NUMLOCKCLEAR = MAUS_KEY_NUM_LOCK,
67+	MGK_KP_DIVIDE = MAUS_KEY_KP_DIVIDE,
68+	MGK_KP_MULTIPLY = MAUS_KEY_KP_MULTIPLY,
69+	MGK_KP_MINUS = MAUS_KEY_KP_SUBTRACT,
70+	MGK_KP_PLUS = MAUS_KEY_KP_ADD,
71+	MGK_KP_ENTER = MAUS_KEY_KP_ENTER,
72+	MGK_KP_0 = MAUS_KEY_KP_0,
73+	MGK_KP_1 = MAUS_KEY_KP_1,
74+	MGK_KP_2 = MAUS_KEY_KP_2,
75+	MGK_KP_3 = MAUS_KEY_KP_3,
76+	MGK_KP_4 = MAUS_KEY_KP_4,
77+	MGK_KP_5 = MAUS_KEY_KP_5,
78+	MGK_KP_6 = MAUS_KEY_KP_6,
79+	MGK_KP_7 = MAUS_KEY_KP_7,
80+	MGK_KP_8 = MAUS_KEY_KP_8,
81+	MGK_KP_9 = MAUS_KEY_KP_9,
82+	MGK_KP_PERIOD = MAUS_KEY_KP_DECIMAL,
83+
84+	MGK_LCTRL = MAUS_KEY_CONTROL_L,
85+	MGK_LSHIFT = MAUS_KEY_SHIFT_L,
86+	MGK_LALT = MAUS_KEY_ALT_L,
87+	MGK_LGUI = MAUS_KEY_SUPER_L,
88+	MGK_RCTRL = MAUS_KEY_CONTROL_R,
89+	MGK_RSHIFT = MAUS_KEY_SHIFT_R,
90+	MGK_RALT = MAUS_KEY_ALT_R,
91+	MGK_RGUI = MAUS_KEY_SUPER_R
92+};
93 #endif
94 /* clang-format on */
95 
+1, -1
1@@ -8,7 +8,7 @@
2 #ifdef MG_PLATFORM_SDL
3 	#include "platform/platform_sdl.h"
4 #elif MG_PLATFORM_MAUS
5-	/* TODO */
6+	#include "platform/platform_maus.h"
7 #endif
8 /* clang-format on */
9 
+12, -0
 1@@ -0,0 +1,12 @@
 2+#ifndef PLATFORM_MAUS_H
 3+#define PLATFORM_MAUS_H
 4+
 5+#include <maus.h>
 6+
 7+typedef Maus MGWindow;
 8+
 9+typedef struct {
10+	void* uhh;
11+} MGVidBE;
12+
13+#endif /* PLATFORM_MAUS_H */
+2, -0
1@@ -1,6 +1,8 @@
2 #ifndef TEST_SCENE_H
3 #define TEST_SCENE_H
4 
5+#include <string.h>
6+
7 #include "mgmath.h"
8 #include "util.h"
9 #include "mcproto/mcproto.h"
+2, -0
1@@ -14,6 +14,8 @@ typedef enum { /* TODO: rename to MG_EC_... */
2 	MG_ERR_SDL_INIT,
3 	MG_ERR_SDL_CREATE_WIN,
4 
5+	MG_ERR_MAUS_CREATE_WIN,
6+
7 	MG_ERR_GL_CTX_CREATE,
8 	MG_ERR_GL_CTX_ACTIVATE,
9 
+13, -3
 1@@ -10,7 +10,6 @@ project(
 2 
 3 cc = meson.get_compiler('c')
 4 math_dep = cc.find_library('m', required: false)
 5-sdl_dep = dependency('sdl2')
 6 zlib_dep = dependency('zlib')
 7 libtls_sp = subproject('libtls-bearssl', default_options: [ 'default_library=static' ])
 8 tls_dep = libtls_sp.get_variable('libtls_dep')
 9@@ -19,6 +18,7 @@ bearssl_dep = cc.find_library('bearssl')
10 
11 renderer_backend = get_option('renderer')
12 window_backend = get_option('windowing')
13+sdl_dep = dependency('sdl2', required: window_backend == 'sdl' or renderer_backend == 'sokol')
14 
15 sources = [
16 	'source/main.c',
17@@ -72,9 +72,14 @@ if window_backend == 'sdl'
18 	platform_c_args += [ '-DMG_PLATFORM_SDL' ]
19 	platform_dep = declare_dependency(dependencies: sdl_dep)
20 elif window_backend == 'maus'
21+	if renderer_backend != 'small3dlib'
22+		error('maus windowing requires -Drenderer=small3dlib')
23+	endif
24+	maus_sp = subproject('maus', default_options: [ 'default_library=static' ])
25+	maus_dep = maus_sp.get_variable('maus_dep')
26 	sources += [ 'source/platform/platform_maus.c' ]
27 	platform_c_args += [ '-DMG_PLATFORM_MAUS' ]
28-	platform_dep = declare_dependency(dependencies: sdl_dep)
29+	platform_dep = maus_dep
30 endif
31 
32 if renderer_backend == 'sokol'
33@@ -99,9 +104,14 @@ if renderer_backend == 'sokol'
34 	renderer_sources += [ 'source/renderer/renderer_sokol.c' ]
35 	renderer_c_args += [ '-DMG_RENDERER_SOKOL' ]
36 else
37+	if window_backend == 'maus'
38+		software_window_dep = platform_dep
39+	else
40+		software_window_dep = sdl_dep
41+	endif
42 	renderer_backend_dep = declare_dependency(
43 		include_directories: vendor_inc,
44-		dependencies: sdl_dep,
45+		dependencies: software_window_dep,
46 	)
47 	renderer_sources += [ 'source/renderer/renderer_small3dlib.c' ]
48 	renderer_c_args += [ '-DMG_RENDERER_SMALL3DLIB' ]
+125, -0
  1@@ -0,0 +1,125 @@
  2+#define _POSIX_C_SOURCE 199309L
  3+
  4+#include <stdlib.h>
  5+#include <time.h>
  6+
  7+#include "util.h"
  8+#include "platform/platform.h"
  9+
 10+static MGContext* platform_ctx;
 11+static i32        mouse_dx;
 12+static i32        mouse_dy;
 13+
 14+void platform_get_drawable_size(MGContext* ctx, i32* width, i32* height)
 15+{
 16+	*width = (i32)ctx->win->width;
 17+	*height = (i32)ctx->win->height;
 18+}
 19+
 20+void platform_get_mouse_state(i32* x, i32* y)
 21+{
 22+	*x = mouse_dx;
 23+	*y = mouse_dy;
 24+	mouse_dx = 0;
 25+	mouse_dy = 0;
 26+}
 27+
 28+u64 platform_get_ticks(void)
 29+{
 30+	return maus_get_time_ns() / 1000000;
 31+}
 32+
 33+void platform_init(MGContext* ctx)
 34+{
 35+	int i;
 36+
 37+	for (i = 0; i < MG_KEYS_DOWN_MAX; i++)
 38+		ctx->keys[i] = MGK_UNKNOWN;
 39+}
 40+
 41+i32 platform_key_down(MGContext* ctx, MGKey key)
 42+{
 43+	if (!ctx || !ctx->win || key == MGK_UNKNOWN)
 44+		return 0;
 45+	if (key < 0 || key >= MAUS_KEY_LAST)
 46+		return 0;
 47+
 48+	return ctx->win->key_syms[key] != 0;
 49+}
 50+
 51+i32 platform_poll(MGContext* ctx)
 52+{
 53+	MausEvent ev;
 54+
 55+	while (maus_event_poll(ctx->win, &ev)) {
 56+		switch (ev.type) {
 57+		case MAUS_EV_CLOSE:
 58+			return 0;
 59+		case MAUS_EV_MOUSE_MOTION:
 60+			mouse_dx += ev.mouse.motion.dx;
 61+			mouse_dy += ev.mouse.motion.dy;
 62+			break;
 63+		case MAUS_EV_RESIZE:
 64+			if (ev.resize.width == 0 || ev.resize.height == 0)
 65+				break;
 66+			if (ctx->win->width == ev.resize.width && ctx->win->height == ev.resize.height)
 67+				break;
 68+			ctx->width = (i32)ev.resize.width;
 69+			ctx->height = (i32)ev.resize.height;
 70+			if (!maus_resize(ctx->win, ev.resize.width, ev.resize.height))
 71+				mg_die(MG_ERR_REN_CREATE, "Failed to resize maus framebuffer");
 72+			break;
 73+		default:
 74+			break;
 75+		}
 76+	}
 77+
 78+	return 1;
 79+}
 80+
 81+void platform_present(MGContext* ctx)
 82+{
 83+	maus_present(ctx->win);
 84+}
 85+
 86+void platform_set_mouse_relative(i8 enabled)
 87+{
 88+	if (!platform_ctx || !platform_ctx->win)
 89+		return;
 90+
 91+	if (enabled) {
 92+		maus_cur_set_mode(platform_ctx->win, MAUS_CURSOR_STATE_HIDDEN);
 93+		maus_cur_set_mode(platform_ctx->win, MAUS_CURSOR_STATE_RELATIVE);
 94+	}
 95+	else {
 96+		maus_cur_set_mode(platform_ctx->win, MAUS_CURSOR_STATE_ABSOLUTE);
 97+		maus_cur_set_mode(platform_ctx->win, MAUS_CURSOR_STATE_VISIBLE);
 98+	}
 99+}
100+
101+void platform_shutdown(MGContext* ctx)
102+{
103+	if (!ctx || !ctx->win)
104+		return;
105+
106+	maus_close(ctx->win);
107+	free(ctx->win);
108+	ctx->win = NULL;
109+	platform_ctx = NULL;
110+}
111+
112+void platform_window_create(MGContext* ctx)
113+{
114+	platform_ctx = ctx;
115+	ctx->win = maus_init(ctx->title, ctx->x, ctx->y, ctx->width, ctx->height);
116+	if (!ctx->win)
117+		mg_die(MG_ERR_MAUS_CREATE_WIN, "Failed to initialise maus");
118+
119+	if (!maus_create_window(ctx->win))
120+		mg_die(MG_ERR_MAUS_CREATE_WIN, "Failed to create maus window");
121+}
122+
123+void platform_wait(u32 ms)
124+{
125+	maus_sleep(ms);
126+}
+49, -4
  1@@ -1,8 +1,11 @@
  2-#include <SDL2/SDL.h>
  3 #include <stdlib.h>
  4 #include <string.h>
  5 #include <stb_image_c89.h>
  6 
  7+#ifdef MG_PLATFORM_SDL
  8+#include <SDL2/SDL.h>
  9+#endif
 10+
 11 #include "util.h"
 12 #include "types.h"
 13 #include "platform/platform.h"
 14@@ -52,10 +55,12 @@ typedef struct {
 15 	i8         pass; /* basically a bool; in
 16 	            render pass or not */
 17 
 18+#ifdef MG_PLATFORM_SDL
 19 	SDL_Texture* screen_texture;
 20-	u16*         framebuffer;
 21-	i32          width;
 22-	i32          height;
 23+#endif
 24+	u16* framebuffer;
 25+	i32  width;
 26+	i32  height;
 27 
 28 	RendererMesh    meshes[MAX_MESHES];
 29 	RendererTexture textures[MAX_TEXTURES];
 30@@ -87,6 +92,7 @@ static S3L_Vec4 clip_to_screen(const ClipVertex* v);
 31 static u32      sample_texture(const RendererMesh* mesh, const RendererTexture* texture,
 32                                const S3L_PixelInfo* pixel);
 33 static u16      rgba_to_rgb565(u32 rgba);
 34+static u32      rgb565_to_argb(u16 rgb);
 35 static u32      pack_texture_pixel(u32 rgba);
 36 
 37 static inline void small3dlib_pixel(S3L_PixelInfo* pixel)
 38@@ -120,8 +126,13 @@ static void create_screen_texture(i32 width, i32 height)
 39 	u64  pixel_count;
 40 	u16* framebuffer;
 41 
 42+#ifdef MG_PLATFORM_SDL
 43 	if (width == ren.width && height == ren.height && ren.framebuffer && ren.screen_texture)
 44 		return;
 45+#else
 46+	if (width == ren.width && height == ren.height && ren.framebuffer)
 47+		return;
 48+#endif
 49 
 50 	if (width <= 0 || height <= 0)
 51 		return;
 52@@ -135,6 +146,7 @@ static void create_screen_texture(i32 width, i32 height)
 53 	if (!framebuffer)
 54 		mg_die(MG_ERR_REN_CREATE, "Failed to allocate small3dlib framebuffer");
 55 
 56+#ifdef MG_PLATFORM_SDL
 57 	if (ren.screen_texture)
 58 		SDL_DestroyTexture(ren.screen_texture);
 59 
 60@@ -144,6 +156,7 @@ static void create_screen_texture(i32 width, i32 height)
 61 	if (!ren.screen_texture)
 62 		mg_die(MG_ERR_REN_CREATE, "Failed to create small3dlib texture: %s",
 63 		       SDL_GetError());
 64+#endif
 65 
 66 	free(ren.framebuffer);
 67 	ren.framebuffer = framebuffer;
 68@@ -156,10 +169,12 @@ static void create_screen_texture(i32 width, i32 height)
 69 
 70 static void destroy_screen_texture(void)
 71 {
 72+#ifdef MG_PLATFORM_SDL
 73 	if (ren.screen_texture) {
 74 		SDL_DestroyTexture(ren.screen_texture);
 75 		ren.screen_texture = NULL;
 76 	}
 77+#endif
 78 
 79 	free(ren.framebuffer);
 80 	ren.framebuffer = NULL;
 81@@ -304,6 +319,19 @@ static u16 rgba_to_rgb565(u32 rgba)
 82 	return (u16)(((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3));
 83 }
 84 
 85+static u32 rgb565_to_argb(u16 rgb)
 86+{
 87+	u32 r = (rgb >> 11) & 31;
 88+	u32 g = (rgb >> 5) & 63;
 89+	u32 b = rgb & 31;
 90+
 91+	r = (r << 3) | (r >> 2);
 92+	g = (g << 2) | (g >> 4);
 93+	b = (b << 3) | (b >> 2);
 94+
 95+	return 0xff000000 | (r << 16) | (g << 8) | b;
 96+}
 97+
 98 static u32 pack_texture_pixel(u32 rgba) { return ((rgba >> 24) << 24) | rgba_to_rgb565(rgba); }
 99 
100 void renderer_backend_create(MGContext* ctx)
101@@ -352,13 +380,30 @@ void renderer_backend_begin(void)
102 
103 void renderer_backend_end(void)
104 {
105+#ifdef MG_PLATFORM_MAUS
106+	i32 x;
107+	i32 y;
108+	u32* dst;
109+#endif
110+
111 	if (!ren.pass)
112 		return;
113 
114+#ifdef MG_PLATFORM_SDL
115 	SDL_UpdateTexture(ren.screen_texture, NULL, ren.framebuffer,
116 	                  ren.width * (i32)sizeof(*ren.framebuffer));
117 	SDL_RenderClear(ren.ctx->vbe.renderer);
118 	SDL_RenderCopy(ren.ctx->vbe.renderer, ren.screen_texture, NULL, NULL);
119+#else
120+	dst = ren.ctx->win->bfb;
121+	for (y = 0; (u32)y < ren.ctx->win->height; y++) {
122+		i32 sy = y * ren.height / ren.ctx->win->height;
123+		for (x = 0; (u32)x < ren.ctx->win->width; x++) {
124+			i32 sx = x * ren.width / ren.ctx->win->width;
125+			dst[(u32)y * ren.ctx->win->stride + (u32)x] = rgb565_to_argb(ren.framebuffer[sy * ren.width + sx]);
126+		}
127+	}
128+#endif
129 	ren.pass = 0;
130 }
131 
+5, -0
1@@ -0,0 +1,5 @@
2+[wrap-git]
3+# url = https://github.com/uint23/maus
4+url = https://srcdump.net/uint/maus.git
5+revision = master
6+depth = 1