From d5486a5af100fb37fac08b60d862ac14943853ce Mon Sep 17 00:00:00 2001 From: Peter Fors Date: Sat, 29 Mar 2025 20:11:56 +0100 Subject: add base code for windowing and opengl crt-shader. --- base/callbacks.c | 72 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 base/callbacks.c (limited to 'base/callbacks.c') diff --git a/base/callbacks.c b/base/callbacks.c new file mode 100644 index 0000000..c5397e9 --- /dev/null +++ b/base/callbacks.c @@ -0,0 +1,72 @@ + + +/* [=]===^=[ framebuffer_callback ]=================================================================^===[=] */ +static void framebuffer_callback(int32_t width, int32_t height) { + state.screen_width = width; + state.screen_height = height; + state.viewport.x = 0; + state.viewport.y = 0; + state.viewport.w = width; + state.viewport.h = height; + + float current_aspect = (float)width / (float)height; + + if(current_aspect > aspect_ratio) { // Window is wider than the desired aspect ratio + float new_width = height * aspect_ratio; // Compute new width based on the height and the desired aspect ratio + state.viewport.x = (width - new_width) / 2; + state.viewport.w = new_width; + } else if(current_aspect < aspect_ratio) { // Window is taller than the desired aspect ratio + float new_height = width / aspect_ratio; // Compute new height based on the width and the desired aspect ratio + state.viewport.y = (height - new_height) / 2; + state.viewport.h = new_height; + } +} + +/* [=]===^=[ key_callback ]=================================================================^===[=] */ +static void key_callback(uint32_t key, uint32_t action, uint32_t mods) { + + if(key == MKS_KEY_ESCAPE) { + if(action == MKS_PRESSED) { + mkfw_set_should_close(true); + } + } + + if(action == MKS_RELEASED) { + switch(key) { + // Handle shader CRT emulation toggle + case MKS_KEY_F12: { + state.toggle_crt_emulation = !state.toggle_crt_emulation; + } break; + + // Handle fullscreen toggle + case MKS_KEY_F11: { + if(!keyboard_state[MKS_KEY_SHIFT]) { + if(state.fullscreen) { + mkfw_fullscreen(false); + state.fullscreen = false; + } else { + mkfw_fullscreen(true); + state.fullscreen = true; + } + } else if(keyboard_state[MKS_KEY_SHIFT]) { +#ifdef PROFILER + state.overlay = !state.overlay; +#endif + } + } break; + + default: break; + } + } +} + +/* [=]===^=[ mouse_move_callback ]=================================================================^===[=] */ +static void mouse_move_callback(int32_t x, int32_t y) { + state.mouse_dx += x; + state.mouse_dy += y; +} + +/* [=]===^=[ mouse_button_callback ]=================================================================^===[=] */ +static void mouse_button_callback(uint8_t button, int action) { + // printf("mouse_button\n"); +} -- cgit v1.2.3