]> de.git.xonotic.org Git - xonotic/darkplaces.git/blob - model_sprite.c
every malloc/calloc/free converted to qmalloc/qfree with tracking (memstats command...
[xonotic/darkplaces.git] / model_sprite.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 /*
28 ===============
29 Mod_SpriteInit
30 ===============
31 */
32 void Mod_SpriteInit (void)
33 {
34 }
35
36 void Mod_Sprite_StripExtension(char *in, char *out)
37 {
38         char *end;
39         end = in + strlen(in);
40         if ((end - in) >= 6)
41                 if (strcmp(end - 6, ".spr32") == 0)
42                         end -= 6;
43         if ((end - in) >= 4)
44                 if (strcmp(end - 4, ".spr") == 0)
45                         end -= 4;
46         while (in < end)
47                 *out++ = *in++;
48         *out++ = 0;
49 }
50
51 /*
52 =================
53 Mod_LoadSpriteFrame
54 =================
55 */
56 void * Mod_LoadSpriteFrame (void * pin, mspriteframe_t **ppframe, int framenum, int bytesperpixel)
57 {
58         dspriteframe_t          *pinframe;
59         mspriteframe_t          *pspriteframe;
60         int                                     i, width, height, size, origin[2];
61         char                            name[256], tempname[256];
62         byte                            *pixbuf, *pixel, *inpixel;
63
64         pinframe = (dspriteframe_t *)pin;
65
66         width = LittleLong (pinframe->width);
67         height = LittleLong (pinframe->height);
68         size = width * height * bytesperpixel;
69
70         pspriteframe = Hunk_AllocName (sizeof (mspriteframe_t),loadname);
71
72         memset (pspriteframe, 0, sizeof (mspriteframe_t));
73
74         *ppframe = pspriteframe;
75
76         pspriteframe->width = width;
77         pspriteframe->height = height;
78         origin[0] = LittleLong (pinframe->origin[0]);
79         origin[1] = LittleLong (pinframe->origin[1]);
80
81         pspriteframe->up = origin[1];
82         pspriteframe->down = origin[1] - height;
83         pspriteframe->left = origin[0];
84         pspriteframe->right = width + origin[0];
85
86         Mod_Sprite_StripExtension(loadmodel->name, tempname);
87         sprintf (name, "%s_%i", tempname, framenum);
88         pspriteframe->gl_texturenum = loadtextureimagewithmask(name, 0, 0, false, true);
89         pspriteframe->gl_fogtexturenum = image_masktexnum;
90         if (pspriteframe->gl_texturenum == 0)
91         {
92                 pspriteframe->gl_texturenum = GL_LoadTexture (name, width, height, (byte *)(pinframe + 1), true, true, bytesperpixel);
93                 // make fog version (just alpha)
94                 pixbuf = pixel = qmalloc(width*height*4);
95                 inpixel = (byte *)(pinframe + 1);
96                 if (bytesperpixel == 1)
97                 {
98                         for (i = 0;i < width*height;i++)
99                         {
100                                 *pixel++ = 255;
101                                 *pixel++ = 255;
102                                 *pixel++ = 255;
103                                 if (*inpixel++ != 255)
104                                         *pixel++ = 255;
105                                 else
106                                         *pixel++ = 0;
107                         }
108                 }
109                 else
110                 {
111                         inpixel+=3;
112                         for (i = 0;i < width*height;i++)
113                         {
114                                 *pixel++ = 255;
115                                 *pixel++ = 255;
116                                 *pixel++ = 255;
117                                 *pixel++ = *inpixel;
118                                 inpixel+=4;
119                         }
120                 }
121                 sprintf (name, "%s_%ifog", loadmodel->name, framenum);
122                 pspriteframe->gl_fogtexturenum = GL_LoadTexture (name, width, height, pixbuf, true, true, 4);
123                 qfree(pixbuf);
124         }
125
126         return (void *)((byte *)pinframe + sizeof (dspriteframe_t) + size);
127 }
128
129
130 /*
131 =================
132 Mod_LoadSpriteGroup
133 =================
134 */
135 void * Mod_LoadSpriteGroup (void * pin, mspriteframe_t **ppframe, int framenum, int bytesperpixel)
136 {
137         dspritegroup_t          *pingroup;
138         mspritegroup_t          *pspritegroup;
139         int                                     i, numframes;
140         dspriteinterval_t       *pin_intervals;
141         float                           *poutintervals;
142         void                            *ptemp;
143
144         pingroup = (dspritegroup_t *)pin;
145
146         numframes = LittleLong (pingroup->numframes);
147
148         pspritegroup = Hunk_AllocName (sizeof (mspritegroup_t) +
149                                 (numframes - 1) * sizeof (pspritegroup->frames[0]), loadname);
150
151         pspritegroup->numframes = numframes;
152
153         *ppframe = (mspriteframe_t *)pspritegroup;
154
155         pin_intervals = (dspriteinterval_t *)(pingroup + 1);
156
157         poutintervals = Hunk_AllocName (numframes * sizeof (float), loadname);
158
159         pspritegroup->intervals = poutintervals;
160
161         for (i=0 ; i<numframes ; i++)
162         {
163                 *poutintervals = LittleFloat (pin_intervals->interval);
164                 if (*poutintervals <= 0.0)
165                         Host_Error ("Mod_LoadSpriteGroup: interval<=0");
166
167                 poutintervals++;
168                 pin_intervals++;
169         }
170
171         ptemp = (void *)pin_intervals;
172
173         for (i=0 ; i<numframes ; i++)
174                 ptemp = Mod_LoadSpriteFrame (ptemp, &pspritegroup->frames[i], framenum * 100 + i, bytesperpixel);
175
176         return ptemp;
177 }
178
179
180 /*
181 =================
182 Mod_LoadSpriteModel
183 =================
184 */
185 void Mod_LoadSpriteModel (model_t *mod, void *buffer)
186 {
187         int                                     i;
188         int                                     version;
189         dsprite_t                       *pin;
190         msprite_t                       *psprite;
191         int                                     numframes;
192         int                                     size;
193         dspriteframetype_t      *pframetype;
194         // LordHavoc: 32bit textures
195         int             bytesperpixel;
196
197         mod->flags = EF_FULLBRIGHT;
198         // LordHavoc: hack to allow sprites to be non-fullbright
199         for (i = 0;i < MAX_QPATH && mod->name[i];i++)
200         {
201                 if (mod->name[i] == '!')
202                 {
203                         mod->flags &= ~EF_FULLBRIGHT;
204                         break;
205                 }
206         }
207
208         pin = (dsprite_t *)buffer;
209
210         version = LittleLong (pin->version);
211         if (version == 2)
212         {
213                 version = 32;
214                 Con_Printf("warning: %s is a version 2 sprite (RGBA), supported for now, please hex edit to version 32 incase HalfLife sprites might be supported at some point.\n", mod->name);
215         }
216         // LordHavoc: 32bit textures
217         if (version != SPRITE_VERSION && version != SPRITE32_VERSION)
218                 Host_Error ("%s has wrong version number "
219                                  "(%i should be %i or %i)", mod->name, version, SPRITE_VERSION, SPRITE32_VERSION);
220         bytesperpixel = 1;
221         if (version == SPRITE32_VERSION)
222                 bytesperpixel = 4;
223
224         numframes = LittleLong (pin->numframes);
225
226         size = sizeof (msprite_t) +     (numframes - 1) * sizeof (psprite->frames);
227
228         psprite = Hunk_AllocName (size, loadname);
229
230         mod->cache.data = psprite;
231
232         psprite->type = LittleLong (pin->type);
233         psprite->maxwidth = LittleLong (pin->width);
234         psprite->maxheight = LittleLong (pin->height);
235         psprite->beamlength = LittleFloat (pin->beamlength);
236         mod->synctype = LittleLong (pin->synctype);
237         psprite->numframes = numframes;
238
239         mod->mins[0] = mod->mins[1] = -psprite->maxwidth/2;
240         mod->maxs[0] = mod->maxs[1] = psprite->maxwidth/2;
241         mod->mins[2] = -psprite->maxheight/2;
242         mod->maxs[2] = psprite->maxheight/2;
243         
244 //
245 // load the frames
246 //
247         if (numframes < 1)
248                 Host_Error ("Mod_LoadSpriteModel: Invalid # of frames: %d\n", numframes);
249
250         mod->numframes = numframes;
251
252         pframetype = (dspriteframetype_t *)(pin + 1);
253
254         for (i=0 ; i<numframes ; i++)
255         {
256                 spriteframetype_t       frametype;
257
258                 frametype = LittleLong (pframetype->type);
259                 psprite->frames[i].type = frametype;
260
261                 if (frametype == SPR_SINGLE)
262                         pframetype = (dspriteframetype_t *) Mod_LoadSpriteFrame (pframetype + 1, &psprite->frames[i].frameptr, i, bytesperpixel);
263                 else
264                         pframetype = (dspriteframetype_t *) Mod_LoadSpriteGroup (pframetype + 1, &psprite->frames[i].frameptr, i, bytesperpixel);
265         }
266
267         mod->type = mod_sprite;
268 }