summaryrefslogtreecommitdiff
path: root/base/base.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/base.c
parentee4f15400998ca704c6ad8fc537f0d924930fabd (diff)
add base code for windowing and opengl crt-shader.
Diffstat (limited to 'base/base.c')
-rw-r--r--base/base.c122
1 files changed, 122 insertions, 0 deletions
diff --git a/base/base.c b/base/base.c
new file mode 100644
index 0000000..4c6fbe3
--- /dev/null
+++ b/base/base.c
@@ -0,0 +1,122 @@
+#define _GNU_SOURCE
+#include <stddef.h>
+#include <stdint.h>
+#include <stdbool.h>
+#include <stdlib.h>
+#include <string.h>
+#include <math.h>
+#include <time.h>
+#include <inttypes.h>
+#include <immintrin.h>
+#include <unistd.h>
+
+#ifdef _WIN32
+ #define WIN32_LEAN_AND_MEAN
+ #define NOMINMAX
+ #undef NOCRYPT
+ #include <windows.h>
+ #include <mmsystem.h>
+#elif defined(__linux__)
+ #include <dlfcn.h>
+ // #include <sched.h>
+ // #include <unistd.h>
+#endif
+
+#include "settings.h"
+#ifdef PROFILER
+#define STB_SPRINTF_IMPLEMENTATION
+#define STB_SPRINTF_NOFLOAT
+#define STB_SPRINTF_STATIC
+#include "stb_sprintf.h"
+#define DEBUG_PRINT(format, ...) do { \
+ char buf[512]; \
+ int len = stbsp_snprintf(buf, sizeof(buf), format, ##__VA_ARGS__); \
+ write(STDOUT_FILENO, buf, len); \
+} while(0)
+#else
+#define DEBUG_PRINT(...)
+#endif
+
+#include "opengl_loader.c"
+
+#include "incbin.h"
+#include "ugg.h"
+#include "state.c"
+#include "common.h"
+
+
+#include "opengl.c"
+#include "render.c"
+#include <mkfw/mkfw.c>
+
+#ifdef PROFILER
+#include "overlay.c"
+#endif
+#include "audio.c"
+#include "callbacks.c"
+
+/* [=]===^=[ main ]=================================================================^===[=] */
+int main(int argc, char **argv) {
+ state.toggle_crt_emulation = true;
+ mkfw_init(SCREEN_WIDTH*3, SCREEN_HEIGHT*3);
+ mkfw_set_swapinterval(0);
+ mkfw_set_window_min_size_and_aspect(SCREEN_WIDTH*3, SCREEN_HEIGHT*3, 4, 3);
+ mkfw_set_key_callback(key_callback);
+ mkfw_set_mouse_move_delta_callback(mouse_move_callback);
+ mkfw_set_mouse_button_callback(mouse_button_callback);
+ mkfw_set_framebuffer_size_callback(framebuffer_callback);
+ opengl_setup(vertex_shader_start, fragment_shader_start);
+ change_resolution(SCREEN_WIDTH, SCREEN_HEIGHT);
+#ifdef PROFILER
+ overlay_init();
+#endif
+ init_callback();
+ audio_initialize();
+
+ set_decay(20);
+
+ bool running = true;
+ uint64_t next_update = mkfw_gettime() + FRAMETIME;
+ while(running && !mkfw_should_close()) {
+ mkfw_pump_messages();
+ if(key_pressed(MKS_KEY_ESCAPE)) { running = false; }
+
+#ifdef PROFILER
+ reset_profiling_data();
+#endif
+ render_callback();
+ apply_phosphor_decay();
+ update_keyboard_state();
+ update_modifier_state();
+ update_mouse_state();
+ state.frame_number++;
+
+#ifndef PERF_TEST
+ render_frame();
+#ifdef PROFILER
+ debug_render();
+#endif
+
+ uint64_t now = mkfw_gettime();
+ int64_t remaining = next_update - now;
+ if(remaining > 0) {
+ if(remaining > SLEEP_MARGIN_NS) {
+ mkfw_sleep(remaining - SLEEP_MARGIN_NS);
+ }
+ while(mkfw_gettime() < next_update) { /**/ }
+ } else {
+ next_update = now;
+ }
+ next_update += FRAMETIME;
+
+ mkfw_swap_buffers();
+#endif
+ }
+
+#ifdef PROFILER
+ overlay_shutdown();
+#endif
+ audio_shutdown();
+ mkfw_cleanup();
+ return 0;
+}