From 28b456c05d7243c91f1902ce0b54cfe417c2f602 Mon Sep 17 00:00:00 2001 From: mazmazz Date: Wed, 19 Sep 2018 23:18:06 -0400 Subject: [PATCH] MP Libopenmpt: Separate loading into sdl/load_libraries.c --- src/sdl/Makefile.cfg | 1 + src/sdl/load_libraries.c | 102 ++++++++++++++++++++++++++++++ src/sdl/load_libraries.h | 52 +++++++++++++++ src/sdl/mixer_sound.c | 133 +-------------------------------------- 4 files changed, 157 insertions(+), 131 deletions(-) create mode 100644 src/sdl/load_libraries.c create mode 100644 src/sdl/load_libraries.h diff --git a/src/sdl/Makefile.cfg b/src/sdl/Makefile.cfg index 58c4d0861..b4d4f83e5 100644 --- a/src/sdl/Makefile.cfg +++ b/src/sdl/Makefile.cfg @@ -79,6 +79,7 @@ ifdef NOMIXER i_sound_o=$(OBJDIR)/sdl_sound.o else i_sound_o=$(OBJDIR)/mixer_sound.o + OBJS+=$(OBJDIR)/load_libraries.o OPTS+=-DHAVE_MIXER SDL_LDFLAGS+=-lSDL2_mixer endif diff --git a/src/sdl/load_libraries.c b/src/sdl/load_libraries.c new file mode 100644 index 000000000..6b9a40cf9 --- /dev/null +++ b/src/sdl/load_libraries.c @@ -0,0 +1,102 @@ +/// +/// Dynamic Library Loading +/// + +#include "load_libraries.h" + +/// +/// OpenMPT Loading +/// + +#ifdef HAVE_OPENMPT + +openmpt_loader openmpt = { + 0, NULL, + NULL, NULL, NULL, // errors + NULL, NULL, // module loading + NULL, // audio callback + NULL, NULL, NULL, // playback settings + NULL, NULL, NULL, NULL, NULL // positioning +}; + +#ifdef OPENMPT_DYNAMIC +#define FUNCTION_LOADER(NAME, FUNC, SIG) \ + openmpt.NAME = (SIG) SDL_LoadFunction(openmpt.handle, #FUNC); \ + if (openmpt.NAME == NULL) { SDL_UnloadObject(openmpt.handle); openmpt.handle = NULL; return; } +#else +#define FUNCTION_LOADER(NAME, FUNC, SIG) \ + openmpt.NAME = FUNC; +#endif + +void load_openmpt(void) +{ + if (openmpt.loaded) + return; + +#ifdef OPENMPT_DYNAMIC +#if defined(_WIN32) || defined(_WIN64) + openmpt.handle = SDL_LoadObject("libopenmpt.dll"); +#else + openmpt.handle = SDL_LoadObject("libopenmpt.so"); +#endif + if (openmpt.handle == NULL) + { + CONS_Printf("libopenmpt not found, not loading.\n"); + return; + } +#endif + + // errors + FUNCTION_LOADER(module_error_get_last, openmpt_module_error_get_last, int (*) ( openmpt_module * mod )) + FUNCTION_LOADER(error_string, openmpt_error_string, const char *(*) ( int error )) + FUNCTION_LOADER(get_string, openmpt_get_string, const char *(*) ( const char * key )) + + // module loading + FUNCTION_LOADER(module_destroy, openmpt_module_destroy, void (*) ( openmpt_module * mod )) + FUNCTION_LOADER(module_create_from_memory2, openmpt_module_create_from_memory2, openmpt_module *(*) ( const void * filedata, size_t filesize, openmpt_log_func logfunc, void * loguser, openmpt_error_func errfunc, void * erruser, int * error, const char * * error_message, const openmpt_module_initial_ctl * ctls )) + + // audio callback + FUNCTION_LOADER(module_read_interleaved_stereo, openmpt_module_read_interleaved_stereo, size_t (*) ( openmpt_module * mod, int32_t samplerate, size_t count, int16_t * interleaved_stereo )) + + // playback settings + FUNCTION_LOADER(module_set_render_param, openmpt_module_set_render_param, int (*) ( openmpt_module * mod, int param, int32_t value )) + FUNCTION_LOADER(module_set_repeat_count, openmpt_module_set_repeat_count, int (*) ( openmpt_module * mod, int32_t repeat_count )) + FUNCTION_LOADER(module_ctl_set, openmpt_module_ctl_set, int (*) ( openmpt_module * mod, const char * ctl, const char * value )) + + // positioning + FUNCTION_LOADER(module_get_duration_seconds, openmpt_module_get_duration_seconds, double (*) ( openmpt_module * mod )) + FUNCTION_LOADER(module_get_position_seconds, openmpt_module_get_position_seconds, double (*) ( openmpt_module * mod )) + FUNCTION_LOADER(module_set_position_seconds, openmpt_module_set_position_seconds, double (*) ( openmpt_module * mod, double seconds )) + FUNCTION_LOADER(module_get_num_subsongs, openmpt_module_get_num_subsongs, int32_t (*) ( openmpt_module * mod )) + FUNCTION_LOADER(module_select_subsong, openmpt_module_select_subsong, int (*) ( openmpt_module * mod, int32_t subsong )) + +#ifdef OPENMPT_DYNAMIC + // this will be unset if a function failed to load + if (openmpt.handle == NULL) + { + CONS_Printf("libopenmpt found but failed to load.\n"); + return; + } +#endif + + CONS_Printf("libopenmpt version: %s\n", openmpt.get_string("library_version")); + CONS_Printf("libopenmpt build date: %s\n", openmpt.get_string("build")); + + openmpt.loaded = 1; +} + +void unload_openmpt(void) +{ +#ifdef OPENMPT_DYNAMIC + if (openmpt.loaded) + { + SDL_UnloadObject(openmpt.handle); + openmpt.handle = NULL; + openmpt.loaded = 0; + } +#endif +} + +#undef FUNCTION_LOADER + +#endif diff --git a/src/sdl/load_libraries.h b/src/sdl/load_libraries.h new file mode 100644 index 000000000..9f097fd47 --- /dev/null +++ b/src/sdl/load_libraries.h @@ -0,0 +1,52 @@ +/// +/// Dynamic Library Loading +/// + +/// +/// OpenMPT Loading +/// + +#ifdef HAVE_OPENMPT + +#include "libopenmpt/libopenmpt.h" + +// Dynamic loading inspired by SDL Mixer +// Why: It's hard to compile for Windows without MSVC dependency, see https://trac.videolan.org/vlc/ticket/13055 +// So let's not force that on the user, and they can download it if they want. +// +// ADD FUNCTIONS HERE AS YOU USE THEM!!!!! +typedef struct { + int loaded; + void *handle; + + // errors + int (*module_error_get_last) ( openmpt_module * mod ); + const char *(*error_string) ( int error ); + const char *(*get_string) ( const char * key ); + + // module loading + void (*module_destroy) ( openmpt_module * mod ); + openmpt_module *(*module_create_from_memory2) ( const void * filedata, size_t filesize, openmpt_log_func logfunc, void * loguser, openmpt_error_func errfunc, void * erruser, int * error, const char * * error_message, const openmpt_module_initial_ctl * ctls ); + + // audio callback + size_t (*module_read_interleaved_stereo) ( openmpt_module * mod, int32_t samplerate, size_t count, int16_t * interleaved_stereo ); + + // playback settings + int (*module_set_render_param) ( openmpt_module * mod, int param, int32_t value ); + int (*module_set_repeat_count) ( openmpt_module * mod, int32_t repeat_count ); + int (*module_ctl_set) ( openmpt_module * mod, const char * ctl, const char * value ); + + // positioning + double (*module_get_duration_seconds) ( openmpt_module * mod ); + double (*module_get_position_seconds) ( openmpt_module * mod ); + double (*module_set_position_seconds) ( openmpt_module * mod, double seconds ); + int32_t (*module_get_num_subsongs) ( openmpt_module * mod ); + int (*module_select_subsong) ( openmpt_module * mod, int32_t subsong ); +} openmpt_loader; + +extern openmpt_loader openmpt; + +void load_openmpt(void); +void unload_openmpt(void); + +#endif diff --git a/src/sdl/mixer_sound.c b/src/sdl/mixer_sound.c index ff1d0ac36..17a4cfb15 100644 --- a/src/sdl/mixer_sound.c +++ b/src/sdl/mixer_sound.c @@ -12,6 +12,8 @@ #include "../z_zone.h" #include "../byteptr.h" +#include "load_libraries.h" + #ifdef _MSC_VER #pragma warning(disable : 4214 4244) #endif @@ -126,137 +128,6 @@ static void var_cleanup(void) /// Audio System /// ------------------------ -/// -/// OpenMPT Loading -/// - -#ifdef HAVE_OPENMPT - -// Dynamic loading inspired by SDL Mixer -// Why: It's hard to compile for Windows without MSVC dependency, see https://trac.videolan.org/vlc/ticket/13055 -// So let's not force that on the user, and they can download it if they want. -// -// ADD FUNCTIONS HERE AS YOU USE THEM!!!!! -typedef struct { - int loaded; - void *handle; - - // errors - int (*module_error_get_last) ( openmpt_module * mod ); - const char *(*error_string) ( int error ); - const char *(*get_string) ( const char * key ); - - // module loading - void (*module_destroy) ( openmpt_module * mod ); - openmpt_module *(*module_create_from_memory2) ( const void * filedata, size_t filesize, openmpt_log_func logfunc, void * loguser, openmpt_error_func errfunc, void * erruser, int * error, const char * * error_message, const openmpt_module_initial_ctl * ctls ); - - // audio callback - size_t (*module_read_interleaved_stereo) ( openmpt_module * mod, int32_t samplerate, size_t count, int16_t * interleaved_stereo ); - - // playback settings - int (*module_set_render_param) ( openmpt_module * mod, int param, int32_t value ); - int (*module_set_repeat_count) ( openmpt_module * mod, int32_t repeat_count ); - int (*module_ctl_set) ( openmpt_module * mod, const char * ctl, const char * value ); - - // positioning - double (*module_get_duration_seconds) ( openmpt_module * mod ); - double (*module_get_position_seconds) ( openmpt_module * mod ); - double (*module_set_position_seconds) ( openmpt_module * mod, double seconds ); - int32_t (*module_get_num_subsongs) ( openmpt_module * mod ); - int (*module_select_subsong) ( openmpt_module * mod, int32_t subsong ); -} openmpt_loader; - -static openmpt_loader openmpt = { - 0, NULL, - NULL, NULL, NULL, // errors - NULL, NULL, // module loading - NULL, // audio callback - NULL, NULL, NULL, // playback settings - NULL, NULL, NULL, NULL, NULL // positioning -}; - -#ifdef OPENMPT_DYNAMIC -#define FUNCTION_LOADER(NAME, FUNC, SIG) \ - openmpt.NAME = (SIG) SDL_LoadFunction(openmpt.handle, #FUNC); \ - if (openmpt.NAME == NULL) { SDL_UnloadObject(openmpt.handle); openmpt.handle = NULL; return; } -#else -#define FUNCTION_LOADER(NAME, FUNC, SIG) \ - openmpt.NAME = FUNC; -#endif - -static void load_openmpt(void) -{ - if (openmpt.loaded) - return; - -#ifdef OPENMPT_DYNAMIC -#if defined(_WIN32) || defined(_WIN64) - openmpt.handle = SDL_LoadObject("libopenmpt.dll"); -#else - openmpt.handle = SDL_LoadObject("libopenmpt.so"); -#endif - if (openmpt.handle == NULL) - { - CONS_Printf("libopenmpt not found, not loading.\n"); - return; - } -#endif - - // errors - FUNCTION_LOADER(module_error_get_last, openmpt_module_error_get_last, int (*) ( openmpt_module * mod )) - FUNCTION_LOADER(error_string, openmpt_error_string, const char *(*) ( int error )) - FUNCTION_LOADER(get_string, openmpt_get_string, const char *(*) ( const char * key )) - - // module loading - FUNCTION_LOADER(module_destroy, openmpt_module_destroy, void (*) ( openmpt_module * mod )) - FUNCTION_LOADER(module_create_from_memory2, openmpt_module_create_from_memory2, openmpt_module *(*) ( const void * filedata, size_t filesize, openmpt_log_func logfunc, void * loguser, openmpt_error_func errfunc, void * erruser, int * error, const char * * error_message, const openmpt_module_initial_ctl * ctls )) - - // audio callback - FUNCTION_LOADER(module_read_interleaved_stereo, openmpt_module_read_interleaved_stereo, size_t (*) ( openmpt_module * mod, int32_t samplerate, size_t count, int16_t * interleaved_stereo )) - - // playback settings - FUNCTION_LOADER(module_set_render_param, openmpt_module_set_render_param, int (*) ( openmpt_module * mod, int param, int32_t value )) - FUNCTION_LOADER(module_set_repeat_count, openmpt_module_set_repeat_count, int (*) ( openmpt_module * mod, int32_t repeat_count )) - FUNCTION_LOADER(module_ctl_set, openmpt_module_ctl_set, int (*) ( openmpt_module * mod, const char * ctl, const char * value )) - - // positioning - FUNCTION_LOADER(module_get_duration_seconds, openmpt_module_get_duration_seconds, double (*) ( openmpt_module * mod )) - FUNCTION_LOADER(module_get_position_seconds, openmpt_module_get_position_seconds, double (*) ( openmpt_module * mod )) - FUNCTION_LOADER(module_set_position_seconds, openmpt_module_set_position_seconds, double (*) ( openmpt_module * mod, double seconds )) - FUNCTION_LOADER(module_get_num_subsongs, openmpt_module_get_num_subsongs, int32_t (*) ( openmpt_module * mod )) - FUNCTION_LOADER(module_select_subsong, openmpt_module_select_subsong, int (*) ( openmpt_module * mod, int32_t subsong )) - -#ifdef OPENMPT_DYNAMIC - // this will be unset if a function failed to load - if (openmpt.handle == NULL) - { - CONS_Printf("libopenmpt found but failed to load.\n"); - return; - } -#endif - - CONS_Printf("libopenmpt version: %s\n", openmpt.get_string("library_version")); - CONS_Printf("libopenmpt build date: %s\n", openmpt.get_string("build")); - - openmpt.loaded = 1; -} - -static void unload_openmpt(void) -{ -#ifdef OPENMPT_DYNAMIC - if (openmpt.loaded) - { - SDL_UnloadObject(openmpt.handle); - openmpt.handle = NULL; - openmpt.loaded = 0; - } -#endif -} - -#undef FUNCTION_LOADER - -#endif - void I_StartupSound(void) { I_Assert(!sound_started);