From 4db07ee9af72d6132990feb409525c67fbba9205 Mon Sep 17 00:00:00 2001
From: hendricks266 <hendricks266@1a8010ca-5511-0410-912e-c29ae57300e0>
Date: Sun, 25 Jun 2017 11:24:19 +0000
Subject: [PATCH] Structify animsounds arrays.

git-svn-id: https://svn.eduke32.com/eduke32@6282 1a8010ca-5511-0410-912e-c29ae57300e0
---
 source/duke3d/src/anim.cpp | 15 ++++++++-------
 source/duke3d/src/anim.h   |  8 ++++++--
 source/duke3d/src/game.cpp | 21 ++++++++-------------
 3 files changed, 22 insertions(+), 22 deletions(-)

diff --git a/source/duke3d/src/anim.cpp b/source/duke3d/src/anim.cpp
index 38317c997..aca3c1a2d 100644
--- a/source/duke3d/src/anim.cpp
+++ b/source/duke3d/src/anim.cpp
@@ -118,12 +118,13 @@ int32_t Anim_Play(const char *fn)
         return 0;
     }
 
-    int32_t framenum = 0, soundidx = 0;  // custom anim sounds
+    uint16_t soundidx = 0;  // custom anim sounds
     int32_t running = 1, i;
 
     I_ClearAllInput();
 
 #ifdef USE_LIBVPX
+    uint16_t framenum = 0;
     while (getrendermode() >= REND_POLYMOST)  // if, really
     {
         char vpxfn[BMAX_PATH];
@@ -193,9 +194,9 @@ int32_t Anim_Play(const char *fn)
 
             // after rendering the frame but before displaying: maybe play sound...
             framenum++;
-            while (soundidx < anim->numsounds && anim->sounds[soundidx << 1] == framenum)
+            while (soundidx < anim->numsounds && anim->sounds[soundidx].frame == framenum)
             {
-                S_PlaySound(anim->sounds[(soundidx << 1) + 1]);
+                S_PlaySound(anim->sounds[soundidx].sound);
                 soundidx++;
             }
 
@@ -332,13 +333,13 @@ int32_t Anim_Play(const char *fn)
         if (!anim->numsounds && anim->sound_func)
             anim->sound_func(i);
 
-        framenum = i++;
-
-        while (soundidx < anim->numsounds && anim->sounds[soundidx << 1] == framenum)
+        while (soundidx < anim->numsounds && anim->sounds[soundidx].frame == (uint16_t)i)
         {
-            S_PlaySound(anim->sounds[(soundidx << 1) + 1]);
+            S_PlaySound(anim->sounds[soundidx].sound);
             soundidx++;
         }
+
+        ++i;
     } while (i < numframes);
 
 end_anim_restore_gl:
diff --git a/source/duke3d/src/anim.h b/source/duke3d/src/anim.h
index adf529971..5270debe8 100644
--- a/source/duke3d/src/anim.h
+++ b/source/duke3d/src/anim.h
@@ -23,12 +23,16 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 #ifndef anim_h_
 #define anim_h_
 
+typedef struct {
+    uint16_t frame, sound;
+} animsound_t;
+
 typedef struct
 {
     uint8_t* animbuf;
     void (*sound_func)(int32_t);
-    uint16_t *sounds;
-    int16_t numsounds;
+    animsound_t *sounds;
+    uint16_t numsounds;
     uint8_t framedelay;
     char animlock;
 } dukeanim_t;
diff --git a/source/duke3d/src/game.cpp b/source/duke3d/src/game.cpp
index 224d9d058..4ebc3a4d7 100644
--- a/source/duke3d/src/game.cpp
+++ b/source/duke3d/src/game.cpp
@@ -5286,11 +5286,13 @@ static int parsedefinitions_game(scriptfile *pScript, int firstPass)
                     initprintf("Warning: overwriting already defined hi-anim %s's sounds on line %s:%d\n", fileName,
                         pScript->filename, scriptfile_getlinum(pScript, tokenPtr));
                     Bfree(animPtr->sounds);
-                    animPtr->numsounds = 0;
                 }
 
-                animPtr->sounds = (uint16_t *) Xcalloc(allocSize, 2 * sizeof(uint16_t));
+                animPtr->sounds = (animsound_t *)Xmalloc(allocSize * sizeof(animsound_t));
+                animPtr->numsounds = 0;
             }
+            else
+                break;
 
             while (pScript->textptr < animSoundsEnd)
             {
@@ -5310,10 +5312,6 @@ static int parsedefinitions_game(scriptfile *pScript, int firstPass)
 
                 defError = 1;
 
-                // TODO: look carefully at whether this can be removed.
-                if (animPtr->sounds == NULL)  // Bcalloc check
-                    break;
-
                 if (scriptfile_getsymbol(pScript, &soundNum))
                     break;
 
@@ -5344,18 +5342,15 @@ static int parsedefinitions_game(scriptfile *pScript, int firstPass)
 
                 if (numPairs >= allocSize)
                 {
-                    void *newptr;
-
                     allocSize *= 2;
-                    newptr = Xrealloc(animPtr->sounds, allocSize * 2 * sizeof(uint16_t));
-
-                    animPtr->sounds = (uint16_t *)newptr;
+                    animPtr->sounds = (animsound_t *)Xrealloc(animPtr->sounds, allocSize * sizeof(animsound_t));
                 }
 
                 defError = 0;
 
-                animPtr->sounds[2 * numPairs]     = frameNum;
-                animPtr->sounds[2 * numPairs + 1] = soundNum;
+                animsound_t & sound = animPtr->sounds[numPairs];
+                sound.frame = frameNum;
+                sound.sound = soundNum;
 
                 ++numPairs;
             }