medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit f61be9338e2ffbfa6d2e46e1d9000b3d12e4371e
parent 67b0bed585c8d35a782fdf15fe28b1f402caaa52
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Thu Mar 10 22:30:57 +0000

Add platform agnostic shared library interface

Diffstat:
main.cc | 14+++++++-------
platform_library.h | 10++++++++++
unix_library.h | 24++++++++++++++++++++++++
win32_library.h | 27+++++++++++++++++++++++++++
4 files changed, 68 insertions(+), 7 deletions(-)
diff --git a/main.cc b/main.cc @@ -1,6 +1,5 @@ #include <stdio.h> #include <time.h> -#include <dlfcn.h> #include <sys/stat.h> #include <sys/types.h> // TODO: this won't work on windows @@ -15,9 +14,10 @@ #include "intrinsics.h" #include "gl.h" #include "keys.h" +#include "platform_library.h" struct Game { - void * lib; + Library lib; GameInit * init; GameFrame * frame; @@ -36,10 +36,10 @@ static time_t file_last_write_time( const char * const path ) { static Game load_game( const char * const path ) { Game game = { }; - game.lib = dlopen( path, RTLD_NOW ); + game.lib = library_open( path ); if( game.lib ) { - game.init = ( GameInit * ) dlsym( game.lib, "game_init" ); - game.frame = ( GameFrame * ) dlsym( game.lib, "game_frame" ); + game.init = ( GameInit * ) library_function( game.lib, "game_init" ); + game.frame = ( GameFrame * ) library_function( game.lib, "game_frame" ); if( !game.init || !game.frame ) { WARN( "load_game: couldn't find game functions (init = %p, frame = %p)", game.init, game.frame ); @@ -51,7 +51,7 @@ static Game load_game( const char * const path ) { game.lib_write_time = file_last_write_time( path ); - const char * const error = dlerror(); + const char * error = library_last_error(); if( error ) { WARN( "load_game: %s", error ); } @@ -61,7 +61,7 @@ static Game load_game( const char * const path ) { static void unload_game( Game * game ) { if( game->lib ) { - dlclose( game->lib ); + library_close( game->lib ); } game->init = NULL; diff --git a/platform_library.h b/platform_library.h @@ -0,0 +1,10 @@ +#ifndef _PLATFORM_LIBRARY_H_ +#define _PLATFORM_LIBRARY_H_ + +#if defined( __linux__ ) || defined( __APPLE__ ) || defined( __OpenBSD__ ) +#include "unix_library.h" +#elif defined( WIN32 ) +#include "win32_library.h" +#endif + +#endif // _PLATFORM_LIBRARY_H_ diff --git a/unix_library.h b/unix_library.h @@ -0,0 +1,24 @@ +#ifndef _UNIX_LIBRARY_H_ +#define _UNIX_LIBRARY_H_ + +#include <dlfcn.h> + +typedef void * Library; + +Library library_open( const char * path ) { + return dlopen( path, RTLD_NOW ); +} + +void * library_function( Library lib, const char * name ) { + return dlsym( lib, name ); +} + +const char * library_last_error() { + return dlerror(); +} + +bool library_close( Library lib ) { + return dlclose( lib ) == 0; +} + +#endif // _UNIX_LIBRARY_H_ diff --git a/win32_library.h b/win32_library.h @@ -0,0 +1,27 @@ +#ifndef _WIN32_LIBRARY_H_ +#define _WIN32_LIBRARY_H_ + +#include <windows.h> + +typedef HMODULE Library; + +Library library_open( const char * path ) { + return LoadLibraryA( path ); +} + +void * library_function( Library lib, const char * name ) { + return GetProcAddress( lib, name ); +} + +const char * library_last_error() { + u32 error = GetLastError(); + static char buf[ 8 ]; + sprintf( buf, "%d", error ); + return buf; +} + +bool library_close( Library lib ) { + return FreeLibrary( lib ) == 0; +} + +#endif // _WIN32_LIBRARY_H_