a-game

2D platformer written from scratch.
git clone git://git.amin.space/a-game.git
Log | Files | Refs | README | LICENSE

commit 71cc0b2476ba24d62a16203dbf13c4192e1541b7
parent c7a57149e4dd3faee96fa81f92e7d23a9a181630
Author: amin <dev@aminmesbah.com>
Date:   Thu,  2 May 2019 00:05:26 +0000

Fix up Windows platform layer

FossilOrigin-Name: d180182551b3a50dad899b0f6b13ce04b6ae18d30a87632e1a48718d9e75aa58
Diffstat:
Msrc/platform_windows.c | 89+++++++++++--------------------------------------------------------------------
Msrc/platform_windows.h | 7-------
2 files changed, 12 insertions(+), 84 deletions(-)

diff --git a/src/platform_windows.c b/src/platform_windows.c @@ -51,23 +51,25 @@ int main(void) srand((uint32_t)time(NULL)); #endif - struct GameState game_state = {0}; struct GameMemory game_memory = { - .game_state = &game_state, + .buffer_size = MEBIBYTES(64), .platform = { &windows_read_entire_file, }, }; + // TODO: replace with VirtualAlloc + game_memory.buffer = malloc(game_memory.buffer_size); + if (!game_memory.buffer) + { + fprintf(stderr, "Game memory allocation failed\n"); + glfwTerminate(); + return -1; + } struct GameInput game_input = {0}; glfwSetWindowUserPointer(window, &game_input); game_init(&game_memory, (v2u) {PLATFORM_SCR_WIDTH, PLATFORM_SCR_HEIGHT}); -#ifdef PLATFORM_HOTLOAD_GAME_CODE - struct GameCode game_code = load_game_code(PLATFORM_GAME_LIB_PATH); - game_code.game_load_opengl_symbols(); -#endif - // NOTE(amin): lag should always be very small, so it's fine to use a float // to store it. We will not run out of precision. uint64_t previous_wall_clock_time = glfwGetTimerValue(); @@ -93,23 +95,7 @@ int main(void) assert(framebuffer_height >= 0); v2u framebuffer = {framebuffer_width, framebuffer_height}; -#ifdef PLATFORM_HOTLOAD_GAME_CODE - time_t last_write_time = file_get_modified_time(PLATFORM_GAME_LIB_PATH); - bool game_code_has_changed = last_write_time && (last_write_time != game_code.last_write_time); - if (game_code_has_changed) - { - unload_game_code(&game_code); - game_code = load_game_code(PLATFORM_GAME_LIB_PATH); - game_code.game_load_opengl_symbols(); - if (!game_code.is_valid) - { - // TODO: fall back to backup? - } - } - game_code.game_update_and_render(&game_memory, &game_input, framebuffer); -#else game_update_and_render(&game_memory, &game_input, framebuffer); -#endif // PLATFORM_HOTLOAD_GAME_CODE // TODO: Do something sane when vsync is disabled glfwSwapBuffers(window); @@ -118,6 +104,9 @@ int main(void) game_cleanup(&game_memory); + // TODO: replace with VirtualFree + free(game_memory.buffer); + glfwDestroyWindow(window); glfwTerminate(); return 0; @@ -232,60 +221,6 @@ internal PLATFORM_READ_ENTIRE_FILE(windows_read_entire_file) return buffer; } -#ifdef PLATFORM_HOTLOAD_GAME_CODE -internal void unload_game_code(struct GameCode *game_code) -{ - if (!game_code) - { - fprintf(stderr, "ERROR: Invalid pointer *game_code\n"); - return; - } - - if (game_code->game_code_library) - { - dlclose(game_code->game_code_library); - game_code->game_code_library = NULL; - } - game_code->is_valid = false; - game_code->game_load_opengl_symbols = NULL; - game_code->game_update_and_render = NULL; -} - - -// TODO: Add backup dll in case loading fails -internal struct GameCode load_game_code(char *source_lib_path) -{ - struct GameCode game_code = {0}; - - game_code.last_write_time = file_get_modified_time(source_lib_path); - if (game_code.last_write_time) - { - game_code.game_code_library = dlopen(source_lib_path, RTLD_LAZY); - if (game_code.game_code_library) - { - // NOTE: The C standard (as of C99) distinguishes function pointers - // from object pointers (`void *` is an object pointer). Technically it - // is undefined behavior to cast between these two pointer types. In - // practice, it works on most modern platforms. - // - // In this case, we are protected by POSIX, which specifies that - // function and data pointers must be the same size. We will only ever - // be using dlsym on POSIX-compliant platforms. - game_code.game_load_opengl_symbols = (game_load_opengl_symbols_func *) dlsym(game_code.game_code_library, "game_load_opengl_symbols"); - game_code.game_update_and_render = (game_update_and_render_func *) dlsym(game_code.game_code_library, "game_update_and_render"); - game_code.is_valid = (game_code.game_load_opengl_symbols && game_code.game_update_and_render); - } - } - - if (!game_code.is_valid) - { - fprintf(stderr, "ERROR: Game code is not valid: %s\n", dlerror()); - } - - return game_code; -} -#endif // PLATFORM_HOTLOAD_GAME_CODE - #ifdef GLAD_DEBUG internal void pre_gl_callback(const char *func_name, void *func_ptr, int len_args, ...) { diff --git a/src/platform_windows.h b/src/platform_windows.h @@ -6,13 +6,6 @@ internal void framebuffer_size_callback(GLFWwindow* window, int width, int heigh internal void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); internal PLATFORM_READ_ENTIRE_FILE(windows_read_entire_file); -#ifdef PLATFORM_HOTLOAD_GAME_CODE -#define PLATFORM_GAME_LIB_PATH "./out/release/game.so" -internal time_t file_get_modified_time(char *file_path); -internal void unload_game_code(struct GameCode *game_code); -internal struct GameCode load_game_code(char *source_lib_path); -#endif - #ifdef GLAD_DEBUG internal void pre_gl_callback(const char *func_name, void *func_ptr, int len_args, ...); #endif