summaryrefslogtreecommitdiff
path: root/base/callbacks.c
diff options
context:
space:
mode:
authorPeter Fors <peter.fors@mindkiller.com>2025-03-29 20:11:56 +0100
committerPeter Fors <peter.fors@mindkiller.com>2025-03-29 20:11:56 +0100
commitd5486a5af100fb37fac08b60d862ac14943853ce (patch)
treee2206f620745ba49c94f0f29d180587e03bc9fda /base/callbacks.c
parentee4f15400998ca704c6ad8fc537f0d924930fabd (diff)
add base code for windowing and opengl crt-shader.
Diffstat (limited to 'base/callbacks.c')
-rw-r--r--base/callbacks.c72
1 files changed, 72 insertions, 0 deletions
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");
+}