Share Mod_Load* functions

442fe10f27
This commit is contained in:
Denis Pauk 2023-01-21 23:45:49 +02:00
parent 6c3c2f3ec2
commit 659a5c489a
3 changed files with 345 additions and 261 deletions

View File

@ -75,7 +75,7 @@ typedef enum
#define MAX_LBM_HEIGHT 480
extern void R_Printf(int level, const char* msg, ...) __attribute__ ((format (printf, 2, 3)));
extern void R_Printf(int level, const char* msg, ...) PRINTF_ATTR(2, 3);
/* Shared images load */
typedef struct image_s* (*loadimage_t)(const char *name, byte *pic, int width, int realwidth,
@ -193,5 +193,19 @@ extern struct image_s* R_LoadImage(const char *name, const char* namewe, const c
extern void Mod_LoadNodes(const char *name, cplane_t *planes, int numplanes,
mleaf_t *leafs, int numleafs, mnode_t **nodes, int *numnodes,
const byte *mod_base, const lump_t *l);
extern void Mod_LoadVertexes(const char *name, mvertex_t **vertexes, int *numvertexes,
const byte *mod_base, const lump_t *l, int extra);
extern void Mod_LoadVisibility(dvis_t **vis, const byte *mod_base, const lump_t *l);
extern void Mod_LoadLighting(byte **lightdata, const byte *mod_base, const lump_t *l);
extern void Mod_LoadTexinfo(const char *name, mtexinfo_t **texinfo, int *numtexinfo,
const byte *mod_base, const lump_t *l, findimage_t find_image,
struct image_s *notexture, int extra);
extern void Mod_LoadEdges(const char *name, medge_t **edges, int *numedges,
const byte *mod_base, const lump_t *l, int extra);
extern void Mod_LoadPlanes (const char *name, cplane_t **planes, int *numplanes,
const byte *mod_base, const lump_t *l, int extra);
extern void Mod_LoadSurfedges (const char *name, int **surfedges, int *numsurfedges,
const byte *mod_base, const lump_t *l, int extra);
extern int Mod_CalcLumpHunkSize(const lump_t *l, int inSize, int outSize, int extra);
#endif /* SRC_CLIENT_REFRESH_REF_SHARED_H_ */

View File

@ -433,3 +433,320 @@ Mod_LoadNodes(const char *name, cplane_t *planes, int numplanes, mleaf_t *leafs,
numvisleafs = 0;
Mod_NumberLeafs (leafs, *nodes, r_leaftovis, r_vistoleaf, &numvisleafs);
}
/*
=================
Mod_LoadVisibility
=================
*/
void
Mod_LoadVisibility (dvis_t **vis, const byte *mod_base, const lump_t *l)
{
dvis_t *out;
int i;
if (!l->filelen)
{
*vis = NULL;
return;
}
out = Hunk_Alloc(l->filelen);
*vis = out;
memcpy(out, mod_base + l->fileofs, l->filelen);
out->numclusters = LittleLong(out->numclusters);
for (i = 0; i < out->numclusters; i++)
{
out->bitofs[i][0] = LittleLong(out->bitofs[i][0]);
out->bitofs[i][1] = LittleLong(out->bitofs[i][1]);
}
}
/*
=================
Mod_LoadVertexes
extra for skybox
=================
*/
void
Mod_LoadVertexes(const char *name, mvertex_t **vertexes, int *numvertexes,
const byte *mod_base, const lump_t *l, int extra)
{
dvertex_t *in;
mvertex_t *out;
int i, count;
in = (void *)(mod_base + l->fileofs);
if (l->filelen % sizeof(*in))
{
ri.Sys_Error(ERR_DROP, "%s: funny lump size in %s",
__func__, name);
}
count = l->filelen / sizeof(*in);
out = Hunk_Alloc((count + extra)*sizeof(*out));
/*
* FIXME: Recheck with soft render
* Fix for the problem where the games dumped core
* when changing levels.
*/
memset(out, 0, (count + extra) * sizeof(*out));
*vertexes = out;
*numvertexes = count;
for (i = 0; i < count; i++, in++, out++)
{
out->position[0] = LittleFloat(in->point[0]);
out->position[1] = LittleFloat(in->point[1]);
out->position[2] = LittleFloat(in->point[2]);
}
}
/*
=================
Mod_LoadLighting
=================
*/
void
Mod_LoadLighting(byte **lightdata, const byte *mod_base, const lump_t *l)
{
int size;
if (!l->filelen)
{
*lightdata = NULL;
return;
}
size = l->filelen;
*lightdata = Hunk_Alloc(size);
memcpy(*lightdata, mod_base + l->fileofs, size);
}
/*
=================
Mod_LoadTexinfo
extra for skybox in soft render
=================
*/
void
Mod_LoadTexinfo(const char *name, mtexinfo_t **texinfo, int *numtexinfo,
const byte *mod_base, const lump_t *l, findimage_t find_image,
struct image_s *notexture, int extra)
{
texinfo_t *in;
mtexinfo_t *out, *step;
int i, count;
in = (void *)(mod_base + l->fileofs);
if (l->filelen % sizeof(*in))
{
ri.Sys_Error(ERR_DROP, "%s: funny lump size in %s",
__func__, name);
}
count = l->filelen / sizeof(*in);
out = Hunk_Alloc((count + extra)*sizeof(*out));
*texinfo = out;
*numtexinfo = count;
for ( i=0 ; i<count ; i++, in++, out++)
{
struct image_s *image;
int j, next;
for (j = 0; j < 4; j++)
{
out->vecs[0][j] = LittleFloat(in->vecs[0][j]);
out->vecs[1][j] = LittleFloat(in->vecs[1][j]);
}
out->flags = LittleLong (in->flags);
next = LittleLong (in->nexttexinfo);
if (next > 0)
{
out->next = *texinfo + next;
}
else
{
/*
* Fix for the problem where the game
* domed core when loading a new level.
*/
out->next = NULL;
}
image = GetTexImage(in->texture, find_image);
if (!image)
{
R_Printf(PRINT_ALL, "%s: Couldn't load %s\n",
__func__, in->texture);
image = notexture;
}
out->image = image;
}
// count animation frames
for (i=0 ; i<count ; i++)
{
out = (*texinfo) + i;
out->numframes = 1;
for (step = out->next ; step && step != out ; step=step->next)
{
out->numframes++;
}
}
}
/*
=================
Mod_LoadEdges
extra is used for skybox, which adds 6 surfaces
=================
*/
void
Mod_LoadEdges(const char *name, medge_t **edges, int *numedges,
const byte *mod_base, const lump_t *l, int extra)
{
dedge_t *in;
medge_t *out;
int i, count;
in = (void *)(mod_base + l->fileofs);
if (l->filelen % sizeof(*in))
{
ri.Sys_Error(ERR_DROP, "%s: funny lump size in %s",
__func__, name);
}
count = l->filelen / sizeof(*in);
out = Hunk_Alloc((count + extra) * sizeof(*out));
*edges = out;
*numedges = count;
for ( i=0 ; i<count ; i++, in++, out++)
{
out->v[0] = (unsigned short)LittleShort(in->v[0]);
out->v[1] = (unsigned short)LittleShort(in->v[1]);
}
}
/*
=================
Mod_LoadPlanes
extra is used for skybox, which adds 6 surfaces
=================
*/
void
Mod_LoadPlanes(const char *name, cplane_t **planes, int *numplanes,
const byte *mod_base, const lump_t *l, int extra)
{
int i;
cplane_t *out;
dplane_t *in;
int count;
in = (void *)(mod_base + l->fileofs);
if (l->filelen % sizeof(*in))
{
ri.Sys_Error(ERR_DROP, "%s: funny lump size in %s",
__func__, name);
}
count = l->filelen / sizeof(*in);
// FIXME: why double of count
out = Hunk_Alloc((count * 2 + extra) * sizeof(*out));
*planes = out;
*numplanes = count;
for ( i=0 ; i<count ; i++, in++, out++)
{
int bits, j;
bits = 0;
for (j=0 ; j<3 ; j++)
{
out->normal[j] = LittleFloat (in->normal[j]);
if (out->normal[j] < 0)
bits |= 1<<j;
}
out->dist = LittleFloat (in->dist);
out->type = LittleLong (in->type);
out->signbits = bits;
}
}
/*
=================
Mod_LoadSurfedges
=================
*/
void
Mod_LoadSurfedges(const char *name, int **surfedges, int *numsurfedges,
const byte *mod_base, const lump_t *l, int extra)
{
int i, count;
int *in, *out;
in = (void *)(mod_base + l->fileofs);
if (l->filelen % sizeof(*in))
{
ri.Sys_Error(ERR_DROP, "%s: funny lump size in %s",
__func__, name);
}
count = l->filelen / sizeof(*in);
out = Hunk_Alloc((count + extra)*sizeof(*out)); // extra for skybox
*surfedges = out;
*numsurfedges = count;
for ( i=0 ; i<count ; i++)
out[i] = LittleLong (in[i]);
}
/*
=================
Mod_LoadSurfedges
calculate the size that Hunk_Alloc(), called by Mod_Load*() from Mod_LoadBrushModel(),
will use (=> includes its padding), so we'll know how big the hunk needs to be
extra is used for skybox, which adds 6 surfaces
=================
*/
int
Mod_CalcLumpHunkSize(const lump_t *l, int inSize, int outSize, int extra)
{
if (l->filelen % inSize)
{
// Mod_Load*() will error out on this because of "funny size"
// don't error out here because in Mod_Load*() it can print the functionname
// (=> tells us what kind of lump) before shutting down the game
return 0;
}
int count = l->filelen / inSize + extra;
int size = count * outSize;
// round to cacheline, like Hunk_Alloc() does
size = (size + 31) & ~31;
return size;
}

View File

@ -197,84 +197,6 @@ void Mod_FreeModelsKnown (void)
===============================================================================
*/
/*
=================
Mod_LoadLighting
=================
*/
static void
Mod_LoadLighting (model_t *loadmodel, const byte *mod_base, const lump_t *l)
{
if (!l->filelen)
{
loadmodel->lightdata = NULL;
return;
}
loadmodel->lightdata = Hunk_Alloc ( l->filelen);
memcpy (loadmodel->lightdata, mod_base + l->fileofs, l->filelen);
}
/*
=================
Mod_LoadVisibility
=================
*/
static void
Mod_LoadVisibility (model_t *loadmodel, const byte *mod_base, const lump_t *l)
{
int i;
if (!l->filelen)
{
loadmodel->vis = NULL;
return;
}
loadmodel->vis = Hunk_Alloc ( l->filelen);
memcpy (loadmodel->vis, mod_base + l->fileofs, l->filelen);
loadmodel->vis->numclusters = LittleLong (loadmodel->vis->numclusters);
for (i=0 ; i<loadmodel->vis->numclusters ; i++)
{
loadmodel->vis->bitofs[i][0] = LittleLong (loadmodel->vis->bitofs[i][0]);
loadmodel->vis->bitofs[i][1] = LittleLong (loadmodel->vis->bitofs[i][1]);
}
}
/*
=================
Mod_LoadVertexes
=================
*/
static void
Mod_LoadVertexes (model_t *loadmodel, const byte *mod_base, const lump_t *l)
{
dvertex_t *in;
mvertex_t *out;
int i, count;
in = (void *)(mod_base + l->fileofs);
if (l->filelen % sizeof(*in))
{
ri.Sys_Error(ERR_DROP, "%s: funny lump size in %s",
__func__, loadmodel->name);
}
count = l->filelen / sizeof(*in);
out = Hunk_Alloc ( count*sizeof(*out));
loadmodel->vertexes = out;
loadmodel->numvertexes = count;
for ( i=0 ; i<count ; i++, in++, out++)
{
out->position[0] = LittleFloat (in->point[0]);
out->position[1] = LittleFloat (in->point[1]);
out->position[2] = LittleFloat (in->point[2]);
}
}
/*
=================
Mod_LoadSubmodels
@ -337,102 +259,6 @@ Mod_LoadSubmodels (model_t *loadmodel, const byte *mod_base, const lump_t *l)
}
}
/*
=================
Mod_LoadEdges
=================
*/
static void
Mod_LoadEdges (model_t *loadmodel, const byte *mod_base, const lump_t *l)
{
dedge_t *in;
medge_t *out;
int i, count;
in = (void *)(mod_base + l->fileofs);
if (l->filelen % sizeof(*in))
{
ri.Sys_Error(ERR_DROP, "%s: funny lump size in %s",
__func__, loadmodel->name);
}
count = l->filelen / sizeof(*in);
out = Hunk_Alloc ( (count + 1) * sizeof(*out));
loadmodel->edges = out;
loadmodel->numedges = count;
for ( i=0 ; i<count ; i++, in++, out++)
{
out->v[0] = (unsigned short)LittleShort(in->v[0]);
out->v[1] = (unsigned short)LittleShort(in->v[1]);
}
}
/*
=================
Mod_LoadTexinfo
=================
*/
static void
Mod_LoadTexinfo (model_t *loadmodel, const byte *mod_base, const lump_t *l)
{
texinfo_t *in;
mtexinfo_t *out, *step;
int i, j, count;
in = (void *)(mod_base + l->fileofs);
if (l->filelen % sizeof(*in))
{
ri.Sys_Error(ERR_DROP, "%s: funny lump size in %s",
__func__, loadmodel->name);
}
count = l->filelen / sizeof(*in);
out = Hunk_Alloc ( count*sizeof(*out));
loadmodel->texinfo = out;
loadmodel->numtexinfo = count;
for ( i=0 ; i<count ; i++, in++, out++)
{
image_t *image;
int next;
for (j=0 ; j<4 ; j++)
{
out->vecs[0][j] = LittleFloat (in->vecs[0][j]);
out->vecs[1][j] = LittleFloat (in->vecs[1][j]);
}
out->flags = LittleLong (in->flags);
next = LittleLong (in->nexttexinfo);
if (next > 0)
out->next = loadmodel->texinfo + next;
else
out->next = NULL;
image = GetTexImage(in->texture, (findimage_t)Vk_FindImage);
if (!image)
{
R_Printf(PRINT_ALL, "%s: Couldn't load %s\n",
__func__, in->texture);
image = r_notexture;
}
out->image = image;
}
// count animation frames
for (i=0 ; i<count ; i++)
{
out = &loadmodel->texinfo[i];
out->numframes = 1;
for (step = out->next ; step && step != out ; step=step->next)
out->numframes++;
}
}
/*
================
CalcSurfaceExtents
@ -768,85 +594,6 @@ Mod_LoadMarksurfaces (model_t *loadmodel, const byte *mod_base, const lump_t *l)
}
}
/*
=================
Mod_LoadSurfedges
=================
*/
static void
Mod_LoadSurfedges (model_t *loadmodel, const byte *mod_base, const lump_t *l)
{
int i, count;
int *in, *out;
in = (void *)(mod_base + l->fileofs);
if (l->filelen % sizeof(*in))
{
ri.Sys_Error(ERR_DROP, "%s: funny lump size in %s",
__func__, loadmodel->name);
}
count = l->filelen / sizeof(*in);
if (count < 1 || count >= MAX_MAP_SURFEDGES)
ri.Sys_Error (ERR_DROP, "%s: bad surfedges count in %s: %i",
__func__, loadmodel->name, count);
out = Hunk_Alloc ( count*sizeof(*out));
loadmodel->surfedges = out;
loadmodel->numsurfedges = count;
for ( i=0 ; i<count ; i++)
out[i] = LittleLong (in[i]);
}
/*
=================
Mod_LoadPlanes
=================
*/
static void
Mod_LoadPlanes (model_t *loadmodel, const byte *mod_base, const lump_t *l)
{
int i;
cplane_t *out;
dplane_t *in;
int count;
in = (void *)(mod_base + l->fileofs);
if (l->filelen % sizeof(*in))
{
ri.Sys_Error(ERR_DROP, "%s: funny lump size in %s",
__func__, loadmodel->name);
}
count = l->filelen / sizeof(*in);
out = Hunk_Alloc ( count*2*sizeof(*out));
loadmodel->planes = out;
loadmodel->numplanes = count;
for ( i=0 ; i<count ; i++, in++, out++)
{
int bits, j;
bits = 0;
for (j=0 ; j<3 ; j++)
{
out->normal[j] = LittleFloat (in->normal[j]);
if (out->normal[j] < 0)
bits |= 1<<j;
}
out->dist = LittleFloat (in->dist);
out->type = LittleLong (in->type);
out->signbits = bits;
}
}
// calculate the size that Hunk_Alloc(), called by Mod_Load*() from Mod_LoadBrushModel(),
// will use (=> includes its padding), so we'll know how big the hunk needs to be
static int calcLumpHunkSize(const lump_t *l, int inSize, int outSize)
@ -919,15 +666,21 @@ Mod_LoadBrushModel (model_t *mod, const void *buffer, int modfilelen)
mod->numframes = 2; // regular and alternate animation
// load into heap
Mod_LoadVertexes (mod, mod_base, &header->lumps[LUMP_VERTEXES]);
Mod_LoadEdges (mod, mod_base, &header->lumps[LUMP_EDGES]);
Mod_LoadSurfedges (mod, mod_base, &header->lumps[LUMP_SURFEDGES]);
Mod_LoadLighting (mod, mod_base, &header->lumps[LUMP_LIGHTING]);
Mod_LoadPlanes (mod, mod_base, &header->lumps[LUMP_PLANES]);
Mod_LoadTexinfo (mod, mod_base, &header->lumps[LUMP_TEXINFO]);
Mod_LoadVertexes (mod->name, &mod->vertexes, &mod->numvertexes, mod_base,
&header->lumps[LUMP_VERTEXES], 0);
Mod_LoadEdges (mod->name, &mod->edges, &mod->numedges,
mod_base, &header->lumps[LUMP_EDGES], 1);
Mod_LoadSurfedges(mod->name, &mod->surfedges, &mod->numsurfedges,
mod_base, &header->lumps[LUMP_SURFEDGES], 0);
Mod_LoadLighting (&mod->lightdata, mod_base, &header->lumps[LUMP_LIGHTING]);
Mod_LoadPlanes (mod->name, &mod->planes, &mod->numplanes,
mod_base, &header->lumps[LUMP_PLANES], 0);
Mod_LoadTexinfo (mod->name, &mod->texinfo, &mod->numtexinfo,
mod_base, &header->lumps[LUMP_TEXINFO], (findimage_t)Vk_FindImage,
r_notexture, 0);
Mod_LoadFaces (mod, mod_base, &header->lumps[LUMP_FACES]);
Mod_LoadMarksurfaces (mod, mod_base, &header->lumps[LUMP_LEAFFACES]);
Mod_LoadVisibility (mod, mod_base, &header->lumps[LUMP_VISIBILITY]);
Mod_LoadVisibility (&mod->vis, mod_base, &header->lumps[LUMP_VISIBILITY]);
Mod_LoadLeafs (mod, mod_base, &header->lumps[LUMP_LEAFS]);
Mod_LoadNodes (mod->name, mod->planes, mod->numplanes,
mod->leafs, mod->numleafs, &mod->nodes,