]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - model_shared.c
put in dprinting of model names as they are loaded
[xonotic/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 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);
35
36 #define MAX_MOD_KNOWN   512
37 model_t mod_known[MAX_MOD_KNOWN];
38 int             mod_numknown;
39
40 extern void Mod_BrushInit();
41 extern void Mod_AliasInit();
42 extern void Mod_SpriteInit();
43
44 /*
45 ===============
46 Mod_Init
47 ===============
48 */
49 void Mod_Init (void)
50 {
51         Mod_BrushInit();
52         Mod_AliasInit();
53         Mod_SpriteInit();
54 }
55
56 /*
57 ===============
58 Mod_Init
59
60 Caches the data if needed
61 ===============
62 */
63 void *Mod_Extradata (model_t *mod)
64 {
65         void    *r;
66         
67         r = Cache_Check (&mod->cache);
68         if (r)
69                 return r;
70
71         Mod_LoadModel (mod, true);
72         
73         if (!mod->cache.data)
74                 Host_Error ("Mod_Extradata: caching failed");
75         return mod->cache.data;
76 }
77
78 /*
79 ===================
80 Mod_ClearAll
81 ===================
82 */
83 void Mod_ClearAll (void)
84 {
85         int             i;
86         model_t *mod;
87         
88         for (i=0 , mod=mod_known ; i<mod_numknown ; i++, mod++)
89                 if (mod->type != mod_alias)
90                         mod->needload = true;
91 }
92
93 /*
94 ==================
95 Mod_FindName
96
97 ==================
98 */
99 model_t *Mod_FindName (char *name)
100 {
101         int             i;
102         model_t *mod;
103         
104         if (!name[0])
105                 Host_Error ("Mod_ForName: NULL name");
106                 
107 //
108 // search the currently loaded models
109 //
110         for (i=0 , mod=mod_known ; i<mod_numknown ; i++, mod++)
111                 if (!strcmp (mod->name, name) )
112                         break;
113                         
114         if (i == mod_numknown)
115         {
116                 if (mod_numknown == MAX_MOD_KNOWN)
117                         Host_Error ("mod_numknown == MAX_MOD_KNOWN");
118                 strcpy (mod->name, name);
119                 mod->needload = true;
120                 mod_numknown++;
121         }
122
123         return mod;
124 }
125
126 /*
127 ==================
128 Mod_TouchModel
129
130 ==================
131 */
132 void Mod_TouchModel (char *name)
133 {
134         model_t *mod;
135         
136         mod = Mod_FindName (name);
137         
138         if (!mod->needload)
139         {
140                 if (mod->type == mod_alias)
141                         Cache_Check (&mod->cache);
142         }
143 }
144
145 /*
146 ==================
147 Mod_LoadModel
148
149 Loads a model into the cache
150 ==================
151 */
152 model_t *Mod_LoadModel (model_t *mod, qboolean crash)
153 {
154         void    *d;
155         unsigned *buf;
156 //      byte    stackbuf[1024];         // avoid dirtying the cache heap
157         int             blah = 0;
158
159         if (!mod->needload)
160         {
161                 if (mod->type == mod_alias)
162                 {
163                         d = Cache_Check (&mod->cache);
164                         if (d)
165                                 return mod;
166                 }
167                 else
168                         return mod;             // not cached at all
169         }
170
171         Con_DPrintf("loading model %s\n", mod->name);
172
173 // load the file
174         buf = (unsigned *)COM_LoadMallocFile (mod->name, false);
175 //      buf = (unsigned *)COM_LoadStackFile (mod->name, stackbuf, sizeof(stackbuf), false);
176         if (!buf)
177         {
178                 if (crash)
179                         Host_Error ("Mod_NumForName: %s not found", mod->name); // LordHavoc: Sys_Error was *ANNOYING*
180                 return NULL;
181         }
182         
183 // allocate a new model
184         COM_FileBase (mod->name, loadname);
185         
186         loadmodel = mod;
187
188 // call the apropriate loader
189         mod->needload = false;
190         
191         switch (LittleLong(*(unsigned *)buf))
192         {
193         case IDPOLYHEADER:
194                 Mod_LoadAliasModel (mod, buf);
195                 break;
196
197         case MD2IDALIASHEADER: // LordHavoc: added Quake2 model support
198                 Mod_LoadQ2AliasModel (mod, buf);
199                 break;
200                 
201         case IDSPRITEHEADER:
202                 Mod_LoadSpriteModel (mod, buf);
203                 break;
204         
205         default:
206                 Mod_LoadBrushModel (mod, buf);
207                 break;
208         }
209         free(buf);
210
211         return mod;
212 }
213
214 /*
215 ==================
216 Mod_ForName
217
218 Loads in a model for the given name
219 ==================
220 */
221 model_t *Mod_ForName (char *name, qboolean crash)
222 {
223         model_t *mod;
224         
225         mod = Mod_FindName (name);
226         
227         return Mod_LoadModel (mod, crash);
228 }
229
230 byte    *mod_base;
231
232 /*
233 =================
234 RadiusFromBounds
235 =================
236 */
237 float RadiusFromBounds (vec3_t mins, vec3_t maxs)
238 {
239         int             i;
240         vec3_t  corner;
241
242         for (i=0 ; i<3 ; i++)
243                 corner[i] = fabs(mins[i]) > fabs(maxs[i]) ? fabs(mins[i]) : fabs(maxs[i]);
244
245         return Length (corner);
246 }
247
248 //=============================================================================
249
250 /*
251 ================
252 Mod_Print
253 ================
254 */
255 void Mod_Print (void)
256 {
257         int             i;
258         model_t *mod;
259
260         Con_Printf ("Cached models:\n");
261         for (i=0, mod=mod_known ; i < mod_numknown ; i++, mod++)
262         {
263                 Con_Printf ("%8p : %s\n",mod->cache.data, mod->name);
264         }
265 }
266
267