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