newtree/source/model.c

261 lines
4.4 KiB
C
Raw Normal View History

/*
model.c
2000-05-22 07:10:16 +00:00
model loading and caching
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
// models are the only shared resource between a client and server running
// on the same machine.
2000-05-17 10:03:19 +00:00
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include "model.h"
#include "quakefs.h"
#include "cvar.h"
#include "server.h"
#include "qendian.h"
2000-05-10 11:29:38 +00:00
void Mod_LoadAliasModel(model_t *mod, void *buf);
void Mod_LoadSpriteModel(model_t *mod, void *buf);
void Mod_LoadBrushModel(model_t *mod, void *buf);
2000-05-13 05:20:33 +00:00
2000-05-10 11:29:38 +00:00
model_t *loadmodel;
char loadname[32]; // for hunk tags
2000-05-13 05:20:33 +00:00
#define MAX_MOD_KNOWN 512
2000-05-10 11:29:38 +00:00
model_t mod_known[MAX_MOD_KNOWN];
int mod_numknown;
cvar_t *gl_subdivide_size;
extern byte mod_novis[MAX_MAP_LEAFS/8];
texture_t *r_notexture_mip;
2000-05-10 11:29:38 +00:00
/*
===============
Mod_Init
===============
*/
void Mod_Init (void)
{
gl_subdivide_size = Cvar_Get("gl_subdivide_size", "128", CVAR_ARCHIVE, "None");
2000-05-10 11:29:38 +00:00
memset (mod_novis, 0xff, sizeof(mod_novis));
}
/*
===================
Mod_ClearAll
===================
*/
void Mod_ClearAll (void)
{
int i;
model_t *mod;
for (i=0 , mod=mod_known ; i<mod_numknown ; i++, mod++)
if (mod->type != mod_alias)
mod->needload = true;
}
/*
==================
Mod_FindName
==================
*/
model_t *Mod_FindName (char *name)
{
int i;
model_t *mod;
if (!name[0])
SV_Error ("Mod_ForName: NULL name");
2000-05-10 11:29:38 +00:00
//
// search the currently loaded models
//
for (i=0 , mod=mod_known ; i<mod_numknown ; i++, mod++)
if (!strcmp (mod->name, name) )
break;
if (i == mod_numknown)
{
if (mod_numknown == MAX_MOD_KNOWN)
SV_Error ("mod_numknown == MAX_MOD_KNOWN");
2000-05-10 11:29:38 +00:00
strcpy (mod->name, name);
mod->needload = true;
mod_numknown++;
}
return mod;
}
/*
==================
Mod_LoadModel
Loads a model into the cache
==================
*/
model_t *Mod_LoadModel (model_t *mod, qboolean crash)
{
void *d;
unsigned int *buf;
2000-05-10 11:29:38 +00:00
byte stackbuf[1024]; // avoid dirtying the cache heap
if (!mod->needload)
{
if (mod->type == mod_alias)
{
d = Cache_Check (&mod->cache);
if (d)
return mod;
}
else
return mod; // not cached at all
}
2000-05-13 20:25:02 +00:00
2000-05-10 11:29:38 +00:00
//
// load the file
//
buf = (unsigned int *)COM_LoadStackFile (mod->name, stackbuf, sizeof(stackbuf));
2000-05-10 11:29:38 +00:00
if (!buf)
{
if (crash)
SV_Error ("Mod_NumForName: %s not found", mod->name);
2000-05-10 11:29:38 +00:00
return NULL;
}
//
// allocate a new model
//
COM_FileBase (mod->name, loadname);
loadmodel = mod;
//
// fill it in
//
// call the apropriate loader
mod->needload = false;
mod->hasfullbrights = false;
2000-05-10 11:29:38 +00:00
switch (LittleLong(*(unsigned int *)buf))
{
case IDPOLYHEADER:
Mod_LoadAliasModel (mod, buf);
break;
case IDSPRITEHEADER:
Mod_LoadSpriteModel (mod, buf);
break;
default:
Mod_LoadBrushModel (mod, buf);
break;
}
2000-05-10 11:29:38 +00:00
return mod;
}
/*
==================
Mod_ForName
Loads in a model for the given name
==================
*/
model_t *Mod_ForName (char *name, qboolean crash)
{
model_t *mod;
mod = Mod_FindName (name);
return Mod_LoadModel (mod, crash);
}
/*
===============
Mod_Init
2000-05-10 11:29:38 +00:00
Caches the data if needed
===============
2000-05-10 11:29:38 +00:00
*/
void *Mod_Extradata (model_t *mod)
2000-05-10 11:29:38 +00:00
{
void *r;
2000-05-10 11:29:38 +00:00
r = Cache_Check (&mod->cache);
if (r)
return r;
2000-05-10 11:29:38 +00:00
Mod_LoadModel (mod, true);
2000-05-10 11:29:38 +00:00
if (!mod->cache.data)
SV_Error ("Mod_Extradata: caching failed");
return mod->cache.data;
2000-05-10 11:29:38 +00:00
}
/*
==================
Mod_TouchModel
2000-05-10 11:29:38 +00:00
==================
2000-05-10 11:29:38 +00:00
*/
void Mod_TouchModel (char *name)
2000-05-10 11:29:38 +00:00
{
model_t *mod;
2000-05-10 11:29:38 +00:00
mod = Mod_FindName (name);
2000-05-10 11:29:38 +00:00
if (!mod->needload)
2000-05-10 11:29:38 +00:00
{
if (mod->type == mod_alias)
Cache_Check (&mod->cache);
2000-05-10 11:29:38 +00:00
}
}
/*
================
Mod_Print
================
*/
void Mod_Print (void)
{
int i;
model_t *mod;
Con_Printf ("Cached models:\n");
for (i=0, mod=mod_known ; i < mod_numknown ; i++, mod++)
{
Con_Printf ("%8p : %s\n",mod->cache.data, mod->name);
}
}
2000-05-10 11:29:38 +00:00