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)
41 void * Mod_LoadSpriteFrame (void * pin, mspriteframe_t **ppframe, int framenum, int bytesperpixel)
43 dspriteframe_t *pinframe;
44 mspriteframe_t *pspriteframe;
45 int i, width, height, size, origin[2];
47 byte *pixbuf, *pixel, *inpixel;
49 pinframe = (dspriteframe_t *)pin;
51 width = LittleLong (pinframe->width);
52 height = LittleLong (pinframe->height);
53 size = width * height * bytesperpixel;
55 pspriteframe = Hunk_AllocName (sizeof (mspriteframe_t),loadname);
57 memset (pspriteframe, 0, sizeof (mspriteframe_t));
59 *ppframe = pspriteframe;
61 pspriteframe->width = width;
62 pspriteframe->height = height;
63 origin[0] = LittleLong (pinframe->origin[0]);
64 origin[1] = LittleLong (pinframe->origin[1]);
66 pspriteframe->up = origin[1];
67 pspriteframe->down = origin[1] - height;
68 pspriteframe->left = origin[0];
69 pspriteframe->right = width + origin[0];
71 sprintf (name, "%s_%i", loadmodel->name, framenum);
72 pspriteframe->gl_texturenum = loadtextureimagewithmask(name, 0, 0, false, true);
73 pspriteframe->gl_fogtexturenum = image_masktexnum;
74 if (pspriteframe->gl_texturenum == 0)
76 pspriteframe->gl_texturenum = GL_LoadTexture (name, width, height, (byte *)(pinframe + 1), true, true, bytesperpixel);
77 // make fog version (just alpha)
78 pixbuf = pixel = malloc(width*height*4);
79 inpixel = (byte *)(pinframe + 1);
80 if (bytesperpixel == 1)
82 for (i = 0;i < width*height;i++)
87 if (*inpixel++ != 255)
96 for (i = 0;i < width*height;i++)
105 sprintf (name, "%s_%ifog", loadmodel->name, framenum);
106 pspriteframe->gl_fogtexturenum = GL_LoadTexture (name, width, height, pixbuf, true, true, 4);
110 return (void *)((byte *)pinframe + sizeof (dspriteframe_t) + size);
119 void * Mod_LoadSpriteGroup (void * pin, mspriteframe_t **ppframe, int framenum, int bytesperpixel)
121 dspritegroup_t *pingroup;
122 mspritegroup_t *pspritegroup;
124 dspriteinterval_t *pin_intervals;
125 float *poutintervals;
128 pingroup = (dspritegroup_t *)pin;
130 numframes = LittleLong (pingroup->numframes);
132 pspritegroup = Hunk_AllocName (sizeof (mspritegroup_t) +
133 (numframes - 1) * sizeof (pspritegroup->frames[0]), loadname);
135 pspritegroup->numframes = numframes;
137 *ppframe = (mspriteframe_t *)pspritegroup;
139 pin_intervals = (dspriteinterval_t *)(pingroup + 1);
141 poutintervals = Hunk_AllocName (numframes * sizeof (float), loadname);
143 pspritegroup->intervals = poutintervals;
145 for (i=0 ; i<numframes ; i++)
147 *poutintervals = LittleFloat (pin_intervals->interval);
148 if (*poutintervals <= 0.0)
149 Host_Error ("Mod_LoadSpriteGroup: interval<=0");
155 ptemp = (void *)pin_intervals;
157 for (i=0 ; i<numframes ; i++)
159 ptemp = Mod_LoadSpriteFrame (ptemp, &pspritegroup->frames[i], framenum * 100 + i, bytesperpixel);
171 void Mod_LoadSpriteModel (model_t *mod, void *buffer)
179 dspriteframetype_t *pframetype;
180 // LordHavoc: 32bit textures
183 mod->flags = EF_FULLBRIGHT;
184 // LordHavoc: hack to allow sprites to be non-fullbright
185 for (i = 0;i < MAX_QPATH && mod->name[i];i++)
187 if (mod->name[i] == '!')
189 mod->flags &= ~EF_FULLBRIGHT;
194 pin = (dsprite_t *)buffer;
196 version = LittleLong (pin->version);
200 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);
202 // LordHavoc: 32bit textures
203 if (version != SPRITE_VERSION && version != SPRITE32_VERSION)
204 Host_Error ("%s has wrong version number "
205 "(%i should be %i or %i)", mod->name, version, SPRITE_VERSION, SPRITE32_VERSION);
207 if (version == SPRITE32_VERSION)
210 numframes = LittleLong (pin->numframes);
212 size = sizeof (msprite_t) + (numframes - 1) * sizeof (psprite->frames);
214 psprite = Hunk_AllocName (size, loadname);
216 mod->cache.data = psprite;
218 psprite->type = LittleLong (pin->type);
219 psprite->maxwidth = LittleLong (pin->width);
220 psprite->maxheight = LittleLong (pin->height);
221 psprite->beamlength = LittleFloat (pin->beamlength);
222 mod->synctype = LittleLong (pin->synctype);
223 psprite->numframes = numframes;
225 mod->mins[0] = mod->mins[1] = -psprite->maxwidth/2;
226 mod->maxs[0] = mod->maxs[1] = psprite->maxwidth/2;
227 mod->mins[2] = -psprite->maxheight/2;
228 mod->maxs[2] = psprite->maxheight/2;
234 Host_Error ("Mod_LoadSpriteModel: Invalid # of frames: %d\n", numframes);
236 mod->numframes = numframes;
238 pframetype = (dspriteframetype_t *)(pin + 1);
240 for (i=0 ; i<numframes ; i++)
242 spriteframetype_t frametype;
244 frametype = LittleLong (pframetype->type);
245 psprite->frames[i].type = frametype;
247 if (frametype == SPR_SINGLE)
248 pframetype = (dspriteframetype_t *) Mod_LoadSpriteFrame (pframetype + 1, &psprite->frames[i].frameptr, i, bytesperpixel);
250 pframetype = (dspriteframetype_t *) Mod_LoadSpriteGroup (pframetype + 1, &psprite->frames[i].frameptr, i, bytesperpixel);
253 mod->type = mod_sprite;