changeset 1017:af20650a052f

SpriteFrameTable search simplified
author Grumpy7
date Sun, 19 May 2013 20:41:19 +0200
parents c45d51b3f4f4
children 9ac94d00012e
files Sprites.cpp Sprites.h
diffstat 2 files changed, 71 insertions(+), 102 deletions(-) [+]
line wrap: on
line diff
--- a/Sprites.cpp	Sun May 19 17:55:19 2013 +0200
+++ b/Sprites.cpp	Sun May 19 20:41:19 2013 +0200
@@ -1,4 +1,5 @@
 #include <string.h>
+#include <algorithm>
 
 #include "Allocator.h"
 #include "Sprites.h"
@@ -217,75 +218,43 @@
 
 //----- (0044D813) --------------------------------------------------------
 signed int SpriteFrameTable::FastFindSprite( char *pSpriteName )
-    {
-  SpriteFrameTable *v2; // esi@1
-  int v3; // eax@1
+{
   signed int result; // eax@2
 
-  v2 = this;
-  BinarySearch(0, this->uNumEFrames, pSpriteName);
-  v3 = v2->field_8;
-  if ( v3 < 0 )
+  int searchResult = BinarySearch(pSpriteName);
+  if ( searchResult < 0 )
     result = 0;
   else
-    result = v2->pSpriteEFrames[v3];
+      result = this->pSpriteEFrames[searchResult];
   return result;
 }
 
 //----- (0044D83A) --------------------------------------------------------
-void SpriteFrameTable::BinarySearch(int a2, int a3, const char *pSpriteName)
+int SpriteFrameTable::BinarySearch( const char *pSpriteName )
 {
-  int v4; // ebx@1
-  SpriteFrameTable *v5; // edi@1
-  int v6; // esi@2
-  int v7; // eax@2
-  int a2a; // [sp+14h] [bp+8h]@2
-
-  v4 = a2;
-  v5 = this;
-  while ( 1 )
-  {
-    a2a = a3 - v4;
-    v6 = (a3 - v4) / 2 + v4;
-    v7 = _strcmpi(pSpriteName, v5->pSpritePFrames[v6]->pIconName);
-    if ( !v7 )
-      v5->field_8 = v6;
-    if ( v4 == a3 )
-      break;
-    if ( v7 >= 0 )
+    int startPos = 0;
+    int endPos = uNumEFrames;
+    while ( 1 )
     {
-      if ( a2a <= 4 )
-      {
-        while ( v4 < a3 )
+        int searchRange = endPos - startPos;
+        int middleFrameIndex = (endPos - startPos) / 2 + startPos;
+        int comparisonResult = _stricmp(pSpriteName, this->pSpritePFrames[middleFrameIndex]->pIconName);
+        if ( !comparisonResult )
         {
-          if ( !_strcmpi(pSpriteName, v5->pSpritePFrames[v4]->pIconName) )
-          {
-LABEL_19:
-            v5->field_8 = v4;
-            return;
-          }
-          ++v4;
+            return middleFrameIndex;
+        }
+        if ( startPos == endPos )
+        {
+            return -1;
         }
-        break;
-      }
-      v4 += (a3 - v4) / 2;
+        if ( comparisonResult >= 0 )
+        {
+            startPos += max(((endPos - startPos) / 2), 1);
+        }
+        else{
+            endPos = max(((endPos - startPos) / 2), 1) + startPos;
+        }
     }
-    else
-    {
-      if ( a2a <= 4 )
-      {
-        while ( v4 < a3 )
-        {
-          if ( !_strcmpi(pSpriteName, v5->pSpritePFrames[v4]->pIconName) )
-            goto LABEL_19;
-          ++v4;
-        }
-        break;
-      }
-      a3 = (a3 - v4) / 2 + v4;
-    }
-  }
-  v5->field_8 = -1;
 }
 
 //----- (0044D8D0) --------------------------------------------------------
--- a/Sprites.h	Sun May 19 17:55:19 2013 +0200
+++ b/Sprites.h	Sun May 19 20:41:19 2013 +0200
@@ -5,18 +5,18 @@
 #pragma pack(push, 1)
 struct Sprite  //28h
 {
-  void Release();
+    void Release();
 
-  const char *pName;  //0
-  int uPaletteID; //4
-  struct IDirectDrawSurface4 *pTextureSurface;  //8
-  struct IDirect3DTexture2 *pTexture;   //ch
-  int uAreaX;  //10h
-  int uAreaY;  //14h
-  int uBufferWidth;  //18h
-  int uBufferHeight;  //1ch
-  int uAreaWidth;  //20h
-  int uAreaHeight; //24h
+    const char *pName;  //0
+    int uPaletteID; //4
+    struct IDirectDrawSurface4 *pTextureSurface;  //8
+    struct IDirect3DTexture2 *pTexture;   //ch
+    int uAreaX;  //10h
+    int uAreaY;  //14h
+    int uBufferWidth;  //18h
+    int uBufferHeight;  //1ch
+    int uAreaWidth;  //20h
+    int uAreaHeight; //24h
 };
 #pragma pack(pop)
 
@@ -25,17 +25,17 @@
 #pragma pack(push, 1)
 struct SpriteFrame
 {
-  char pIconName[12]; 
-  char pTextureName[12]; //c
-  __int16 pHwSpriteIDs[8]; //18h
-  int scale; //28h
-  int uFlags; //2c
-  __int16 uGlowRadius; //30
-  __int16 uPaletteID;  //32
-  __int16 uPaletteIndex;
-  __int16 uAnimTime;
-  __int16 uAnimLength;
-  __int16 _pad;
+    char pIconName[12]; 
+    char pTextureName[12]; //c
+    __int16 pHwSpriteIDs[8]; //18h
+    int scale; //28h
+    int uFlags; //2c
+    __int16 uGlowRadius; //30
+    __int16 uPaletteID;  //32
+    __int16 uPaletteIndex;
+    __int16 uAnimTime;
+    __int16 uAnimLength;
+    __int16 _pad;
 };
 #pragma pack(pop)
 
@@ -43,31 +43,31 @@
 #pragma pack(push, 1)
 struct SpriteFrameTable
 {
-  //----- (0044D4BA) --------------------------------------------------------
-  inline SpriteFrameTable()
-  {
-    uNumSpriteFrames = 0;
-    pSpriteSFrames = nullptr;
-    pSpritePFrames = nullptr;
-    pSpriteEFrames = nullptr;
-  }
-  void ToFile();
-  void FromFile(void *pSerialized);
-  bool FromFileTxt(const char *Args);
-  void ReleaseSFrames();
-  void ResetSomeSpriteFlags();
-  void InitializeSprite(signed int uSpriteID);
-  signed int FastFindSprite(char *pSpriteName);
-  void BinarySearch(int a2, int a3, const char *pSpriteName);
-  SpriteFrame *GetFrame(unsigned int uSpriteID, unsigned int uTime);
-  SpriteFrame *GetFrameBy_x(unsigned int uSpriteID, signed int a3);
+    //----- (0044D4BA) --------------------------------------------------------
+    inline SpriteFrameTable()
+    {
+        uNumSpriteFrames = 0;
+        pSpriteSFrames = nullptr;
+        pSpritePFrames = nullptr;
+        pSpriteEFrames = nullptr;
+    }
+    void ToFile();
+    void FromFile(void *pSerialized);
+    bool FromFileTxt(const char *Args);
+    void ReleaseSFrames();
+    void ResetSomeSpriteFlags();
+    void InitializeSprite(signed int uSpriteID);
+    signed int FastFindSprite(char *pSpriteName);
+    int BinarySearch(const char *pSpriteName);
+    SpriteFrame *GetFrame(unsigned int uSpriteID, unsigned int uTime);
+    SpriteFrame *GetFrameBy_x(unsigned int uSpriteID, signed int a3);
 
-  signed int uNumSpriteFrames;
-  unsigned int uNumEFrames;//field_4;
-  int field_8;
-  struct SpriteFrame *pSpriteSFrames;  //0c
-  struct SpriteFrame **pSpritePFrames; //10h
-  __int16 *pSpriteEFrames; //14h
+    signed int uNumSpriteFrames;
+    unsigned int uNumEFrames;//field_4;
+    int unused_field;          //field_8
+    struct SpriteFrame *pSpriteSFrames;  //0c
+    struct SpriteFrame **pSpritePFrames; //10h
+    __int16 *pSpriteEFrames; //14h
 };
 #pragma pack(pop)