5 #define CGVM_RENDERENTITIES 1024
7 static entity_render_t cgvm_renderentities[CGVM_RENDERENTITIES];
8 static int cgvm_renderentity;
10 static mempool_t *cgvm_mempool;
12 static void (*cgvm_networkcode[256])(unsigned char num);
14 static byte *cgvm_netbuffer;
15 static int cgvm_netbufferlength;
16 static int cgvm_netbufferpos;
18 #define MAX_CGVM_MODELS 128
19 #define MAX_CGVM_MODELNAME 32
20 static char cgvm_modelname[MAX_CGVM_MODELS][MAX_CGVM_MODELNAME];
21 static model_t *cgvm_model[MAX_CGVM_MODELS];
23 void CL_CGVM_Init(void)
25 cgvm_mempool = Mem_AllocPool("CGVM");
28 void CL_CGVM_Clear(void)
30 Mem_EmptyPool(cgvm_mempool);
31 memset(cgvm_networkcode, 0, sizeof(cgvm_networkcode));
32 memset(cgvm_modelname, 0, sizeof(cgvm_modelname));
33 memset(cgvm_model, 0, sizeof(cgvm_model));
36 void CL_CGVM_Frame(void)
38 cgvm_renderentity = 0;
39 CG_Frame(cl.time); // API call
42 // starts the cgame code
43 void CL_CGVM_Start(void)
46 CG_Init(); // API call
49 void CL_CGVM_ParseNetwork(byte *netbuffer, int length)
52 cgvm_netbuffer = netbuffer;
53 cgvm_netbufferlength = length;
54 cgvm_netbufferpos = 0;
55 while (cgvm_netbufferpos < cgvm_netbufferlength)
57 num = CGVM_MSG_ReadByte();
58 if (cgvm_networkcode[num])
59 cgvm_networkcode[num]((byte)num);
61 Host_Error("CL_CGVM_ParseNetwork: unregistered network code %i", num);
72 void CGVM_RegisterNetworkCode(const unsigned char num, void (*netcode)(unsigned char num))
74 if (cgvm_networkcode[num])
75 Host_Error("CGVM_RegisterNetworkCode: value %i already registered", num);
76 cgvm_networkcode[num] = netcode;
79 unsigned char CGVM_MSG_ReadByte(void)
81 if (cgvm_netbufferpos < cgvm_netbufferlength)
82 return cgvm_netbuffer[cgvm_netbufferpos++];
87 short CGVM_MSG_ReadShort(void)
90 num = CGVM_MSG_ReadByte() | (CGVM_MSG_ReadByte() << 8);
94 int CGVM_MSG_ReadLong(void)
97 num = CGVM_MSG_ReadByte() | (CGVM_MSG_ReadByte() << 8) | (CGVM_MSG_ReadByte() << 16) | (CGVM_MSG_ReadByte() << 24);
101 float CGVM_MSG_ReadFloat(void)
104 num = CGVM_MSG_ReadByte() | (CGVM_MSG_ReadByte() << 8) | (CGVM_MSG_ReadByte() << 16) | (CGVM_MSG_ReadByte() << 24);
105 return *((float *)&num);
108 float CGVM_MSG_ReadCoord(void)
110 return CGVM_MSG_ReadFloat();
113 float CGVM_MSG_ReadAngle(void)
115 return CGVM_MSG_ReadByte() * 360.0f / 256.0f;
118 float CGVM_MSG_ReadPreciseAngle(void)
120 return ((unsigned short)CGVM_MSG_ReadShort()) * 360.0f / 65536.0f;
123 void CGVM_Draw_Entity(const cgdrawentity_t *e)
126 //Con_Printf("CGVM_Draw_Entity: origin %f %f %f angles %f %f %f alpha %f scale %f model %i frame1 %i frame2 %i framelerp %f skinnum %i\n", e->origin[0], e->origin[1], e->origin[2], e->angles[0], e->angles[1], e->angles[2], e->alpha, e->scale, e->model, e->frame1, e->frame2, e->framelerp, e->skinnum);
131 if (cgvm_renderentity >= CGVM_RENDERENTITIES
132 || r_refdef.numentities >= MAX_VISEDICTS)
135 r = cgvm_renderentities + cgvm_renderentity;
136 VectorCopy(e->origin, r->origin);
137 VectorCopy(e->angles, r->angles);
140 if (e->model < 0 || e->model >= MAX_CGVM_MODELS || !cgvm_model[e->model])
142 Con_Printf("CGVM_Draw_Entity: invalid model index %i\n", e->model);
145 r->model = cgvm_model[e->model]; //Mod_ForName(e->model, false, false, false);
149 Con_Printf("CGVM_Draw_Entity: unable to find model \"%s\"");
154 r->frame = e->frame2;
155 // FIXME: support colormapping?
157 // FIXME: support effects?
159 r->skinnum = e->skinnum;
160 // FIXME: any flags worth setting?
163 r->frame1 = e->frame1;
164 r->frame2 = e->frame2;
165 r->framelerp = e->framelerp;
169 r_refdef.entities[r_refdef.numentities++] = r;
174 void CGVM_Draw_Light(const cgdrawlight_t *l)
176 CL_AllocDlight(NULL, (float *) l->origin, 1, l->light[0], l->light[1], l->light[2], 0, 0);
179 void *CGVM_Malloc(const int size)
181 return Mem_Alloc(cgvm_mempool, size);
184 void CGVM_Free(void *mem)
189 float CGVM_RandomRange(const float r1, const float r2)
191 return lhrandom(r1, r2);
194 float CGVM_TracePhysics(const float *start, const float *end, const float *worldmins, const float *worldmaxs, const float *entitymins, const float *entitymaxs, const cgphysentity_t *physentities, const int numphysentities, float *impactpos, float *impactnormal, int *impactentnum)
197 vec3_t start2, end2, middle;
198 // FIXME: do tracing agains network entities and physentities here
199 // placeholder world only code assuming 0 size
200 middle[0] = (worldmins[0] + worldmaxs[0]) * 0.5f;
201 middle[1] = (worldmins[1] + worldmaxs[1]) * 0.5f;
202 middle[2] = (worldmins[2] + worldmaxs[2]) * 0.5f;
203 VectorAdd(start, middle, start2);
204 VectorAdd(end, middle, end2);
205 frac = TraceLine((float *)start2, (float *)end2, impactpos, impactnormal, 0, true);
206 VectorSubtract(impactpos, middle, impactpos);
207 //VectorCopy(end, impactpos);
208 //VectorClear(impactnormal);
213 char *CGVM_GetCvarString(const char *name)
216 cvar = Cvar_FindVar((char *)name);
223 float CGVM_GetCvarFloat(const char *name)
226 cvar = Cvar_FindVar((char *)name);
233 int CGVM_GetCvarInt(const char *name)
236 cvar = Cvar_FindVar((char *)name);
238 return cvar->integer;
243 double CGVM_Time(void)
248 int CGVM_Model(const char *name)
252 if (strlen(name) > (MAX_CGVM_MODELNAME - 1))
254 for (i = 1;i < MAX_CGVM_MODELS;i++)
256 if (!cgvm_modelname[i][0])
258 if (!strcmp(name, cgvm_modelname[i]))
261 if (i >= MAX_CGVM_MODELS)
263 model = Mod_ForName((char *)name, false, false, false);
266 strcpy(cgvm_modelname[i], name);
267 cgvm_model[i] = model;
271 void CGVM_Stain(const float *origin, float radius, int cr1, int cg1, int cb1, int ca1, int cr2, int cg2, int cb2, int ca2)
273 R_Stain((float *)origin, radius, cr1, cg1, cb1, ca1, cr2, cg2, cb2, ca2);