ohsp

Prototype for a game with dual thruster controls.
git clone git://git.amin.space/ohsp.git
Log | Files | Refs | LICENSE

commit ea60c4512057f56ca607741fbd3b2eaf998648a4
parent 1f03ca1da44e87d146c8b333c2ee7431e513e3f9
Author: amin <dev@aminmesbah.com>
Date:   Fri,  1 Mar 2024 08:28:37 +0000

Get everything working with clangd

FossilOrigin-Name: 8a61dc5d83b12a936afeabcc21cc8019155d4ef98c2d9e82a46fcd762431f282
Diffstat:
Acompile_flags.txt | 2++
Msrc/platform_sdl.c | 77+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----
Msrc/platform_sdl.h | 2+-
Dsrc/util_linux.h | 78------------------------------------------------------------------------------
4 files changed, 76 insertions(+), 83 deletions(-)

diff --git a/compile_flags.txt b/compile_flags.txt @@ -0,0 +1,2 @@ +--std=c11 +-include src/game.h diff --git a/src/platform_sdl.c b/src/platform_sdl.c @@ -1,9 +1,78 @@ #include "platform_sdl.h" -#ifdef __linux__ -#include "util_linux.h" -#elif __WIN32 -#endif +#include <dlfcn.h> +#include <sys/stat.h> +#include <stdio.h> +#include <time.h> + +time_t file_get_modified_time(char *file_path) +{ + time_t mtime = 0; + struct stat file_status = {0}; + if (stat(file_path, &file_status) == 0) + { + //printf("File: %s last modified: %s\n", file_path, ctime(&file_status.st_mtime)); + mtime = file_status.st_mtime; + } + else + { + fprintf(stderr, "ERROR: Failed to stat file: %s\n", file_path); + } + return mtime; +} + + +void unload_game_code(struct SDLGameCode *game_code) +{ + if (!game_code) + { + printf("Invalid pointer *game_code\n"); + return; + } + + if (game_code->game_code_library) + { + dlclose(game_code->game_code_library); + game_code->game_code_library = 0; + } + game_code->is_valid = false; + game_code->game_update = 0; + game_code->game_render = 0; +} + + +// TODO: Add backup dll in case loading fails +struct SDLGameCode load_game_code(char *source_lib_path) +{ + struct SDLGameCode 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_update = (game_update_t *) dlsym(game_code.game_code_library, "game_update"); + game_code.game_render = (game_render_t *) dlsym(game_code.game_code_library, "game_render"); + game_code.is_valid = (game_code.game_update && game_code.game_render); + } + } + + if (!game_code.is_valid) + { + fprintf(stderr, "ERROR: Game code is not valid: %s\n", dlerror()); + } + + return game_code; +} #include <stdbool.h> #include <stdio.h> diff --git a/src/platform_sdl.h b/src/platform_sdl.h @@ -1,6 +1,6 @@ #ifndef PLATFORM_SDL_H -#include "SDL.h" +#include <SDL2/SDL.h> #include "game.h" #include <stdbool.h> diff --git a/src/util_linux.h b/src/util_linux.h @@ -1,78 +0,0 @@ -#ifndef UTIL_LINUX_H -#define UTIL_LINUX_H - -#include <dlfcn.h> -#include <sys/stat.h> - - -time_t file_get_modified_time(char *file_path) -{ - time_t mtime = 0; - struct stat file_status = {0}; - if (stat(file_path, &file_status) == 0) - { - //printf("File: %s last modified: %s\n", file_path, ctime(&file_status.st_mtime)); - mtime = file_status.st_mtime; - } - else - { - fprintf(stderr, "ERROR: Failed to stat file: %s\n", file_path); - } - return mtime; -} - - -void unload_game_code(struct SDLGameCode *game_code) -{ - if (!game_code) - { - printf("Invalid pointer *game_code\n"); - return; - } - - if (game_code->game_code_library) - { - dlclose(game_code->game_code_library); - game_code->game_code_library = 0; - } - game_code->is_valid = false; - game_code->game_update = 0; - game_code->game_render = 0; -} - - -// TODO: Add backup dll in case loading fails -struct SDLGameCode load_game_code(char *source_lib_path) -{ - struct SDLGameCode 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_update = (game_update_t *) dlsym(game_code.game_code_library, "game_update"); - game_code.game_render = (game_render_t *) dlsym(game_code.game_code_library, "game_render"); - game_code.is_valid = (game_code.game_update && game_code.game_render); - } - } - - if (!game_code.is_valid) - { - fprintf(stderr, "ERROR: Game code is not valid: %s\n", dlerror()); - } - - return game_code; -} - - -#endif