2 Copyright (C) 1996-1997 Id Software, Inc.
4 This program is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License
6 as published by the Free Software Foundation; either version 2
7 of the License, or (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 See the GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20 // models.c -- model loading and caching
22 // models are the only shared resource between a client and server running
23 // on the same machine.
28 char loadname[32]; // for hunk tags
30 void Mod_LoadSpriteModel (model_t *mod, void *buffer);
31 void Mod_LoadBrushModel (model_t *mod, void *buffer);
32 void Mod_LoadAliasModel (model_t *mod, void *buffer);
33 void Mod_LoadQ2AliasModel (model_t *mod, void *buffer);
34 model_t *Mod_LoadModel (model_t *mod, qboolean crash);
36 #define MAX_MOD_KNOWN 512
37 model_t mod_known[MAX_MOD_KNOWN];
40 extern void Mod_BrushInit();
41 extern void Mod_AliasInit();
42 extern void Mod_SpriteInit();
60 Caches the data if needed
63 void *Mod_Extradata (model_t *mod)
67 r = Cache_Check (&mod->cache);
71 Mod_LoadModel (mod, true);
74 Sys_Error ("Mod_Extradata: caching failed");
75 return mod->cache.data;
83 void Mod_ClearAll (void)
88 for (i=0 , mod=mod_known ; i<mod_numknown ; i++, mod++)
89 if (mod->type != mod_alias)
99 model_t *Mod_FindName (char *name)
105 Sys_Error ("Mod_ForName: NULL name");
108 // search the currently loaded models
110 for (i=0 , mod=mod_known ; i<mod_numknown ; i++, mod++)
111 if (!strcmp (mod->name, name) )
114 if (i == mod_numknown)
116 if (mod_numknown == MAX_MOD_KNOWN)
117 Sys_Error ("mod_numknown == MAX_MOD_KNOWN");
118 strcpy (mod->name, name);
119 mod->needload = true;
132 void Mod_TouchModel (char *name)
136 mod = Mod_FindName (name);
140 if (mod->type == mod_alias)
141 Cache_Check (&mod->cache);
149 Loads a model into the cache
152 model_t *Mod_LoadModel (model_t *mod, qboolean crash)
156 byte stackbuf[1024]; // avoid dirtying the cache heap
160 if (mod->type == mod_alias)
162 d = Cache_Check (&mod->cache);
167 return mod; // not cached at all
171 buf = (unsigned *)COM_LoadStackFile (mod->name, stackbuf, sizeof(stackbuf), false);
175 Host_Error ("Mod_NumForName: %s not found", mod->name); // LordHavoc: Sys_Error was *ANNOYING*
179 // allocate a new model
180 COM_FileBase (mod->name, loadname);
184 // call the apropriate loader
185 mod->needload = false;
187 switch (LittleLong(*(unsigned *)buf))
190 Mod_LoadAliasModel (mod, buf);
193 case MD2IDALIASHEADER: // LordHavoc: added Quake2 model support
194 Mod_LoadQ2AliasModel (mod, buf);
198 Mod_LoadSpriteModel (mod, buf);
202 Mod_LoadBrushModel (mod, buf);
213 Loads in a model for the given name
216 model_t *Mod_ForName (char *name, qboolean crash)
220 mod = Mod_FindName (name);
222 return Mod_LoadModel (mod, crash);
232 float RadiusFromBounds (vec3_t mins, vec3_t maxs)
237 for (i=0 ; i<3 ; i++)
238 corner[i] = fabs(mins[i]) > fabs(maxs[i]) ? fabs(mins[i]) : fabs(maxs[i]);
240 return Length (corner);
243 //=============================================================================
250 void Mod_Print (void)
255 Con_Printf ("Cached models:\n");
256 for (i=0, mod=mod_known ; i < mod_numknown ; i++, mod++)
258 Con_Printf ("%8p : %s\n",mod->cache.data, mod->name);