]> granicus.if.org Git - nethack/commitdiff
more windows Makefile tinkering
authornhmall <nhmall@nethack.org>
Tue, 31 Jan 2023 05:45:27 +0000 (00:45 -0500)
committernhmall <nhmall@nethack.org>
Tue, 31 Jan 2023 05:45:27 +0000 (00:45 -0500)
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.

include/windconf.h
sound/windsound/windsound.c
src/sounds.c
sys/windows/Makefile.mingw32
sys/windows/windmain.c

index 6eb5b268734b868e327e7fbb43a145fbc9e482fd..f9378e2197beb2676445937c8935db2316535371 100644 (file)
@@ -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
index 11c2801da95b442490e70eeab70ad2904cde57b5..ed4a7f9960097ca4b9f42edd2d66312f4032e72f 100644 (file)
@@ -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;
     }
 
index c3d95970e155de25eb88dbfaaa3d4484e07d71da..2bdd97fb29868905663a759a419368ebc5a4b8e9 100644 (file)
@@ -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;
 }
index 352072f2c1f4e2463541e21f8244c38eccad1dca..aa45e102a8869e0f079b4f1ca4d5dbd8c79208ca 100644 (file)
@@ -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:
index 23ec924ef3b9d1555d4f5e596faf31976cf244ef..7c89037d030ca6a97ca600b812f9c21306352d9e 100644 (file)
@@ -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)
 {