newtree/source/skin.c

320 lines
6.4 KiB
C
Raw Normal View History

/*
skin.c
(description)
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:
Free Software Foundation, Inc.
59 Temple Place - Suite 330
Boston, MA 02111-1307, USA
$Id$
*/
2000-05-10 11:29:38 +00:00
2000-05-17 10:03:19 +00:00
#ifdef HAVE_CONFIG_H
# include "config.h"
2000-05-17 10:03:19 +00:00
#endif
#include <string.h>
#include "client.h"
#include "cmd.h"
#include "console.h"
#include "msg.h"
#include "pcx.h"
#include "sys.h"
2000-12-01 08:59:44 +00:00
#include "qendian.h"
#include "va.h"
#include "skin.h"
#include "cl_parse.h"
cvar_t *baseskin;
cvar_t *noskins;
char allskins[128];
2000-05-10 11:29:38 +00:00
#define MAX_CACHED_SKINS 128
skin_t skins[MAX_CACHED_SKINS];
int numskins;
2000-05-10 11:29:38 +00:00
/*
================
Skin_Find
Determines the best skin for the given scoreboard
slot, and sets scoreboard->skin
================
*/
void
Skin_Find (player_info_t *sc)
2000-05-10 11:29:38 +00:00
{
skin_t *skin;
int i;
char name[128], *s;
2000-05-10 11:29:38 +00:00
if (allskins[0])
strcpy (name, allskins);
else {
2000-05-10 11:29:38 +00:00
s = Info_ValueForKey (sc->userinfo, "skin");
if (s && s[0])
strcpy (name, s);
else
strcpy (name, baseskin->string);
2000-05-10 11:29:38 +00:00
}
if (strstr (name, "..") || *name == '.')
strcpy (name, "base");
COM_StripExtension (name, name);
for (i = 0; i < numskins; i++) {
if (!strcmp (name, skins[i].name)) {
2000-05-10 11:29:38 +00:00
sc->skin = &skins[i];
Skin_Cache (sc->skin);
return;
}
}
if (numskins == MAX_CACHED_SKINS) { // ran out of spots, so flush
// everything
2000-05-10 11:29:38 +00:00
Skin_Skins_f ();
return;
}
skin = &skins[numskins];
sc->skin = skin;
numskins++;
memset (skin, 0, sizeof (*skin));
strncpy (skin->name, name, sizeof (skin->name) - 1);
2000-05-10 11:29:38 +00:00
}
/*
==========
Skin_Cache
Returns a pointer to the skin bitmap, or NULL to use the default
==========
*/
byte *
Skin_Cache (skin_t *skin)
2000-05-10 11:29:38 +00:00
{
char name[1024];
byte *raw;
byte *out, *pix;
pcx_t *pcx;
int x, y;
int dataByte;
int runLength;
2000-05-10 11:29:38 +00:00
if (cls.downloadtype == dl_skin)
return NULL; // use base until downloaded
2000-05-10 11:29:38 +00:00
if (noskins->int_val) // JACK: So NOSKINS > 1 will show
// skins, but
return NULL; // not download new ones.
2000-05-10 11:29:38 +00:00
if (skin->failedload)
return NULL;
out = Cache_Check (&skin->cache);
if (out)
return out;
//
// load the pic from disk
//
snprintf (name, sizeof (name), "skins/%s.pcx", skin->name);
2000-05-10 11:29:38 +00:00
raw = COM_LoadTempFile (name);
if (!raw) {
2000-05-10 11:29:38 +00:00
Con_Printf ("Couldn't load skin %s\n", name);
snprintf (name, sizeof (name), "skins/%s.pcx", baseskin->string);
2000-05-10 11:29:38 +00:00
raw = COM_LoadTempFile (name);
if (!raw) {
2000-05-10 11:29:38 +00:00
skin->failedload = true;
return NULL;
}
}
//
// parse the PCX file
//
pcx = (pcx_t *) raw;
2000-05-10 11:29:38 +00:00
raw = &pcx->data;
2000-12-01 08:59:44 +00:00
pcx->xmax = LittleShort (pcx->xmax);
pcx->xmin = LittleShort (pcx->xmin);
pcx->ymax = LittleShort (pcx->ymax);
pcx->ymin = LittleShort (pcx->ymin);
pcx->hres = LittleShort (pcx->hres);
pcx->vres = LittleShort (pcx->vres);
pcx->bytes_per_line = LittleShort (pcx->bytes_per_line);
pcx->palette_type = LittleShort (pcx->palette_type);
2000-05-10 11:29:38 +00:00
if (pcx->manufacturer != 0x0a
|| pcx->version != 5
|| pcx->encoding != 1
|| pcx->bits_per_pixel != 8 || pcx->xmax >= 320 || pcx->ymax >= 200) {
2000-05-10 11:29:38 +00:00
skin->failedload = true;
Con_Printf ("Bad skin %s\n", name);
return NULL;
}
out = Cache_Alloc (&skin->cache, 320 * 200, skin->name);
2000-05-10 11:29:38 +00:00
if (!out)
Sys_Error ("Skin_Cache: couldn't allocate");
pix = out;
memset (out, 0, 320 * 200);
for (y = 0; y < pcx->ymax; y++, pix += 320) {
for (x = 0; x <= pcx->xmax;) {
if (raw - (byte *) pcx > com_filesize) {
Cache_Free (&skin->cache);
skin->failedload = true;
Con_Printf ("Skin %s was malformed. You should delete it.\n",
name);
return NULL;
}
2000-05-10 11:29:38 +00:00
dataByte = *raw++;
if ((dataByte & 0xC0) == 0xC0) {
2000-05-10 11:29:38 +00:00
runLength = dataByte & 0x3F;
if (raw - (byte *) pcx > com_filesize) {
Cache_Free (&skin->cache);
skin->failedload = true;
Con_Printf
("Skin %s was malformed. You should delete it.\n",
name);
return NULL;
}
2000-05-10 11:29:38 +00:00
dataByte = *raw++;
} else
2000-05-10 11:29:38 +00:00
runLength = 1;
// skin sanity check
if (runLength + x > pcx->xmax + 2) {
Cache_Free (&skin->cache);
skin->failedload = true;
Con_Printf ("Skin %s was malformed. You should delete it.\n",
name);
return NULL;
2000-05-10 11:29:38 +00:00
}
while (runLength-- > 0)
2000-05-10 11:29:38 +00:00
pix[x++] = dataByte;
}
}
if (raw - (byte *) pcx > com_filesize) {
2000-05-10 11:29:38 +00:00
Cache_Free (&skin->cache);
skin->failedload = true;
Con_Printf ("Skin %s was malformed. You should delete it.\n", name);
return NULL;
}
skin->failedload = false;
return out;
}
/*
=================
Skin_NextDownload
=================
*/
void
Skin_NextDownload (void)
2000-05-10 11:29:38 +00:00
{
player_info_t *sc;
int i;
2000-05-10 11:29:38 +00:00
if (cls.downloadnumber == 0)
Con_Printf ("Checking skins...\n");
cls.downloadtype = dl_skin;
for (; cls.downloadnumber != MAX_CLIENTS; cls.downloadnumber++) {
2000-05-10 11:29:38 +00:00
sc = &cl.players[cls.downloadnumber];
if (!sc->name[0])
continue;
Skin_Find (sc);
if (noskins->int_val)
2000-05-10 11:29:38 +00:00
continue;
if (!CL_CheckOrDownloadFile (va ("skins/%s.pcx", sc->skin->name)))
return; // started a download
2000-05-10 11:29:38 +00:00
}
cls.downloadtype = dl_none;
// now load them in for real
for (i = 0; i < MAX_CLIENTS; i++) {
2000-05-10 11:29:38 +00:00
sc = &cl.players[i];
if (!sc->name[0])
continue;
Skin_Cache (sc->skin);
sc->skin = NULL;
2000-05-10 11:29:38 +00:00
}
if (cls.state != ca_active) { // get next signon phase
2000-05-10 11:29:38 +00:00
MSG_WriteByte (&cls.netchan.message, clc_stringcmd);
MSG_WriteString (&cls.netchan.message, va ("begin %i", cl.servercount));
Cache_Report (); // print remaining memory
2000-05-10 11:29:38 +00:00
}
}
/*
==========
Skin_Skins_f
Refind all skins, downloading if needed.
==========
*/
void
Skin_Skins_f (void)
2000-05-10 11:29:38 +00:00
{
int i;
2000-05-10 11:29:38 +00:00
for (i = 0; i < numskins; i++) {
2000-05-10 11:29:38 +00:00
if (skins[i].cache.data)
Cache_Free (&skins[i].cache);
}
numskins = 0;
cls.downloadnumber = 0;
cls.downloadtype = dl_skin;
Skin_NextDownload ();
}
/*
==========
Skin_AllSkins_f
Sets all skins to one specific one
==========
*/
void
Skin_AllSkins_f (void)
2000-05-10 11:29:38 +00:00
{
strcpy (allskins, Cmd_Argv (1));
2000-05-10 11:29:38 +00:00
Skin_Skins_f ();
}