MP Libopenmpt: Separate loading into sdl/load_libraries.c

This commit is contained in:
mazmazz 2018-09-19 23:18:06 -04:00
parent 65b8dd559c
commit d5b3d74e6f
4 changed files with 157 additions and 131 deletions

View file

@ -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

102
src/sdl/load_libraries.c Normal file
View file

@ -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

52
src/sdl/load_libraries.h Normal file
View file

@ -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

View file

@ -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);