forked from SpiritQuaddicted/reQuiem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gl_sprite.c
300 lines (237 loc) · 8.39 KB
/
gl_sprite.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
/*
Copyright (C) 1996-1997 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
// gl_sprite.c -- sprite model loading and caching (see gl_drawsprite.c for draw routines)
// models are the only shared resource between a client and server running
// on the same machine.
#ifndef RQM_SV_ONLY
#include "quakedef.h"
extern cvar_t nospr32;
static int spr_version;
#ifndef RQM_SV_ONLY
static const char *spr_paths[] = {"textures/sprites/", "textures/", NULL, NULL};
// 3rd path set dynamically based on sprite's folder
/*
=================
Mod_Load24bitSprite
=================
*/
int Mod_Load24bitSprite (const char *sprite_name, const char *identifier, int framenum, int numingroup, int flags)
{
char basename[MAX_QPATH], filename[MAX_QPATH], path[MAX_QPATH];
char *name;
const char *namelist[3] = {filename, COM_SkipPath(identifier), NULL};
int texturenum;
COM_StripExtension (sprite_name, basename, sizeof(basename));
name = COM_SkipPath (basename);
if (name != basename)
{
Q_strncpy (path, sizeof(path), basename, name-basename);
spr_paths[2] = path;
}
else
spr_paths[2] = NULL;
if (numingroup > 1)
Q_snprintfz (filename, sizeof(filename), "%s_%i_%i", name, framenum/100, framenum%100);
else
Q_snprintfz (filename, sizeof(filename), "%s_%i", name, framenum);
texturenum = GL_LoadTextureImage_MultiSource (spr_paths, namelist, identifier, flags, mod_loadpath->dir_level);
/* Q_snprintfz (loadpath, sizeof(loadpath), "%s_%i", basename, framenum);
texturenum = GL_LoadTextureImage ("textures/sprites/", loadpath, identifier, flags, mod_loadpath->dir_level);
if (texturenum == 0)
{
texturenum = GL_LoadTextureImage ("textures/", loadpath, identifier, flags, mod_loadpath->dir_level);
if ( texturenum == 0 )
{
Q_snprintfz (loadpath, sizeof(loadpath), "%s", identifier);
texturenum = GL_LoadTextureImage ("textures/", loadpath, identifier, flags, mod_loadpath->dir_level);
}
}
*/
return texturenum;
}
/*
=================
Mod_LoadSpriteModelTexture
=================
*/
void Mod_LoadSpriteModelTexture (const char *sprite_name, const dspriteframe_t *pinframe,
mspriteframe_t *pspriteframe, int framenum, int numingroup, int texture_flag)
{
char identifier[MAX_QPATH];
int bytes;
if (numingroup > 1)
Q_snprintfz (identifier, sizeof(identifier), "%s_%i_%i", sprite_name, framenum/100, framenum%100);
else
Q_snprintfz (identifier, sizeof(identifier), "%s_%i", sprite_name, framenum);
if (!no24bit && !isDedicated) // JDH: added no24bit check here
{
pspriteframe->gl_texturenum = Mod_Load24bitSprite (sprite_name, identifier, framenum, numingroup, texture_flag);
if (pspriteframe->gl_texturenum)
{
pspriteframe->is_tex_external = true;
return;
}
}
bytes = (spr_version == SPRITE32_VERSION) ? 4 : 1;
pspriteframe->gl_texturenum = GL_LoadTexture (identifier, pspriteframe->width, pspriteframe->height,
(byte *)(pinframe + 1), texture_flag, bytes);
}
#endif //#ifndef RQM_SV_ONLY
/*
=================
Mod_LoadSpriteFrame
=================
*/
void *Mod_LoadSpriteFrame (model_t *mod, void *pin, mspriteframe_t **ppframe, int framenum, int numingroup)
{
dspriteframe_t *pinframe;
mspriteframe_t *pspriteframe;
int width, height, size, origin[2];
#ifndef RQM_SV_ONLY
int texture_flag;
#endif
pinframe = (dspriteframe_t *) pin;
width = LittleLong (pinframe->width);
height = LittleLong (pinframe->height);
size = width * height * ((mod->modhint == MOD_SPR32) ? 4 : 1);
pspriteframe = Hunk_AllocName (sizeof(mspriteframe_t), mod_loadname);
memset (pspriteframe, 0, sizeof(mspriteframe_t));
*ppframe = pspriteframe;
pspriteframe->width = width;
pspriteframe->height = height;
origin[0] = LittleLong (pinframe->origin[0]);
origin[1] = LittleLong (pinframe->origin[1]);
pspriteframe->up = origin[1];
pspriteframe->down = origin[1] - height;
pspriteframe->left = origin[0];
pspriteframe->right = width + origin[0];
pspriteframe->is_tex_external = false;
#ifndef RQM_SV_ONLY
// texture_flag = (gl_picmip_all.value ? TEX_MIPMAP : 0) | TEX_ALPHA | TEX_NOTILE;
texture_flag = TEX_MIPMAP | TEX_ALPHA | TEX_NOTILE;
Mod_LoadSpriteModelTexture (mod->name, pinframe, pspriteframe, framenum, numingroup, texture_flag);
#else
pspriteframe->gl_texturenum = 0;
#endif
return (byte *)(pinframe + 1) + size;
}
/*
=================
Mod_LoadSpriteGroup
=================
*/
void *Mod_LoadSpriteGroup (model_t *mod, void *pin, mspriteframe_t **ppframe, int framenum)
{
dspritegroup_t *pingroup;
mspritegroup_t *pspritegroup;
int i, numframes;
dspriteinterval_t *pin_intervals;
float *poutintervals;
void *ptemp;
pingroup = (dspritegroup_t *)pin;
numframes = LittleLong (pingroup->numframes);
pspritegroup = Hunk_AllocName (sizeof(mspritegroup_t) + (numframes - 1) * sizeof(mspriteframe_t), mod_loadname);
pspritegroup->numframes = numframes;
*ppframe = (mspriteframe_t *)pspritegroup;
pin_intervals = (dspriteinterval_t *)(pingroup + 1);
poutintervals = Hunk_AllocName (numframes * sizeof(float), mod_loadname);
pspritegroup->intervals = poutintervals;
for (i=0 ; i<numframes ; i++)
{
*poutintervals = LittleFloat (pin_intervals->interval);
if (*poutintervals <= 0.0)
Sys_Error ("Mod_LoadSpriteGroup: interval <= 0");
poutintervals++;
pin_intervals++;
}
ptemp = (void *)pin_intervals;
for (i=0 ; i<numframes ; i++)
ptemp = Mod_LoadSpriteFrame (mod, ptemp, &pspritegroup->frames[i], framenum * 100 + i, numframes);
return ptemp;
}
/*
=================
Mod_LoadSpriteFrames
=================
*/
void Mod_LoadSpriteFrames (model_t *mod, dspriteframetype_t *pinframe, msprite_t *psprite)
{
int i;
spriteframetype_t frametype;
for (i=0 ; i<psprite->numframes ; i++)
{
frametype = LittleLong (pinframe->type);
psprite->frames[i].type = frametype;
if (frametype == SPR_SINGLE)
{
pinframe = (dspriteframetype_t *)Mod_LoadSpriteFrame (mod, pinframe + 1, &psprite->frames[i].frameptr, i, 1);
}
else
{
pinframe = (dspriteframetype_t *)Mod_LoadSpriteGroup (mod, pinframe + 1, &psprite->frames[i].frameptr, i);
}
}
}
/*
=================
Mod_LoadSpriteHeader
=================
*/
void Mod_LoadSpriteHeader (const dsprite_t *pin, msprite_t *psprite)
{
psprite->type = LittleLong (pin->type);
psprite->maxwidth = LittleLong (pin->width);
psprite->maxheight = LittleLong (pin->height);
psprite->beamlength = LittleFloat (pin->beamlength);
psprite->numframes = LittleLong (pin->numframes);
}
/*
=================
Mod_LoadSpriteModel
=================
*/
void Mod_LoadSpriteModel (model_t *mod, const void *buffer)
{
int numframes, size;
dsprite_t *pin;
msprite_t *psprite;
dspriteframetype_t *pframetype;
pin = (dsprite_t *)buffer;
spr_version = LittleLong (pin->version);
if (spr_version != SPRITE_VERSION && spr_version != SPRITE32_VERSION)
Sys_Error ("%s has wrong version number (%i should be %i or %i)", mod->name, spr_version, SPRITE_VERSION, SPRITE32_VERSION);
if (nospr32.value && spr_version == SPRITE32_VERSION)
return;
// joe: indicating whether it's a 32bit sprite or not
mod->modhint = (spr_version == SPRITE32_VERSION) ? MOD_SPR32 : MOD_SPR;
numframes = LittleLong (pin->numframes);
// load the frames
if (numframes < 1)
Sys_Error ("Mod_LoadSpriteModel: Invalid # of frames: %d\n", numframes);
size = sizeof(msprite_t) + (numframes - 1) * sizeof(mspriteframedesc_t);
psprite = Hunk_AllocName (size, mod_loadname);
Mod_LoadSpriteHeader (pin, psprite);
mod->cache.data = psprite;
mod->synctype = LittleLong (pin->synctype);
mod->numframes = numframes;
mod->type = mod_sprite;
mod->mins[0] = mod->mins[1] = -psprite->maxwidth/2;
mod->maxs[0] = mod->maxs[1] = psprite->maxwidth/2;
mod->mins[2] = -psprite->maxheight/2;
mod->maxs[2] = psprite->maxheight/2;
pframetype = (dspriteframetype_t *)(pin + 1);
Mod_LoadSpriteFrames (mod, pframetype, psprite);
}
#endif // #ifndef RQM_SV_ONLY