newtree/source/model.c

274 lines
4.7 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"
2000-05-17 10:03:19 +00:00
#endif
2000-10-16 15:34:53 +00:00
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif
2000-12-30 02:16:36 +00:00
#include "cvar.h"
#include "model.h"
2000-12-30 02:16:36 +00:00
#include "qendian.h"
#include "quakefs.h"
#include "server.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
model_t *loadmodel;
char loadname[32]; // for hunk tags
2000-05-10 11:29:38 +00:00
2000-05-13 05:20:33 +00:00
#define MAX_MOD_KNOWN 512
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)
2000-05-10 11:29:38 +00:00
{
memset (mod_novis, 0xff, sizeof (mod_novis));
2000-05-10 11:29:38 +00:00
}
void
Mod_Init_Cvars (void)
{
gl_subdivide_size =
2000-12-31 04:46:04 +00:00
Cvar_Get ("gl_subdivide_size", "128", CVAR_ARCHIVE, "Sets the division value for the sky brushes.");
}
2000-05-10 11:29:38 +00:00
/*
===================
Mod_ClearAll
===================
*/
void
Mod_ClearAll (void)
2000-05-10 11:29:38 +00:00
{
int i;
model_t *mod;
for (i = 0, mod = mod_known; i < mod_numknown; i++, mod++)
2000-05-10 11:29:38 +00:00
if (mod->type != mod_alias)
mod->needload = true;
}
/*
==================
Mod_FindName
==================
*/
model_t *
Mod_FindName (char *name)
2000-05-10 11:29:38 +00:00
{
int i;
model_t *mod;
2000-05-10 11:29:38 +00:00
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))
2000-05-10 11:29:38 +00:00
break;
if (i == mod_numknown) {
2000-05-10 11:29:38 +00:00
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)
2000-05-10 11:29:38 +00:00
{
void *d;
unsigned int *buf;
byte stackbuf[1024]; // avoid dirtying the cache heap
2000-05-10 11:29:38 +00:00
if (!mod->needload) {
if (mod->type == mod_alias) {
2000-05-10 11:29:38 +00:00
d = Cache_Check (&mod->cache);
if (d)
return mod;
} else
return mod; // not cached at all
2000-05-10 11:29:38 +00:00
}
//
// load the file
//
buf =
(unsigned int *) COM_LoadStackFile (mod->name, stackbuf,
sizeof (stackbuf));
if (!buf) {
2000-05-10 11:29:38 +00:00
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);
2000-05-10 11:29:38 +00:00
loadmodel = mod;
//
// fill it in
//
// call the apropriate loader
mod->needload = false;
mod->hasfullbrights = false;
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)
2000-05-10 11:29:38 +00:00
{
model_t *mod;
2000-05-10 11:29:38 +00:00
mod = Mod_FindName (name);
Con_DPrintf ("Mod_ForName: %s, %p\n", name, mod);
2000-05-10 11:29:38 +00:00
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;
r = Cache_Check (&mod->cache);
if (r)
return r;
2000-05-10 11:29:38 +00:00
Mod_LoadModel (mod, true);
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;
mod = Mod_FindName (name);
if (!mod->needload) {
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);
}
}