著名物理引擎Hawk的源代码
源代码在线查看: object.c
/* object.c, HAWK game engine
*
* Copyright 1997-1998 by Phil Frisbie, Jr.
* for Hawk Software
*
*/
#include "hawk.h"
#include "internal.h"
int nfreeobjects;
OBJECT *firstobject;
OBJECT *firstfreeobject;
OBJECT *objects;
OBJECT *lastobject;
static BOOL needinit = TRUE;
void objInit(void)
{
int i;
GLevel.nobjects = 0;
GLevel.objects = TagMalloc(MAX_OBJECTS * sizeof(GLevel.objects[0]), TAG_LEVEL);
memset(GLevel.objects, 0, MAX_OBJECTS * sizeof(GLevel.objects[0]));
objects = GLevel.objects;
for(i=0;i {
if(i != (MAX_OBJECTS - 1))
objects[i].next = &objects[i+1];
else
objects[i].next = NULL;
if(i != 0)
objects[i].prev = &objects[i-1];
else
objects[i].prev = NULL;
}
firstfreeobject = &objects[0];
firstobject = NULL;
lastobject = NULL;
needinit = FALSE;
}
OBJECT *objAdd(void)
{
int i;
if(needinit)
objInit();
/* find an unused object */
if(GLevel.nobjects == 0) /* cannot chain for first object */
{
GLevel.objects[0].next = NULL;
if(!GLevel.objects[0].trace)
GLevel.objects[0].trace = TagMalloc(sizeof(TRACE), TAG_LEVEL);
GLevel.objects[0].weapon = NO_MODEL;
GLevel.nobjects++;
return &GLevel.objects[0];
}
else
{
for(i=0;i {
if(GLevel.objects[i].type == O_UNUSED) /* found an unused object */
{
int j;
/* find the last object in the chain */
for(j=0;j {
if((!GLevel.objects[j].next)
&& (GLevel.objects[j].type)) /* found the last used object in the chain */ {
GLevel.objects[j].next = &GLevel.objects[i];
GLevel.objects[i].next = NULL;
if(!GLevel.objects[i].trace)
GLevel.objects[i].trace = TagMalloc(sizeof(TRACE), TAG_LEVEL);
GLevel.objects[i].weapon = NO_MODEL;
GLevel.nobjects++;
return &GLevel.objects[i];
}
}
}
}
}
/* we must be out of objects */
return NULL;
}
void objDelete(OBJECT *o)
{
int i;
/* find the object */
for(i=0;i {
if(o == &GLevel.objects[i]) /* found the object */
{
int j;
/* find the previous object in the chain */
for(j=0;j {
if(o == (OBJECT *)GLevel.objects[j].next) /* found the previous object in the chain */
{
GLevel.objects[j].next = o->next;
o->next = NULL;
o->type = O_UNUSED;
GLevel.nobjects--;
}
}
}
}
}