commit c9839fb

uint  ·  2026-06-23 00:57:56 +0000 UTC
parent f0020b6
Implement win32 window resizing
2 files changed,  +55, -0
+4, -0
 1@@ -2,6 +2,7 @@
 2 #define MAUS_WIN_H
 3 
 4 #include <stdbool.h>
 5+#include <stdint.h>
 6 
 7 #include <windows.h>
 8 
 9@@ -11,6 +12,9 @@ typedef struct {
10 	HDC            hdc;
11 	HBITMAP        hbm;
12 	HDC            memdc;
13+	bool           resized;
14+	uint32_t       rw;
15+	uint32_t       rh;
16 } MausBackend;
17 
18 #endif /* MAUS_WIN_H */
+51, -0
 1@@ -246,6 +246,17 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wp, LPARAM lp)
 2 		return 0;
 3 	}
 4 
 5+	if (msg == WM_SIZE) {
 6+		Maus* mw = (Maus*)GetWindowLongPtr(hwnd, GWLP_USERDATA);
 7+		if (mw) {
 8+			MausBackend* be = &mw->backend;
 9+			be->resized = true;
10+			be->rw = LOWORD(lp);
11+			be->rh = HIWORD(lp);
12+		}
13+		return 0;
14+	}
15+
16 	return DefWindowProc(hwnd, msg, wp, lp);
17 }
18 
19@@ -363,6 +374,7 @@ Maus* maus_init(const char* title, int x, int y, int width, int height)
20 
21 bool maus_event_poll(Maus* mw, MausEvent* ev)
22 {
23+	MausBackend* be = &mw->backend;
24 	MSG msg;
25 	while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
26 		TranslateMessage(&msg);
27@@ -373,11 +385,21 @@ bool maus_event_poll(Maus* mw, MausEvent* ev)
28 			return true;
29 	}
30 
31+	/* WM_SIZE doesnt is handled separately */
32+	if (be->resized) {
33+		be->resized = false;
34+		ev->type = MAUS_EV_RESIZE;
35+		ev->resize.width = be->rw;
36+		ev->resize.height = be->rh;
37+		return true;
38+	}
39+
40 	return false;
41 }
42 
43 void maus_event_wait(Maus* mw, MausEvent* ev)
44 {
45+	MausBackend* be = &mw->backend;
46 	MSG msg;
47 
48 	for (;;) {
49@@ -391,6 +413,15 @@ void maus_event_wait(Maus* mw, MausEvent* ev)
50 		DispatchMessage(&msg);
51 		ev->type = MAUS_EV_NONE;
52 
53+		/* WM_SIZE doesnt is handled separately */
54+                if (be->resized) {
55+			be->resized = false;
56+			ev->type = MAUS_EV_RESIZE;
57+			ev->resize.width = be->rw;
58+			ev->resize.height = be->rh;
59+			return;
60+		}
61+
62 		if (handle_event(&msg, ev, mw))
63 			return;
64 	}
65@@ -412,7 +443,27 @@ void maus_present(Maus* mw)
66 
67 bool maus_resize(Maus* mw, uint32_t width, uint32_t height)
68 {
69+	if (width == 0 || height == 0 ||
70+	    (mw->width == width && mw->height == height))
71+		return false;
72+
73+	fb_destroy(mw);
74+	if (mw->bfb) {
75+		free(mw->bfb);
76+		mw->bfb = NULL;
77+	}
78+
79+	mw->width = width;
80+	mw->height = height;
81+
82+	if (!fb_create(mw))
83+		return false;
84 
85+	mw->bfb = calloc(mw->stride * mw->height, sizeof(uint32_t));
86+	if (!mw->bfb)
87+		return false;
88+
89+	return true;
90 }
91 
92 void maus_cur_set_mode(Maus* mw, MausCursorState state)