From: nhmall Date: Tue, 31 Jan 2023 05:45:27 +0000 (-0500) Subject: more windows Makefile tinkering X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=ce0a4f60caab06f06cef4f62c1c92b2874acac02;p=nethack more windows Makefile tinkering It looks like the Windows API call for PlaySound using SND_RESOURCE, from a mingw32 built program, cannot find the resources that are embeded into the .exe by the mingw32 resource compiler. That works fine from visual studio. For now, fall back to not using the SND_RESOURCE flag, use an ordinary wav file name in the filesystem. Makefile.mingw32 has been modified to copy the wav files to the binary directory along with the exe. This probably won't be the final approach, but it will get things working for now. --- diff --git a/include/windconf.h b/include/windconf.h index 6eb5b2687..f9378e219 100644 --- a/include/windconf.h +++ b/include/windconf.h @@ -119,7 +119,8 @@ extern errno_t tmpfile_s(FILE * restrict * restrict streamptr); #define __USE_MINGW_ANSI_STDIO 1 #endif /* extern int getlock(void); */ -#endif +extern char *exepath(void); +#endif /* __MINGW32__ */ #ifdef _MSC_VER #define MD_USE_TMPFILE_S diff --git a/sound/windsound/windsound.c b/sound/windsound/windsound.c index 11c2801da..ed4a7f996 100644 --- a/sound/windsound/windsound.c +++ b/sound/windsound/windsound.c @@ -56,15 +56,42 @@ windsound_soundeffect(char *desc, int32_t seid, int32_t volume) { #ifdef SND_SOUNDEFFECTS_AUTOMAP int reslt = 0; + int32_t sefnflag = 0; char buf[PATHLEN]; const char *filename; DWORD fdwsound; if (seid >= se_squeak_C || seid <= se_squeak_B) { - filename = get_sound_effect_filename(seid, buf, sizeof buf, 1); - fdwsound = SND_ASYNC | SND_RESOURCE; +#if defined(__MINGW32__) + /* The mingw32 resources don't seem to be able to be retrieved by the + * API PlaySound function with the SND_RESOURCE flag. Use files from + * the file system instead. */ + extern char *sounddir; /* in sounds.c, set in files.c */ + char *exedir; + + if (!sounddir) { + exedir = exepath(); + if (exedir) + if (strlen(exedir) < sizeof buf - 30) { + Strcpy(buf, exedir); + sefnflag = 2; /* 2 = use the directory name already in buf */ + } + } + filename = get_sound_effect_filename(seid, buf, sizeof buf, sefnflag); + fdwsound = SND_ASYNC | SND_NODEFAULT; +#else + sefnflag = 1; + /* sefnflag = 1 means just obtain the soundeffect base name with + * no directory name and no file extension. That's because we're + * going to use the base soundeffect name as the name of a resource + * that's embedded into the .exe file, passing SND_RESOURCE flag to + * Windows API PlaySound(). + */ + filename = get_sound_effect_filename(seid, buf, sizeof buf, sefnflag); + fdwsound = SND_ASYNC | SND_RESOURCE; +#endif } else { - filename = get_sound_effect_filename(seid, buf, sizeof buf, 0); + filename = get_sound_effect_filename(seid, buf, sizeof buf, sefnflag); fdwsound = SND_ASYNC | SND_NODEFAULT; } diff --git a/src/sounds.c b/src/sounds.c index c3d95970e..2bdd97fb2 100644 --- a/src/sounds.c +++ b/src/sounds.c @@ -2071,13 +2071,18 @@ get_sound_effect_filename( int32_t seidint, char *buf, size_t bufsz, - int32_t baseflag) /* non-zero means return without - * directory or extension suffix */ + int32_t baseflag) /* 0 = soundir + '/' + sound + '.wav' + * 1 = sound only (no dir, no extension) ' + * 2 = dir is already in buf incl '/', + * add sound + ".wav" + */ { static const char prefix[] = "se_", suffix[] = ".wav"; - size_t consumes = 0, baselen = 0; + size_t consumes = 0, baselen = 0, existinglen = 0; /* enum sound_effect_entries seid = (enum sound_effect_entries) seidint; */ char *ourdir = sounddir; /* sounddir would get set in files.c */ + char *cp = buf; + boolean needslash = TRUE; if (!buf || (!ourdir && baseflag == 0)) return (char *) 0; @@ -2091,28 +2096,63 @@ get_sound_effect_filename( baselen = strlen(semap_basenames[seidint]); consumes = (sizeof prefix - 1) + baselen; - if (baseflag == 0) - consumes += (sizeof suffix - 1) + strlen(ourdir); - consumes += 1 + 1; /* '\0' and '/' */ - if (baselen <= 0 || consumes > bufsz) + if (baseflag == 0) { + consumes += (sizeof suffix - 1) + strlen(ourdir) + 1; /* 1 for '/' */ + } else if (baseflag == 2) { + + consumes += (sizeof suffix - 1); + existinglen = strlen(buf); + if (existinglen > 0) { + cp = buf + existinglen; /* points at trailing NUL */ + cp--; /* points at last character */ + if (*cp == '/' || *cp == '\\') + needslash = FALSE; + cp++; /* points back at trailing NUL */ + } + if (needslash) + consumes++; /* for '/' */ + consumes += (sizeof suffix - 1); + consumes += existinglen; + } + consumes += 1; /* for trailing NUL */ + /* existinglen could be >= bufsz if caller didn't initialize buf + * to properly include a trailing NUL */ + if (baselen <= 0 || consumes > bufsz || existinglen >= bufsz) return (char *) 0; #if 0 - if (!baseflag) { + if (baseflag == 0) { Strcpy(buf, ourdir); Strcat(buf, "/"); + } else if (baseflag == 2) { + if (needslash) + Strcat(buf, "/"); + } else if (baseflag == 1) { + buf[0] = '\0'; } Strcat(buf, prefix); Strcat(buf, semap_basenames[seidint]); - if (!baseflag) { + if (baseflag == 0 || baseflag == 2) { Strcat(buf, suffix); } #else - if (!baseflag) + if (baseflag == 0) { Snprintf(buf, bufsz , "%s/%s%s%s", ourdir, prefix, semap_basenames[seidint], suffix); - else - Snprintf(buf, bufsz , "%s%s", prefix, semap_basenames[seidint]); + } else if (baseflag == 2) { + if (needslash) { + *cp = '/'; + cp++; + *cp = '\0'; + existinglen++; + } + Snprintf(cp, bufsz - (existinglen + 1) , "%s%s%s", + prefix, semap_basenames[seidint], suffix); + } else if (baseflag == 1) { + Snprintf(buf, bufsz, "%s%s", prefix, semap_basenames[seidint]); + } else { + return (char *) 0; + } #endif return buf; } diff --git a/sys/windows/Makefile.mingw32 b/sys/windows/Makefile.mingw32 index 352072f2c..aa45e102a 100644 --- a/sys/windows/Makefile.mingw32 +++ b/sys/windows/Makefile.mingw32 @@ -251,7 +251,7 @@ MSWIN =../win/win32 WCURSES =../win/curses WSHR =../win/share QT =../win/Qt -SNDWAVDIR =../sound/wav +SNDWAVDIR = ../sound/wav # # Object directory. @@ -845,14 +845,6 @@ endif # Soundlib Support #========================================== -ifeq "$(HAVE_SOUNDLIB)" "Y" -ifeq "$(NEED_USERSOUNDS)" "Y" -SOUNDLIBDEFS += -DUSER_SOUNDS -endif -ifeq "$(NEED_SEAUTOMAP)" "Y" -SOUNDLIBDEFS += -DSND_SOUNDEFFECTS_AUTOMAP -endif -ifeq "$(NEED_WAV)" "Y" WAVLIST = se_squeak_A se_squeak_B se_squeak_B_flat se_squeak_C \ se_squeak_D se_squeak_D_flat se_squeak_E \ se_squeak_E_flat se_squeak_F se_squeak_F_sharp \ @@ -875,7 +867,21 @@ WAVLIST = se_squeak_A se_squeak_B se_squeak_B_flat se_squeak_C \ sound_Wooden_Harp_E sound_Wooden_Harp_F \ sound_Wooden_Harp_G WAVS = $(addprefix $(SNDWAVDIR)/, $(addsuffix .wav, $(WAVLIST))) + +ifeq "$(HAVE_SOUNDLIB)" "Y" +ifeq "$(NEED_USERSOUNDS)" "Y" +SOUNDLIBDEFS += -DUSER_SOUNDS +endif +ifeq "$(NEED_SEAUTOMAP)" "Y" +SOUNDLIBDEFS += -DSND_SOUNDEFFECTS_AUTOMAP +endif +ifeq "$(NEED_WAV)" "Y" +$(info Built-in sound file integration included) +#RCFLAGS = --include-dir=$(SNDWAVDIR) --define RCWAV +WAV = $(WAVS) endif # NEED_WAV +else +$(info No soundlib integration) endif # HAVE_SOUNDLIB #========================================== @@ -953,8 +959,8 @@ $(ONHW)/%.o: $(MSWIN)/%.c $(NHLUAH) | $(ONHW) $(ONHW)/%.o: $(WSHR)/%.c $(NHLUAH) | $(ONHW) $(cc) $(CFLAGSW) $< -o$@ -$(NHWRES): $(MSWIN)/NetHackW.rc $(BMPS) $(WAVS) $(MSWIN)/NetHack.ico | $(ONHW) - $(rc) --include-dir=$(MSWIN) --include-dir=$(SNDWAVDIR) --input=$< -o$@ +$(NHWRES): $(MSWIN)/NetHackW.rc $(BMPS) $(WAV) $(MSWIN)/NetHack.ico | $(ONHW) + $(rc) --include-dir=$(MSWIN) $(RCFLAGS) --input=$< -o$@ $(MSWIN)/tiles.bmp: $(U)tile2bmp.exe $(TILEFILES) $< $@ @@ -971,7 +977,7 @@ $(ONHW): @mkdir -p $@ CLEAN_DIR += $(ONHW) -CLEAN_FILE += $(NHWTARGETS) $(NHWOBJS) $(NHWRES) $(BMPS) $(WAVS) +CLEAN_FILE += $(NHWTARGETS) $(NHWOBJS) $(NHWRES) $(BMPS) $(WAV) #========================================== # nethack @@ -1042,8 +1048,8 @@ $(ONH)/%.o: $(TTY)/%.c $(NHLUAH) | $(ONH) $(ONH)/%.o: $(WCURSES)/%.c $(NHLUAH) | $(ONH) $(cc) $(CFLAGSNH) $(PDCINCL) $< -o$@ -$(NHRES): $(MSWIN)/NetHack.rc $(WAVS) $(MSWIN)/NetHack.ico | $(ONH) - $(rc) --include-dir=$(MSWIN) --include-dir=$(SNDWAVDIR) --input=$< -o$@ +$(NHRES): $(MSWIN)/NetHack.rc $(WAV) $(MSWIN)/NetHack.ico | $(ONH) + $(rc) --include-dir=$(MSWIN) $(RCFLAGS) --input=$< -o$@ $(ONH): @mkdir -p $@ @@ -1069,6 +1075,10 @@ TO_INSTALL = $(GAMEDIR)/NetHack.exe $(RTARGETS) $(GAMEDIRDLLS) \ $(addprefix $(GAMEDIR)/, $(addsuffix .template, sysconf .nethackrc) \ Guidebook.txt NetHack.txt license opthelp record symbols) +ifeq "$(HAVE_SOUNDLIB)" "Y" +TO_INSTALL += $(addprefix $(GAMEDIR)/, $(addsuffix .wav, $(WAVLIST))) +endif + ifeq "$(USE_LUADLL)" "Y" TO_INSTALL += $(LUADLL) endif @@ -1115,6 +1125,9 @@ $(GAMEDIR)/%: $(DOC)/% $(GAMEDIR)/%: $(MSWSYS)/% cp $< $@ +$(GAMEDIR)/%: $(SNDWAVDIR)/% + cp $< $@ + CLEAN_FILE += $(TO_INSTALL) clean: diff --git a/sys/windows/windmain.c b/sys/windows/windmain.c index 23ec924ef..7c89037d0 100644 --- a/sys/windows/windmain.c +++ b/sys/windows/windmain.c @@ -1056,10 +1056,12 @@ void freefakeconsole(void) } #endif +static boolean path_buffer_set = FALSE; +static char path_buffer[MAX_PATH]; + char * get_executable_path(void) { - static char path_buffer[MAX_PATH]; #ifdef UNICODE { @@ -1077,9 +1079,22 @@ get_executable_path(void) if (seperator) *seperator = '\0'; + path_buffer_set = TRUE; return path_buffer; } +#ifdef __MINGW32__ +char * +exepath(void) +{ + char *p = (char *) 0; + + if (path_buffer_set) + p = path_buffer; + return p; +} +#endif + char * translate_path_variables(const char* str, char* buf) {