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.
29 // LordHavoc: increased from 512 to 2048
30 #define MAX_MOD_KNOWN 2048
31 static model_t mod_known[MAX_MOD_KNOWN];
33 rtexture_t *r_notexture;
34 rtexturepool_t *r_notexturepool;
36 texture_t r_surf_notexture;
38 void Mod_SetupNoTexture(void)
43 // this makes a light grey/dark grey checkerboard texture
44 for (y = 0;y < 16;y++)
46 for (x = 0;x < 16;x++)
48 if ((y < 8) ^ (x < 8))
65 r_notexturepool = R_AllocTexturePool();
66 r_notexture = R_LoadTexture(r_notexturepool, "notexture", 16, 16, &pix[0][0][0], TEXTYPE_RGBA, TEXF_MIPMAP);
69 extern void Mod_BrushStartup (void);
70 extern void Mod_BrushShutdown (void);
72 static void mod_start(void)
75 for (i = 0;i < MAX_MOD_KNOWN;i++)
76 if (mod_known[i].name[0])
77 Mod_UnloadModel(&mod_known[i]);
83 static void mod_shutdown(void)
86 for (i = 0;i < MAX_MOD_KNOWN;i++)
87 if (mod_known[i].name[0])
88 Mod_UnloadModel(&mod_known[i]);
90 R_FreeTexturePool(&r_notexturepool);
94 static void mod_newmap(void)
103 static void Mod_Print (void);
104 static void Mod_Flush (void);
111 Cmd_AddCommand ("modellist", Mod_Print);
112 Cmd_AddCommand ("modelflush", Mod_Flush);
115 void Mod_RenderInit(void)
117 R_RegisterModule("Models", mod_start, mod_shutdown, mod_newmap);
120 void Mod_FreeModel (model_t *mod)
122 R_FreeTexturePool(&mod->texturepool);
123 Mem_FreePool(&mod->mempool);
125 // clear the struct to make it available
126 memset(mod, 0, sizeof(model_t));
129 void Mod_UnloadModel (model_t *mod)
131 char name[MAX_QPATH];
132 qboolean isworldmodel;
133 strcpy(name, mod->name);
134 isworldmodel = mod->isworldmodel;
136 strcpy(mod->name, name);
137 mod->isworldmodel = isworldmodel;
138 mod->needload = true;
148 static model_t *Mod_LoadModel (model_t *mod, qboolean crash, qboolean checkdisk, qboolean isworldmodel)
155 if (mod->name[0] == '*') // submodel
164 buf = COM_LoadFile (mod->name, false);
168 Host_Error ("Mod_LoadModel: %s not found", mod->name); // LordHavoc: Sys_Error was *ANNOYING*
172 crc = CRC_Block(buf, com_filesize);
177 if (mod->crc == crc && mod->isworldmodel == isworldmodel)
181 return mod; // already loaded
185 Con_DPrintf("loading model %s\n", mod->name);
189 buf = COM_LoadFile (mod->name, false);
193 Host_Error ("Mod_LoadModel: %s not found", mod->name);
196 crc = CRC_Block(buf, com_filesize);
199 // allocate a new model
202 // LordHavoc: unload the existing model in this slot (if there is one)
203 Mod_UnloadModel(mod);
204 mod->isworldmodel = isworldmodel;
205 mod->needload = false;
209 // all models use memory, so allocate a memory pool
210 mod->mempool = Mem_AllocPool(mod->name);
211 // all models load textures, so allocate a texture pool
212 if (cls.state != ca_dedicated)
213 mod->texturepool = R_AllocTexturePool();
215 // call the apropriate loader
216 if (!memcmp(buf, "IDPO" , 4)) Mod_LoadAliasModel (mod, buf);
217 else if (!memcmp(buf, "IDP2" , 4)) Mod_LoadQ2AliasModel(mod, buf);
218 else if (!memcmp(buf, "ZYMOTIC" , 7)) Mod_LoadZymoticModel(mod, buf);
219 else if (!memcmp(buf, "IDSP" , 4)) Mod_LoadSpriteModel (mod, buf);
220 else Mod_LoadBrushModel (mod, buf);
227 void Mod_CheckLoaded (model_t *mod)
232 Mod_LoadModel(mod, true, true, mod->isworldmodel);
235 if (mod->type == mod_invalid)
236 Host_Error("Mod_CheckLoaded: invalid model\n");
248 void Mod_ClearAll (void)
252 void Mod_ClearUsed(void)
257 for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
262 void Mod_PurgeUnused(void)
267 for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
279 model_t *Mod_FindName (char *name)
282 model_t *mod, *freemod;
285 Host_Error ("Mod_ForName: NULL name");
287 // search the currently loaded models
289 for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
293 if (!strcmp (mod->name, name))
299 else if (freemod == NULL)
306 strcpy (mod->name, name);
307 mod->needload = true;
312 Host_Error ("Mod_FindName: ran out of models\n");
322 void Mod_TouchModel (char *name)
326 mod = Mod_FindName (name);
334 Loads in a model for the given name
337 model_t *Mod_ForName (char *name, qboolean crash, qboolean checkdisk, qboolean isworldmodel)
339 return Mod_LoadModel (Mod_FindName (name), crash, checkdisk, isworldmodel);
345 //=============================================================================
352 static void Mod_Print (void)
357 Con_Printf ("Loaded models:\n");
358 for (i = 0, mod = mod_known;i < MAX_MOD_KNOWN;i++, mod++)
360 Con_Printf ("%4iK %s\n", mod->mempool ? (mod->mempool->totalsize + 1023) / 1024 : 0, mod->name);
363 static void Mod_Flush (void)
367 Con_Printf ("Unloading models\n");
368 for (i = 0;i < MAX_MOD_KNOWN;i++)
369 if (mod_known[i].name[0])
370 Mod_UnloadModel(&mod_known[i]);