]> icculus.org git repositories - divverent/darkplaces.git/blob - model_shared.c
-safe now does something approximately close to correct. Not a full fix
[divverent/darkplaces.git] / model_shared.c
1 /*
2 Copyright (C) 1996-1997 Id Software, Inc.
3
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.
8
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.  
12
13 See the GNU General Public License for more details.
14
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.
18
19 */
20 // models.c -- model loading and caching
21
22 // models are the only shared resource between a client and server running
23 // on the same machine.
24
25 #include "quakedef.h"
26
27 model_t *loadmodel;
28 char    loadname[32];   // for hunk tags
29
30 // LordHavoc: increased from 512 to 2048
31 #define MAX_MOD_KNOWN   2048
32 model_t mod_known[MAX_MOD_KNOWN];
33 int             mod_numknown;
34
35 /*
36 ===============
37 Mod_Init
38 ===============
39 */
40 void Mod_Init (void)
41 {
42         Mod_BrushInit();
43         Mod_AliasInit();
44         Mod_SpriteInit();
45 }
46
47 /*
48 ===============
49 Mod_Init
50
51 Caches the data if needed
52 ===============
53 */
54 void *Mod_Extradata (model_t *mod)
55 {
56         void    *r;
57
58         r = Cache_Check (&mod->cache);
59         if (r)
60                 return r;
61
62         Mod_LoadModel (mod, true);
63
64         if (!mod->cache.data)
65                 Host_Error ("Mod_Extradata: caching failed");
66         return mod->cache.data;
67 }
68
69 /*
70 ===================
71 Mod_ClearAll
72 ===================
73 */
74 void Mod_ClearAll (void)
75 {
76         int             i;
77         model_t *mod;
78
79         for (i=0 , mod=mod_known ; i<mod_numknown ; i++, mod++)
80                 if (!mod->cachesize)
81                         mod->needload = true;
82 }
83
84 /*
85 ==================
86 Mod_FindName
87
88 ==================
89 */
90 model_t *Mod_FindName (char *name)
91 {
92         int             i;
93         model_t *mod;
94
95         if (!name[0])
96                 Host_Error ("Mod_ForName: NULL name");
97
98 //
99 // search the currently loaded models
100 //
101         for (i=0 , mod=mod_known ; i<mod_numknown ; i++, mod++)
102                 if (!strcmp (mod->name, name) )
103                         break;
104
105         if (i == mod_numknown)
106         {
107                 if (mod_numknown == MAX_MOD_KNOWN)
108                         Host_Error ("mod_numknown == MAX_MOD_KNOWN");
109                 strcpy (mod->name, name);
110                 mod->needload = true;
111                 mod_numknown++;
112         }
113
114         return mod;
115 }
116
117 /*
118 ==================
119 Mod_TouchModel
120
121 ==================
122 */
123 void Mod_TouchModel (char *name)
124 {
125         model_t *mod;
126
127         mod = Mod_FindName (name);
128
129         if (!mod->needload)
130                 if (mod->cachesize)
131                         Cache_Check (&mod->cache);
132 }
133
134 /*
135 ==================
136 Mod_LoadModel
137
138 Loads a model into the cache
139 ==================
140 */
141 model_t *Mod_LoadModel (model_t *mod, qboolean crash)
142 {
143         void    *d;
144         unsigned *buf;
145
146         if (!mod->needload)
147         {
148                 if (mod->cachesize)
149                 {
150                         d = Cache_Check (&mod->cache);
151                         if (d)
152                                 return mod;
153                 }
154                 else
155                         return mod;             // not cached at all
156         }
157
158         Con_DPrintf("loading model %s\n", mod->name);
159
160 // load the file
161         buf = (unsigned *)COM_LoadMallocFile (mod->name, false);
162         if (!buf)
163         {
164                 if (crash)
165                         Host_Error ("Mod_NumForName: %s not found", mod->name); // LordHavoc: Sys_Error was *ANNOYING*
166                 return NULL;
167         }
168
169 // allocate a new model
170 //      COM_FileBase (mod->name, loadname);
171         strcpy(loadname, mod->name);
172
173         loadmodel = mod;
174
175 // call the apropriate loader
176         mod->needload = false;
177
178         // LordHavoc: clear some important stuff in the model_t structure
179         mod->flags = 0;
180         mod->flags2 = 0;
181
182         switch (LittleLong(*(unsigned *)buf))
183         {
184         case IDPOLYHEADER:
185                 Mod_LoadAliasModel (mod, buf);
186                 break;
187
188         case MD2IDALIASHEADER: // LordHavoc: added Quake2 model support
189                 Mod_LoadQ2AliasModel (mod, buf);
190                 break;
191
192         case (('O'<<24)+('M'<<16)+('Y'<<8)+'Z'):
193                 Mod_LoadZymoticModel(mod, buf);
194                 break;
195
196         case IDSPRITEHEADER:
197                 Mod_LoadSpriteModel (mod, buf);
198                 break;
199         
200         default:
201                 Mod_LoadBrushModel (mod, buf);
202                 break;
203         }
204         qfree(buf);
205
206         return mod;
207 }
208
209 /*
210 ==================
211 Mod_ForName
212
213 Loads in a model for the given name
214 ==================
215 */
216 model_t *Mod_ForName (char *name, qboolean crash)
217 {
218         model_t *mod;
219         
220         mod = Mod_FindName (name);
221         
222         return Mod_LoadModel (mod, crash);
223 }
224
225 byte    *mod_base;
226
227 /*
228 =================
229 RadiusFromBounds
230 =================
231 */
232 float RadiusFromBounds (vec3_t mins, vec3_t maxs)
233 {
234         int             i;
235         vec3_t  corner;
236
237         for (i=0 ; i<3 ; i++)
238                 corner[i] = fabs(mins[i]) > fabs(maxs[i]) ? fabs(mins[i]) : fabs(maxs[i]);
239
240         return Length (corner);
241 }
242
243 //=============================================================================
244
245 /*
246 ================
247 Mod_Print
248 ================
249 */
250 void Mod_Print (void)
251 {
252         int             i;
253         model_t *mod;
254
255         Con_Printf ("Cached models:\n");
256         for (i=0, mod=mod_known ; i < mod_numknown ; i++, mod++)
257         {
258                 Con_Printf ("%4iK : %8p : %s\n", (mod->cachesize + 1023) / 1024, mod->cache.data, mod->name);
259         }
260 }
261
262