diff --git a/include/textscreen/include/txt_fileselect.c b/include/textscreen/include/txt_fileselect.c index d9c754f..b273edc 100644 --- a/include/textscreen/include/txt_fileselect.c +++ b/include/textscreen/include/txt_fileselect.c @@ -603,7 +603,7 @@ static char *ExpandExtension(const char *orig) c = newext; for (i = 0; i < oldlen; ++i) { - if (isalpha(orig[i])) + if (isalpha((int)orig[i])) { *c++ = '['; *c++ = tolower(orig[i]); diff --git a/include/textscreen/include/txt_gui.c b/include/textscreen/include/txt_gui.c index d7f4362..ed4f84e 100644 --- a/include/textscreen/include/txt_gui.c +++ b/include/textscreen/include/txt_gui.c @@ -204,7 +204,6 @@ void TXT_DrawSpecialSeparator(int x, int y, int w, int h, int sepcolor, int cust int bx; int x1; - int b; TXT_SaveColors(&colors); diff --git a/include/textscreen/include/txt_window.c b/include/textscreen/include/txt_window.c index 9cfaab4..c1cd6fd 100644 --- a/include/textscreen/include/txt_window.c +++ b/include/textscreen/include/txt_window.c @@ -625,12 +625,11 @@ void TXT_OpenURL(const char *url) #endif retval = system(cmd); - free(cmd); if (retval != 0) { - fprintf(stderr, "TXT_OpenURL: error executing '%s'; return code %d\n", - cmd, retval); + fprintf(stderr, "TXT_OpenURL: error executing '%s'; return code %d\n", cmd, retval); } + free(cmd); } #endif /* #ifndef _WIN32 */ diff --git a/src/demo.cpp b/src/demo.cpp index 51177a2..ba60d7e 100644 --- a/src/demo.cpp +++ b/src/demo.cpp @@ -184,9 +184,7 @@ DEMO_LoadFile( void ) { - int filesize; - - filesize = GLB_ReadFile(demo_name, 0); + GLB_ReadFile(demo_name, 0); GLB_ReadFile(demo_name, (char*)playback); cur_play = 1; diff --git a/src/fileids.h b/src/fileids.h index 04f0474..b649346 100644 --- a/src/fileids.h +++ b/src/fileids.h @@ -1,7 +1,7 @@ #pragma once +#define FILE_NULL UINT32_MAX //FILE0000.GLB Items - #define FILE000_ATENTION_TXT 0x00000 #define FILE001_LASTSCR1_TXT 0x00001 #define FILE002_LASTSCR2_TXT 0x00002 @@ -1730,4 +1730,4 @@ #define FILE4d3_EBOSS11_PIC 0x400d3 #define FILE4d4_STARTG4TILES 0x400d4 #define FILE415_ENDG4TILES 0x40615 -#define FILE416_BOGUS4 0x40616 +#define FILE416_BOGUS4 0x40616 \ No newline at end of file diff --git a/src/fx.cpp b/src/fx.cpp index 753f9c2..8824cbe 100644 --- a/src/fx.cpp +++ b/src/fx.cpp @@ -116,7 +116,6 @@ SND_InitSound( dig_flag = 0; fx_device = SND_NONE; - music_volume = INI_GetPreferenceLong("Music", "Volume", 127); music_card = INI_GetPreferenceLong("Music", "CardType", M_NONE); sys_midi = INI_GetPreferenceLong("Setup", "sys_midi", 0); @@ -125,6 +124,10 @@ SND_InitSound( core_midi_port = INI_GetPreferenceLong("Setup", "core_midi_port", 0); alsaclient = INI_GetPreferenceLong("Setup", "alsa_output_client", 128); alsaport = INI_GetPreferenceLong("Setup", "alsa_output_port", 0); + music_samplesperloop = INI_GetPreferenceLong("Music", "SamplesPerLoop", 16); + + if(music_samplesperloop < 1 || music_samplesperloop > spec.samples) + music_samplesperloop = 16; switch (music_card) { @@ -206,7 +209,10 @@ SND_InitSound( fx_channels = 1; if (fx_card == M_ADLIB || fx_card == M_WAVE || fx_card == M_CANVAS || fx_card == M_GMIDI) + { + fx_gus = 1; GSS_Init(fx_card, 0); + } SDL_PauseAudioDevice(fx_dev, 0); diff --git a/src/gfxapi.cpp b/src/gfxapi.cpp index 2bec0e7..1424d81 100644 --- a/src/gfxapi.cpp +++ b/src/gfxapi.cpp @@ -639,7 +639,7 @@ GFX_ShadeArea( ) { int loop; - char *buf, *cur_table; + char *buf, *cur_table = ltable; if (!GFX_ClipLines(NULL, &x, &y, &lx, &ly)) return; @@ -682,7 +682,7 @@ GFX_ShadeShape( GFX_PIC* h = (GFX_PIC*)inmem; GFX_SPRITE* ah; char rval; - char *cur_table; + char *cur_table = ltable; char *dest; int ox = x; int oy = y; @@ -753,7 +753,7 @@ GFX_VShadeLine( int ly // INPUT : length of line ) { - char *cur_table; + char *cur_table = ltable; char *outbuf; int lx = 1; @@ -798,7 +798,7 @@ GFX_HShadeLine( int lx // INPUT : length of line ) { - char *cur_table; + char *cur_table = ltable; char *outbuf; int ly = 1; @@ -1522,7 +1522,7 @@ GFX_StrPixelLen( for (loop = 0; loop < maxloop; loop++) { - outlen += infont->width[instr[loop]] + fontspacing; + outlen += infont->width[(int)instr[loop]] + fontspacing; } return outlen; diff --git a/src/glbapi.cpp b/src/glbapi.cpp index 45b00f3..12be3b7 100644 --- a/src/glbapi.cpp +++ b/src/glbapi.cpp @@ -8,6 +8,7 @@ #include "common.h" #include "glbapi.h" #include "vmemapi.h" +#include "fileids.h" #ifdef _WIN32 #include @@ -35,10 +36,16 @@ char* strupr(char* s) static const char* serial = "32768GLB"; static char exePath[PATH_MAX]; static int num_glbs; -static KEYFILE g_key; static char prefix[5] = "FILE"; static bool fVmem = 0; +//This is the list of loctions to look for files in. +static const char *lookNdirs[] = { + "", + exePath, + "soundfonts/", + NULL +}; /* * define file descriptor used to access file. */ @@ -153,8 +160,7 @@ GLB_FindFile( const char *permissions // INPUT : file access permissions ) { - const char* routine = "GLB_FindFile"; - char filename[PATH_MAX]; + char *filename; FILE *handle; FILEDESC* fd; @@ -173,22 +179,22 @@ GLB_FindFile( * create a file name and attempt to open it local first, then if it * fails use the exe path and try again. */ - sprintf(filename, "%s%04u.GLB", prefix, filenum); - if ((handle = fopen(filename, permissions)) == NULL) - { - sprintf(filename, "%s%s%04u.GLB", exePath, prefix, filenum); - - if ((handle = fopen(filename, permissions)) == NULL) - { - if (return_on_failure) - return NULL; - - sprintf(filename, "%s%04u.GLB", prefix, filenum); - EXIT_Error("GLB_FindFile: %s, Error #%d,%s", - filename, errno, strerror(errno)); + char *name = (char*)malloc((strlen(prefix)+9) * sizeof(char)); + sprintf(name, "%s%04u.GLB", prefix, filenum); + filename = GLB_FindFilePath(name); + + if(filename == NULL){ + if (return_on_failure){ + free(name); + return NULL; } + + EXIT_Error("GLB_FindFile: %s, Error #%d,%s", name, errno, strerror(errno)); } - + free(name); + + handle = fopen(filename, permissions); + /* * Keep file handle */ @@ -198,9 +204,12 @@ GLB_FindFile( fd->permissions = permissions; fd->handle = handle; + free(filename); + return handle; } + /*------------------------------------------------------------------------ GLB_OpenFile() - Opens & Caches file handle ------------------------------------------------------------------------*/ @@ -243,7 +252,7 @@ GLB_OpenFile( /*------------------------------------------------------------------------ GLB_CloseFiles() - Closes all cached files. ------------------------------------------------------------------------*/ -static void +void GLB_CloseFiles( void ) @@ -281,9 +290,8 @@ GLB_NumItems( fseek(handle, 0L, SEEK_SET); if (!fread(&key, sizeof(KEYFILE), 1, handle)) - { EXIT_Error("GLB_NumItems: Read failed!"); - } + #ifdef _SCOTTGAME GLB_DeCrypt(serial, (uint8_t*)&key, sizeof(KEYFILE)); @@ -316,10 +324,11 @@ GLB_LoadIDT( { k = fd->items - j; - if (k > ASIZE(key)) + if (k > (int)ASIZE(key)) k = ASIZE(key); - fread(key, sizeof(KEYFILE), k, handle); + if (!fread(key, sizeof(KEYFILE), k, handle)) + EXIT_Error("GLB_NumItems: Read failed!"); for (n = 0; n < k; n++) { @@ -429,7 +438,6 @@ GLB_Load( { FILE *handle; ITEMINFO* ii; - ASSERT(filenum >= 0 && filenum < num_glbs); handle = filedesc[filenum].handle; @@ -449,7 +457,9 @@ GLB_Load( else { fseek(handle, ii->offset, SEEK_SET); - fread(inmem, ii->size, 1, handle); + if(!fread(inmem, ii->size, 1, handle)) + EXIT_Error("GLB_Load: Failed to read data\n"); + #ifdef _SCOTTGAME if (ii->flags & ITF_ENCODED) { @@ -475,7 +485,7 @@ GLB_FetchItem( ITEM_H itm; ITEMINFO* ii; - if (handle == ~0) + if (handle == FILE_NULL) { EXIT_Error("GLB_FetchItem: empty handle."); return NULL; @@ -544,7 +554,7 @@ GLB_FetchItem( ***************************************************************************/ char* GLB_CacheItem( - int handle + uint32_t handle ) { return GLB_FetchItem(handle, FI_CACHE); @@ -555,7 +565,7 @@ GLB_CacheItem( ***************************************************************************/ char* GLB_GetItem( - int handle // INPUT : handle of item + uint32_t handle // INPUT : handle of item ) { return GLB_FetchItem(handle, FI_DISCARD); @@ -566,7 +576,7 @@ GLB_GetItem( ***************************************************************************/ char* GLB_LockItem( - int handle + uint32_t handle ) { return GLB_FetchItem(handle, FI_LOCK); @@ -577,13 +587,13 @@ GLB_LockItem( ***************************************************************************/ void GLB_UnlockItem( - int handle + uint32_t handle ) { ITEM_H itm; ITEMINFO* ii; - if (handle == ~0) + if (handle == FILE_NULL) return; itm.handle = handle; @@ -617,13 +627,13 @@ GLB_UnlockItem( ***************************************************************************/ int // RETURN: TRUE = Label GLB_IsLabel( - int handle // INPUT : handle of item + uint32_t handle // INPUT : handle of item ) { ITEM_H itm; ITEMINFO* ii; - if (handle == ~0) + if (handle == FILE_NULL) return 0; itm.handle = handle; @@ -642,14 +652,14 @@ GLB_IsLabel( ***************************************************************************/ void GLB_ReadItem( - int handle, // INPUT : handle of item + uint32_t handle, // INPUT : handle of item char* mem // INPUT : pointer to memory ) { ITEM_H itm; ITEMINFO* ii; - if (handle == ~0) + if (handle == FILE_NULL) return; ASSERT(mem != NULL); @@ -684,7 +694,7 @@ GLB_GetItemID( ASSERT(in_name != NULL); - itm.handle = ~0; + itm.handle = FILE_NULL; if (*in_name != ' ' && *in_name != '\0') { for (filenum = 0; filenum < num_glbs; filenum++) @@ -714,13 +724,13 @@ GLB_GetItemID( ***************************************************************************/ char* // RETURN: pointer to item GLB_GetPtr( - int handle // INPUT : handle of item + uint32_t handle // INPUT : handle of item ) { ITEM_H itm; ITEMINFO* ii; - if (handle == ~0) + if (handle == FILE_NULL) return NULL; itm.handle = handle; @@ -740,13 +750,13 @@ GLB_GetPtr( ***************************************************************************/ void GLB_FreeItem( - int handle // INPUT : handle of item + uint32_t handle // INPUT : handle of item ) { ITEM_H itm; ITEMINFO* ii; - if (handle == ~0) + if (handle == FILE_NULL) return; itm.handle = handle; @@ -813,13 +823,13 @@ GLB_FreeAll( ***************************************************************************/ int // RETURN: sizeof ITEM GLB_ItemSize( - int handle // INPUT : handle of item + uint32_t handle // INPUT : handle of item ) { ITEM_H itm; ITEMINFO* ii; - if (handle == ~0) + if (handle == FILE_NULL) return 0; itm.handle = handle; @@ -899,4 +909,43 @@ GLB_SaveFile( } fclose(handle); +} + +/*************************************************************************** + * GLB_FindFilePath() - Finds the path to a filename by searching in multiple directories. + * + * This function iterates through the 'lookNdirs' array and tries to open each file path. + * The first path that is successfully opened is returned as a dynamically allocated string. + ***************************************************************************/ +char * +GLB_FindFilePath( + char *file +) +{ + char filename[PATH_MAX]; + + FILE *handle; + + int lookat = 0; + + while(lookNdirs[lookat] != NULL){ + sprintf(filename, "%s%s", lookNdirs[lookat], file); + lookat++; + if ((handle = fopen(filename, "r")) == NULL) + { + if(lookNdirs[lookat] == NULL) + return NULL; + + } else { + fclose(handle); + break; + } + } + + char * retChar = (char*)malloc(strlen(filename)+1 * sizeof(char)); + strcpy(retChar, filename); + + printf("Found: %s\n", filename); + + return retChar; } \ No newline at end of file diff --git a/src/glbapi.h b/src/glbapi.h index eea35ab..4c8e7e1 100644 --- a/src/glbapi.h +++ b/src/glbapi.h @@ -23,21 +23,22 @@ typedef struct void GLB_UseVM(void); int GLB_InitSystem(const char *exepath, int innum, const char *iprefix); -char *GLB_LockItem(int handle); -void GLB_UnlockItem(int handle); -char *GLB_GetItem(int handle); -char *GLB_CacheItem(int handle); -void GLB_FreeItem(int handle); +char *GLB_LockItem(uint32_t handle); +void GLB_UnlockItem(uint32_t handle); +char *GLB_GetItem(uint32_t handle); +char *GLB_CacheItem(uint32_t handle); +void GLB_FreeItem(uint32_t handle); void GLB_FreeAll(void); int GLB_GetItemID(const char *in_name); -int GLB_ItemSize(int handle); +int GLB_ItemSize(uint32_t handle); void GLB_EnCrypt(const char *key, void *buf, int length); void GLB_DeCrypt(const char *key, void *buf, int length); int GLB_ReadFile(const char *name, char *buffer); void GLB_SaveFile(char *name, char *buffer, int length); int GLB_Load(char *inmem, int filenum, int itemnum); -int GLB_IsLabel(int handle); -void GLB_ReadItem(int handle, char *mem); -//char *GLB_GetPtr(int handle); +int GLB_IsLabel(uint32_t handle); +void GLB_ReadItem(uint32_t handle, char *mem); +char * GLB_FindFilePath(char *file); +//char *GLB_GetPtr(uint32_t handle); //void GLB_SetItemPointer(int a1, char* a2); //void GLB_SetItemSize(int a1, int a2); \ No newline at end of file diff --git a/src/gssapi.cpp b/src/gssapi.cpp index 596917f..b921a0a 100644 --- a/src/gssapi.cpp +++ b/src/gssapi.cpp @@ -25,37 +25,50 @@ int gss_type; int gss_lastnote, gss_lastsmp; /*************************************************************************** -GSS_Init () - +GSS_Init () - ***************************************************************************/ -void -GSS_Init( - int device, - int option -) +void GSS_Init( + int device, + int option) { if (gss_init) return; - + switch (device) { case M_NONE: gss_device = NULL; break; - + case M_ADLIB: case M_PAS: case M_SB: gss_device = &mus_device_opl; break; - + case M_WAVE: case M_CANVAS: case M_GMIDI: default: - - #ifdef _WIN32 - gss_device = &mus_device_winmm; - #endif // _WIN32 + if (sys_midi) + { + #ifdef _WIN32 + gss_device = &mus_device_winmm; + #endif // _WIN32 + + #ifdef __linux__ + gss_device = &mus_device_alsa; + #endif // __linux__ + + #ifdef __APPLE__ + if (core_dls_synth) + gss_device = &mus_device_corea; + else + gss_device = &mus_device_corem; + #endif // __APPLE__ + } + else + gss_device = &mus_device_tsf; break; } @@ -75,17 +88,15 @@ GSS_Init( /*************************************************************************** GSS_DeInit () - ***************************************************************************/ -void -GSS_DeInit( - void -) +void GSS_DeInit( + void) { if (!gss_init) return; if (gss_device && gss_device->DeInit) gss_device->DeInit(); - + gsshack = 0; gss_device = NULL; gss_init = 0; @@ -94,21 +105,19 @@ GSS_DeInit( /*************************************************************************** GSS_Service () - ***************************************************************************/ -void -GSS_Service( - void -) +void GSS_Service( + void) { if (!gss_init) return; - + switch (gss_type) { case 1: { - gss1_t *gss = (gss1_t*)gss_ptr; + gss1_t *gss = (gss1_t *)gss_ptr; int smp, note, bend; - + if (gss_currentptr == 0) { // setup playback @@ -116,7 +125,7 @@ GSS_Service( { if (gss_device && gss_device->KeyOffEvent) gss_device->KeyOffEvent(14, gss_lastnote); - + gss_lastnote = 0; } if (gss_device && gss_device->ControllerEvent) @@ -134,7 +143,7 @@ GSS_Service( { if (gss_device && gss_device->KeyOffEvent) gss_device->KeyOffEvent(14, gss_lastnote); - + gss_lastnote = 0; } gss_type = 0; @@ -142,32 +151,32 @@ GSS_Service( break; } smp = gss->data[gss_currentptr++]; - + if (gss_lastsmp != smp) { if (smp == 0 && gss_lastnote) { if (gss_device && gss_device->KeyOffEvent) gss_device->KeyOffEvent(14, gss_lastnote); - + gss_lastnote = 0; } else { note = 29 + ((smp - 1) >> 1); bend = (smp & 1) ? 127 : 159; - + if (note != gss_lastnote) { if (gss_device && gss_device->KeyOffEvent) gss_device->KeyOffEvent(14, gss_lastnote); - + if (gss_device && gss_device->PitchBendEvent) gss_device->PitchBendEvent(14, bend); - + if (gss_device && gss_device->KeyOnEvent) gss_device->KeyOnEvent(14, note, gss_volume); - + gss_lastnote = note; } else @@ -186,14 +195,14 @@ GSS_Service( /*************************************************************************** GSS_Mix () - ***************************************************************************/ -void -GSS_Mix( - int16_t *stream, - int len -) +void GSS_Mix( + int16_t *stream, + int len) { + int32_t L, R; int i; - + int16_t GSSsample[2]; + if (!gss_init) return; @@ -202,9 +211,25 @@ GSS_Mix( for (i = 0; i < len; i++) { - gss_device->Mix(stream, 1); + gss_device->Mix(GSSsample, 1); gss_cnt += gssrate; - + + L = (GSSsample[0] + stream[0]) >> 2; + R = (GSSsample[1] + stream[1]) >> 2; + + if (L < INT16_MIN) + L = INT16_MIN; + else if (L > INT16_MAX) + L = INT16_MAX; + + if (R < INT16_MIN) + R = INT16_MIN; + else if (R > INT16_MAX) + R = INT16_MAX; + + stream[0] = L; + stream[1] = R; + while (gss_cnt >= fx_freq) { gss_cnt -= fx_freq; @@ -217,20 +242,18 @@ GSS_Mix( /*************************************************************************** GSS_Poll () - ***************************************************************************/ -void -GSS_Poll( - void -) +void GSS_Poll( + void) { if (!gss_init || !gss_device || gss_device->Mix) return; int now = SDL_GetTicks(); - + while (gss_timer < now) { gss_cnt += gssrate; - + if (gss_cnt >= 1000) { gss_cnt -= 1000; @@ -243,18 +266,16 @@ GSS_Poll( /*************************************************************************** GSS_PlayPatch () - ***************************************************************************/ -int -GSS_PlayPatch( - void *gss, - int sep, - int pitch, - int volume, - int priority -) +int GSS_PlayPatch( + void *gss, + int sep, + int pitch, + int volume, + int priority) { - int format = *(int16_t*)gss; + int format = *(int16_t *)gss; int handle = (gss_handcnt++) & FXHAND_MASK; - + if (format != 1 && format != 2) return -1; @@ -268,14 +289,14 @@ GSS_PlayPatch( if (gss_type == 0) gss_lastnote = 0; - + gss_type = 0; gss_ptr = 0; gss_priority = priority; gss_sep = sep; gss_volume = volume; - + switch (format) { case 1: @@ -286,7 +307,7 @@ GSS_PlayPatch( gss_type = 1; break; } - + case 2: gss_currentptr = 0; gss_ptr = gss; @@ -296,32 +317,30 @@ GSS_PlayPatch( gss_handle = handle; SND_Unlock(); - + return handle | FXHAND_GSS1; } /*************************************************************************** GSS_StopPatch () - ***************************************************************************/ -void -GSS_StopPatch( - int handle -) +void GSS_StopPatch( + int handle) { if (!gss_init) return; - + handle &= FXHAND_MASK; - + SND_Lock(); - + if (handle == gss_handle) { switch (gss_type) { case 1: { - gss1_t *gss = (gss1_t*)gss_ptr; + gss1_t *gss = (gss1_t *)gss_ptr; gss_currentptr = gss->len; } } @@ -332,24 +351,22 @@ GSS_StopPatch( /*************************************************************************** GSS_PatchIsPlaying () - ***************************************************************************/ -int -GSS_PatchIsPlaying( - int handle -) +int GSS_PatchIsPlaying( + int handle) { int stat = 0; - + if (!gss_init) return 0; - + handle &= FXHAND_MASK; - + SND_Lock(); - + if (handle == gss_handle && gss_type != 0) stat = 1; - + SND_Unlock(); - + return stat; } diff --git a/src/i_oplmusic.cpp b/src/i_oplmusic.cpp index 6d731ec..99804f6 100644 --- a/src/i_oplmusic.cpp +++ b/src/i_oplmusic.cpp @@ -1369,5 +1369,5 @@ musdevice_t mus_device_opl = { ControllerEvent, PitchBendEvent, ProgramChgEvent, - AllOffEvent, + AllOffEvent }; diff --git a/src/loadsave.cpp b/src/loadsave.cpp index 2146985..9878b61 100644 --- a/src/loadsave.cpp +++ b/src/loadsave.cpp @@ -36,7 +36,7 @@ #define MAX_SAVE 10 -char cdpath[PATH_MAX]; +char cdpath[PATH_MAX>>2]; char g_setup_ini[PATH_MAX]; int cdflag = 0; @@ -46,8 +46,8 @@ int map_item = -1; int curplr_diff = 2; int srwpos = 0; -static const char *fmt = "CHAR%04u.FIL"; -static const char *cdfmt = "%sCHAR%04u.FIL"; +#define FMT "CHAR%04u.FIL" +#define CDFMT "%sCHAR%04u.FIL" MAZELEVEL *mapmem; CSPRITE *csprite; @@ -369,9 +369,9 @@ RAP_AreSavedFiles( for (loop = 0; loop < MAX_SAVE; loop++) { if (cdflag) - sprintf(temp, cdfmt, cdpath, loop); + snprintf(temp, sizeof(temp), CDFMT, cdpath, loop); else - sprintf(temp, fmt, loop); + snprintf(temp, sizeof(temp), FMT, loop); if (!access(temp, 0)) return 1; @@ -392,7 +392,8 @@ RAP_ReadFile( { FILE *handle; handle = fopen(name, "rb"); - + size_t flen; + if (!handle) { WIN_Msg("File open Error"); @@ -400,16 +401,16 @@ RAP_ReadFile( } savebuffer = (char*)malloc(sizerec); - fread(savebuffer, 1, sizerec, handle); + flen = fread(savebuffer, 1, sizerec, handle); - GLB_DeCrypt(gdmodestr, savebuffer, sizerec); + GLB_DeCrypt(gdmodestr, savebuffer, flen); ReadPlayer((PLAYEROBJ*)buffer); fclose(handle); free(savebuffer); SaveResetReadWritePosition(); - return sizerec; + return flen; } /*************************************************************************** @@ -429,9 +430,9 @@ RAP_FFSaveFile( for (loop = 0; loop < MAX_SAVE; loop++) { if (cdflag) - sprintf(temp, cdfmt, cdpath, loop); + snprintf(temp, sizeof(temp), CDFMT, cdpath, loop); else - sprintf(temp, fmt, loop); + snprintf(temp, sizeof(temp), FMT, loop); if (access(temp, 0) != 0) { @@ -457,22 +458,28 @@ RAP_IsSaveFile( char temp[PATH_MAX]; int rval, loop; FILE *handle; + size_t flen; + rval = 0; for (loop = 0; loop < MAX_SAVE; loop++) { if (cdflag) - sprintf(temp, cdfmt, cdpath, loop); + snprintf(temp, sizeof(temp), CDFMT, cdpath, loop); else - sprintf(temp, fmt, loop); + snprintf(temp, sizeof(temp), FMT, loop); handle = fopen(temp, "rb"); if (handle) { savebuffer = (char*)malloc(sizeof(tp)); - fread(savebuffer, 1, sizeof(tp), handle); + flen = fread(savebuffer, 1, sizeof(tp), handle); + if(flen != sizeof(tp)) + { + EXIT_Error("Error reading save file.\n"); + } //GLB_DeCrypt(gdmodestr, savebuffer, sizeof(tp)); //missing in v1.2 ReadPlayer(&tp); @@ -505,6 +512,7 @@ RAP_LoadPlayer( char *dchrobj; int size; int rval, loop; + size_t flen; FILE *handle; OBJ inobj; @@ -518,9 +526,9 @@ RAP_LoadPlayer( memset(&plr, 0, sizeof(plr)); if (cdflag) - sprintf(filename, cdfmt, cdpath, filepos); + snprintf(filename, sizeof(filename), CDFMT, cdpath, filepos); else - sprintf(filename, fmt, filepos); + snprintf(filename, sizeof(filename), FMT, filepos); handle = fopen(filename, "rb"); @@ -538,13 +546,22 @@ RAP_LoadPlayer( savebuffer = (char*)malloc(size); dchrobj = (char*)malloc(size - sizeof(plr)); - fread(dchrplr, 1, sizeof(plr), handle); + flen = fread(dchrplr, 1, sizeof(plr), handle); + if(flen != sizeof(plr)) + { + EXIT_Error("Error reading save file.\n"); + } fseek(handle, sizeof(plr), SEEK_SET); GLB_DeCrypt(gdmodestr, dchrplr, sizeof(plr)); memcpy(savebuffer, dchrplr, sizeof(plr)); - fread(dchrobj, 1, size - sizeof(plr), handle); + flen = fread(dchrobj, 1, size - sizeof(plr), handle); + + if(flen != (size - sizeof(plr))) + { + EXIT_Error("Error reading save file.\n"); + } GLB_DeCrypt(gdmodestr, dchrobj, size - sizeof(plr)); @@ -609,9 +626,9 @@ RAP_SavePlayer( EXIT_Error("RAP_Save() ERR: Try to save Dead player"); if (cdflag) - sprintf(filename, cdfmt, cdpath, filepos); + snprintf(filename, sizeof(filename), CDFMT, cdpath, filepos); else - sprintf(filename, fmt, filepos); + snprintf(filename, sizeof(filename), FMT, filepos); handle = fopen(filename, "wb"); @@ -761,15 +778,15 @@ RAP_LoadWin( for (loop = 0; loop < MAX_SAVE; loop++) { if (cdflag) - sprintf(temp, cdfmt, cdpath, loop); + snprintf(temp, sizeof(temp), CDFMT, cdpath, loop); else - sprintf(temp, fmt, loop); + snprintf(temp, sizeof(temp), FMT, loop); if (!access(temp, 0)) { if (pos == -1) pos = loop; - strncpy(filenames[loop], temp, PATH_MAX); + strncpy(filenames[loop], temp, sizeof(temp)); } } diff --git a/src/mpualsa.cpp b/src/mpualsa.cpp index e86f93c..cb82ff8 100644 --- a/src/mpualsa.cpp +++ b/src/mpualsa.cpp @@ -255,6 +255,6 @@ musdevice_t mus_device_alsa = { ControllerEvent, PitchBendEvent, ProgramEvent, - AllNotesOffEvent, + AllNotesOffEvent }; #endif diff --git a/src/mputsf.cpp b/src/mputsf.cpp index bae61ad..84069a6 100644 --- a/src/mputsf.cpp +++ b/src/mputsf.cpp @@ -7,8 +7,11 @@ #define TSF_IMPLEMENTATION #include "tsf.h" +#include "fx.h" + #include "musapi.h" #include "prefapi.h" +#include "glbapi.h" static tsf* g_TinySoundFont; @@ -17,14 +20,11 @@ AudioCallback() - ***************************************************************************/ void AudioCallback( - void* data, - uint8_t* stream, + int16_t *stream, int len ) { - int SampleCount = (len / (2 * sizeof(short))); //2 output channels - - tsf_render_short(g_TinySoundFont, (short*)stream, SampleCount, 0); + tsf_render_short(g_TinySoundFont, (short*)stream, len, 0); } /*************************************************************************** @@ -35,41 +35,34 @@ TSF_Init( int option ) { - SDL_AudioSpec OutputAudioSpec; - OutputAudioSpec.freq = 44100; - OutputAudioSpec.format = AUDIO_S16SYS; - OutputAudioSpec.channels = 2; - OutputAudioSpec.samples = 512; - OutputAudioSpec.callback = AudioCallback; - - char fn[128]; - INI_GetPreference("Setup", "SoundFont", fn, 127, "SoundFont.sf2"); + char NameOfFile[127]; + + INI_GetPreference("Setup", "SoundFont", NameOfFile, 127, "TimGM6mb.sf2"); - // Load the SoundFont from a file + //Get the path to the file. + char* fn = GLB_FindFilePath(NameOfFile); + + //Load the SoundFont from a file g_TinySoundFont = tsf_load_filename(fn); - + + if(fn) + free(fn); + if (!g_TinySoundFont) { char errmsg[255]; fprintf(stderr, "Could not load %s\n", fn); sprintf(errmsg,"Could not load %s\n", fn); + #ifndef SDL12 SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Raptor", errmsg, NULL); + #endif EXIT_Error("Could not load SoundFont."); return 0; } - - // Set the SoundFont rendering output mode - tsf_set_output(g_TinySoundFont, TSF_STEREO_INTERLEAVED, OutputAudioSpec.freq, 0.0f); - - // Request the desired audio output format - if (SDL_OpenAudio(&OutputAudioSpec, NULL) < 0) - { - fprintf(stderr, "Could not open the audio hardware or the desired audio output format\n"); - EXIT_Error("Could not open the audio hardware or the desired audio output format."); - return 0; - } + // Set the SoundFont rendering output mode + tsf_set_output(g_TinySoundFont, TSF_STEREO_INTERLEAVED, fx_freq, 0.0f); return 1; } @@ -166,36 +159,36 @@ AllNotesOffEvent( tsf_note_off_all(g_TinySoundFont); } -/*************************************************************************** -SetChannelVolume() - - ***************************************************************************/ -static void -SetChannelVolume( - unsigned int chan, - unsigned int param -) -{ - float volume = (float)param; - volume /= 127; +// /*************************************************************************** +// SetChannelVolume() - +// ***************************************************************************/ +// static void +// SetChannelVolume( +// unsigned int chan, +// unsigned int param +// ) +// { +// float volume = (float)param; +// volume /= 127; - tsf_channel_set_volume(g_TinySoundFont, MPU_MapChannel(chan), volume); -} - -/*************************************************************************** -SetChannelPan() - - ***************************************************************************/ -static void -SetChannelPan( - unsigned int chan, - unsigned int param -) -{ - - float pan = (float)param; - pan /= 127; +// tsf_channel_set_volume(g_TinySoundFont, MPU_MapChannel(chan), volume); +// } + +// /*************************************************************************** +// SetChannelPan() - +// ***************************************************************************/ +// static void +// SetChannelPan( +// unsigned int chan, +// unsigned int param +// ) +// { + +// float pan = (float)param; +// pan /= 127; - tsf_channel_set_pan(g_TinySoundFont, MPU_MapChannel(chan), pan); -} +// tsf_channel_set_pan(g_TinySoundFont, MPU_MapChannel(chan), pan); +// } /*************************************************************************** ControllerEvent() - @@ -218,12 +211,12 @@ ControllerEvent( musdevice_t mus_device_tsf = { TSF_Init, TSF_DeInit, - NULL, + AudioCallback, KeyOffEvent, KeyOnEvent, ControllerEvent, PitchBendEvent, ProgramEvent, - AllNotesOffEvent, + AllNotesOffEvent }; \ No newline at end of file diff --git a/src/mpuwinmm.cpp b/src/mpuwinmm.cpp index c2a2169..54b39ba 100644 --- a/src/mpuwinmm.cpp +++ b/src/mpuwinmm.cpp @@ -223,6 +223,6 @@ musdevice_t mus_device_winmm = { ControllerEvent, PitchBendEvent, ProgramEvent, - AllNotesOffEvent, + AllNotesOffEvent }; #endif // _WIN32 diff --git a/src/musapi.cpp b/src/musapi.cpp index 93c6ba7..3a18a50 100644 --- a/src/musapi.cpp +++ b/src/musapi.cpp @@ -8,6 +8,8 @@ static int musrate = 70; static int musfaderate = 50; + +int music_samplesperloop = 1; int music_init; int music_startoffset; int music_len; @@ -237,8 +239,9 @@ MUS_Reset( music_device->ControllerEvent(i, 3, newvol); } - if (music_device && music_device->AllNotesOffEvent) - music_device->AllNotesOffEvent(i,0); + + if (music_device && music_device->AllNotesOffEvent) + music_device->AllNotesOffEvent(i,0); } } @@ -359,7 +362,6 @@ MUS_Service( param = music_vol; if (music_device && music_device->ControllerEvent) music_device->ControllerEvent(chan, 3, param); - break; default: break; @@ -592,21 +594,27 @@ MUS_Mix( ) { int i; - + int mRate = musrate * music_samplesperloop; + int fRate = musfaderate * music_samplesperloop; + int gRate = gssrate * music_samplesperloop; + int SPLx2 = music_samplesperloop * 2; + if (!music_init || !music_device || !music_device->Mix) return; - - for (i = 0; i < len; i++) + + for (i = 0; i < len; i+=music_samplesperloop) { - music_device->Mix(stream, 1); - music_cnt += musrate; + + music_device->Mix(stream, music_samplesperloop); + + music_cnt += mRate; while (music_cnt >= fx_freq) { music_cnt -= fx_freq; MUS_Service(); } - music_cnt2 += musfaderate; + music_cnt2 += fRate; while (music_cnt2 >= fx_freq) { @@ -616,7 +624,7 @@ MUS_Mix( if (gsshack) { - music_cnt3 += gssrate; + music_cnt3 += gRate; while (music_cnt3 >= fx_freq) { @@ -624,7 +632,8 @@ MUS_Mix( GSS_Service(); } } - stream += 2; + + stream += SPLx2; } } diff --git a/src/musapi.h b/src/musapi.h index 9a94540..81a3b8e 100644 --- a/src/musapi.h +++ b/src/musapi.h @@ -15,6 +15,7 @@ struct musdevice_t { void (*AllNotesOffEvent)(unsigned int chan, unsigned int param); }; +extern int music_samplesperloop; extern musdevice_t mus_device_opl, mus_device_winmm, mus_device_tsf, mus_device_alsa, mus_device_corea, mus_device_corem; extern musdevice_t *music_device; diff --git a/src/opl3.cpp b/src/opl3.cpp index 56dcce5..1a57297 100644 --- a/src/opl3.cpp +++ b/src/opl3.cpp @@ -954,12 +954,12 @@ static void OPL3_ChannelWriteC0(opl3_channel *channel, Bit8u data) } if (channel->chip->newm) { - channel->cha = ((data >> 4) & 0x01) ? ~0 : 0; - channel->chb = ((data >> 5) & 0x01) ? ~0 : 0; + channel->cha = ((data >> 4) & 0x01) ? UINT16_MAX : 0; + channel->chb = ((data >> 5) & 0x01) ? UINT16_MAX : 0; } else { - channel->cha = channel->chb = (Bit16u)~0; + channel->cha = channel->chb = UINT16_MAX; } } diff --git a/src/prefapi.cpp b/src/prefapi.cpp index faab246..e64b03f 100644 --- a/src/prefapi.cpp +++ b/src/prefapi.cpp @@ -107,7 +107,7 @@ WritePrivateProfileString( FILE* fptr; short found = 0; short len; - int pos1, pos2, pos3; + int pos1, pos2 = 0, pos3; int exist_len, new_len, delta; int size; int cnt; @@ -260,7 +260,8 @@ WritePrivateProfileString( chsize(fileno(fptr), pos1); #endif #ifdef __GNUC__ - ftruncate(fileno(fptr), pos1); + if(ftruncate(fileno(fptr), pos1)) + EXIT_Error("WritePrivateProfileString: ftruncate Failed.\n"); #endif } else if (delta > 0) // Expand file, starting at the end diff --git a/src/swdapi.cpp b/src/swdapi.cpp index e5171cf..53045eb 100644 --- a/src/swdapi.cpp +++ b/src/swdapi.cpp @@ -1605,7 +1605,7 @@ SWD_ReformatFieldData( SFIELD32* swdfield32 = (SFIELD32*)((char*)header + header->fldofs); SFIELD* swdfield = (SFIELD*)((char*)swdNewData + swdNewData->fldofs); - for (size_t loop = 0; loop < header->numflds; loop++) + for (size_t loop = 0; loop < (size_t)header->numflds; loop++) { swdfield[loop].opt = swdfield32[loop].opt; swdfield[loop].id = swdfield32[loop].id; diff --git a/src/vmemapi.cpp b/src/vmemapi.cpp index 1af950b..2b7ddd5 100644 --- a/src/vmemapi.cpp +++ b/src/vmemapi.cpp @@ -123,7 +123,7 @@ vm_DiscardMem( do { - oldage = lowsize = lowcnt = (uint32_t)~0; + oldage = lowsize = lowcnt = UINT32_MAX; low_mcb = NULL; mcb = (MCBL*)(pool.blk[0]); mem_freed = 0;