From 10cc3047785dd29d519349788ab65ea4630e858d Mon Sep 17 00:00:00 2001 From: Denis Pauk Date: Mon, 5 Feb 2024 01:29:56 +0200 Subject: [PATCH] models: fix cppcheck warnings --- README.md | 2 + src/client/refresh/files/light.c | 15 ++--- src/client/refresh/files/maps.c | 34 +++++++---- src/client/refresh/files/mesh.c | 4 +- src/client/refresh/files/models.c | 88 +++++++++++++-------------- src/client/refresh/files/models_md5.c | 34 +++++------ src/client/refresh/ref_shared.h | 7 ++- 7 files changed, 99 insertions(+), 85 deletions(-) diff --git a/README.md b/README.md index 590d567e..453f58f1 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,8 @@ Goals (finished): * Add debug progress loading code for maps. Goals (none of it finished): + * md5 improve load speed, + * md3 fix possible memory coruption, * Single player support, * modified ReRelease game code support with removed KEX only related code. diff --git a/src/client/refresh/files/light.c b/src/client/refresh/files/light.c index c088d52e..c81e8df6 100644 --- a/src/client/refresh/files/light.c +++ b/src/client/refresh/files/light.c @@ -33,9 +33,7 @@ static byte *s_bufferlights = NULL, *s_bufferlights_max = NULL; static int BSPX_LightGridSingleValue(const bspxlightgrid_t *grid, const lightstyle_t *lightstyles, int x, int y, int z, vec3_t res_diffuse) { - int i; unsigned int node; - struct bspxlgsamp_s *samp; node = grid->rootnode; while (!(node & LGNODE_LEAF)) @@ -52,6 +50,9 @@ BSPX_LightGridSingleValue(const bspxlightgrid_t *grid, const lightstyle_t *light { struct bspxlgleaf_s *leaf = &grid->leafs[node & ~LGNODE_LEAF]; + struct bspxlgsamp_s *samp; + int i; + x -= leaf->mins[0]; y -= leaf->mins[1]; z -= leaf->mins[2]; @@ -495,7 +496,7 @@ R_BuildLightMap(const msurface_t *surf, byte *dest, int stride, const byte *dest { int smax, tmax; int r, g, b, a, max; - int i, j, size, nummaps; + int i, j, size, numlightmaps; byte *lightmap; float scale[4]; float *bl; @@ -529,15 +530,15 @@ R_BuildLightMap(const msurface_t *surf, byte *dest, int stride, const byte *dest } /* count the # of maps */ - for (nummaps = 0; nummaps < MAXLIGHTMAPS && surf->styles[nummaps] != 255; - nummaps++) + for (numlightmaps = 0; numlightmaps < MAXLIGHTMAPS && surf->styles[numlightmaps] != 255; + numlightmaps++) { } lightmap = surf->samples; /* add all the lightmaps */ - if (nummaps == 1) + if (numlightmaps == 1) { int maps; @@ -702,7 +703,7 @@ store: } static void -R_MarkSurfaceLights(dlight_t *light, int bit, mnode_t *node, int r_dlightframecount, +R_MarkSurfaceLights(dlight_t *light, int bit, const mnode_t *node, int r_dlightframecount, msurface_t *surfaces) { msurface_t *surf; diff --git a/src/client/refresh/files/maps.c b/src/client/refresh/files/maps.c index e3e64ad1..72383341 100644 --- a/src/client/refresh/files/maps.c +++ b/src/client/refresh/files/maps.c @@ -56,7 +56,7 @@ Mod_NumberLeafs(mleaf_t *leafs, mnode_t *node, int *r_leaftovis, int *r_vistolea { if (node->contents != CONTENTS_NODE) { - mleaf_t *leaf; + const mleaf_t *leaf; int leafnum; leaf = (mleaf_t *)node; @@ -324,7 +324,8 @@ Mod_LoadLighting(byte **lightdata, int *size, const byte *mod_base, const lump_t } void -Mod_LoadSetSurfaceLighting(byte *lightdata, int size, msurface_t *out, byte *styles, int lightofs) +Mod_LoadSetSurfaceLighting(byte *lightdata, int size, msurface_t *out, + const byte *styles, int lightofs) { int i; @@ -349,7 +350,7 @@ Mod_LoadSetSurfaceLighting(byte *lightdata, int size, msurface_t *out, byte *sty * Fills in s->texturemins[] and s->extents[] */ void -Mod_CalcSurfaceExtents(int *surfedges, mvertex_t *vertexes, medge_t *edges, +Mod_CalcSurfaceExtents(const int *surfedges, mvertex_t *vertexes, medge_t *edges, msurface_t *s) { double mins[2], maxs[2]; @@ -588,8 +589,9 @@ void Mod_LoadSurfedges(const char *name, int **surfedges, int *numsurfedges, const byte *mod_base, const lump_t *l) { - int i, count; - int *in, *out; + const int *in; + int i, count; + int *out; in = (void *)(mod_base + l->fileofs); @@ -740,10 +742,12 @@ Mod_LoadBSPX(int filesize, const byte *mod_base) int Mod_LoadBSPXDecoupledLM(const dlminfo_t* lminfos, int surfnum, msurface_t *out) { + int lmwidth, lmheight, i; const dlminfo_t *lminfo; - unsigned short lmwidth, lmheight; + float v0, v1; - if (lminfos == NULL) { + if (lminfos == NULL) + { return -1; } @@ -756,8 +760,12 @@ Mod_LoadBSPXDecoupledLM(const dlminfo_t* lminfos, int surfnum, msurface_t *out) return -1; } - for (int i = 0; i < 2; i++) { - for (int j = 0; j < 4; j++) { + for (i = 0; i < 2; i++) + { + int j; + + for (j = 0; j < 4; j++) + { out->lmvecs[i][j] = LittleFloat(lminfo->vecs[i][j]); } } @@ -768,10 +776,10 @@ Mod_LoadBSPXDecoupledLM(const dlminfo_t* lminfos, int surfnum, msurface_t *out) out->texturemins[0] = 0; out->texturemins[1] = 0; - float v0 = VectorLength(out->lmvecs[0]); + v0 = VectorLength(out->lmvecs[0]); out->lmvlen[0] = v0 > 0.0f ? 1.0f / v0 : 0.0f; - float v1 = VectorLength(out->lmvecs[1]); + v1 = VectorLength(out->lmvecs[1]); out->lmvlen[1] = v1 > 0.0f ? 1.0f / v1 : 0.0f; return LittleLong(lminfo->lightofs); @@ -782,7 +790,7 @@ Mod_LoadMarksurfaces(const char *name, msurface_t ***marksurfaces, unsigned int msurface_t *surfaces, int numsurfaces, const byte *mod_base, const lump_t *l) { int i, count; - short *in; + const short *in; msurface_t **out; in = (void *)(mod_base + l->fileofs); @@ -820,7 +828,7 @@ Mod_LoadQMarksurfaces(const char *name, msurface_t ***marksurfaces, unsigned int msurface_t *surfaces, int numsurfaces, const byte *mod_base, const lump_t *l) { int i, count; - int *in; + const int *in; msurface_t **out; in = (void *)(mod_base + l->fileofs); diff --git a/src/client/refresh/files/mesh.c b/src/client/refresh/files/mesh.c index 6430aade..8a284b24 100644 --- a/src/client/refresh/files/mesh.c +++ b/src/client/refresh/files/mesh.c @@ -35,7 +35,7 @@ byte R_CompressNormalMDL(const float *normal) { byte i, besti; - float dot, bestdot; + float bestdot; bestdot = normal[0] * r_avertexnormals[0][0] + normal[1] * r_avertexnormals[0][1] + @@ -44,6 +44,8 @@ R_CompressNormalMDL(const float *normal) for (i = 1; i < NUMVERTEXNORMALS; i++) { + float dot; + dot = normal[0] * r_avertexnormals[i][0] + normal[1] * r_avertexnormals[i][1] + normal[2] * r_avertexnormals[i][2]; diff --git a/src/client/refresh/files/models.c b/src/client/refresh/files/models.c index 22ca5b77..ead09ea7 100644 --- a/src/client/refresh/files/models.c +++ b/src/client/refresh/files/models.c @@ -35,7 +35,7 @@ load base s and t vertices (not used in gl version) ================= */ static void -Mod_LoadSTvertList(dmdx_t *pheader, dstvert_t *pinst) +Mod_LoadSTvertList(dmdx_t *pheader, const dstvert_t *pinst) { dstvert_t *poutst; int i; @@ -57,7 +57,7 @@ Load the glcmds ================= */ static void -Mod_LoadCmdList(const char *mod_name, dmdx_t *pheader, int *pincmd) +Mod_LoadCmdList(const char *mod_name, dmdx_t *pheader, const int *pincmd) { int *poutcmd; int i; @@ -127,7 +127,7 @@ Load triangle lists ================= */ static void -Mod_LoadDTriangleList(dmdx_t *pheader, dtriangle_t *pintri) +Mod_LoadDTriangleList(dmdx_t *pheader, const dtriangle_t *pintri) { dtriangle_t *pouttri; int i; @@ -154,7 +154,7 @@ Load DKM triangle lists ================= */ static void -Mod_LoadDkmTriangleList(dmdx_t *pheader, dkmtriangle_t *pintri) +Mod_LoadDkmTriangleList(dmdx_t *pheader, const dkmtriangle_t *pintri) { dtriangle_t *pouttri; int i; @@ -183,7 +183,8 @@ Load the DKM glcmds static void Mod_LoadDKMCmdList(const char *mod_name, dmdx_t *pheader, int *pincmd) { - int *poutcmd, *pendcmd; + const int *pendcmd; + int *poutcmd; int i; poutcmd = (int *)((char*)pheader + pheader->ofs_glcmds); @@ -413,7 +414,7 @@ Mod_LoadModel_MDL(const char *mod_name, const void *buffer, int modfilelen, const byte *curr_pos; mdl_triangle_t *triangles; - mdl_texcoord_t *texcoords; + const mdl_texcoord_t *texcoords; curr_pos = (byte*)buffer + sizeof(mdl_header_t); @@ -614,8 +615,11 @@ static void * Mod_LoadModel_MD3(const char *mod_name, const void *buffer, int modfilelen, struct image_s ***skins, int *numskins, modtype_t *type) { + dmdx_t *pheader = NULL; + const int *baseglcmds; md3_header_t pinmodel; - void *extradata; + void *extradata; + int *pglcmds; int i; if (modfilelen < sizeof(pinmodel)) @@ -663,7 +667,7 @@ Mod_LoadModel_MD3(const char *mod_name, const void *buffer, int modfilelen, byte * meshofs = (byte*)buffer + pinmodel.ofs_meshes; for (i = 0; i < pinmodel.num_meshes; i++) { - md3_mesh_t *md3_mesh = (md3_mesh_t*)meshofs; + const md3_mesh_t *md3_mesh = (md3_mesh_t*)meshofs; num_xyz += LittleLong(md3_mesh->num_xyz); num_tris += LittleLong(md3_mesh->num_tris); @@ -691,8 +695,6 @@ Mod_LoadModel_MD3(const char *mod_name, const void *buffer, int modfilelen, int ofs_st = ofs_tris + num_tris * sizeof(dtriangle_t); int ofs_end = ofs_st + num_tris * 3 * sizeof(dstvert_t); - dmdx_t *pheader = NULL; - *numskins = num_skins; extradata = Hunk_Begin(ofs_end + Q_max(*numskins, MAX_MD2SKINS) * sizeof(struct image_s *)); pheader = Hunk_Alloc(ofs_end); @@ -716,13 +718,10 @@ Mod_LoadModel_MD3(const char *mod_name, const void *buffer, int modfilelen, pheader->ofs_glcmds = ofs_glcmds; pheader->ofs_end = ofs_end; - meshofs = (byte*)buffer + pinmodel.ofs_meshes; num_xyz = 0; num_tris = 0; - int *pglcmds, *baseglcmds; - - pglcmds = baseglcmds = (int *)((byte *)pheader + pheader->ofs_glcmds); + baseglcmds = pglcmds = (int *)((byte *)pheader + pheader->ofs_glcmds); dmdxmesh_t *mesh_nodes = (dmdxmesh_t *)((byte *)pheader + pheader->ofs_meshes); dtriangle_t *tris = (dtriangle_t*)((byte *)pheader + pheader->ofs_tris); dstvert_t *st = (dstvert_t*)((byte *)pheader + pheader->ofs_st); @@ -732,14 +731,14 @@ Mod_LoadModel_MD3(const char *mod_name, const void *buffer, int modfilelen, meshofs = (byte*)buffer + pinmodel.ofs_meshes; for (i = 0; i < pinmodel.num_meshes; i++) { - int j; - md3_mesh_t *md3_mesh = (md3_mesh_t*)meshofs; + const md3_mesh_t *md3_mesh = (md3_mesh_t*)meshofs; const float *fst = (const float*)(meshofs + md3_mesh->ofs_st); + int j; /* load shaders */ for (j = 0; j < md3_mesh->num_shaders; j++) { - md3_shader_t *md3_shader = (md3_shader_t*)(meshofs + md3_mesh->ofs_shaders) + j; + const md3_shader_t *md3_shader = (md3_shader_t*)(meshofs + md3_mesh->ofs_shaders) + j; strncpy(skin, md3_shader->name, MAX_SKINNAME - 1); skin += MAX_SKINNAME; @@ -759,7 +758,7 @@ Mod_LoadModel_MD3(const char *mod_name, const void *buffer, int modfilelen, for (j = 0; j < md3_mesh->num_tris * 3; j++) { int vert_id; - vec2_t st; + vec2_t cmdst; /* count */ if ((j % 3) == 0) @@ -769,9 +768,9 @@ Mod_LoadModel_MD3(const char *mod_name, const void *buffer, int modfilelen, } /* st */ - st[0] = LittleFloat(fst[LittleLong(p[j]) * 2 + 0]); - st[1] = LittleFloat(fst[LittleLong(p[j]) * 2 + 1]); - memcpy(pglcmds, &st, sizeof(st)); + cmdst[0] = LittleFloat(fst[LittleLong(p[j]) * 2 + 0]); + cmdst[1] = LittleFloat(fst[LittleLong(p[j]) * 2 + 1]); + memcpy(pglcmds, &cmdst, sizeof(cmdst)); pglcmds += 2; /* index */ vert_id = LittleLong(p[j]) + num_xyz; @@ -824,7 +823,7 @@ Mod_LoadModel_MD3(const char *mod_name, const void *buffer, int modfilelen, { daliasxframe_t *frame = (daliasxframe_t *)( (byte *)pheader + pheader->ofs_frames + i * pheader->framesize); - md3_frameinfo_t *md3_frameinfo = (md3_frameinfo_t*)inframe; + const md3_frameinfo_t *md3_frameinfo = (md3_frameinfo_t*)inframe; strncpy(frame->name, md3_frameinfo->name, sizeof(frame->name) - 1); PrepareFrameVertex(vertx + i * pheader->num_xyz, @@ -834,15 +833,16 @@ Mod_LoadModel_MD3(const char *mod_name, const void *buffer, int modfilelen, } free(vertx); + /* TODO: make separate function */ for (i = 0; i < pheader->num_skins; i++) { - char *skin; + char *skinname; - skin = (char *)pheader + pheader->ofs_skins + i * MAX_SKINNAME; - skin[MAX_SKINNAME - 1] = 0; + skinname = (char *)pheader + pheader->ofs_skins + i * MAX_SKINNAME; + skinname[MAX_SKINNAME - 1] = 0; R_Printf(PRINT_DEVELOPER, "%s: %s #%d: Should load external '%s'\n", - __func__, mod_name, i, skin); + __func__, mod_name, i, skinname); } *type = mod_alias; @@ -859,16 +859,16 @@ static void * Mod_LoadModel_MD2(const char *mod_name, const void *buffer, int modfilelen, struct image_s ***skins, int *numskins, modtype_t *type) { - vec3_t translate = {0, 0, 0}; - dmdl_t pinmodel; - dmdx_t *pheader; - dtriangle_t *pintri; - dstvert_t *pinst; - dmdxmesh_t *mesh_nodes; - int *pincmd; - void *extradata; - int i, framesize; int ofs_meshes, ofs_skins, ofs_st, ofs_tris, ofs_glcmds, ofs_frames, ofs_end; + vec3_t translate = {0, 0, 0}; + const dtriangle_t *pintri; + const dstvert_t *pinst; + const int *pincmd; + dmdl_t pinmodel; + dmdx_t *pheader; + dmdxmesh_t *mesh_nodes; + void *extradata; + int i, framesize; if (modfilelen < sizeof(pinmodel)) { @@ -1087,13 +1087,14 @@ Mod_LoadModel_Flex(const char *mod_name, const void *buffer, int modfilelen, modtype_t *type) { char *src = (char *)buffer; - int version, size, inframesize = 0; + int inframesize = 0; void *extradata = NULL; dmdx_t *pheader = NULL; while (modfilelen > 0) { char blockname[32]; + int version, size; memcpy(blockname, src, sizeof(blockname)); @@ -1106,9 +1107,9 @@ Mod_LoadModel_Flex(const char *mod_name, const void *buffer, int modfilelen, if (Q_strncasecmp(blockname, "header", sizeof(blockname)) == 0) { - int framesize; + const fmheader_t *header = (fmheader_t *)src; dmdx_t dmdxheader; - fmheader_t *header = (fmheader_t *)src; + int framesize; if (sizeof(fmheader_t) > size) { @@ -1515,9 +1516,10 @@ Mod_LoadSprite_SP2 (const char *mod_name, const void *buffer, int modfilelen, struct image_s ***skins, int *numskins, modtype_t *type) { - dsprite_t *sprin, *sprout; - void *extradata; + const dsprite_t *sprin; + dsprite_t *sprout; int i, numframes; + void *extradata; sprin = (dsprite_t *)buffer; numframes = LittleLong(sprin->numframes); @@ -1590,7 +1592,7 @@ Mod_LoadMinMaxUpdate(const char *mod_name, vec3_t mins, vec3_t maxs, void *extra if (type == mod_alias) { daliasxframe_t *frame; - dmdx_t *pheader; + const dmdx_t *pheader; int i; pheader = (dmdx_t *)extradata; @@ -1615,8 +1617,6 @@ Mod_LoadMinMaxUpdate(const char *mod_name, vec3_t mins, vec3_t maxs, void *extra maxs[1] = -9999; maxs[2] = -9999; - frame = (daliasxframe_t *)((char*)extradata + pheader->ofs_frames); - for (i = 0; i < pheader->num_frames; i++) { int j; @@ -1803,7 +1803,7 @@ Mod_LoadFileMD5Merge(const char *namewe, void **buffer) sprintf(skins_list, "\nnumskins %d\n", numskins); for(i = 0; i < numskins; i++) { - char *skinname = skins + MAX_SKINNAME * i; + const char *skinname = skins + MAX_SKINNAME * i; if (!md5path) { diff --git a/src/client/refresh/files/models_md5.c b/src/client/refresh/files/models_md5.c index f55da660..039ea863 100644 --- a/src/client/refresh/files/models_md5.c +++ b/src/client/refresh/files/models_md5.c @@ -223,37 +223,37 @@ BuildFrameSkeleton(const md5_joint_info_t *jointInfos, if (jointInfos[i].flags & 1) /* Tx */ { animatedPos[0] = animFrameData[jointInfos[i].startIndex + j]; - ++j; + j++; } if (jointInfos[i].flags & 2) /* Ty */ { animatedPos[1] = animFrameData[jointInfos[i].startIndex + j]; - ++j; + j++; } if (jointInfos[i].flags & 4) /* Tz */ { animatedPos[2] = animFrameData[jointInfos[i].startIndex + j]; - ++j; + j++; } if (jointInfos[i].flags & 8) /* Qx */ { animatedOrient[0] = animFrameData[jointInfos[i].startIndex + j]; - ++j; + j++; } if (jointInfos[i].flags & 16) /* Qy */ { animatedOrient[1] = animFrameData[jointInfos[i].startIndex + j]; - ++j; + j++; } if (jointInfos[i].flags & 32) /* Qz */ { animatedOrient[2] = animFrameData[jointInfos[i].startIndex + j]; - ++j; + j++; } /* Compute orient quaternion's w value */ @@ -976,7 +976,7 @@ MD5_ComputeNormals(md5_model_t *md5file) for(i = 0; i < md5file->num_frames; ++i) { - md5_frame_t *frame_in; + const md5_frame_t *frame_in; int k, vert_step = 0; frame_in = md5file->skelFrames + i; @@ -1033,11 +1033,13 @@ Mod_LoadModel_MD5(const char *mod_name, const void *buffer, int modfilelen, int mesh_size, anim_size; md5_model_t *md5file; void *extradata = NULL; - byte *startbuffer, *endbuffer; + const byte *endbuffer; + byte *startbuffer; int i, num_verts = 0, num_tris = 0, num_glcmds = 0; int framesize, ofs_skins, ofs_frames, ofs_glcmds, ofs_meshes, ofs_tris, ofs_st, ofs_end; dmdx_t *pheader = NULL; - int *pglcmds, *baseglcmds; + const int *baseglcmds; + int *pglcmds; dmdxmesh_t *mesh_nodes; dtriangle_t *tris; dstvert_t *st; @@ -1064,9 +1066,9 @@ Mod_LoadModel_MD5(const char *mod_name, const void *buffer, int modfilelen, return NULL; } - if (md5file && anim_size > 0) + if (anim_size > 0) { - ReadMD5Anim(md5file, buffer + mesh_size + 1, anim_size); + ReadMD5Anim(md5file, (char*)buffer + mesh_size + 1, anim_size); } if (!md5file->num_frames) @@ -1150,7 +1152,7 @@ Mod_LoadModel_MD5(const char *mod_name, const void *buffer, int modfilelen, num_tris = 0; - pglcmds = baseglcmds = (int *)((byte *)pheader + pheader->ofs_glcmds); + baseglcmds = pglcmds = (int *)((byte *)pheader + pheader->ofs_glcmds); mesh_nodes = (dmdxmesh_t *)((byte *)pheader + pheader->ofs_meshes); tris = (dtriangle_t*)((byte *)pheader + pheader->ofs_tris); st = (dstvert_t*)((byte *)pheader + pheader->ofs_st); @@ -1211,11 +1213,9 @@ Mod_LoadModel_MD5(const char *mod_name, const void *buffer, int modfilelen, __func__, mod_name, i, skin); } - if (md5file) - { - FreeModelMd5(md5file); - free(md5file); - } + + FreeModelMd5(md5file); + free(md5file); *type = mod_alias; diff --git a/src/client/refresh/ref_shared.h b/src/client/refresh/ref_shared.h index ada9c105..aa080a50 100644 --- a/src/client/refresh/ref_shared.h +++ b/src/client/refresh/ref_shared.h @@ -347,9 +347,10 @@ extern void Mod_LoadQBSPEdges(const char *name, medge_t **edges, int *numedges, extern void Mod_LoadVertexes(const char *name, mvertex_t **vertexes, int *numvertexes, const byte *mod_base, const lump_t *l); extern void Mod_LoadLighting(byte **lightdata, int *size, const byte *mod_base, const lump_t *l); -extern void Mod_LoadSetSurfaceLighting(byte *lightdata, int size, msurface_t *out, byte *styles, int lightofs); -extern void Mod_CalcSurfaceExtents(int *surfedges, mvertex_t *vertexes, medge_t *edges, - msurface_t *s); +extern void Mod_LoadSetSurfaceLighting(byte *lightdata, int size, msurface_t *out, + const byte *styles, int lightofs); +extern void Mod_CalcSurfaceExtents(const int *surfedges, mvertex_t *vertexes, + medge_t *edges, msurface_t *s); 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);