From 20d68b22a39475474f75e643cd1bcac22581d14c Mon Sep 17 00:00:00 2001 From: avolkov Date: Mon, 7 Nov 2005 03:52:58 +0000 Subject: [PATCH] MikMod i/o hacks removal, stage 1 git-svn-id: svn://svn.code.sf.net/p/sc2/code/trunk@1957 8092fc87-c524-0410-9efc-e669fe64eaf9 --- sc2/src/msvc++/UrQuanMasters.dsp | 4 + .../libs/sound/decoders/mikmod/drv_openal.c | 38 +++++++- .../libs/sound/decoders/mikmod/drv_openal.h | 25 +++++ sc2/src/sc2code/libs/sound/decoders/modaud.c | 95 ++++++++++++++++++- 4 files changed, 156 insertions(+), 6 deletions(-) create mode 100644 sc2/src/sc2code/libs/sound/decoders/mikmod/drv_openal.h diff --git a/sc2/src/msvc++/UrQuanMasters.dsp b/sc2/src/msvc++/UrQuanMasters.dsp index f38375120..9b1926b53 100644 --- a/sc2/src/msvc++/UrQuanMasters.dsp +++ b/sc2/src/msvc++/UrQuanMasters.dsp @@ -558,6 +558,10 @@ SOURCE=..\sc2code\libs\sound\decoders\mikmod\drv_nos.c # End Source File # Begin Source File +SOURCE=..\sc2code\libs\sound\decoders\mikmod\drv_openal.h +# End Source File +# Begin Source File + SOURCE=..\sc2code\libs\sound\decoders\mikmod\drv_openal.c # End Source File # Begin Source File diff --git a/sc2/src/sc2code/libs/sound/decoders/mikmod/drv_openal.c b/sc2/src/sc2code/libs/sound/decoders/mikmod/drv_openal.c index b235481eb..01d52574b 100644 --- a/sc2/src/sc2code/libs/sound/decoders/mikmod/drv_openal.c +++ b/sc2/src/sc2code/libs/sound/decoders/mikmod/drv_openal.c @@ -1,7 +1,36 @@ -// OpenAL driver for mikmod +/* + This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of + the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + 02111-1307, USA. +*/ + +// Memory driver for mikmod // based on SDL driver (c) Sam Lantinga -#include "mikmod_internals.h" +#include "mikmod.h" + +static void* buffer; +ULONG bufsize; +ULONG written; + +ULONG* ALDRV_SetOutputBuffer(void* buf, ULONG size) +{ + buffer = buf; + bufsize = size; + written = 0; + return &written; +} static BOOL ALDRV_IsThere(void) @@ -25,7 +54,10 @@ static void ALDRV_Exit(void) static void ALDRV_Update(void) { - /* does nothing, buffers are updated in the background */ + if (!buffer || bufsize == 0) + return; + + written = VC_WriteBytes(buffer, bufsize); } diff --git a/sc2/src/sc2code/libs/sound/decoders/mikmod/drv_openal.h b/sc2/src/sc2code/libs/sound/decoders/mikmod/drv_openal.h new file mode 100644 index 000000000..883b83dd5 --- /dev/null +++ b/sc2/src/sc2code/libs/sound/decoders/mikmod/drv_openal.h @@ -0,0 +1,25 @@ +/* + This library is free software; you can redistribute it and/or modify + it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of + the License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA + 02111-1307, USA. +*/ + +// Memory driver for mikmod +// based on SDL driver (c) Sam Lantinga + +#include "mikmod.h" + +extern MIKMODAPI MDRIVER drv_openal; + +extern ULONG* ALDRV_SetOutputBuffer(void* buf, ULONG size); diff --git a/sc2/src/sc2code/libs/sound/decoders/modaud.c b/sc2/src/sc2code/libs/sound/decoders/modaud.c index d200353ed..5fac89d3b 100644 --- a/sc2/src/sc2code/libs/sound/decoders/modaud.c +++ b/sc2/src/sc2code/libs/sound/decoders/modaud.c @@ -27,6 +27,7 @@ #include "uio.h" #include "decoder.h" #include "mikmod/mikmod.h" +#include "mikmod/drv_openal.h" #include "libs/sound/audiocore.h" #include "modaud.h" @@ -72,9 +73,72 @@ typedef struct tfb_modsounddecoder } TFB_ModSoundDecoder; -MIKMODAPI extern struct MDRIVER drv_openal; static const TFB_DecoderFormats* moda_formats = NULL; +// MikMod READER interface +// we provide our own so that we can do loading via uio +// +typedef struct MUIOREADER +{ + MREADER core; + uio_Stream* file; + +} MUIOREADER; + +static BOOL +moda_uioReader_Eof (MREADER* reader) +{ + return uio_feof (((MUIOREADER*)reader)->file); +} + +static BOOL +moda_uioReader_Read (MREADER* reader, void* ptr, size_t size) +{ + return uio_fread (ptr, size, 1, ((MUIOREADER*)reader)->file); +} + +static int +moda_uioReader_Get (MREADER* reader) +{ + return uio_fgetc (((MUIOREADER*)reader)->file); +} + +static BOOL +moda_uioReader_Seek (MREADER* reader, long offset, int whence) +{ + return uio_fseek (((MUIOREADER*)reader)->file, offset, whence); +} + +static long +moda_uioReader_Tell (MREADER* reader) +{ + return uio_ftell (((MUIOREADER*)reader)->file); +} + +MREADER* +moda_new_uioReader (uio_Stream* fp) +{ + MUIOREADER* reader = (MUIOREADER*) HMalloc (sizeof(MUIOREADER)); + if (reader) + { + reader->core.Eof = &moda_uioReader_Eof; + reader->core.Read = &moda_uioReader_Read; + reader->core.Get = &moda_uioReader_Get; + reader->core.Seek = &moda_uioReader_Seek; + reader->core.Tell = &moda_uioReader_Tell; + reader->file = fp; + } + return (MREADER*)reader; +} + +void +moda_delete_uioReader (MREADER* reader) +{ + if (reader) + HFree (reader); +} + + static const char* moda_GetName (void) { @@ -166,9 +230,30 @@ static bool moda_Open (THIS_PTR, uio_DirHandle *dir, const char *filename) { TFB_ModSoundDecoder* moda = (TFB_ModSoundDecoder*) This; + uio_Stream *fp; + MREADER* reader; MODULE* mod; - mod = Player_Load (dir, (char*) filename, 4, 0); + fp = uio_fopen (dir, filename, "rb"); + if (!fp) + { + moda->last_error = errno; + return false; + } + + reader = moda_new_uioReader (fp); + if (!reader) + { + moda->last_error = -1; + uio_fclose (fp); + return false; + } + + mod = Player_LoadGeneric (reader, 8, 0); + + // can already dispose of reader and fileh + moda_delete_uioReader (reader); + uio_fclose (fp); if (!mod) { fprintf (stderr, "moda_Open(): could not load %s\n", filename); @@ -206,12 +291,16 @@ static int moda_Decode (THIS_PTR, void* buf, sint32 bufsize) { TFB_ModSoundDecoder* moda = (TFB_ModSoundDecoder*) This; + volatile ULONG* poutsize; Player_Start (moda->module); if (!Player_Active()) return 0; - return VC_WriteBytes (buf, bufsize); + poutsize = ALDRV_SetOutputBuffer (buf, bufsize); + MikMod_Update (); + + return *poutsize; } static uint32