diff --git a/src/hardware/Sourcefile b/src/hardware/Sourcefile index e66482c3d..31802a9b5 100644 --- a/src/hardware/Sourcefile +++ b/src/hardware/Sourcefile @@ -2,7 +2,7 @@ hw_bsp.c hw_draw.c hw_main.cpp hw_clip.c -hw_md2.c +hw_md2.cpp hw_cache.c hw_md2load.c hw_md3load.c diff --git a/src/hardware/hw_data.h b/src/hardware/hw_data.h index d66fe0662..f77a7146c 100644 --- a/src/hardware/hw_data.h +++ b/src/hardware/hw_data.h @@ -23,6 +23,7 @@ typedef enum GLTextureFormat_e { + GL_TEXFMT_NULL = 0x00, GL_TEXFMT_P_8 = 0x01, /* 8-bit palette */ GL_TEXFMT_AP_88 = 0x02, /* 8-bit alpha, 8-bit palette */ diff --git a/src/hardware/hw_md2.c b/src/hardware/hw_md2.cpp similarity index 90% rename from src/hardware/hw_md2.c rename to src/hardware/hw_md2.cpp index 60f76b370..bae3d438e 100644 --- a/src/hardware/hw_md2.c +++ b/src/hardware/hw_md2.cpp @@ -13,6 +13,7 @@ /// Inspired from md2.c by Mete Ciragan (mete@swissquake.ch) +#include "hw_data.h" #ifdef __GNUC__ #include #endif @@ -72,6 +73,12 @@ #include #endif +#include + +struct ColorMipMap { + std::unordered_map map; +}; + md2_t md2_models[NUMSPRITES] = {}; md2_t md2_playermodels[MAXSKINS] = {}; md2_t md2_localplayermodels[MAXLOCALSKINS] = {}; @@ -93,10 +100,10 @@ static void md2_freeModel (model_t *model) static model_t *md2_readModel(const char *filename) { //Filename checking fixed ~Monster Iestyn and Golden - if (FIL_FileExists(va("%s"PATHSEP"%s", srb2home, filename))) - return LoadModel(va("%s"PATHSEP"%s", srb2home, filename), PU_STATIC); - else if (FIL_FileExists(va("%s"PATHSEP"%s", srb2path, filename))) - return LoadModel(va("%s"PATHSEP"%s", srb2path, filename), PU_STATIC); + if (FIL_FileExists(va("%s" PATHSEP "%s", srb2home, filename))) + return LoadModel(va("%s" PATHSEP "%s", srb2home, filename), PU_STATIC); + else if (FIL_FileExists(va("%s" PATHSEP "%s", srb2path, filename))) + return LoadModel(va("%s" PATHSEP "%s", srb2path, filename), PU_STATIC); return NULL; } @@ -159,18 +166,18 @@ static GLTextureFormat_t PNG_Load(const char *filename, int *w, int *h, GLPatch_ #endif volatile png_FILE_p png_FILE; //Filename checking fixed ~Monster Iestyn and Golden - char *pngfilename = va("%s"PATHSEP"mdls"PATHSEP"%s", srb2home, filename); + char *pngfilename = va("%s" PATHSEP "mdls" PATHSEP "%s", srb2home, filename); FIL_ForceExtension(pngfilename, ".png"); png_FILE = fopen(pngfilename, "rb"); if (!png_FILE) { - pngfilename = va("%s"PATHSEP"mdls"PATHSEP"%s", srb2path, filename); + pngfilename = va("%s" PATHSEP "mdls" PATHSEP "%s", srb2path, filename); FIL_ForceExtension(pngfilename, ".png"); png_FILE = fopen(pngfilename, "rb"); //CONS_Debug(DBG_RENDER, "M_SavePNG: Error on opening %s for loading\n", filename); if (!png_FILE) - return 0; + return GL_TEXFMT_NULL; } png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, @@ -179,7 +186,7 @@ static GLTextureFormat_t PNG_Load(const char *filename, int *w, int *h, GLPatch_ { CONS_Debug(DBG_RENDER, "PNG_Load: Error on initialize libpng\n"); fclose(png_FILE); - return 0; + return GL_TEXFMT_NULL; } png_info_ptr = png_create_info_struct(png_ptr); @@ -188,7 +195,7 @@ static GLTextureFormat_t PNG_Load(const char *filename, int *w, int *h, GLPatch_ CONS_Debug(DBG_RENDER, "PNG_Load: Error on allocate for libpng\n"); png_destroy_read_struct(&png_ptr, NULL, NULL); fclose(png_FILE); - return 0; + return GL_TEXFMT_NULL; } #ifdef USE_FAR_KEYWORD @@ -201,7 +208,7 @@ static GLTextureFormat_t PNG_Load(const char *filename, int *w, int *h, GLPatch_ png_destroy_read_struct(&png_ptr, &png_info_ptr, NULL); fclose(png_FILE); Z_Free(glpatch->mipmap->data); - return 0; + return GL_TEXFMT_NULL; } #ifdef USE_FAR_KEYWORD png_memcpy(png_jmpbuf(png_ptr), jmpbuf, sizeof jmp_buf); @@ -241,8 +248,8 @@ static GLTextureFormat_t PNG_Load(const char *filename, int *w, int *h, GLPatch_ { png_uint_32 i, pitch = png_get_rowbytes(png_ptr, png_info_ptr); - png_bytep PNG_image = Z_Malloc(pitch*height, PU_HWRMODELTEXTURE, &glpatch->mipmap->data); - png_bytepp row_pointers = png_malloc(png_ptr, height * sizeof (png_bytep)); + png_bytep PNG_image = static_cast(Z_Malloc(pitch*height, PU_HWRMODELTEXTURE, &glpatch->mipmap->data)); + png_bytepp row_pointers = static_cast(png_malloc(png_ptr, height * sizeof (png_bytep))); for (i = 0; i < height; i++) row_pointers[i] = PNG_image + i*pitch; png_read_image(png_ptr, row_pointers); @@ -292,42 +299,42 @@ static GLTextureFormat_t PCX_Load(const char *filename, int *w, int *h, INT32 ch, rep; FILE *file; //Filename checking fixed ~Monster Iestyn and Golden - char *pcxfilename = va("%s"PATHSEP"mdls"PATHSEP"%s", srb2home, filename); + char *pcxfilename = va("%s" PATHSEP "mdls" PATHSEP "%s", srb2home, filename); FIL_ForceExtension(pcxfilename, ".pcx"); file = fopen(pcxfilename, "rb"); if (!file) { - pcxfilename = va("%s"PATHSEP"mdls"PATHSEP"%s", srb2path, filename); + pcxfilename = va("%s" PATHSEP "mdls" PATHSEP "%s", srb2path, filename); FIL_ForceExtension(pcxfilename, ".pcx"); file = fopen(pcxfilename, "rb"); if (!file) - return 0; + return GL_TEXFMT_NULL; } if (fread(&header, sizeof (PcxHeader), 1, file) != 1) { fclose(file); - return 0; + return GL_TEXFMT_NULL; } if (header.bitsPerPixel != 8) { fclose(file); - return 0; + return GL_TEXFMT_NULL; } fseek(file, -PALSIZE, SEEK_END); pw = *w = header.xmax - header.xmin + 1; ph = *h = header.ymax - header.ymin + 1; - image = Z_Malloc(pw*ph*4, PU_HWRMODELTEXTURE, &glpatch->mipmap->data); + image = static_cast(Z_Malloc(pw*ph*4, PU_HWRMODELTEXTURE, &glpatch->mipmap->data)); if (fread(palette, sizeof (UINT8), PALSIZE, file) != PALSIZE) { Z_Free(image); fclose(file); - return 0; + return GL_TEXFMT_NULL; } fseek(file, sizeof (PcxHeader), SEEK_SET); @@ -369,8 +376,8 @@ static void md2_loadTexture(md2_t *model) if (model->glpatch) { - patch = model->glpatch; - glPatch = (GLPatch_t *)(patch->hardware); + patch = static_cast(model->glpatch); + glPatch = static_cast(patch->hardware); if (glPatch) Z_Free(glPatch->mipmap->data); } @@ -381,7 +388,7 @@ static void md2_loadTexture(md2_t *model) Patch_AllocateHardwarePatch(patch); if (glPatch == NULL) - glPatch = (GLPatch_t *)(patch->hardware); + glPatch = static_cast(patch->hardware); if (!glPatch->mipmap->downloaded && !glPatch->mipmap->data) { @@ -389,10 +396,10 @@ static void md2_loadTexture(md2_t *model) #ifdef HAVE_PNG glPatch->mipmap->format = PNG_Load(filename, &w, &h, glPatch); - if (glPatch->mipmap->format == 0) + if (glPatch->mipmap->format == GL_TEXFMT_NULL) #endif glPatch->mipmap->format = PCX_Load(filename, &w, &h, glPatch); - if (glPatch->mipmap->format == 0) + if (glPatch->mipmap->format == GL_TEXFMT_NULL) { model->notexturefile = true; // mark it so its not searched for again repeatedly return; @@ -412,7 +419,7 @@ static void md2_loadTexture(md2_t *model) UINT32 size; RGBA_t *image; // Lactozilla: Apply colour cube - image = glPatch->mipmap->data; + image = static_cast(glPatch->mipmap->data); size = w*h; while (size--) { @@ -433,7 +440,7 @@ static void md2_loadBlendTexture(md2_t *model) patch_t *patch; GLPatch_t *glPatch = NULL; - char *filename = Z_Malloc(strlen(model->filename)+7, PU_STATIC, NULL); + char *filename = static_cast(Z_Malloc(strlen(model->filename)+7, PU_STATIC, NULL)); strcpy(filename, model->filename); @@ -441,8 +448,8 @@ static void md2_loadBlendTexture(md2_t *model) if (model->blendglpatch) { - patch = model->blendglpatch; - glPatch = (GLPatch_t *)(patch->hardware); + patch = static_cast(model->blendglpatch); + glPatch = static_cast(patch->hardware); if (glPatch) Z_Free(glPatch->mipmap->data); } @@ -453,17 +460,17 @@ static void md2_loadBlendTexture(md2_t *model) Patch_AllocateHardwarePatch(patch); if (glPatch == NULL) - glPatch = (GLPatch_t *)(patch->hardware); + glPatch = static_cast(patch->hardware); if (!glPatch->mipmap->downloaded && !glPatch->mipmap->data) { int w = 0, h = 0; #ifdef HAVE_PNG glPatch->mipmap->format = PNG_Load(filename, &w, &h, glPatch); - if (glPatch->mipmap->format == 0) + if (glPatch->mipmap->format == GL_TEXFMT_NULL) #endif glPatch->mipmap->format = PCX_Load(filename, &w, &h, glPatch); - if (glPatch->mipmap->format == 0) + if (glPatch->mipmap->format == GL_TEXFMT_NULL) { model->noblendfile = true; // mark it so its not searched for again repeatedly Z_Free(filename); @@ -532,11 +539,11 @@ void HWR_InitMD2(void) // read the mdls.dat file //Filename checking fixed ~Monster Iestyn and Golden - f = fopen(va("%s"PATHSEP"%s", srb2home, "mdls.dat"), "rt"); + f = fopen(va("%s" PATHSEP "%s", srb2home, "mdls.dat"), "rt"); if (!f) { - f = fopen(va("%s"PATHSEP"%s", srb2path, "mdls.dat"), "rt"); + f = fopen(va("%s" PATHSEP "%s", srb2path, "mdls.dat"), "rt"); if (!f) { CONS_Printf("%s %s\n", M_GetText("Error while loading mdls.dat:"), strerror(errno)); @@ -593,11 +600,11 @@ void HWR_AddPlayerMD2(int skin, boolean local) // For MD2's that were added afte // read the mdls.dat file //Filename checking fixed ~Monster Iestyn and Golden - f = fopen(va("%s"PATHSEP"%s", srb2home, "mdls.dat"), "rt"); + f = fopen(va("%s" PATHSEP "%s", srb2home, "mdls.dat"), "rt"); if (!f) { - f = fopen(va("%s"PATHSEP"%s", srb2path, "mdls.dat"), "rt"); + f = fopen(va("%s" PATHSEP "%s", srb2path, "mdls.dat"), "rt"); if (!f) { CONS_Printf("%s %s\n", M_GetText("Error while loading mdls.dat:"), strerror(errno)); @@ -645,11 +652,11 @@ void HWR_AddSpriteMD2(size_t spritenum) // For MD2s that were added after startu // Read the md2.dat file //Filename checking fixed ~Monster Iestyn and Golden - f = fopen(va("%s"PATHSEP"%s", srb2home, "mdls.dat"), "rt"); + f = fopen(va("%s" PATHSEP "%s", srb2home, "mdls.dat"), "rt"); if (!f) { - f = fopen(va("%s"PATHSEP"%s", srb2path, "mdls.dat"), "rt"); + f = fopen(va("%s" PATHSEP "%s", srb2path, "mdls.dat"), "rt"); if (!f) { @@ -689,8 +696,8 @@ spritemd2found: static void HWR_CreateBlendedTexture(patch_t *gpatch, patch_t *blendgpatch, GLMipmap_t *glMipmap, INT32 skinnum, skincolors_t color) { - GLPatch_t *hwrPatch = gpatch->hardware; - GLPatch_t *hwrBlendPatch = blendgpatch->hardware; + GLPatch_t *hwrPatch = static_cast(gpatch->hardware); + GLPatch_t *hwrBlendPatch = static_cast(blendgpatch->hardware); UINT16 w = gpatch->width, h = gpatch->height; UINT32 size = w*h; RGBA_t *image, *blendimage, *cur, blendcolor; @@ -722,11 +729,11 @@ static void HWR_CreateBlendedTexture(patch_t *gpatch, patch_t *blendgpatch, GLMi glMipmap->data = NULL; } - cur = Z_Malloc(size*4, PU_HWRMODELTEXTURE, &glMipmap->data); + cur = static_cast(Z_Malloc(size*4, PU_HWRMODELTEXTURE, &glMipmap->data)); memset(cur, 0x00, size*4); - image = hwrPatch->mipmap->data; - blendimage = hwrBlendPatch->mipmap->data; + image = static_cast(hwrPatch->mipmap->data); + blendimage = static_cast(hwrBlendPatch->mipmap->data); // TC_METALSONIC includes an actual skincolor translation, on top of its flashing. if (skinnum == TC_METALSONIC) @@ -972,15 +979,15 @@ static void HWR_CreateBlendedTexture(patch_t *gpatch, patch_t *blendgpatch, GLMi colorbright = 1; // no dividing by 0 please tempcolor = (brightness * blendcolor.s.red) / colorbright; - tempcolor = min(255, tempcolor); + tempcolor = std::min(255, tempcolor); cur->s.red = (UINT8)tempcolor; tempcolor = (brightness * blendcolor.s.green) / colorbright; - tempcolor = min(255, tempcolor); + tempcolor = std::min(255, tempcolor); cur->s.green = (UINT8)tempcolor; tempcolor = (brightness * blendcolor.s.blue) / colorbright; - tempcolor = min(255, tempcolor); + tempcolor = std::min(255, tempcolor); cur->s.blue = (UINT8)tempcolor; cur->s.alpha = image->s.alpha; } @@ -990,15 +997,15 @@ static void HWR_CreateBlendedTexture(patch_t *gpatch, patch_t *blendgpatch, GLMi INT32 tempcolor; tempcolor = ((image->s.red * (255-blendimage->s.alpha)) / 255) + ((blendcolor.s.red * blendimage->s.alpha) / 255); - tempcolor = min(255, tempcolor); + tempcolor = std::min(255, tempcolor); cur->s.red = (UINT8)tempcolor; tempcolor = ((image->s.green * (255-blendimage->s.alpha)) / 255) + ((blendcolor.s.green * blendimage->s.alpha) / 255); - tempcolor = min(255, tempcolor); + tempcolor = std::min(255, tempcolor); cur->s.green = (UINT8)tempcolor; tempcolor = ((image->s.blue * (255-blendimage->s.alpha)) / 255) + ((blendcolor.s.blue * blendimage->s.alpha) / 255); - tempcolor = min(255, tempcolor); + tempcolor = std::min(255, tempcolor); cur->s.blue = (UINT8)tempcolor; cur->s.alpha = image->s.alpha; } @@ -1033,29 +1040,31 @@ skippixel: static void HWR_GetBlendedTexture(patch_t *patch, patch_t *blendgpatch, INT32 skinnum, const UINT8 *colormap, skincolors_t color) { - (void)patch; - (void)blendgpatch; - (void)skinnum; - (void)colormap; - (void)color; - /* // mostly copied from HWR_GetMappedPatch, hence the similarities and comment - GLPatch_t *glPatch = patch->hardware; - GLMipmap_t *glMipmap, *newMipmap; + GLPatch_t *glPatch = static_cast(patch->hardware); + GLMipmap_t *newMipmap; - - if (blendgpatch == NULL || colormap == colormaps || colormap == NULL) + if (blendgpatch == NULL || colormap == colormaps + || colormap == NULL || colormap == (const UINT8*)(COLORMAP_REMAPOFFSET)) { // Don't do any blending GL_SetTexture(glPatch->mipmap); return; } - // search for the Mipmap - // skip the first (no colormap translated) - for (glMipmap = glPatch->mipmap; glMipmap->nextcolormap; ) + // gotta make our map if theres none + if (!glPatch->mipmap->nextcolormap) { - glMipmap = glMipmap->nextcolormap; + glPatch->mipmap->nextcolormap = new ColorMipMap(); + } + + // search for the Mipmap + auto& map = glPatch->mipmap->nextcolormap->map; + auto it = map.find(colormap); + + if (it != map.end()) + { + GLMipmap_t *glMipmap = it->second; if (glMipmap->colormap && glMipmap->colormap->source == colormap) { @@ -1083,12 +1092,14 @@ static void HWR_GetBlendedTexture(patch_t *patch, patch_t *blendgpatch, INT32 sk // (it have a liste of mipmap) // this malloc is cleared in HWR_FreeTextureCache // (...) unfortunately z_malloc fragment alot the memory :(so malloc is better - newMipmap = calloc(1, sizeof (*newMipmap)); + newMipmap = static_cast(calloc(1, sizeof(*newMipmap))); if (newMipmap == NULL) I_Error("%s: Out of memory", "HWR_GetBlendedTexture"); - glMipmap->nextcolormap = newMipmap; - newMipmap->colormap = Z_Calloc(sizeof(*newMipmap->colormap), PU_HWRPATCHCOLMIPMAP, NULL); + // add to nextcolormap map + map[colormap] = newMipmap; + + newMipmap->colormap = static_cast(Z_Calloc(sizeof(*newMipmap->colormap), PU_HWRPATCHCOLMIPMAP, NULL)); newMipmap->colormap->source = colormap; memcpy(newMipmap->colormap->data, colormap, 256 * sizeof(UINT8)); @@ -1096,7 +1107,6 @@ static void HWR_GetBlendedTexture(patch_t *patch, patch_t *blendgpatch, INT32 sk GL_SetTexture(newMipmap); Z_ChangeTag(newMipmap->data, PU_HWRMODELTEXTURE_UNLOCKED); - */ } // -----------------+ @@ -1186,7 +1196,7 @@ void HWR_DrawMD2(gl_vissprite_t *spr) else blendmode = spr->mobj->blendmode; - blendmode = min(AST_MODULATE, blendmode); + blendmode = std::min(AST_MODULATE, blendmode); if (spr->mobj->flags2 & MF2_SHADOW) { @@ -1275,7 +1285,7 @@ void HWR_DrawMD2(gl_vissprite_t *spr) } //Hurdler: arf, I don't like that implementation at all... too much crappy - gpatch = md2->glpatch; + gpatch = static_cast(md2->glpatch); if (gpatch) hwrPatch = ((GLPatch_t *)gpatch->hardware); @@ -1284,12 +1294,12 @@ void HWR_DrawMD2(gl_vissprite_t *spr) md2_loadTexture(md2); // Load it again, because it isn't being loaded into gpatch after md2_loadtexture... - gpatch = md2->glpatch; + gpatch = static_cast(md2->glpatch); if (gpatch) hwrPatch = ((GLPatch_t *)gpatch->hardware); // Load blend texture - blendgpatch = md2->blendglpatch; + blendgpatch = static_cast(md2->blendglpatch); if (blendgpatch) hwrBlendPatch = ((GLPatch_t *)blendgpatch->hardware); @@ -1299,7 +1309,7 @@ void HWR_DrawMD2(gl_vissprite_t *spr) md2_loadBlendTexture(md2); // Load it again, because it isn't being loaded into blendgpatch after md2_loadblendtexture... - blendgpatch = md2->blendglpatch; + blendgpatch = static_cast(md2->blendglpatch); if (blendgpatch) hwrBlendPatch = ((GLPatch_t *)blendgpatch->hardware);