From 5eab9441573c8f9b34d8a68a3a730d0a327252f0 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sat, 8 Dec 2018 23:28:35 +0100 Subject: [PATCH] - started separating the texture class from the image format handlers. --- src/CMakeLists.txt | 2 + src/polyrenderer/scene/poly_playersprite.cpp | 3 + src/textures/formats/buildtexture.cpp | 1 - src/textures/formats/ddstexture.cpp | 25 ----- src/textures/formats/imgztexture.cpp | 1 - src/textures/formats/jpegtexture.cpp | 12 --- src/textures/formats/multipatchtexture.cpp | 17 ---- src/textures/formats/patchtexture.cpp | 32 +++---- src/textures/formats/pcxtexture.cpp | 13 --- src/textures/formats/pngtexture.cpp | 22 ----- src/textures/formats/tgatexture.cpp | 12 --- src/textures/image.cpp | 84 +++++++++++++++++ src/textures/image.h | 97 ++++++++++++++++++++ src/textures/imagetexture.cpp | 97 ++++++++++++++++++++ src/textures/texture.cpp | 5 - src/textures/textures.h | 3 - 16 files changed, 297 insertions(+), 129 deletions(-) create mode 100644 src/textures/image.cpp create mode 100644 src/textures/image.h create mode 100644 src/textures/imagetexture.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 035e2016c..6166ea2fe 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1098,6 +1098,8 @@ set (PCH_SOURCES textures/anim_switches.cpp textures/bitmap.cpp textures/texture.cpp + textures/image.cpp + textures/imagetexture.cpp textures/texturemanager.cpp textures/skyboxtexture.cpp textures/formats/automaptexture.cpp diff --git a/src/polyrenderer/scene/poly_playersprite.cpp b/src/polyrenderer/scene/poly_playersprite.cpp index 64a115290..93cfca926 100644 --- a/src/polyrenderer/scene/poly_playersprite.cpp +++ b/src/polyrenderer/scene/poly_playersprite.cpp @@ -386,6 +386,8 @@ void RenderPolyPlayerSprites::RenderSprite(PolyRenderThread *thread, DPSprite *p { noaccel = true; } +#if 0 + // The HW 2D drawer should be able to handle this without problems // If the main colormap has fixed lights, and this sprite is being drawn with that // colormap, disable acceleration so that the lights can remain fixed. PolyCameraLight *cameraLight = PolyCameraLight::Instance(); @@ -395,6 +397,7 @@ void RenderPolyPlayerSprites::RenderSprite(PolyRenderThread *thread, DPSprite *p { noaccel = true; } +#endif } else { diff --git a/src/textures/formats/buildtexture.cpp b/src/textures/formats/buildtexture.cpp index ed7bbe1f8..ede4a5a76 100644 --- a/src/textures/formats/buildtexture.cpp +++ b/src/textures/formats/buildtexture.cpp @@ -59,7 +59,6 @@ public: TArray Get8BitPixels(bool alphatex) override; int CopyPixels(FBitmap *bmp) override; bool UseBasePalette() override { return false; } - FTextureFormat GetFormat() override { return TEX_RGB; } protected: const uint8_t *RawPixels; diff --git a/src/textures/formats/ddstexture.cpp b/src/textures/formats/ddstexture.cpp index cbcb1b562..4d84c365a 100644 --- a/src/textures/formats/ddstexture.cpp +++ b/src/textures/formats/ddstexture.cpp @@ -163,7 +163,6 @@ class FDDSTexture : public FWorldTexture public: FDDSTexture (FileReader &lump, int lumpnum, void *surfdesc); - FTextureFormat GetFormat () override; TArray Get8BitPixels(bool alphatex) override; protected: @@ -375,30 +374,6 @@ void FDDSTexture::CalcBitShift (uint32_t mask, uint8_t *lshiftp, uint8_t *rshift // //========================================================================== -FTextureFormat FDDSTexture::GetFormat() -{ -#if 0 - switch (Format) - { - case ID_DXT1: return TEX_DXT1; - case ID_DXT2: return TEX_DXT2; - case ID_DXT3: return TEX_DXT3; - case ID_DXT4: return TEX_DXT4; - case ID_DXT5: return TEX_DXT5; - default: return TEX_RGB; - } -#else - // For now, create a true color texture to preserve all colors. - return TEX_RGB; -#endif -} - -//========================================================================== -// -// -// -//========================================================================== - TArray FDDSTexture::Get8BitPixels(bool alphatex) { auto lump = Wads.OpenLumpReader (SourceLump); diff --git a/src/textures/formats/imgztexture.cpp b/src/textures/formats/imgztexture.cpp index 7523d2080..bc70f5e67 100644 --- a/src/textures/formats/imgztexture.cpp +++ b/src/textures/formats/imgztexture.cpp @@ -71,7 +71,6 @@ public: int CopyPixels(FBitmap *bmp) override; bool UseBasePalette() override { return !isalpha; } - FTextureFormat GetFormat() override { return isalpha ? TEX_RGB : TEX_Pal; } // should be TEX_Gray instead of TEX_RGB. Maybe later when all is working. }; diff --git a/src/textures/formats/jpegtexture.cpp b/src/textures/formats/jpegtexture.cpp index 647fc139c..5a39dc9e8 100644 --- a/src/textures/formats/jpegtexture.cpp +++ b/src/textures/formats/jpegtexture.cpp @@ -184,7 +184,6 @@ class FJPEGTexture : public FWorldTexture public: FJPEGTexture (int lumpnum, int width, int height); - FTextureFormat GetFormat () override; int CopyPixels(FBitmap *bmp) override; bool UseBasePalette() override; TArray Get8BitPixels(bool alphatex) override; @@ -262,17 +261,6 @@ FJPEGTexture::FJPEGTexture (int lumpnum, int width, int height) // //========================================================================== -FTextureFormat FJPEGTexture::GetFormat() -{ - return TEX_RGB; -} - -//========================================================================== -// -// -// -//========================================================================== - TArray FJPEGTexture::Get8BitPixels(bool doalpha) { auto lump = Wads.OpenLumpReader (SourceLump); diff --git a/src/textures/formats/multipatchtexture.cpp b/src/textures/formats/multipatchtexture.cpp index f8e7078d8..63cffb95d 100644 --- a/src/textures/formats/multipatchtexture.cpp +++ b/src/textures/formats/multipatchtexture.cpp @@ -153,7 +153,6 @@ public: FMultiPatchTexture (FScanner &sc, ETextureType usetype); ~FMultiPatchTexture (); - FTextureFormat GetFormat() override; bool UseBasePalette() override; virtual void SetFrontSkyLayer () override; @@ -523,22 +522,6 @@ int FMultiPatchTexture::CopyPixels(FBitmap *bmp) return retv; } -//========================================================================== -// -// FMultiPatchTexture :: GetFormat -// -// only returns 'paletted' if all patches use the base palette. -// -//========================================================================== - -FTextureFormat FMultiPatchTexture::GetFormat() -{ - if (bComplex) return TEX_RGB; - if (NumParts == 1) return Parts[0].Texture->GetFormat(); - return UseBasePalette() ? TEX_Pal : TEX_RGB; -} - - //=========================================================================== // // FMultipatchTexture::UseBasePalette diff --git a/src/textures/formats/patchtexture.cpp b/src/textures/formats/patchtexture.cpp index 25492916b..f988b9c02 100644 --- a/src/textures/formats/patchtexture.cpp +++ b/src/textures/formats/patchtexture.cpp @@ -39,6 +39,7 @@ #include "v_palette.h" #include "v_video.h" #include "bitmap.h" +#include "image.h" #include "imagehelpers.h" @@ -57,18 +58,16 @@ bool checkPatchForAlpha(const void *buffer, uint32_t length); // //========================================================================== -class FPatchTexture : public FWorldTexture +class FPatchTexture : public FImageSource { bool badflag = false; bool isalpha = false; + bool bNoRemap0 = false; // Unfortunately this was done as a very bad hack in ZDoom and will need impovement public: FPatchTexture (int lumpnum, patch_t *header, bool isalphatex); TArray Get8BitPixels(bool alphatex) override; int CopyPixels(FBitmap *bmp) override; void DetectBadPatches(); - - bool UseBasePalette() override { return !isalpha; } - FTextureFormat GetFormat() override { return isalpha ? TEX_RGB : TEX_Pal; } // should be TEX_Gray instead of TEX_RGB. Maybe later when all is working. }; //========================================================================== @@ -81,11 +80,10 @@ static bool CheckIfPatch(FileReader & file, bool &isalpha) { if (file.GetLength() < 13) return false; // minimum length of a valid Doom patch - uint8_t *data = new uint8_t[file.GetLength()]; file.Seek(0, FileReader::SeekSet); - file.Read(data, file.GetLength()); + auto data = file.Read(file.GetLength()); - const patch_t *foo = (const patch_t *)data; + const patch_t *foo = (const patch_t *)data.Data(); int height = LittleShort(foo->height); int width = LittleShort(foo->width); @@ -108,7 +106,6 @@ static bool CheckIfPatch(FileReader & file, bool &isalpha) } else if (ofs >= (uint32_t)(file.GetLength())) // Need one byte for an empty column (but there's patches that don't know that!) { - delete [] data; return false; } } @@ -116,12 +113,10 @@ static bool CheckIfPatch(FileReader & file, bool &isalpha) { // only check this if the texture passed validation. // Here is a good point because we already have a valid buffer of the lump's data. - isalpha = checkPatchForAlpha(data, (uint32_t)file.GetLength()); + isalpha = checkPatchForAlpha(data.Data(), (uint32_t)file.GetLength()); } - delete[] data; return !gapAtStart; } - delete [] data; return false; } @@ -142,7 +137,7 @@ FTexture *PatchTexture_TryCreate(FileReader & file, int lumpnum) header.height = file.ReadUInt16(); header.leftoffset = file.ReadInt16(); header.topoffset = file.ReadInt16(); - return new FPatchTexture(lumpnum, &header, isalpha); + return new FImageTexture(new FPatchTexture(lumpnum, &header, isalpha)); } //========================================================================== @@ -152,13 +147,14 @@ FTexture *PatchTexture_TryCreate(FileReader & file, int lumpnum) //========================================================================== FPatchTexture::FPatchTexture (int lumpnum, patch_t * header, bool isalphatex) -: FWorldTexture(NULL, lumpnum) +: FImageSource(lumpnum) { + bUseGamePalette = !isalphatex; isalpha = isalphatex; Width = header->width; Height = header->height; - _LeftOffset[1] = _LeftOffset[0] = header->leftoffset; - _TopOffset[1] = _TopOffset[0] = header->topoffset; + LeftOffset = header->leftoffset; + TopOffset = header->topoffset; DetectBadPatches(); } @@ -267,7 +263,7 @@ TArray FPatchTexture::Get8BitPixels(bool alphatex) int FPatchTexture::CopyPixels(FBitmap *bmp) { - if (!isalpha) return FTexture::CopyPixels(bmp); + if (!isalpha) return FImageSource::CopyPixels(bmp); else return CopyTranslatedPixels(bmp, translationtables[TRANSLATION_Standard][STD_Grayscale]->Palette); } @@ -305,8 +301,8 @@ void FPatchTexture::DetectBadPatches () return; // More than one post in a column! } } - _LeftOffset[1] = _LeftOffset[0] = 0; - _TopOffset[1] = _TopOffset[0] = 0; + LeftOffset = 0; + TopOffset = 0; badflag = true; bMasked = false; // Hacked textures don't have transparent parts. } diff --git a/src/textures/formats/pcxtexture.cpp b/src/textures/formats/pcxtexture.cpp index 14531c5c6..7a28dffb0 100644 --- a/src/textures/formats/pcxtexture.cpp +++ b/src/textures/formats/pcxtexture.cpp @@ -84,8 +84,6 @@ class FPCXTexture : public FWorldTexture public: FPCXTexture (int lumpnum, PCXHeader &); - FTextureFormat GetFormat () override; - int CopyPixels(FBitmap *bmp) override; bool UseBasePalette() override; @@ -157,17 +155,6 @@ FPCXTexture::FPCXTexture(int lumpnum, PCXHeader & hdr) // //========================================================================== -FTextureFormat FPCXTexture::GetFormat() -{ - return TEX_RGB; -} - -//========================================================================== -// -// -// -//========================================================================== - void FPCXTexture::ReadPCX1bit (uint8_t *dst, FileReader & lump, PCXHeader *hdr) { int y, i, bytes; diff --git a/src/textures/formats/pngtexture.cpp b/src/textures/formats/pngtexture.cpp index 75ae9f5d7..cbdf6f3aa 100644 --- a/src/textures/formats/pngtexture.cpp +++ b/src/textures/formats/pngtexture.cpp @@ -53,7 +53,6 @@ public: FPNGTexture (FileReader &lump, int lumpnum, const FString &filename, int width, int height, uint8_t bitdepth, uint8_t colortype, uint8_t interlace); ~FPNGTexture(); - FTextureFormat GetFormat () override; int CopyPixels(FBitmap *bmp) override; bool UseBasePalette() override; TArray Get8BitPixels(bool alphatex) override; @@ -349,27 +348,6 @@ FPNGTexture::~FPNGTexture () // //========================================================================== -FTextureFormat FPNGTexture::GetFormat() -{ -#if 0 - switch (ColorType) - { - case 3: return TEX_Pal; - case 0: return TEX_Gray; - default: return TEX_RGB; - } -#else - // For now, create a true color texture to preserve all colors. - return TEX_RGB; -#endif -} - -//========================================================================== -// -// -// -//========================================================================== - void FPNGTexture::ReadAlphaRemap(FileReader *lump, uint8_t *alpharemap) { auto p = lump->Tell(); diff --git a/src/textures/formats/tgatexture.cpp b/src/textures/formats/tgatexture.cpp index 12b420e19..a97d26dba 100644 --- a/src/textures/formats/tgatexture.cpp +++ b/src/textures/formats/tgatexture.cpp @@ -80,7 +80,6 @@ class FTGATexture : public FWorldTexture public: FTGATexture (int lumpnum, TGAHeader *); - FTextureFormat GetFormat () override; int CopyPixels(FBitmap *bmp) override; bool UseBasePalette() override; @@ -145,17 +144,6 @@ FTGATexture::FTGATexture (int lumpnum, TGAHeader * hdr) // //========================================================================== -FTextureFormat FTGATexture::GetFormat() -{ - return TEX_RGB; -} - -//========================================================================== -// -// -// -//========================================================================== - void FTGATexture::ReadCompressed(FileReader &lump, uint8_t * buffer, int bytesperpixel) { uint8_t data[4]; diff --git a/src/textures/image.cpp b/src/textures/image.cpp new file mode 100644 index 000000000..ac217d171 --- /dev/null +++ b/src/textures/image.cpp @@ -0,0 +1,84 @@ +/* +** texture.cpp +** The base texture class +** +**--------------------------------------------------------------------------- +** Copyright 2004-2007 Randy Heit +** Copyright 2006-2018 Christoph Oelckers +** All rights reserved. +** +** Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions +** are met: +** +** 1. Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** 2. Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in the +** documentation and/or other materials provided with the distribution. +** 3. The name of the author may not be used to endorse or promote products +** derived from this software without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +**--------------------------------------------------------------------------- +** +** +*/ + +#include "v_video.h" +#include "bitmap.h" +#include "image.h" + + +//=========================================================================== +// +// the default just returns an empty texture. +// +//=========================================================================== + +TArray FImageSource::Get8BitPixels(bool alphatex) +{ + TArray Pixels(Width * Height, true); + memset(Pixels.Data(), 0, Width * Height); + return Pixels; +} + + +//=========================================================================== +// +// FImageSource::CopyPixels +// +// this is the generic case that can handle +// any properly implemented texture for software rendering. +// Its drawback is that it is limited to the base palette which is +// why all classes that handle different palettes should subclass this +// method +// +//=========================================================================== + +int FImageSource::CopyPixels(FBitmap *bmp) +{ + PalEntry *palette = screen->GetPalette(); + for(int i=1;i<256;i++) palette[i].a = 255; // set proper alpha values + auto ppix = Get8BitPixels(false); // should use composition cache + bmp->CopyPixelData(0, 0, ppix.Data(), Width, Height, Height, 1, 0, palette, nullptr); + for(int i=1;i<256;i++) palette[i].a = 0; + return 0; +} + +int FImageSource::CopyTranslatedPixels(FBitmap *bmp, PalEntry *remap) +{ + auto ppix = Get8BitPixels(false); // should use composition cache + bmp->CopyPixelData(0, 0, ppix.Data(), Width, Height, Height, 1, 0, remap, nullptr); + return 0; +} + diff --git a/src/textures/image.h b/src/textures/image.h new file mode 100644 index 000000000..e868c557d --- /dev/null +++ b/src/textures/image.h @@ -0,0 +1,97 @@ +#pragma once + +#include +#include "tarray.h" +#include "textures/bitmap.h" + + +// This represents a naked image. It has no high level logic attached to it. +// All it can do is provide raw image data to its users. +class FImageSource +{ + friend class FBrightmapImage; +protected: + int SourceLump; + int Width = 0, Height = 0; + int LeftOffset = 0, TopOffset = 0; // Offsets stored in the image. + bool bUseGamePalette = false; // true if this is an image without its own color set. + + // internal builder functions for true color textures. + +public: + + bool bMasked = true; // Image (might) have holes (Assume true unless proven otherwise!) + int8_t bTranslucent = -1; // Image has pixels with a non-0/1 value. (-1 means the user needs to do a real check) + + // Returns the whole texture, paletted and true color versions respectively. + virtual TArray Get8BitPixels(bool alphatex); + virtual int CopyPixels(FBitmap *bmp); + int CopyTranslatedPixels(FBitmap *bmp, PalEntry *remap); + + // Conversion option + enum EType + { + normal = 0, + luminance = 1, + noremap0 = 2 + }; + + FImageSource(int sourcelump = -1) : SourceLump(sourcelump) {} + virtual ~FImageSource() {} + + // Creates an image from the given lump. + static FImageSource *CreateImageSource(int lumpnum); + + int GetWidth() const + { + return Width; + } + + int GetHeight() const + { + return Height; + } + + std::pair GetSize() const + { + return std::make_pair(Width, Height); + } + + std::pair GetOffsets() const + { + return std::make_pair(LeftOffset, TopOffset); + } + + int LumpNum() const + { + return SourceLump; + } + + bool UseGamePalette() const + { + return bUseGamePalette; + } + + bool UseBasePalette() = delete; +}; + +//========================================================================== +// +// a TGA texture +// +//========================================================================== + +class FImageTexture : public FWorldTexture +{ + FImageSource *mImage; +public: + FImageTexture (FImageSource *image); + virtual TArray Get8BitPixels(bool alphatex); + + bool UseBasePalette() override; + +protected: + int CopyPixels(FBitmap *bmp) override; + +}; + diff --git a/src/textures/imagetexture.cpp b/src/textures/imagetexture.cpp new file mode 100644 index 000000000..00326664c --- /dev/null +++ b/src/textures/imagetexture.cpp @@ -0,0 +1,97 @@ +/* +** imagetexture.cpp +** Texture class based on FImageSource +** +**--------------------------------------------------------------------------- +** Copyright 2018 Christoph Oelckers +** All rights reserved. +** +** Redistribution and use in source and binary forms, with or without +** modification, are permitted provided that the following conditions +** are met: +** +** 1. Redistributions of source code must retain the above copyright +** notice, this list of conditions and the following disclaimer. +** 2. Redistributions in binary form must reproduce the above copyright +** notice, this list of conditions and the following disclaimer in the +** documentation and/or other materials provided with the distribution. +** 3. The name of the author may not be used to endorse or promote products +** derived from this software without specific prior written permission. +** +** THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR +** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. +** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, +** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT +** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF +** THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +**--------------------------------------------------------------------------- +** +** +*/ + +#include "doomtype.h" +#include "files.h" +#include "w_wad.h" +#include "templates.h" +#include "bitmap.h" +#include "v_video.h" +#include "image.h" + + +//========================================================================== +// +// +// +//========================================================================== + +FImageTexture::FImageTexture(FImageSource *img) +: FWorldTexture(nullptr, img->LumpNum()) +{ + mImage = img; + Wads.GetLumpName (Name, img->LumpNum()); + Width = img->GetWidth(); + Height = img->GetHeight(); + + auto offsets = img->GetOffsets(); + _LeftOffset[1] = _LeftOffset[0] = offsets.first;; + _TopOffset[1] = _TopOffset[0] = offsets.second; + + bMasked = img->bMasked; + bTranslucent = img->bTranslucent; +} + +//=========================================================================== +// +// +// +//=========================================================================== + +int FImageTexture::CopyPixels(FBitmap *bmp) +{ + return mImage->CopyPixels(bmp); +} + +//=========================================================================== +// +// +// +//=========================================================================== + +TArray FImageTexture::Get8BitPixels(bool alpha) +{ + return mImage->Get8BitPixels(alpha); +} + +//=========================================================================== +// +// +//=========================================================================== + +bool FImageTexture::UseBasePalette() +{ + return mImage->UseGamePalette(); +} diff --git a/src/textures/texture.cpp b/src/textures/texture.cpp index 06e73a6d8..c8969d978 100644 --- a/src/textures/texture.cpp +++ b/src/textures/texture.cpp @@ -240,11 +240,6 @@ FTexture::~FTexture () // //========================================================================== -FTextureFormat FTexture::GetFormat() -{ - return TEX_Pal; -} - void FTexture::SetFrontSkyLayer () { bNoRemap0 = true; diff --git a/src/textures/textures.h b/src/textures/textures.h index 3541f432f..46ef35601 100644 --- a/src/textures/textures.h +++ b/src/textures/textures.h @@ -396,9 +396,6 @@ protected: virtual bool UseBasePalette(); virtual FTexture *GetRedirect(); - // Returns the native pixel format for this image - virtual FTextureFormat GetFormat(); - void SetSpeed(float fac) { shaderspeed = fac; } int GetWidth () { return Width; }