main_w32.c (3652B)
1 /* See LICENSE for license details. */ 2 #include "compiler.h" 3 4 #if !OS_WINDOWS 5 #error This file is only meant to be compiled for Win32 6 #endif 7 8 #include "beamformer.h" 9 10 #include "os_win32.c" 11 12 #define OS_DEBUG_LIB_NAME ".\\beamformer.dll" 13 #define OS_DEBUG_LIB_TEMP_NAME ".\\beamformer_temp.dll" 14 15 #define OS_CUDA_LIB_NAME "external\\cuda_toolkit.dll" 16 #define OS_CUDA_LIB_TEMP_NAME "external\\cuda_toolkit_temp.dll" 17 18 #define OS_RENDERDOC_SONAME "renderdoc.dll" 19 20 iptr glfwGetWGLContext(iptr); 21 function iptr 22 os_get_native_gl_context(iptr window) 23 { 24 return glfwGetWGLContext(window); 25 } 26 27 function iptr 28 os_gl_proc_address(char *name) 29 { 30 return wglGetProcAddress(name); 31 } 32 33 #include "static.c" 34 35 function void 36 dispatch_file_watch(FileWatchDirectory *fw_dir, u8 *buf, Arena arena) 37 { 38 i64 offset = 0; 39 TempArena save_point = {0}; 40 w32_file_notify_info *fni = (w32_file_notify_info *)buf; 41 do { 42 end_temp_arena(save_point); 43 save_point = begin_temp_arena(&arena); 44 45 Stream path = {.data = arena_commit(&arena, KB(1)), .cap = KB(1)}; 46 47 if (fni->action != FILE_ACTION_MODIFIED) { 48 stream_append_s8(&path, s8("unknown file watch event: ")); 49 stream_append_u64(&path, fni->action); 50 stream_append_byte(&path, '\n'); 51 os_write_file(os_error_handle(), stream_to_s8(&path)); 52 stream_reset(&path, 0); 53 } 54 55 stream_append_s8(&path, fw_dir->name); 56 stream_append_byte(&path, '\\'); 57 58 s8 file_name = s16_to_s8(&arena, (s16){.data = fni->filename, 59 .len = fni->filename_size / 2}); 60 stream_append_s8(&path, file_name); 61 stream_append_byte(&path, 0); 62 stream_commit(&path, -1); 63 64 u64 hash = u64_hash_from_s8(file_name); 65 for (u32 i = 0; i < fw_dir->count; i++) { 66 FileWatch *fw = fw_dir->data + i; 67 if (fw->hash == hash) { 68 fw->callback(stream_to_s8(&path), fw->user_data, arena); 69 break; 70 } 71 } 72 73 offset = fni->next_entry_offset; 74 fni = (w32_file_notify_info *)((u8 *)fni + offset); 75 } while (offset); 76 } 77 78 function void 79 clear_io_queue(BeamformerInput *input, Arena arena) 80 { 81 iptr handle = os_w32_context.io_completion_handle; 82 w32_overlapped *overlapped; 83 u32 bytes_read; 84 uptr user_data; 85 while (GetQueuedCompletionStatus(handle, &bytes_read, &user_data, &overlapped, 0)) { 86 w32_io_completion_event *event = (w32_io_completion_event *)user_data; 87 switch (event->tag) { 88 case W32_IO_FILE_WATCH: { 89 FileWatchDirectory *dir = (FileWatchDirectory *)event->context; 90 dispatch_file_watch(dir, dir->buffer.beg, arena); 91 zero_struct(overlapped); 92 ReadDirectoryChangesW(dir->handle, dir->buffer.beg, 4096, 0, 93 FILE_NOTIFY_CHANGE_LAST_WRITE, 0, overlapped, 0); 94 } break; 95 case W32_IO_PIPE: break; 96 } 97 } 98 } 99 100 extern i32 101 main(void) 102 { 103 os_common_init(); 104 105 Arena program_memory = os_alloc_arena(MB(16) + KB(4)); 106 107 BeamformerCtx *ctx = 0; 108 BeamformerInput *input = 0; 109 110 os_w32_context.arena = sub_arena(&program_memory, KB(4), KB(4)); 111 os_w32_context.error_handle = GetStdHandle(STD_ERROR_HANDLE); 112 os_w32_context.io_completion_handle = CreateIoCompletionPort(INVALID_FILE, 0, 0, 0); 113 114 setup_beamformer(&program_memory, &ctx, &input); 115 116 u64 last_time = os_get_timer_counter(); 117 while (!ctx->should_exit) { 118 clear_io_queue(input, program_memory); 119 120 u64 now = os_get_timer_counter(); 121 input->last_mouse = input->mouse; 122 input->mouse.rl = GetMousePosition(); 123 input->dt = (f32)((f64)(now - last_time) / (f64)os_w32_context.timer_frequency); 124 last_time = now; 125 126 beamformer_frame_step(ctx, input); 127 128 input->executable_reloaded = 0; 129 } 130 131 beamformer_invalidate_shared_memory(ctx); 132 beamformer_debug_ui_deinit(ctx); 133 }