dynamic_mod.c
author Sam Lantinga <slouken@libsdl.org>
Sat, 02 Jan 2016 22:12:31 -0800
changeset 712 7e59d684b070
parent 711 f40c5ac95b12
child 725 bdf7b8d20566
permissions -rw-r--r--
Updated to version 2.0.1
slouken@419
     1
/*
slouken@518
     2
  SDL_mixer:  An audio mixer library based on the SDL library
slouken@711
     3
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
slouken@419
     4
slouken@518
     5
  This software is provided 'as-is', without any express or implied
slouken@518
     6
  warranty.  In no event will the authors be held liable for any damages
slouken@518
     7
  arising from the use of this software.
slouken@419
     8
slouken@518
     9
  Permission is granted to anyone to use this software for any purpose,
slouken@518
    10
  including commercial applications, and to alter it and redistribute it
slouken@518
    11
  freely, subject to the following restrictions:
slouken@419
    12
slouken@518
    13
  1. The origin of this software must not be misrepresented; you must not
slouken@518
    14
     claim that you wrote the original software. If you use this software
slouken@518
    15
     in a product, an acknowledgment in the product documentation would be
slouken@518
    16
     appreciated but is not required.
slouken@518
    17
  2. Altered source versions must be plainly marked as such, and must not be
slouken@518
    18
     misrepresented as being the original software.
slouken@518
    19
  3. This notice may not be removed or altered from any source distribution.
slouken@419
    20
*/
slouken@419
    21
slouken@419
    22
#ifdef MOD_MUSIC
slouken@419
    23
slouken@419
    24
#include "SDL_loadso.h"
slouken@629
    25
#include "SDL_mixer.h"
slouken@419
    26
#include "dynamic_mod.h"
slouken@419
    27
slouken@419
    28
mikmod_loader mikmod = {
slouken@617
    29
    0, NULL
slouken@419
    30
};
slouken@419
    31
slouken@419
    32
#ifdef MOD_DYNAMIC
slouken@419
    33
int Mix_InitMOD()
slouken@419
    34
{
slouken@617
    35
    if ( mikmod.loaded == 0 ) {
slouken@617
    36
        mikmod.handle = SDL_LoadObject(MOD_DYNAMIC);
slouken@617
    37
        if ( mikmod.handle == NULL ) {
slouken@617
    38
            return -1;
slouken@617
    39
        }
slouken@617
    40
        mikmod.MikMod_Exit =
slouken@617
    41
            (void (*)(void))
slouken@617
    42
            SDL_LoadFunction(mikmod.handle, "MikMod_Exit");
slouken@617
    43
        if ( mikmod.MikMod_Exit == NULL ) {
slouken@617
    44
            SDL_UnloadObject(mikmod.handle);
slouken@617
    45
            return -1;
slouken@617
    46
        }
slouken@617
    47
        mikmod.MikMod_InfoDriver =
slouken@617
    48
            (CHAR* (*)(void))
slouken@617
    49
            SDL_LoadFunction(mikmod.handle, "MikMod_InfoDriver");
slouken@617
    50
        if ( mikmod.MikMod_InfoDriver == NULL ) {
slouken@617
    51
            SDL_UnloadObject(mikmod.handle);
slouken@617
    52
            return -1;
slouken@617
    53
        }
slouken@617
    54
        mikmod.MikMod_InfoLoader =
slouken@617
    55
            (CHAR* (*)(void))
slouken@617
    56
            SDL_LoadFunction(mikmod.handle, "MikMod_InfoLoader");
slouken@617
    57
        if ( mikmod.MikMod_InfoLoader == NULL ) {
slouken@617
    58
            SDL_UnloadObject(mikmod.handle);
slouken@617
    59
            return -1;
slouken@617
    60
        }
slouken@617
    61
        mikmod.MikMod_Init =
slouken@617
    62
            (BOOL (*)(CHAR*))
slouken@617
    63
            SDL_LoadFunction(mikmod.handle, "MikMod_Init");
slouken@617
    64
        if ( mikmod.MikMod_Init == NULL ) {
slouken@617
    65
            SDL_UnloadObject(mikmod.handle);
slouken@617
    66
            return -1;
slouken@617
    67
        }
slouken@617
    68
        mikmod.MikMod_RegisterAllLoaders =
slouken@617
    69
            (void (*)(void))
slouken@617
    70
            SDL_LoadFunction(mikmod.handle, "MikMod_RegisterAllLoaders");
slouken@617
    71
        if ( mikmod.MikMod_RegisterAllLoaders == NULL ) {
slouken@617
    72
            SDL_UnloadObject(mikmod.handle);
slouken@617
    73
            return -1;
slouken@617
    74
        }
slouken@617
    75
        mikmod.MikMod_RegisterDriver =
slouken@617
    76
            (void (*)(struct MDRIVER*))
slouken@617
    77
            SDL_LoadFunction(mikmod.handle, "MikMod_RegisterDriver");
slouken@617
    78
        if ( mikmod.MikMod_RegisterDriver == NULL ) {
slouken@617
    79
            SDL_UnloadObject(mikmod.handle);
slouken@617
    80
            return -1;
slouken@617
    81
        }
slouken@617
    82
        mikmod.MikMod_errno =
slouken@617
    83
            (int*)
slouken@617
    84
            SDL_LoadFunction(mikmod.handle, "MikMod_errno");
slouken@617
    85
        if ( mikmod.MikMod_errno == NULL ) {
slouken@617
    86
            SDL_UnloadObject(mikmod.handle);
slouken@617
    87
            return -1;
slouken@617
    88
        }
slouken@617
    89
        mikmod.MikMod_strerror =
slouken@617
    90
            (char* (*)(int))
slouken@617
    91
            SDL_LoadFunction(mikmod.handle, "MikMod_strerror");
slouken@617
    92
        if ( mikmod.MikMod_strerror == NULL ) {
slouken@617
    93
            SDL_UnloadObject(mikmod.handle);
slouken@617
    94
            return -1;
slouken@617
    95
        }
slouken@617
    96
        mikmod.MikMod_free =
slouken@617
    97
            (void (*)(void*))
slouken@617
    98
            SDL_LoadFunction(mikmod.handle, "MikMod_free");
slouken@617
    99
        if ( mikmod.MikMod_free == NULL ) {
slouken@617
   100
            /* libmikmod 3.1 and earlier doesn't have it */
slouken@617
   101
            mikmod.MikMod_free = free;
slouken@617
   102
        }
slouken@617
   103
        mikmod.Player_Active =
slouken@617
   104
            (BOOL (*)(void))
slouken@617
   105
            SDL_LoadFunction(mikmod.handle, "Player_Active");
slouken@617
   106
        if ( mikmod.Player_Active == NULL ) {
slouken@617
   107
            SDL_UnloadObject(mikmod.handle);
slouken@617
   108
            return -1;
slouken@617
   109
        }
slouken@617
   110
        mikmod.Player_Free =
slouken@617
   111
            (void (*)(MODULE*))
slouken@617
   112
            SDL_LoadFunction(mikmod.handle, "Player_Free");
slouken@617
   113
        if ( mikmod.Player_Free == NULL ) {
slouken@617
   114
            SDL_UnloadObject(mikmod.handle);
slouken@617
   115
            return -1;
slouken@617
   116
        }
slouken@617
   117
        mikmod.Player_LoadGeneric =
slouken@617
   118
            (MODULE* (*)(MREADER*,int,BOOL))
slouken@617
   119
            SDL_LoadFunction(mikmod.handle, "Player_LoadGeneric");
slouken@617
   120
        if ( mikmod.Player_LoadGeneric == NULL ) {
slouken@617
   121
            SDL_UnloadObject(mikmod.handle);
slouken@617
   122
            return -1;
slouken@617
   123
        }
slouken@617
   124
        mikmod.Player_SetPosition =
slouken@617
   125
            (void (*)(UWORD))
slouken@617
   126
            SDL_LoadFunction(mikmod.handle, "Player_SetPosition");
slouken@617
   127
        if ( mikmod.Player_SetPosition == NULL ) {
slouken@617
   128
            SDL_UnloadObject(mikmod.handle);
slouken@617
   129
            return -1;
slouken@617
   130
        }
slouken@617
   131
        mikmod.Player_SetVolume =
slouken@617
   132
            (void (*)(SWORD))
slouken@617
   133
            SDL_LoadFunction(mikmod.handle, "Player_SetVolume");
slouken@617
   134
        if ( mikmod.Player_SetVolume == NULL ) {
slouken@617
   135
            SDL_UnloadObject(mikmod.handle);
slouken@617
   136
            return -1;
slouken@617
   137
        }
slouken@617
   138
        mikmod.Player_Start =
slouken@617
   139
            (void (*)(MODULE*))
slouken@617
   140
            SDL_LoadFunction(mikmod.handle, "Player_Start");
slouken@617
   141
        if ( mikmod.Player_Start == NULL ) {
slouken@617
   142
            SDL_UnloadObject(mikmod.handle);
slouken@617
   143
            return -1;
slouken@617
   144
        }
slouken@617
   145
        mikmod.Player_Stop =
slouken@617
   146
            (void (*)(void))
slouken@617
   147
            SDL_LoadFunction(mikmod.handle, "Player_Stop");
slouken@617
   148
        if ( mikmod.Player_Stop == NULL ) {
slouken@617
   149
            SDL_UnloadObject(mikmod.handle);
slouken@617
   150
            return -1;
slouken@617
   151
        }
slouken@617
   152
        mikmod.VC_WriteBytes =
slouken@617
   153
            (ULONG (*)(SBYTE*,ULONG))
slouken@617
   154
            SDL_LoadFunction(mikmod.handle, "VC_WriteBytes");
slouken@617
   155
        if ( mikmod.VC_WriteBytes == NULL ) {
slouken@617
   156
            SDL_UnloadObject(mikmod.handle);
slouken@617
   157
            return -1;
slouken@617
   158
        }
slouken@617
   159
        mikmod.drv_nos =
slouken@617
   160
            (MDRIVER*)
slouken@617
   161
            SDL_LoadFunction(mikmod.handle, "drv_nos");
slouken@617
   162
        if ( mikmod.drv_nos == NULL ) {
slouken@617
   163
            SDL_UnloadObject(mikmod.handle);
slouken@617
   164
            return -1;
slouken@617
   165
        }
slouken@617
   166
        mikmod.md_device =
slouken@617
   167
            (UWORD*)
slouken@617
   168
            SDL_LoadFunction(mikmod.handle, "md_device");
slouken@617
   169
        if ( mikmod.md_device == NULL ) {
slouken@617
   170
            SDL_UnloadObject(mikmod.handle);
slouken@617
   171
            return -1;
slouken@617
   172
        }
slouken@617
   173
        mikmod.md_mixfreq =
slouken@617
   174
            (UWORD*)
slouken@617
   175
            SDL_LoadFunction(mikmod.handle, "md_mixfreq");
slouken@617
   176
        if ( mikmod.md_mixfreq == NULL ) {
slouken@617
   177
            SDL_UnloadObject(mikmod.handle);
slouken@617
   178
            return -1;
slouken@617
   179
        }
slouken@617
   180
        mikmod.md_mode =
slouken@617
   181
            (UWORD*)
slouken@617
   182
            SDL_LoadFunction(mikmod.handle, "md_mode");
slouken@617
   183
        if ( mikmod.md_mode == NULL ) {
slouken@617
   184
            SDL_UnloadObject(mikmod.handle);
slouken@617
   185
            return -1;
slouken@617
   186
        }
slouken@617
   187
        mikmod.md_musicvolume =
slouken@617
   188
            (UBYTE*)
slouken@617
   189
            SDL_LoadFunction(mikmod.handle, "md_musicvolume");
slouken@617
   190
        if ( mikmod.md_musicvolume == NULL ) {
slouken@617
   191
            SDL_UnloadObject(mikmod.handle);
slouken@617
   192
            return -1;
slouken@617
   193
        }
slouken@617
   194
        mikmod.md_pansep =
slouken@617
   195
            (UBYTE*)
slouken@617
   196
            SDL_LoadFunction(mikmod.handle, "md_pansep");
slouken@617
   197
        if ( mikmod.md_pansep == NULL ) {
slouken@617
   198
            SDL_UnloadObject(mikmod.handle);
slouken@617
   199
            return -1;
slouken@617
   200
        }
slouken@617
   201
        mikmod.md_reverb =
slouken@617
   202
            (UBYTE*)
slouken@617
   203
            SDL_LoadFunction(mikmod.handle, "md_reverb");
slouken@617
   204
        if ( mikmod.md_reverb == NULL ) {
slouken@617
   205
            SDL_UnloadObject(mikmod.handle);
slouken@617
   206
            return -1;
slouken@617
   207
        }
slouken@617
   208
        mikmod.md_sndfxvolume =
slouken@617
   209
            (UBYTE*)
slouken@617
   210
            SDL_LoadFunction(mikmod.handle, "md_sndfxvolume");
slouken@617
   211
        if ( mikmod.md_sndfxvolume == NULL ) {
slouken@617
   212
            SDL_UnloadObject(mikmod.handle);
slouken@617
   213
            return -1;
slouken@617
   214
        }
slouken@617
   215
        mikmod.md_volume =
slouken@617
   216
            (UBYTE*)
slouken@617
   217
            SDL_LoadFunction(mikmod.handle, "md_volume");
slouken@617
   218
        if ( mikmod.md_volume == NULL ) {
slouken@617
   219
            SDL_UnloadObject(mikmod.handle);
slouken@617
   220
            return -1;
slouken@617
   221
        }
slouken@617
   222
    }
slouken@617
   223
    ++mikmod.loaded;
slouken@419
   224
slouken@617
   225
    return 0;
slouken@419
   226
}
slouken@419
   227
void Mix_QuitMOD()
slouken@419
   228
{
slouken@617
   229
    if ( mikmod.loaded == 0 ) {
slouken@617
   230
        return;
slouken@617
   231
    }
slouken@617
   232
    if ( mikmod.loaded == 1 ) {
slouken@617
   233
        SDL_UnloadObject(mikmod.handle);
slouken@617
   234
    }
slouken@617
   235
    --mikmod.loaded;
slouken@419
   236
}
slouken@419
   237
#else
slouken@419
   238
int Mix_InitMOD()
slouken@419
   239
{
slouken@617
   240
    if ( mikmod.loaded == 0 ) {
slouken@629
   241
#ifdef __MACOSX__
slouken@629
   242
        extern void Player_Start(MODULE*) __attribute__((weak_import));
slouken@629
   243
        if ( Player_Start == NULL )
slouken@629
   244
        {
slouken@629
   245
            /* Missing weakly linked framework */
slouken@629
   246
            Mix_SetError("Missing mikmod.framework");
slouken@629
   247
            return -1;
slouken@629
   248
        }
slouken@629
   249
#endif // __MACOSX__
slouken@629
   250
slouken@617
   251
        mikmod.MikMod_Exit = MikMod_Exit;
slouken@617
   252
        mikmod.MikMod_InfoDriver = MikMod_InfoDriver;
slouken@617
   253
        mikmod.MikMod_InfoLoader = MikMod_InfoLoader;
slouken@617
   254
        mikmod.MikMod_Init = MikMod_Init;
slouken@617
   255
        mikmod.MikMod_RegisterAllLoaders = MikMod_RegisterAllLoaders;
slouken@617
   256
        mikmod.MikMod_RegisterDriver = MikMod_RegisterDriver;
slouken@617
   257
        mikmod.MikMod_errno = &MikMod_errno;
slouken@617
   258
        mikmod.MikMod_strerror = MikMod_strerror;
slouken@588
   259
#if LIBMIKMOD_VERSION < ((3<<16)|(2<<8))
slouken@617
   260
        mikmod.MikMod_free = free;
slouken@588
   261
#else
slouken@617
   262
        mikmod.MikMod_free = MikMod_free;
slouken@588
   263
#endif
slouken@617
   264
        mikmod.Player_Active = Player_Active;
slouken@617
   265
        mikmod.Player_Free = Player_Free;
slouken@617
   266
        mikmod.Player_LoadGeneric = Player_LoadGeneric;
slouken@617
   267
        mikmod.Player_SetPosition = Player_SetPosition;
slouken@617
   268
        mikmod.Player_SetVolume = Player_SetVolume;
slouken@617
   269
        mikmod.Player_Start = Player_Start;
slouken@617
   270
        mikmod.Player_Stop = Player_Stop;
slouken@617
   271
        mikmod.VC_WriteBytes = VC_WriteBytes;
slouken@617
   272
        mikmod.drv_nos = &drv_nos;
slouken@617
   273
        mikmod.md_device = &md_device;
slouken@617
   274
        mikmod.md_mixfreq = &md_mixfreq;
slouken@617
   275
        mikmod.md_mode = &md_mode;
slouken@617
   276
        mikmod.md_musicvolume = &md_musicvolume;
slouken@617
   277
        mikmod.md_pansep = &md_pansep;
slouken@617
   278
        mikmod.md_reverb = &md_reverb;
slouken@617
   279
        mikmod.md_sndfxvolume = &md_sndfxvolume;
slouken@617
   280
        mikmod.md_volume = &md_volume;
slouken@617
   281
    }
slouken@617
   282
    ++mikmod.loaded;
slouken@419
   283
slouken@617
   284
    return 0;
slouken@419
   285
}
slouken@419
   286
void Mix_QuitMOD()
slouken@419
   287
{
slouken@617
   288
    if ( mikmod.loaded == 0 ) {
slouken@617
   289
        return;
slouken@617
   290
    }
slouken@617
   291
    if ( mikmod.loaded == 1 ) {
slouken@617
   292
    }
slouken@617
   293
    --mikmod.loaded;
slouken@419
   294
}
slouken@419
   295
#endif /* MOD_DYNAMIC */
slouken@419
   296
slouken@419
   297
#endif /* MOD_MUSIC */