From c1bca25ad7aace521aa36e1c0f663e45fce1ce6d Mon Sep 17 00:00:00 2001 From: havoc Date: Mon, 5 Aug 2002 09:47:21 +0000 Subject: [PATCH] optimized phys_moveentities (in cgame.c) so it doesn't waste so much time git-svn-id: svn://svn.icculus.org/twilight/trunk/darkplaces@2195 d7cf8633-e32d-0410-b094-e92efae38249 --- cg_math.h | 10 +++++--- cgame.c | 68 ++++++++++++++++++++++++++++++------------------------- 2 files changed, 44 insertions(+), 34 deletions(-) diff --git a/cg_math.h b/cg_math.h index d06eb75b..e3dcece6 100644 --- a/cg_math.h +++ b/cg_math.h @@ -32,6 +32,10 @@ Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. #define M_PI 3.14159265358979323846 // matches value in gcc v2 math.h #endif +#ifndef NULL +#define NULL ((void *)0) +#endif + typedef float vec_t; typedef vec_t vec2_t[2]; typedef vec_t vec3_t[3]; @@ -46,11 +50,11 @@ extern vec3_t vec3_origin; #define nanmask (255<<23) #define IS_NAN(x) (((*(int *)&x)&nanmask)==nanmask) -#define bound(min,num,max) (num >= min ? (num < max ? num : max) : min) +#define bound(min,num,max) ((num) >= (min) ? ((num) < (max) ? (num) : (max)) : (min)) #ifndef min -#define min(A,B) (A < B ? A : B) -#define max(A,B) (A > B ? A : B) +#define min(A,B) ((A) < (B) ? (A) : (B)) +#define max(A,B) ((A) > (B) ? (A) : (B)) #endif #define lhrandom(MIN,MAX) ((rand() & 32767) * (((MAX)-(MIN)) * (1.0f / 32767.0f)) + (MIN)) diff --git a/cgame.c b/cgame.c index 00ca87c0..98d9a16c 100644 --- a/cgame.c +++ b/cgame.c @@ -3,18 +3,11 @@ #include "cgame_api.h" #include "cg_math.h" - -#ifndef NULL -#define NULL ((void *)0) -#endif - static double gametime, frametime; struct localentity_s; typedef struct localentity_s { - int active; // true if the entity is alive (not freed) - float freetime; // time this entity was freed float dietime; vec3_t velocity; vec3_t avelocity; @@ -34,6 +27,10 @@ localentity_t; #define MAX_LOCALENTITIES 1024 static localentity_t *localentity; +// true if the entity is alive (not freed) +static unsigned char *localentityactive; +// time the entity was freed +static float *localentityfreetime; static cgphysentity_t *phys_entity; static int phys_entities; @@ -49,35 +46,38 @@ static void readvector(vec3_t v) static localentity_t *entspawn(void) { - int i; - localentity_t *l; + int i, best; + float bestfreetime; + bestfreetime = gametime + 100.0f; + best = -1; for (i = 0;i < MAX_LOCALENTITIES;i++) { - l = localentity + i; - if (!l->active && l->freetime < gametime) + if (!localentityactive[i] && bestfreetime > localentityfreetime[i]) { - memset(l, 0, sizeof(*l)); - l->active = true; - return l; + bestfreetime = localentityfreetime[i]; + best = i; + if (bestfreetime < gametime) + break; } } - for (i = 0;i < MAX_LOCALENTITIES;i++) + if (best >= 0) { - l = localentity + i; - if (!l->active) - { - memset(l, 0, sizeof(*l)); - l->active = true; - return l; - } + memset(localentity + best, 0, sizeof(*localentity)); + localentityactive[best] = true; + return localentity + best; } return NULL; } static void entremove(localentity_t *e) { - memset(e, 0, sizeof(*e)); - e->freetime = (float)gametime + 1.0f; + int i; + i = (e - localentity) / sizeof(localentity_t); + if (i < 0 || i >= MAX_LOCALENTITIES) + return; // this should be an error + //memset(e, 0, sizeof(*e)); + localentityactive[i] = false; + localentityfreetime[i] = (float)gametime + 1.0f; } static void phys_setupphysentities(void) @@ -86,9 +86,9 @@ static void phys_setupphysentities(void) /* for (i = 0;i < MAX_LOCALENTITIES;i++) { - l = localentities + i; - if (l->active && l->solid) + if (localentityactive[i] && localentities[i].solid) { + l = localentities + i; } } */ @@ -100,12 +100,16 @@ static void phys_moveentities(void) localentity_t *l; for (i = 0;i < MAX_LOCALENTITIES;i++) { - l = localentity + i; - if (l->active) + if (localentityactive[i]) { + l = localentity + i; if (l->framethink) + { l->framethink(l); - if (l->active && l->draw.model) + if (!localentityactive[i]) + continue; + } + if (l->draw.model) CGVM_Draw_Entity(&l->draw); } } @@ -332,8 +336,10 @@ static void net_gibshower(unsigned char num) // called by engine void CG_Init(void) { - localentity = CGVM_Malloc(sizeof(localentity_t) * MAX_LOCALENTITIES); - phys_entity = CGVM_Malloc(sizeof(cgphysentity_t) * MAX_LOCALENTITIES); + 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); CGVM_RegisterNetworkCode(1, net_explosion); CGVM_RegisterNetworkCode(2, net_gibshower); gametime = 0; -- 2.39.2