summaryrefslogtreecommitdiffstats
path: root/src/iman.cpp
diff options
context:
space:
mode:
authoradiblol <adiblol@1tbps.org>2012-03-08 19:32:05 +0100
committeradiblol <adiblol@1tbps.org>2012-03-08 19:32:05 +0100
commita4c804b49ec872b71bd5a0167c3ad45704a3cc30 (patch)
tree8c931235247d662ca46a99695beb328fdfc8e8a8 /src/iman.cpp
downloadcolobot-a4c804b49ec872b71bd5a0167c3ad45704a3cc30.tar.gz
colobot-a4c804b49ec872b71bd5a0167c3ad45704a3cc30.tar.bz2
colobot-a4c804b49ec872b71bd5a0167c3ad45704a3cc30.zip
Initial commit, Copyright (C) 2001-2008, Daniel ROUX & EPSITEC SA, www.epsitec.ch
Diffstat (limited to 'src/iman.cpp')
-rw-r--r--src/iman.cpp149
1 files changed, 149 insertions, 0 deletions
diff --git a/src/iman.cpp b/src/iman.cpp
new file mode 100644
index 0000000..9b64b93
--- /dev/null
+++ b/src/iman.cpp
@@ -0,0 +1,149 @@
+// iman.cpp
+
+#define STRICT
+#define D3D_OVERLOADS
+
+#include <windows.h>
+#include <stdio.h>
+
+#include "struct.h"
+#include "iman.h"
+
+
+
+
+// Constructeur de l'objet.
+
+CInstanceManager::CInstanceManager()
+{
+ int i;
+
+ for ( i=0 ; i<CLASS_MAX ; i++ )
+ {
+ m_table[i].totalPossible = 0;
+ m_table[i].totalUsed = 0;
+ m_table[i].classPointer = 0;
+ }
+}
+
+// Destructeur de l'objet.
+
+CInstanceManager::~CInstanceManager()
+{
+ int i;
+
+ for ( i=0 ; i<CLASS_MAX ; i++ )
+ {
+ if ( m_table[i].classPointer != 0 )
+ {
+ free(m_table[i].classPointer);
+ }
+ }
+}
+
+
+// Vide toute la liste des classes.
+
+void CInstanceManager::Flush()
+{
+ int i;
+
+ for ( i=0 ; i<CLASS_MAX ; i++ )
+ {
+ if ( m_table[i].classPointer != 0 )
+ {
+ free(m_table[i].classPointer);
+ }
+ m_table[i].classPointer = 0;
+ }
+}
+
+// Vide toutes les instances d'une classe donnée.
+
+void CInstanceManager::Flush(ClassType classType)
+{
+ if ( classType < 0 || classType >= CLASS_MAX ) return;
+ if ( m_table[classType].classPointer == 0 ) return;
+
+ free(m_table[classType].classPointer);
+ m_table[classType].classPointer = 0;
+}
+
+
+// Ajoute une nouvelle instance d'une classe.
+
+BOOL CInstanceManager::AddInstance(ClassType classType, void* pointer, int max)
+{
+ int i;
+
+ if ( classType < 0 || classType >= CLASS_MAX ) return FALSE;
+
+ if ( m_table[classType].classPointer == 0 )
+ {
+ m_table[classType].classPointer = (void**)malloc(max*sizeof(void*));
+ m_table[classType].totalPossible = max;
+ m_table[classType].totalUsed = 0;
+ }
+
+ if ( m_table[classType].totalUsed >= m_table[classType].totalPossible ) return FALSE;
+
+ i = m_table[classType].totalUsed++;
+ m_table[classType].classPointer[i] = pointer;
+ return TRUE;
+}
+
+// Supprime une instance d'une classe.
+
+BOOL CInstanceManager::DeleteInstance(ClassType classType, void* pointer)
+{
+ int i;
+
+ if ( classType < 0 || classType >= CLASS_MAX ) return FALSE;
+
+ for ( i=0 ; i<m_table[classType].totalUsed ; i++ )
+ {
+ if ( m_table[classType].classPointer[i] == pointer )
+ {
+ m_table[classType].classPointer[i] = 0;
+ }
+ }
+
+ Compress(classType);
+ return TRUE;
+}
+
+// Cherche une instance existante. Retourne 0 si elle n'existe pas.
+// Doit être super-rapide !
+
+void* CInstanceManager::SearchInstance(ClassType classType, int rank)
+{
+#if _DEBUG
+ if ( classType < 0 || classType >= CLASS_MAX ) return 0;
+ if ( m_table[classType].classPointer == 0 ) return 0;
+#endif
+ if ( rank >= m_table[classType].totalUsed ) return 0;
+
+ return m_table[classType].classPointer[rank];
+}
+
+
+// Bouche les trous dans une table.
+
+void CInstanceManager::Compress(ClassType classType)
+{
+ int i, j;
+
+ if ( classType < 0 || classType >= CLASS_MAX ) return;
+
+ j = 0;
+ for ( i=0 ; i<m_table[classType].totalUsed ; i++ )
+ {
+ if ( m_table[classType].classPointer[i] != 0 )
+ {
+ m_table[classType].classPointer[j++] = m_table[classType].classPointer[i];
+ }
+ }
+ m_table[classType].totalUsed = j;
+}
+
+