]> git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - cgame.c
Fix a bug in the video system.
[xonotic/darkplaces.git] / cgame.c
diff --git a/cgame.c b/cgame.c
index 86da9a53d8074849f5d6ee6981eb276349164949..5b1f3a9f93539f7b207108217bdcafc077dc39f6 100644 (file)
--- a/cgame.c
+++ b/cgame.c
@@ -26,6 +26,7 @@ typedef struct localentity_s
 localentity_t;
 
 #define MAX_LOCALENTITIES 1024
+static int numlocalentities;
 static localentity_t *localentity;
 // true if the entity is alive (not freed)
 static unsigned char *localentityactive;
@@ -62,6 +63,8 @@ static localentity_t *entspawn(void)
        }
        if (best >= 0)
        {
+               // update numlocalentities to include the newly allocated slot
+               numlocalentities = max(numlocalentities, best + 1);
                memset(localentity + best, 0, sizeof(*localentity));
                localentityactive[best] = true;
                return localentity + best;
@@ -72,12 +75,16 @@ static localentity_t *entspawn(void)
 static void entremove(localentity_t *e)
 {
        int i;
-       i = (e - localentity) / sizeof(localentity_t);
-       if (i < 0 || i >= MAX_LOCALENTITIES)
+       i = (int)((e - localentity) / sizeof(localentity_t));
+       if (i < 0 || i >= numlocalentities)
                return; // this should be an error
        //memset(e, 0, sizeof(*e));
        localentityactive[i] = false;
        localentityfreetime[i] = (float)gametime + 1.0f;
+       // since an entity was removed, we may be able to reduce the number of
+       // active entities
+       while (numlocalentities > 0 && !localentityactive[numlocalentities-1])
+               numlocalentities--;
 }
 
 static void phys_setupphysentities(void)
@@ -98,7 +105,7 @@ static void phys_moveentities(void)
 {
        int i;
        localentity_t *l;
-       for (i = 0;i < MAX_LOCALENTITIES;i++)
+       for (i = 0;i < numlocalentities;i++)
        {
                if (localentityactive[i])
                {
@@ -336,10 +343,11 @@ static void net_gibshower(unsigned char num)
 // called by engine
 void CG_Init(void)
 {
-       localentity = CGVM_Malloc(sizeof(*localentity) * MAX_LOCALENTITIES);
-       localentityactive = CGVM_Malloc(sizeof(*localentityactive) * MAX_LOCALENTITIES);
-       localentityfreetime = CGVM_Malloc(sizeof(*localentityfreetime) * MAX_LOCALENTITIES);
-       phys_entity = CGVM_Malloc(sizeof(*phys_entity) * MAX_LOCALENTITIES);
+       numlocalentities = 0;
+       localentity = (localentity_t *)CGVM_Malloc(sizeof(*localentity) * MAX_LOCALENTITIES);
+       localentityactive = (unsigned char *)CGVM_Malloc(sizeof(*localentityactive) * MAX_LOCALENTITIES);
+       localentityfreetime = (float *)CGVM_Malloc(sizeof(*localentityfreetime) * MAX_LOCALENTITIES);
+       phys_entity = (cgphysentity_t *)CGVM_Malloc(sizeof(*phys_entity) * MAX_LOCALENTITIES);
        CGVM_RegisterNetworkCode(1, net_explosion);
        CGVM_RegisterNetworkCode(2, net_gibshower);
        gametime = 0;