1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
|
#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(320*5, 240*5);
mkfw_set_swapinterval(0);
mkfw_set_window_min_size_and_aspect(320*5, 240*5, 320, 240);
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);
framebuffer_callback(SCREEN_WIDTH*3, SCREEN_HEIGHT*3);
#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();
// #ifndef PERF_TEST
apply_phosphor_decay();
// #endif
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
}
shutdown_callback();
#ifdef PROFILER
overlay_shutdown();
#endif
audio_shutdown();
mkfw_cleanup();
return 0;
}
|