dynamic_mod.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 03 Oct 2009 20:43:33 +0000
changeset 419 e27fe0bfe470
child 470 5cebd6c5be2d
permissions -rw-r--r--
Sam Lantinga - Sat Oct 3 13:33:36 PDT 2009
* MOD support uses libmikmod and is dynamically loaded by default
slouken@419
     1
/*
slouken@419
     2
    SDL_mixer:  An audio mixer library based on the SDL library
slouken@419
     3
    Copyright (C) 1997-2009 Sam Lantinga
slouken@419
     4
slouken@419
     5
    This library is free software; you can redistribute it and/or
slouken@419
     6
    modify it under the terms of the GNU Library General Public
slouken@419
     7
    License as published by the Free Software Foundation; either
slouken@419
     8
    version 2 of the License, or (at your option) any later version.
slouken@419
     9
slouken@419
    10
    This library is distributed in the hope that it will be useful,
slouken@419
    11
    but WITHOUT ANY WARRANTY; without even the implied warranty of
slouken@419
    12
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
slouken@419
    13
    Library General Public License for more details.
slouken@419
    14
slouken@419
    15
    You should have received a copy of the GNU Library General Public
slouken@419
    16
    License along with this library; if not, write to the Free
slouken@419
    17
    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
slouken@419
    18
slouken@419
    19
    Sam Lantinga
slouken@419
    20
    slouken@libsdl.org
slouken@419
    21
*/
slouken@419
    22
slouken@419
    23
#ifdef MOD_MUSIC
slouken@419
    24
slouken@419
    25
#include "SDL_loadso.h"
slouken@419
    26
slouken@419
    27
#include "dynamic_mod.h"
slouken@419
    28
slouken@419
    29
mikmod_loader mikmod = {
slouken@419
    30
	0, NULL
slouken@419
    31
};
slouken@419
    32
slouken@419
    33
#ifdef MOD_DYNAMIC
slouken@419
    34
int Mix_InitMOD()
slouken@419
    35
{
slouken@419
    36
	if ( mikmod.loaded == 0 ) {
slouken@419
    37
		mikmod.handle = SDL_LoadObject(MOD_DYNAMIC);
slouken@419
    38
		if ( mikmod.handle == NULL ) {
slouken@419
    39
			return -1;
slouken@419
    40
		}
slouken@419
    41
		mikmod.MikMod_Exit =
slouken@419
    42
			(void (*)(void))
slouken@419
    43
			SDL_LoadFunction(mikmod.handle, "MikMod_Exit");
slouken@419
    44
		if ( mikmod.MikMod_Exit == NULL ) {
slouken@419
    45
			SDL_UnloadObject(mikmod.handle);
slouken@419
    46
			return -1;
slouken@419
    47
		}
slouken@419
    48
		mikmod.MikMod_InfoDriver =
slouken@419
    49
			(CHAR* (*)(void))
slouken@419
    50
			SDL_LoadFunction(mikmod.handle, "MikMod_InfoDriver");
slouken@419
    51
		if ( mikmod.MikMod_InfoDriver == NULL ) {
slouken@419
    52
			SDL_UnloadObject(mikmod.handle);
slouken@419
    53
			return -1;
slouken@419
    54
		}
slouken@419
    55
		mikmod.MikMod_InfoLoader =
slouken@419
    56
			(CHAR* (*)(void))
slouken@419
    57
			SDL_LoadFunction(mikmod.handle, "MikMod_InfoLoader");
slouken@419
    58
		if ( mikmod.MikMod_InfoLoader == NULL ) {
slouken@419
    59
			SDL_UnloadObject(mikmod.handle);
slouken@419
    60
			return -1;
slouken@419
    61
		}
slouken@419
    62
		mikmod.MikMod_Init =
slouken@419
    63
			(BOOL (*)(CHAR*))
slouken@419
    64
			SDL_LoadFunction(mikmod.handle, "MikMod_Init");
slouken@419
    65
		if ( mikmod.MikMod_Init == NULL ) {
slouken@419
    66
			SDL_UnloadObject(mikmod.handle);
slouken@419
    67
			return -1;
slouken@419
    68
		}
slouken@419
    69
		mikmod.MikMod_RegisterAllLoaders =
slouken@419
    70
			(void (*)(void))
slouken@419
    71
			SDL_LoadFunction(mikmod.handle, "MikMod_RegisterAllLoaders");
slouken@419
    72
		if ( mikmod.MikMod_RegisterAllLoaders == NULL ) {
slouken@419
    73
			SDL_UnloadObject(mikmod.handle);
slouken@419
    74
			return -1;
slouken@419
    75
		}
slouken@419
    76
		mikmod.MikMod_RegisterDriver =
slouken@419
    77
			(void (*)(struct MDRIVER*))
slouken@419
    78
			SDL_LoadFunction(mikmod.handle, "MikMod_RegisterDriver");
slouken@419
    79
		if ( mikmod.MikMod_RegisterDriver == NULL ) {
slouken@419
    80
			SDL_UnloadObject(mikmod.handle);
slouken@419
    81
			return -1;
slouken@419
    82
		}
slouken@419
    83
		mikmod.MikMod_errno =
slouken@419
    84
			(int*)
slouken@419
    85
			SDL_LoadFunction(mikmod.handle, "MikMod_errno");
slouken@419
    86
		if ( mikmod.MikMod_errno == NULL ) {
slouken@419
    87
			SDL_UnloadObject(mikmod.handle);
slouken@419
    88
			return -1;
slouken@419
    89
		}
slouken@419
    90
		mikmod.MikMod_strerror =
slouken@419
    91
			(char* (*)(int))
slouken@419
    92
			SDL_LoadFunction(mikmod.handle, "MikMod_strerror");
slouken@419
    93
		if ( mikmod.MikMod_strerror == NULL ) {
slouken@419
    94
			SDL_UnloadObject(mikmod.handle);
slouken@419
    95
			return -1;
slouken@419
    96
		}
slouken@419
    97
		mikmod.Player_Active =
slouken@419
    98
			(BOOL (*)(void))
slouken@419
    99
			SDL_LoadFunction(mikmod.handle, "Player_Active");
slouken@419
   100
		if ( mikmod.Player_Active == NULL ) {
slouken@419
   101
			SDL_UnloadObject(mikmod.handle);
slouken@419
   102
			return -1;
slouken@419
   103
		}
slouken@419
   104
		mikmod.Player_Free =
slouken@419
   105
			(void (*)(MODULE*))
slouken@419
   106
			SDL_LoadFunction(mikmod.handle, "Player_Free");
slouken@419
   107
		if ( mikmod.Player_Free == NULL ) {
slouken@419
   108
			SDL_UnloadObject(mikmod.handle);
slouken@419
   109
			return -1;
slouken@419
   110
		}
slouken@419
   111
		mikmod.Player_LoadGeneric =
slouken@419
   112
			(MODULE* (*)(MREADER*,int,BOOL))
slouken@419
   113
			SDL_LoadFunction(mikmod.handle, "Player_LoadGeneric");
slouken@419
   114
		if ( mikmod.Player_LoadGeneric == NULL ) {
slouken@419
   115
			SDL_UnloadObject(mikmod.handle);
slouken@419
   116
			return -1;
slouken@419
   117
		}
slouken@419
   118
		mikmod.Player_SetPosition =
slouken@419
   119
			(void (*)(UWORD))
slouken@419
   120
			SDL_LoadFunction(mikmod.handle, "Player_SetPosition");
slouken@419
   121
		if ( mikmod.Player_SetPosition == NULL ) {
slouken@419
   122
			SDL_UnloadObject(mikmod.handle);
slouken@419
   123
			return -1;
slouken@419
   124
		}
slouken@419
   125
		mikmod.Player_SetVolume =
slouken@419
   126
			(void (*)(SWORD))
slouken@419
   127
			SDL_LoadFunction(mikmod.handle, "Player_SetVolume");
slouken@419
   128
		if ( mikmod.Player_SetVolume == NULL ) {
slouken@419
   129
			SDL_UnloadObject(mikmod.handle);
slouken@419
   130
			return -1;
slouken@419
   131
		}
slouken@419
   132
		mikmod.Player_Start =
slouken@419
   133
			(void (*)(MODULE*))
slouken@419
   134
			SDL_LoadFunction(mikmod.handle, "Player_Start");
slouken@419
   135
		if ( mikmod.Player_Start == NULL ) {
slouken@419
   136
			SDL_UnloadObject(mikmod.handle);
slouken@419
   137
			return -1;
slouken@419
   138
		}
slouken@419
   139
		mikmod.Player_Stop =
slouken@419
   140
			(void (*)(void))
slouken@419
   141
			SDL_LoadFunction(mikmod.handle, "Player_Stop");
slouken@419
   142
		if ( mikmod.Player_Stop == NULL ) {
slouken@419
   143
			SDL_UnloadObject(mikmod.handle);
slouken@419
   144
			return -1;
slouken@419
   145
		}
slouken@419
   146
		mikmod.VC_WriteBytes =
slouken@419
   147
			(ULONG (*)(SBYTE*,ULONG))
slouken@419
   148
			SDL_LoadFunction(mikmod.handle, "VC_WriteBytes");
slouken@419
   149
		if ( mikmod.VC_WriteBytes == NULL ) {
slouken@419
   150
			SDL_UnloadObject(mikmod.handle);
slouken@419
   151
			return -1;
slouken@419
   152
		}
slouken@419
   153
		mikmod.drv_nos =
slouken@419
   154
			(MDRIVER*)
slouken@419
   155
			SDL_LoadFunction(mikmod.handle, "drv_nos");
slouken@419
   156
		if ( mikmod.drv_nos == NULL ) {
slouken@419
   157
			SDL_UnloadObject(mikmod.handle);
slouken@419
   158
			return -1;
slouken@419
   159
		}
slouken@419
   160
		mikmod.md_device =
slouken@419
   161
			(UWORD*)
slouken@419
   162
			SDL_LoadFunction(mikmod.handle, "md_device");
slouken@419
   163
		if ( mikmod.md_device == NULL ) {
slouken@419
   164
			SDL_UnloadObject(mikmod.handle);
slouken@419
   165
			return -1;
slouken@419
   166
		}
slouken@419
   167
		mikmod.md_mixfreq =
slouken@419
   168
			(UWORD*)
slouken@419
   169
			SDL_LoadFunction(mikmod.handle, "md_mixfreq");
slouken@419
   170
		if ( mikmod.md_mixfreq == NULL ) {
slouken@419
   171
			SDL_UnloadObject(mikmod.handle);
slouken@419
   172
			return -1;
slouken@419
   173
		}
slouken@419
   174
		mikmod.md_mode =
slouken@419
   175
			(UWORD*)
slouken@419
   176
			SDL_LoadFunction(mikmod.handle, "md_mode");
slouken@419
   177
		if ( mikmod.md_mode == NULL ) {
slouken@419
   178
			SDL_UnloadObject(mikmod.handle);
slouken@419
   179
			return -1;
slouken@419
   180
		}
slouken@419
   181
		mikmod.md_musicvolume =
slouken@419
   182
			(UBYTE*)
slouken@419
   183
			SDL_LoadFunction(mikmod.handle, "md_musicvolume");
slouken@419
   184
		if ( mikmod.md_musicvolume == NULL ) {
slouken@419
   185
			SDL_UnloadObject(mikmod.handle);
slouken@419
   186
			return -1;
slouken@419
   187
		}
slouken@419
   188
		mikmod.md_pansep =
slouken@419
   189
			(UBYTE*)
slouken@419
   190
			SDL_LoadFunction(mikmod.handle, "md_pansep");
slouken@419
   191
		if ( mikmod.md_pansep == NULL ) {
slouken@419
   192
			SDL_UnloadObject(mikmod.handle);
slouken@419
   193
			return -1;
slouken@419
   194
		}
slouken@419
   195
		mikmod.md_reverb =
slouken@419
   196
			(UBYTE*)
slouken@419
   197
			SDL_LoadFunction(mikmod.handle, "md_reverb");
slouken@419
   198
		if ( mikmod.md_reverb == NULL ) {
slouken@419
   199
			SDL_UnloadObject(mikmod.handle);
slouken@419
   200
			return -1;
slouken@419
   201
		}
slouken@419
   202
		mikmod.md_sndfxvolume =
slouken@419
   203
			(UBYTE*)
slouken@419
   204
			SDL_LoadFunction(mikmod.handle, "md_sndfxvolume");
slouken@419
   205
		if ( mikmod.md_sndfxvolume == NULL ) {
slouken@419
   206
			SDL_UnloadObject(mikmod.handle);
slouken@419
   207
			return -1;
slouken@419
   208
		}
slouken@419
   209
		mikmod.md_volume =
slouken@419
   210
			(UBYTE*)
slouken@419
   211
			SDL_LoadFunction(mikmod.handle, "md_volume");
slouken@419
   212
		if ( mikmod.md_volume == NULL ) {
slouken@419
   213
			SDL_UnloadObject(mikmod.handle);
slouken@419
   214
			return -1;
slouken@419
   215
		}
slouken@419
   216
	}
slouken@419
   217
	++mikmod.loaded;
slouken@419
   218
slouken@419
   219
	return 0;
slouken@419
   220
}
slouken@419
   221
void Mix_QuitMOD()
slouken@419
   222
{
slouken@419
   223
	if ( mikmod.loaded == 0 ) {
slouken@419
   224
		return;
slouken@419
   225
	}
slouken@419
   226
	if ( mikmod.loaded == 1 ) {
slouken@419
   227
		SDL_UnloadObject(mikmod.handle);
slouken@419
   228
	}
slouken@419
   229
	--mikmod.loaded;
slouken@419
   230
}
slouken@419
   231
#else
slouken@419
   232
int Mix_InitMOD()
slouken@419
   233
{
slouken@419
   234
	if ( mikmod.loaded == 0 ) {
slouken@419
   235
		mikmod.MikMod_Exit = MikMod_Exit;
slouken@419
   236
		mikmod.MikMod_InfoDriver = MikMod_InfoDriver;
slouken@419
   237
		mikmod.MikMod_InfoLoader = MikMod_InfoLoader;
slouken@419
   238
		mikmod.MikMod_Init = MikMod_Init;
slouken@419
   239
		mikmod.MikMod_RegisterAllLoaders = MikMod_RegisterAllLoaders;
slouken@419
   240
		mikmod.MikMod_RegisterDriver = MikMod_RegisterDriver;
slouken@419
   241
		mikmod.MikMod_errno = &MikMod_errno;
slouken@419
   242
		mikmod.MikMod_strerror = MikMod_strerror;
slouken@419
   243
		mikmod.Player_Active = Player_Active;
slouken@419
   244
		mikmod.Player_Free = Player_Free;
slouken@419
   245
		mikmod.Player_LoadGeneric = Player_LoadGeneric;
slouken@419
   246
		mikmod.Player_SetPosition = Player_SetPosition;
slouken@419
   247
		mikmod.Player_SetVolume = Player_SetVolume;
slouken@419
   248
		mikmod.Player_Start = Player_Start;
slouken@419
   249
		mikmod.Player_Stop = Player_Stop;
slouken@419
   250
		mikmod.VC_WriteBytes = VC_WriteBytes;
slouken@419
   251
		mikmod.drv_nos = &drv_nos;
slouken@419
   252
		mikmod.md_device = &md_device;
slouken@419
   253
		mikmod.md_mixfreq = &md_mixfreq;
slouken@419
   254
		mikmod.md_mode = &md_mode;
slouken@419
   255
		mikmod.md_musicvolume = &md_musicvolume;
slouken@419
   256
		mikmod.md_pansep = &md_pansep;
slouken@419
   257
		mikmod.md_reverb = &md_reverb;
slouken@419
   258
		mikmod.md_sndfxvolume = &md_sndfxvolume;
slouken@419
   259
		mikmod.md_volume = &md_volume;
slouken@419
   260
	}
slouken@419
   261
	++mikmod.loaded;
slouken@419
   262
slouken@419
   263
	return 0;
slouken@419
   264
}
slouken@419
   265
void Mix_QuitMOD()
slouken@419
   266
{
slouken@419
   267
	if ( mikmod.loaded == 0 ) {
slouken@419
   268
		return;
slouken@419
   269
	}
slouken@419
   270
	if ( mikmod.loaded == 1 ) {
slouken@419
   271
	}
slouken@419
   272
	--mikmod.loaded;
slouken@419
   273
}
slouken@419
   274
#endif /* MOD_DYNAMIC */
slouken@419
   275
slouken@419
   276
#endif /* MOD_MUSIC */