2 Copyright (C) 1996-1997 Id Software, Inc.
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.
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.
13 See the GNU General Public License for more details.
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.
20 // models.c -- model loading and caching
22 // models are the only shared resource between a client and server running
23 // on the same machine.
32 void Mod_SpriteInit (void)
36 void Mod_Sprite_StripExtension(char *in, char *out)
39 end = in + strlen(in);
41 if (strcmp(end - 6, ".spr32") == 0)
44 if (strcmp(end - 4, ".spr") == 0)
56 void * Mod_LoadSpriteFrame (void * pin, mspriteframe_t **ppframe, int framenum, int bytesperpixel)
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;
64 pinframe = (dspriteframe_t *)pin;
66 width = LittleLong (pinframe->width);
67 height = LittleLong (pinframe->height);
68 size = width * height * bytesperpixel;
70 pspriteframe = Hunk_AllocName (sizeof (mspriteframe_t),loadname);
72 memset (pspriteframe, 0, sizeof (mspriteframe_t));
74 *ppframe = pspriteframe;
76 pspriteframe->width = width;
77 pspriteframe->height = height;
78 origin[0] = LittleLong (pinframe->origin[0]);
79 origin[1] = LittleLong (pinframe->origin[1]);
81 pspriteframe->up = origin[1];
82 pspriteframe->down = origin[1] - height;
83 pspriteframe->left = origin[0];
84 pspriteframe->right = width + origin[0];
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)
92 pspriteframe->gl_texturenum = GL_LoadTexture (name, width, height, (byte *)(pinframe + 1), true, true, bytesperpixel);
93 // make fog version (just alpha)
94 pixbuf = pixel = malloc(width*height*4);
95 inpixel = (byte *)(pinframe + 1);
96 if (bytesperpixel == 1)
98 for (i = 0;i < width*height;i++)
103 if (*inpixel++ != 255)
112 for (i = 0;i < width*height;i++)
121 sprintf (name, "%s_%ifog", loadmodel->name, framenum);
122 pspriteframe->gl_fogtexturenum = GL_LoadTexture (name, width, height, pixbuf, true, true, 4);
126 return (void *)((byte *)pinframe + sizeof (dspriteframe_t) + size);
135 void * Mod_LoadSpriteGroup (void * pin, mspriteframe_t **ppframe, int framenum, int bytesperpixel)
137 dspritegroup_t *pingroup;
138 mspritegroup_t *pspritegroup;
140 dspriteinterval_t *pin_intervals;
141 float *poutintervals;
144 pingroup = (dspritegroup_t *)pin;
146 numframes = LittleLong (pingroup->numframes);
148 pspritegroup = Hunk_AllocName (sizeof (mspritegroup_t) +
149 (numframes - 1) * sizeof (pspritegroup->frames[0]), loadname);
151 pspritegroup->numframes = numframes;
153 *ppframe = (mspriteframe_t *)pspritegroup;
155 pin_intervals = (dspriteinterval_t *)(pingroup + 1);
157 poutintervals = Hunk_AllocName (numframes * sizeof (float), loadname);
159 pspritegroup->intervals = poutintervals;
161 for (i=0 ; i<numframes ; i++)
163 *poutintervals = LittleFloat (pin_intervals->interval);
164 if (*poutintervals <= 0.0)
165 Host_Error ("Mod_LoadSpriteGroup: interval<=0");
171 ptemp = (void *)pin_intervals;
173 for (i=0 ; i<numframes ; i++)
174 ptemp = Mod_LoadSpriteFrame (ptemp, &pspritegroup->frames[i], framenum * 100 + i, bytesperpixel);
185 void Mod_LoadSpriteModel (model_t *mod, void *buffer)
193 dspriteframetype_t *pframetype;
194 // LordHavoc: 32bit textures
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++)
201 if (mod->name[i] == '!')
203 mod->flags &= ~EF_FULLBRIGHT;
208 pin = (dsprite_t *)buffer;
210 version = LittleLong (pin->version);
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);
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);
221 if (version == SPRITE32_VERSION)
224 numframes = LittleLong (pin->numframes);
226 size = sizeof (msprite_t) + (numframes - 1) * sizeof (psprite->frames);
228 psprite = Hunk_AllocName (size, loadname);
230 mod->cache.data = psprite;
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;
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;
248 Host_Error ("Mod_LoadSpriteModel: Invalid # of frames: %d\n", numframes);
250 mod->numframes = numframes;
252 pframetype = (dspriteframetype_t *)(pin + 1);
254 for (i=0 ; i<numframes ; i++)
256 spriteframetype_t frametype;
258 frametype = LittleLong (pframetype->type);
259 psprite->frames[i].type = frametype;
261 if (frametype == SPR_SINGLE)
262 pframetype = (dspriteframetype_t *) Mod_LoadSpriteFrame (pframetype + 1, &psprite->frames[i].frameptr, i, bytesperpixel);
264 pframetype = (dspriteframetype_t *) Mod_LoadSpriteGroup (pframetype + 1, &psprite->frames[i].frameptr, i, bytesperpixel);
267 mod->type = mod_sprite;