vtgl.h (9017B)
1 /* See LICENSE for copyright details */ 2 #ifndef _VTGL_H_ 3 #define _VTGL_H_ 4 5 #ifndef VERSION 6 #define VERSION "unknown" 7 #endif 8 9 #include <stddef.h> 10 #include <stdint.h> 11 12 #ifndef asm 13 #define asm __asm__ 14 #endif 15 16 #ifndef typeof 17 #define typeof __typeof__ 18 #endif 19 20 #ifndef static_assert 21 #define static_assert _Static_assert 22 #endif 23 24 #define function static 25 #define global static 26 #define local_persist static 27 28 #define PI 3.1415926535897932384f 29 30 #define KB(a) ((a) << 10ULL) 31 #define MB(a) ((a) << 20ULL) 32 33 #define countof(a) (iz)(sizeof(a) / sizeof(*a)) 34 #define ARRAY_COUNT(a) (iz)(sizeof(a) / sizeof(*a)) 35 #define ABS(a) ((a) < 0 ? (-a) : (a)) 36 #define BETWEEN(x, a, b) ((x) >= (a) && (x) <= (b)) 37 #define CLAMP(x, a, b) ((x) = (x) < (a) ? (a) : (x) > (b) ? (b) : (x)) 38 #define MAX(a, b) ((a) >= (b) ? (a) : (b)) 39 #define MIN(a, b) ((a) <= (b) ? (a) : (b)) 40 #define SGN(a) ((a) < 0 ? (-1) : (1)) 41 42 #define SAFE_RATIO_1(a, b) ((b) ? (a) / (b) : (a)) 43 44 #define ISCONTROLC0(c) ((c) <= 0x1F || (c) == 0x7F) 45 #define ISCONTROLC1(c) (BETWEEN((c), 0x80, 0x9F)) 46 #define ISCONTROL(c) (ISCONTROLC0(c) || ISCONTROLC1(c)) 47 #define ISSPACE(c) ((c) == ' ' || (c) == '\n' || (c) == '\t') 48 #define ISPRINT(c) BETWEEN((c), ' ', '~') 49 50 #define ISPOWEROFTWO(a) (((a) & ((a) - 1)) == 0) 51 52 /* NOTE: GLFW does not sequentially number keys so switch statement will never be optimized */ 53 #define ENCODE_KEY(action, mod, key) (((action) << 24) | ((mod) << 16) | ((key) & 0xFFFF)) 54 55 #define BACKLOG_SIZE (MB(16)) 56 #define BACKLOG_LINES (8192UL) 57 58 #define ALT_BACKLOG_SIZE (MB(2)) 59 #define ALT_BACKLOG_LINES (1024UL) 60 61 #define I32_MAX INT32_MAX 62 #define I64_MIN INT64_MIN 63 64 typedef float f32; 65 typedef double f64; 66 typedef char c8; 67 typedef int8_t i8; 68 typedef uint8_t u8; 69 typedef int16_t i16; 70 typedef uint16_t u16; 71 typedef int32_t i32; 72 typedef uint32_t u32; 73 typedef uint32_t b32; 74 typedef int64_t i64; 75 typedef uint64_t u64; 76 typedef ptrdiff_t iptr; 77 typedef ptrdiff_t iz; 78 typedef size_t uz; 79 80 #include "intrinsics.c" 81 82 #ifdef _DEBUG 83 #define ASSERT(c) do { if (!(c)) debugbreak(); } while(0) 84 #define DEBUG_EXPORT 85 #define DEBUG_DECL(a) a 86 #else 87 #define ASSERT(c) do { (void)(c); } while(0) 88 #define DEBUG_EXPORT function 89 #define DEBUG_DECL(a) 90 #endif 91 92 typedef struct { u8 *beg, *end; } Arena; 93 typedef struct { Arena *arena; u8 *old_beg; } TempArena; 94 95 typedef struct { iz len; u8 *data; } s8; 96 #define s8(s) (s8){.len = ARRAY_COUNT(s) - 1, .data = (u8 *)s} 97 98 typedef s8 os_mapped_file; 99 100 typedef struct { 101 u8 *data; 102 iz capacity; 103 iz count; 104 b32 errors; 105 } Stream; 106 107 typedef union { 108 struct { i32 x, y; }; 109 struct { i32 w, h; }; 110 i32 E[2]; 111 } iv2; 112 113 typedef union { 114 struct { f32 x, y; }; 115 struct { f32 w, h; }; 116 f32 E[2]; 117 } v2; 118 119 typedef struct { iv2 start, end; } Range; 120 #define INVALID_RANGE_END (iv2){.x = -1, .y = -1} 121 122 #include "os.h" 123 124 /* TODO: for now these are just based on the GLFW keycodes directly. It might 125 * be better for the platform to define these values themselves */ 126 typedef enum { 127 BUTTON_RELEASE, 128 BUTTON_PRESS, 129 BUTTON_REPEAT, 130 } ButtonAction; 131 132 typedef enum { 133 KEY_SPACE = 32, 134 KEY_APOSTROPHE = 39, /* ' */ 135 KEY_COMMA = 44, /* , */ 136 KEY_MINUS = 45, /* - */ 137 KEY_PERIOD = 46, /* . */ 138 KEY_SLASH = 47, /* / */ 139 KEY_0 = 48, 140 KEY_1 = 49, 141 KEY_2 = 50, 142 KEY_3 = 51, 143 KEY_4 = 52, 144 KEY_5 = 53, 145 KEY_6 = 54, 146 KEY_7 = 55, 147 KEY_8 = 56, 148 KEY_9 = 57, 149 KEY_SEMICOLON = 59, /* ; */ 150 KEY_EQUAL = 61, /* = */ 151 KEY_A = 65, 152 KEY_B = 66, 153 KEY_C = 67, 154 KEY_D = 68, 155 KEY_E = 69, 156 KEY_F = 70, 157 KEY_G = 71, 158 KEY_H = 72, 159 KEY_I = 73, 160 KEY_J = 74, 161 KEY_K = 75, 162 KEY_L = 76, 163 KEY_M = 77, 164 KEY_N = 78, 165 KEY_O = 79, 166 KEY_P = 80, 167 KEY_Q = 81, 168 KEY_R = 82, 169 KEY_S = 83, 170 KEY_T = 84, 171 KEY_U = 85, 172 KEY_V = 86, 173 KEY_W = 87, 174 KEY_X = 88, 175 KEY_Y = 89, 176 KEY_Z = 90, 177 KEY_LEFT_BRACKET = 91, /* [ */ 178 KEY_BACKSLASH = 92, /* \ */ 179 KEY_RIGHT_BRACKET = 93, /* ] */ 180 KEY_GRAVE_ACCENT = 96, /* ` */ 181 KEY_WORLD_1 = 161, /* non-US #1 */ 182 KEY_WORLD_2 = 162, /* non-US #2 */ 183 184 /* Function keys */ 185 KEY_ESCAPE = 256, 186 KEY_ENTER = 257, 187 KEY_TAB = 258, 188 KEY_BACKSPACE = 259, 189 KEY_INSERT = 260, 190 KEY_DELETE = 261, 191 KEY_RIGHT = 262, 192 KEY_LEFT = 263, 193 KEY_DOWN = 264, 194 KEY_UP = 265, 195 KEY_PAGE_UP = 266, 196 KEY_PAGE_DOWN = 267, 197 KEY_HOME = 268, 198 KEY_END = 269, 199 KEY_CAPS_LOCK = 280, 200 KEY_SCROLL_LOCK = 281, 201 KEY_NUM_LOCK = 282, 202 KEY_PRINT_SCREEN = 283, 203 KEY_PAUSE = 284, 204 KEY_F1 = 290, 205 KEY_F2 = 291, 206 KEY_F3 = 292, 207 KEY_F4 = 293, 208 KEY_F5 = 294, 209 KEY_F6 = 295, 210 KEY_F7 = 296, 211 KEY_F8 = 297, 212 KEY_F9 = 298, 213 KEY_F10 = 299, 214 KEY_F11 = 300, 215 KEY_F12 = 301, 216 KEY_F13 = 302, 217 KEY_F14 = 303, 218 KEY_F15 = 304, 219 KEY_F16 = 305, 220 KEY_F17 = 306, 221 KEY_F18 = 307, 222 KEY_F19 = 308, 223 KEY_F20 = 309, 224 KEY_F21 = 310, 225 KEY_F22 = 311, 226 KEY_F23 = 312, 227 KEY_F24 = 313, 228 KEY_F25 = 314, 229 KEY_KP_0 = 320, 230 KEY_KP_1 = 321, 231 KEY_KP_2 = 322, 232 KEY_KP_3 = 323, 233 KEY_KP_4 = 324, 234 KEY_KP_5 = 325, 235 KEY_KP_6 = 326, 236 KEY_KP_7 = 327, 237 KEY_KP_8 = 328, 238 KEY_KP_9 = 329, 239 KEY_KP_DECIMAL = 330, 240 KEY_KP_DIVIDE = 331, 241 KEY_KP_MULTIPLY = 332, 242 KEY_KP_SUBTRACT = 333, 243 KEY_KP_ADD = 334, 244 KEY_KP_ENTER = 335, 245 KEY_KP_EQUAL = 336, 246 } KeyboardKey; 247 248 typedef enum { 249 MOUSE_LEFT, 250 MOUSE_RIGHT, 251 MOUSE_MIDDLE, 252 MOUSE_EXTENDED_0, 253 MOUSE_EXTENDED_1, 254 255 KEY_LEFT_SHIFT, 256 KEY_LEFT_CONTROL, 257 KEY_LEFT_ALT, 258 KEY_LEFT_SUPER, 259 KEY_RIGHT_SHIFT, 260 KEY_RIGHT_CONTROL, 261 KEY_RIGHT_ALT, 262 KEY_RIGHT_SUPER, 263 KEY_MENU, 264 265 INPUT_KEY_COUNT, 266 } InputKey; 267 268 typedef enum { 269 MOD_SHIFT = (1 << 0), 270 MOD_CONTROL = (1 << 1), 271 MOD_ALT = (1 << 2), 272 MOD_SUPER = (1 << 3), 273 274 MOD_MASK = (MOD_SHIFT|MOD_CONTROL|MOD_ALT|MOD_SUPER), 275 } InputModifier; 276 277 typedef struct { 278 /* TODO: is this even supported or does GLFW only call you once per poll? */ 279 u8 transitions; 280 u8 ended_down; 281 } ButtonState; 282 283 typedef struct { 284 ButtonState keys[INPUT_KEY_COUNT]; 285 286 iv2 window_size; 287 288 b32 data_available; 289 b32 executable_reloaded; 290 b32 window_refreshed; 291 b32 window_focused; 292 b32 pending_updates; 293 294 InputModifier modifiers; 295 296 v2 mouse; 297 v2 last_mouse; 298 v2 mouse_scroll; 299 300 /* TODO: do we want the 32bit codepoints instead? */ 301 s8 character_input; 302 303 f32 dt; 304 } TerminalInput; 305 306 typedef struct { 307 u64 memory_size; 308 void *memory; 309 310 u64 debug_memory_size; 311 void *debug_memory; 312 } TerminalMemory; 313 314 /************************************************************/ 315 /* NOTE: functions provided by the terminal to the platform */ 316 /************************************************************/ 317 #define VTGL_FRAME_STEP_FN(name) void name(OS *os, TerminalMemory *memory, TerminalInput *input) 318 typedef VTGL_FRAME_STEP_FN(vtgl_frame_step_fn); 319 320 #define VTGL_RENDER_FRAME_FN(name) void name(OS *os, TerminalMemory *memory, TerminalInput *input) 321 typedef VTGL_RENDER_FRAME_FN(vtgl_render_frame_fn); 322 323 #define VTGL_ACTIVE_SELECTION_FN(name) Range name(TerminalMemory *memory, Stream *out) 324 typedef VTGL_ACTIVE_SELECTION_FN(vtgl_active_selection_fn); 325 326 #define VTGL_HANDLE_KEYS_FN(name) void name(OS *os, TerminalMemory *memory, TerminalInput *input, \ 327 KeyboardKey key, ButtonAction action, \ 328 InputModifier modifiers) 329 typedef VTGL_HANDLE_KEYS_FN(vtgl_handle_keys_fn); 330 331 #include "util.h" 332 #include "util.c" 333 334 #include "debug.h" 335 336 #ifdef __ARM_ARCH_ISA_A64 337 #include "os_linux_aarch64.c" 338 #elif defined(__linux__) && (defined(__x86_64__) || defined(_M_X64)) 339 #include "os_linux_amd64.c" 340 #else 341 #error Unsupported Platform! 342 #endif 343 344 #endif /*_VTGL_H_ */