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