rewrote memory system entirely (hunk, cache, and zone are gone, memory pools replaced...
[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 cvar_t r_mipsprites = {CVAR_SAVE, "r_mipsprites", "1"};
28
29 /*
30 ===============
31 Mod_SpriteInit
32 ===============
33 */
34 void Mod_SpriteInit (void)
35 {
36         Cvar_RegisterVariable(&r_mipsprites);
37 }
38
39 void Mod_Sprite_StripExtension(char *in, char *out)
40 {
41         char *end;
42         end = in + strlen(in);
43         if ((end - in) >= 6)
44                 if (strcmp(end - 6, ".spr32") == 0)
45                         end -= 6;
46         if ((end - in) >= 4)
47                 if (strcmp(end - 4, ".spr") == 0)
48                         end -= 4;
49         while (in < end)
50                 *out++ = *in++;
51         *out++ = 0;
52 }
53
54 void Mod_Sprite_SharedSetup(long datapointer, int version, int *palette)
55 {
56         int                                     i, j, k, groupframes, realframes, x, y, origin[2], width, height;
57         dspriteframetype_t      *pinframetype;
58         dspriteframe_t          *pinframe;
59         dspritegroup_t          *pingroup;
60         dspriteinterval_t       *pinintervals;
61         float                           modelradius, interval;
62         char                            tempname[MAX_QPATH], name[MAX_QPATH];
63         byte                            *pixbuf;
64         long                            startframes;
65         modelradius = 0;
66
67         Mod_Sprite_StripExtension(loadmodel->name, tempname);
68
69         if (loadmodel->numframes < 1)
70                 Host_Error ("Mod_Sprite_SharedSetup: Invalid # of frames: %d\n", loadmodel->numframes);
71
72         loadmodel->type = mod_sprite;
73         loadmodel->flags = EF_FULLBRIGHT;
74
75         // LordHavoc: hack to allow sprites to be non-fullbright
76         for (i = 0;i < MAX_QPATH && loadmodel->name[i];i++)
77         {
78                 if (loadmodel->name[i] == '!')
79                 {
80                         loadmodel->flags &= ~EF_FULLBRIGHT;
81                         break;
82                 }
83         }
84
85         // LordHavoc: 32bit textures
86         if (version != SPRITE_VERSION && version != SPRITE32_VERSION && version != SPRITEHL_VERSION)
87                 Host_Error("Mod_Sprite_SharedSetup: unsupported version %i, only %i (quake), %i (HalfLife), and %i (sprite32) supported", version, SPRITE_VERSION, SPRITEHL_VERSION, SPRITE32_VERSION);
88
89 //
90 // load the frames
91 //
92         startframes = datapointer;
93         realframes = 0;
94         for (i = 0;i < loadmodel->numframes;i++)
95         {
96                 pinframetype = (dspriteframetype_t *)datapointer;
97                 datapointer += sizeof(dspriteframetype_t);
98
99                 if (LittleLong (pinframetype->type) == SPR_SINGLE)
100                         groupframes = 1;
101                 else
102                 {
103                         pingroup = (dspritegroup_t *)datapointer;
104                         datapointer += sizeof(dspritegroup_t);
105
106                         groupframes = LittleLong(pingroup->numframes);
107
108                         datapointer += sizeof(dspriteinterval_t) * groupframes;
109                 }
110
111                 for (j = 0;j < groupframes;j++)
112                 {
113                         pinframe = (dspriteframe_t *)datapointer;
114                         if (version == SPRITE32_VERSION)
115                                 datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height) * 4;
116                         else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
117                                 datapointer += sizeof(dspriteframe_t) + LittleLong(pinframe->width) * LittleLong(pinframe->height);
118                 }
119                 realframes += groupframes;
120         }
121
122         loadmodel->animscenes = Mem_Alloc(loadmodel->mempool, sizeof(animscene_t) * loadmodel->numframes);
123         loadmodel->sprdata_frames = Mem_Alloc(loadmodel->mempool, sizeof(mspriteframe_t) * realframes);
124
125         datapointer = startframes;
126         realframes = 0;
127         for (i = 0;i < loadmodel->numframes;i++)
128         {
129                 pinframetype = (dspriteframetype_t *)datapointer;
130                 datapointer += sizeof(dspriteframetype_t);
131
132                 if (LittleLong (pinframetype->type) == SPR_SINGLE)
133                 {
134                         groupframes = 1;
135                         interval = 0.1f;
136                 }
137                 else
138                 {
139                         pingroup = (dspritegroup_t *)datapointer;
140                         datapointer += sizeof(dspritegroup_t);
141
142                         groupframes = LittleLong(pingroup->numframes);
143
144                         pinintervals = (dspriteinterval_t *)datapointer;
145                         datapointer += sizeof(dspriteinterval_t) * groupframes;
146
147                         interval = LittleFloat(pinintervals[0].interval);
148                         if (interval < 0.01f)
149                                 Host_Error("Mod_Sprite_SharedSetup: invalid interval");
150                 }
151
152                 sprintf(loadmodel->animscenes[i].name, "frame %i", i);
153                 loadmodel->animscenes[i].firstframe = realframes;
154                 loadmodel->animscenes[i].framecount = groupframes;
155                 loadmodel->animscenes[i].framerate = 1.0f / interval;
156                 loadmodel->animscenes[i].loop = true;
157
158                 for (j = 0;j < groupframes;j++)
159                 {
160                         pinframe = (dspriteframe_t *)datapointer;
161                         datapointer += sizeof(dspriteframe_t);
162
163                         origin[0] = LittleLong (pinframe->origin[0]);
164                         origin[1] = LittleLong (pinframe->origin[1]);
165                         width = LittleLong (pinframe->width);
166                         height = LittleLong (pinframe->height);
167
168                         loadmodel->sprdata_frames[realframes].left = origin[0];
169                         loadmodel->sprdata_frames[realframes].right = origin[0] + width;
170                         loadmodel->sprdata_frames[realframes].up = origin[1];
171                         loadmodel->sprdata_frames[realframes].down = origin[1] - height;
172
173                         x = max(loadmodel->sprdata_frames[realframes].left * loadmodel->sprdata_frames[realframes].left, loadmodel->sprdata_frames[realframes].right * loadmodel->sprdata_frames[realframes].right);
174                         y = max(loadmodel->sprdata_frames[realframes].up * loadmodel->sprdata_frames[realframes].up, loadmodel->sprdata_frames[realframes].down * loadmodel->sprdata_frames[realframes].down);
175                         if (modelradius < x + y)
176                                 modelradius = x + y;
177
178                         if (groupframes > 1)
179                                 sprintf (name, "%s_%i_%i", tempname, i, j);
180                         else
181                                 sprintf (name, "%s_%i", tempname, i);
182                         loadmodel->sprdata_frames[realframes].texture = loadtextureimagewithmask(loadmodel->texturepool, name, 0, 0, false, r_mipsprites.integer, true);
183                         loadmodel->sprdata_frames[realframes].fogtexture = image_masktex;
184
185                         if (!loadmodel->sprdata_frames[realframes].texture)
186                         {
187                                 pixbuf = Mem_Alloc(tempmempool, width*height*4);
188                                 if (version == SPRITE32_VERSION)
189                                         memcpy(pixbuf, (byte *)datapointer, width*height*4);
190                                 else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
191                                         Image_Copy8bitRGBA((byte *)datapointer, pixbuf, width*height, palette);
192
193                                 loadmodel->sprdata_frames[realframes].texture = R_LoadTexture (loadmodel->texturepool, name, width, height, pixbuf, TEXTYPE_RGBA, TEXF_ALPHA | (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_PRECACHE);
194
195                                 // make fog version (just alpha)
196                                 for (k = 0;k < width*height;k++)
197                                 {
198                                         pixbuf[k*4+0] = 255;
199                                         pixbuf[k*4+1] = 255;
200                                         pixbuf[k*4+2] = 255;
201                                 }
202                                 if (groupframes > 1)
203                                         sprintf (name, "%s_%i_%ifog", tempname, i, j);
204                                 else
205                                         sprintf (name, "%s_%ifog", tempname, i);
206                                 loadmodel->sprdata_frames[realframes].fogtexture = R_LoadTexture (loadmodel->texturepool, name, width, height, pixbuf, TEXTYPE_RGBA, TEXF_ALPHA | (r_mipsprites.integer ? TEXF_MIPMAP : 0) | TEXF_PRECACHE);
207
208                                 Mem_Free(pixbuf);
209                         }
210
211                         if (version == SPRITE32_VERSION)
212                                 datapointer += width * height * 4;
213                         else //if (version == SPRITE_VERSION || version == SPRITEHL_VERSION)
214                                 datapointer += width * height;
215                         realframes++;
216                 }
217         }
218
219         modelradius = sqrt(modelradius);
220         for (i = 0;i < 3;i++)
221         {
222                 loadmodel->normalmins[i] = loadmodel->yawmins[i] = loadmodel->rotatedmins[i] = -modelradius;
223                 loadmodel->normalmaxs[i] = loadmodel->yawmaxs[i] = loadmodel->rotatedmaxs[i] = modelradius;
224         }
225 //      loadmodel->modelradius = modelradius;
226 }
227
228 void Mod_Sprite_SERAddEntity(void)
229 {
230         R_ClipSprite();
231 }
232
233
234 /*
235 =================
236 Mod_LoadSpriteModel
237 =================
238 */
239 void Mod_LoadSpriteModel (model_t *mod, void *buffer)
240 {
241         int version;
242         loadmodel->SERAddEntity = Mod_Sprite_SERAddEntity;
243         loadmodel->Draw = R_DrawSpriteModel;
244         loadmodel->DrawSky = NULL;
245         loadmodel->DrawShadow = NULL;
246
247         version = LittleLong(((dsprite_t *)buffer)->version);
248         if (version == SPRITE_VERSION || SPRITE32_VERSION)
249         {
250                 dsprite_t                       *pinsprite;
251                 long                            datapointer;
252
253                 datapointer = (long) buffer;
254
255                 pinsprite = (dsprite_t *)datapointer;
256                 datapointer += sizeof(dsprite_t);
257
258                 loadmodel->numframes = LittleLong (pinsprite->numframes);
259                 loadmodel->sprnum_type = LittleLong (pinsprite->type);
260                 loadmodel->synctype = LittleLong (pinsprite->synctype);
261
262                 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinsprite->version), d_8to24table);
263         }
264         else if (version == SPRITEHL_VERSION)
265         {
266                 int                                     i, rendermode;
267                 byte                            palette[256][4], *in;
268                 dspritehl_t                     *pinsprite;
269                 long                            datapointer;
270
271                 datapointer = (long) buffer;
272
273                 pinsprite = (dspritehl_t *)datapointer;
274                 datapointer += sizeof(dspritehl_t);
275
276                 loadmodel->numframes = LittleLong (pinsprite->numframes);
277                 loadmodel->sprnum_type = LittleLong (pinsprite->type);
278                 loadmodel->synctype = LittleLong (pinsprite->synctype);
279                 rendermode = pinsprite->rendermode;
280
281                 in = (byte *)datapointer;
282                 datapointer += 2;
283                 i = in[0] + in[1] * 256;
284                 if (i != 256)
285                         Host_Error ("Mod_LoadHLSprite: unexpected number of palette colors %i (should be 256)", i);
286                 in = (byte *)datapointer;
287                 datapointer += 768;
288                 switch(rendermode)
289                 {
290                 case SPRHL_NORMAL:
291                         for (i = 0;i < 255;i++)
292                         {
293                                 palette[i][0] = *in++;
294                                 palette[i][1] = *in++;
295                                 palette[i][2] = *in++;
296                                 palette[i][3] = 255;
297                         }
298                         palette[255][0] = palette[255][1] = palette[255][2] = palette[255][3] = 0;
299                         break;
300                 case SPRHL_ADDITIVE:
301                         for (i = 0;i < 256;i++)
302                         {
303                                 palette[i][0] = *in++;
304                                 palette[i][1] = *in++;
305                                 palette[i][2] = *in++;
306                                 palette[i][3] = 255;
307                         }
308         //              palette[255][0] = palette[255][1] = palette[255][2] = palette[255][3] = 0;
309                         loadmodel->flags |= EF_ADDITIVE;
310                         break;
311                 case SPRHL_INDEXALPHA:
312                         for (i = 0;i < 256;i++)
313                         {
314                                 palette[i][0] = 255;
315                                 palette[i][1] = 255;
316                                 palette[i][2] = 255;
317                                 palette[i][3] = i;
318                                 in += 3;
319                         }
320                         break;
321                 case SPRHL_ALPHATEST:
322                         for (i = 0;i < 255;i++)
323                         {
324                                 palette[i][0] = *in++;
325                                 palette[i][1] = *in++;
326                                 palette[i][2] = *in++;
327                                 palette[i][3] = 255;
328                         }
329                         palette[0][0] = palette[0][1] = palette[0][2] = palette[0][3] = 0;
330                         break;
331                 default:
332                         Host_Error("Mod_LoadHLSprite: unknown texFormat (%i, should be 0, 1, 2, or 3)\n", i);
333                         return;
334                 }
335
336                 Mod_Sprite_SharedSetup(datapointer, LittleLong (pinsprite->version), (int *)(&palette[0][0]));
337         }
338         else
339                 Host_Error ("Mod_LoadSpriteModel: %s has wrong version number (%i should be 1 (quake) or 32 (sprite32) or 2 (halflife)", loadmodel->name, version);
340 }