vinci_run -> vinci_hidle now only implements 1 tick non blocking event handling

This commit is contained in:
Paolo Marrone 2025-03-19 19:35:51 +01:00
parent 8d3d429101
commit 96b15113a5
2 changed files with 36 additions and 51 deletions

View File

@ -37,13 +37,12 @@ struct window {
}; };
struct vinci { struct vinci {
char keep_running;
window *windows; window *windows;
char className[20]; char className[66];
}; };
static uint32_t get_mouse_state(WPARAM wParam) { static uint32_t get_mouse_state(WPARAM wParam) {
return (wParam & (MK_LBUTTON | MK_RBUTTON)) | ((wParam & MK_MBUTTON) >> 2); return (wParam & (MK_LBUTTON | MK_RBUTTON)) | ((wParam & MK_MBUTTON) >> 2);
} }
static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
@ -52,25 +51,25 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPara
return DefWindowProc(hwnd, msg, wParam, lParam); return DefWindowProc(hwnd, msg, wParam, lParam);
POINTS points; POINTS points;
switch(msg) switch(msg)
{ {
case WM_LBUTTONDOWN: case WM_LBUTTONDOWN:
case WM_MBUTTONDOWN: case WM_MBUTTONDOWN:
case WM_RBUTTONDOWN: case WM_RBUTTONDOWN:
if (w->cbs.on_mouse_press) { if (w->cbs.on_mouse_press) {
points = MAKEPOINTS(lParam); points = MAKEPOINTS(lParam);
SetCapture(hwnd); SetCapture(hwnd);
w->cbs.on_mouse_press(w, points.x, points.y, get_mouse_state(wParam)); w->cbs.on_mouse_press(w, points.x, points.y, get_mouse_state(wParam));
} }
break; break;
case WM_LBUTTONUP: case WM_LBUTTONUP:
case WM_MBUTTONUP: case WM_MBUTTONUP:
case WM_RBUTTONUP: case WM_RBUTTONUP:
if (w->cbs.on_mouse_release) { if (w->cbs.on_mouse_release) {
points = MAKEPOINTS(lParam); points = MAKEPOINTS(lParam);
if (get_mouse_state(wParam) == 0) { if (get_mouse_state(wParam) == 0) {
ReleaseCapture(); ReleaseCapture();
} }
w->cbs.on_mouse_release(w, points.x, points.y, get_mouse_state(wParam)); w->cbs.on_mouse_release(w, points.x, points.y, get_mouse_state(wParam));
} }
break; break;
@ -78,16 +77,16 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPara
points = MAKEPOINTS(lParam); points = MAKEPOINTS(lParam);
if (!w->mouse_tracking) { if (!w->mouse_tracking) {
if (w->cbs.on_mouse_enter) if (w->cbs.on_mouse_enter)
w->cbs.on_mouse_enter(w, points.x, points.y, 1); w->cbs.on_mouse_enter(w, points.x, points.y, 1);
TRACKMOUSEEVENT tme; TRACKMOUSEEVENT tme;
tme.cbSize = sizeof(tme); tme.cbSize = sizeof(tme);
tme.hwndTrack = hwnd; tme.hwndTrack = hwnd;
tme.dwFlags = TME_HOVER | TME_LEAVE; tme.dwFlags = TME_HOVER | TME_LEAVE;
tme.dwHoverTime = HOVER_DEFAULT; tme.dwHoverTime = HOVER_DEFAULT;
TrackMouseEvent(&tme); TrackMouseEvent(&tme);
w->mouse_tracking = 1; w->mouse_tracking = 1;
} }
if (w->cbs.on_mouse_move) if (w->cbs.on_mouse_move)
w->cbs.on_mouse_move(w, points.x, points.y, get_mouse_state(wParam)); w->cbs.on_mouse_move(w, points.x, points.y, get_mouse_state(wParam));
break; break;
case WM_MOUSELEAVE: case WM_MOUSELEAVE:
@ -138,15 +137,15 @@ static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lPara
w->cbs.on_window_close(w); w->cbs.on_window_close(w);
} }
break; break;
} }
return DefWindowProc(hwnd, msg, wParam, lParam); return DefWindowProc(hwnd, msg, wParam, lParam);
} }
vinci* vinci_new(void) { vinci* vinci_new(void) {
vinci *g = (vinci*)malloc(sizeof(vinci)); vinci *g = (vinci*)malloc(sizeof(vinci));
if (!g) if (!g)
return NULL; return NULL;
snprintf(g->className, 20, "vinClass%p", (void*)g); // Nice random name snprintf(g->className, 66, "vc%p", (void*)g); // Nice random name
WNDCLASSEX wc; WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX); wc.cbSize = sizeof(WNDCLASSEX);
@ -165,7 +164,6 @@ vinci* vinci_new(void) {
free(g); free(g);
return NULL; return NULL;
} }
g->keep_running = 0;
g->windows = NULL; g->windows = NULL;
return g; return g;
} }
@ -175,32 +173,20 @@ void vinci_destroy(vinci *g) {
free(g); free(g);
} }
void vinci_run(vinci *g, char single) { void vinci_idle(vinci *g) {
g->keep_running = 1; (void) g;
MSG msg; MSG msg;
while (g->keep_running) { while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) {
if (single) {
BOOL b = PeekMessage(&msg, NULL, 0, 0, PM_REMOVE);
if (!b)
break;
} else {
BOOL b = GetMessage(&msg, NULL, 0, 0);
if (b <= 0)
break;
}
TranslateMessage(&msg); TranslateMessage(&msg);
DispatchMessage(&msg); DispatchMessage(&msg);
} }
} }
void vinci_stop(vinci *g) {
g->keep_running = 0;
}
window* window_new(vinci *g, void* parent, uint32_t width, uint32_t height, window_cbs *cbs) { window* window_new(vinci *g, void* parent, uint32_t width, uint32_t height, window_cbs *cbs) {
window *w = (window*)malloc(sizeof(window)); window *w = (window*)malloc(sizeof(window));
if (w == NULL) if (w == NULL)
return NULL; return NULL;
memset((void*) w, 0, sizeof(window));
w->g = g; w->g = g;
w->handle = CreateWindowEx(0, g->className, NULL, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, width, height, NULL, NULL, NULL, NULL); w->handle = CreateWindowEx(0, g->className, NULL, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, width, height, NULL, NULL, NULL, NULL);
@ -226,7 +212,7 @@ window* window_new(vinci *g, void* parent, uint32_t width, uint32_t height, wind
} }
if (parent) { if (parent) {
SetParent(w->handle, *((HWND *)parent)); SetParent(w->handle, (HWND)parent);
SetWindowLong(w->handle, GWL_STYLE, GetWindowLong(w->handle, GWL_STYLE) & ~(WS_BORDER | WS_SIZEBOX | WS_DLGFRAME)); SetWindowLong(w->handle, GWL_STYLE, GetWindowLong(w->handle, GWL_STYLE) & ~(WS_BORDER | WS_SIZEBOX | WS_DLGFRAME));
SetWindowPos(w->handle, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); // SWP_FRAMECHANGED triggers WndProc call with WM_SIZE right here. May they die hard SetWindowPos(w->handle, HWND_TOP, 0, 0, 0, 0, SWP_NOSIZE | SWP_NOZORDER | SWP_FRAMECHANGED); // SWP_FRAMECHANGED triggers WndProc call with WM_SIZE right here. May they die hard
} }

View File

@ -44,8 +44,7 @@ typedef struct window_cbs {
vinci* vinci_new (void); vinci* vinci_new (void);
void vinci_destroy (vinci *g); void vinci_destroy (vinci *g);
void vinci_run (vinci *g, char single); void vinci_idle (vinci *g);
void vinci_stop (vinci *g);
window* window_new (vinci *g, void* p, uint32_t width, uint32_t height, window_cbs *cbs); window* window_new (vinci *g, void* p, uint32_t width, uint32_t height, window_cbs *cbs);
void window_free (window *w); void window_free (window *w);
void window_draw (window *w, unsigned char *data, int32_t dx, int32_t dy, int32_t dw, int32_t dh, int32_t wx, int32_t wy, int32_t width, int32_t height); void window_draw (window *w, unsigned char *data, int32_t dx, int32_t dy, int32_t dw, int32_t dh, int32_t wx, int32_t wy, int32_t width, int32_t height);