diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 7b15df3f4..140282f07 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1041,6 +1041,7 @@ set (PCH_SOURCES common/textures/skyboxtexture.cpp common/textures/animtexture.cpp common/textures/v_collection.cpp + common/textures/animlib.cpp common/textures/formats/automaptexture.cpp common/textures/formats/brightmaptexture.cpp common/textures/formats/buildtexture.cpp @@ -1059,6 +1060,7 @@ set (PCH_SOURCES common/textures/formats/shadertexture.cpp common/textures/formats/tgatexture.cpp common/textures/formats/stbtexture.cpp + common/textures/formats/anmtexture.cpp common/textures/hires/hqresize.cpp common/models/models_md3.cpp common/models/models_md2.cpp diff --git a/src/common/textures/animlib.cpp b/src/common/textures/animlib.cpp new file mode 100644 index 000000000..d9cdccc7d --- /dev/null +++ b/src/common/textures/animlib.cpp @@ -0,0 +1,287 @@ +//------------------------------------------------------------------------- +/* +Copyright (C) 1996, 2003 - 3D Realms Entertainment + +This file is part of Duke Nukem 3D version 1.5 - Atomic Edition + +Duke Nukem 3D 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 the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +Original Source: 1996 - Todd Replogle +Prepared for public release: 03/21/2003 - Charlie Wiederhold, 3D Realms +Modifications for JonoF's port by Jonathon Fowler (jf@jonof.id.au) +*/ +//------------------------------------------------------------------------- + +#include "animlib.h" +#include "m_swap.h" +#include "m_alloc.h" + +//**************************************************************************** +// +// LOCALS +// +//**************************************************************************** + +//**************************************************************************** +// +// findpage () +// - return the large page number a given frame resides in +// +//**************************************************************************** + +static inline uint16_t findpage(anim_t *anim, uint16_t framenumber) +{ + // curlpnum is initialized to 0xffff, obviously + size_t i = anim->curlpnum & ~0xffff; + size_t const nLps = anim->lpheader->nLps; + bool j = true; + + if (framenumber < anim->currentframe) + i = 0, j = false; + + // this scans the last used page and higher first and then scans the + // previously accessed pages afterwards if it doesn't find anything + do + { + for (; i < nLps; ++i) + { + lp_descriptor & lp = anim->LpArray[i]; + if (lp.baseRecord <= framenumber && framenumber < lp.baseRecord + lp.nRecords) + return (uint16_t)i; + } + + if (j && i == nLps) + { + // handle out of order pages... I don't think any Duke .ANM files + // have them, but they're part of the file spec + i = 0, j = false; + continue; + } + + break; + } + while (1); + + return (uint16_t)i; +} + + +//**************************************************************************** +// +// loadpage () +// - seek out and set pointers to the large page specified +// +//**************************************************************************** + +static inline void loadpage(anim_t *anim, uint16_t pagenumber, uint16_t **pagepointer) +{ + if (anim->curlpnum == pagenumber) + return; + + anim->curlpnum = pagenumber; + anim->curlp = &anim->LpArray[pagenumber]; + *pagepointer = (uint16_t *)(anim->buffer + 0xb00 + (pagenumber*IMAGEBUFFERSIZE) + + sizeof(lp_descriptor) + sizeof(uint16_t)); +} + + +//**************************************************************************** +// +// decodeframe () +// - I found this less obfuscated version of the .ANM "decompressor", +// (c) 1998 "Jari Komppa aka Sol/Trauma". This code is public domain +// but has been mostly rewritten by me. +// +// - As a side note, it looks like this format came about in 1989 and +// never went anywhere after that, and appears to have been the format +// used by Electronic Arts' DeluxePaint Animation, which never made it +// past version 1.0. +// +//**************************************************************************** + +static void decodeframe(uint8_t * srcP, uint8_t * dstP) +{ + do + { + { + /* short op */ + uint8_t count = *srcP++; + + if (!count) /* short RLE */ + { + uint8_t color = *(srcP+1); + count = *(uint8_t *)srcP; + srcP += sizeof(int16_t); + memset(dstP, color, count); + dstP += count; + continue; + } + else if ((count & 0x80) == 0) /* short copy */ + { + memcpy(dstP, srcP, count); + dstP += count; + srcP += count; + continue; + } + else if ((count &= ~0x80) > 0) /* short skip */ + { + dstP += count; + continue; + } + } + + { + /* long op */ + uint16_t count = LittleShort((uint16_t)GetShort(srcP)); + srcP += sizeof(int16_t); + + if (!count) /* stop sign */ + return; + else if ((count & 0x8000) == 0) /* long skip */ + { + dstP += count; + continue; + } + else if ((count &= ~0x8000) & 0x4000) /* long RLE */ + { + uint8_t color = *srcP++; + count &= ~0x4000; + memset(dstP, color, count); + dstP += count; + continue; + } + + /* long copy */ + memcpy(dstP, srcP, count); + dstP += count; + srcP += count; + } + } + while (1); +} + + +//**************************************************************************** +// +// renderframe () +// - draw the frame sepcified from the large page in the buffer pointed to +// +//**************************************************************************** + +static void renderframe(anim_t *anim, uint16_t framenumber, uint16_t *pagepointer) +{ + uint16_t offset = 0; + uint16_t frame = framenumber - anim->curlp->baseRecord; + + while (frame--) offset += LittleShort(pagepointer[frame]); + + uint8_t *ppointer = (uint8_t *)(pagepointer) + anim->curlp->nRecords*2 + offset + 4; + + if ((ppointer-4)[1]) + { + uint16_t const temp = LittleShort(((uint16_t *)(ppointer-4))[1]); + ppointer += temp + (temp & 1); + } + + decodeframe((uint8_t *)ppointer, (uint8_t *)anim->imagebuffer); +} + + +//**************************************************************************** +// +// drawframe () +// - high level frame draw routine +// +//**************************************************************************** + +static inline void drawframe(anim_t *anim, uint16_t framenumber) +{ + loadpage(anim, findpage(anim, framenumber), &anim->thepage); + renderframe(anim, framenumber, anim->thepage); +} + +// is the file size, for consistency checking. +int32_t ANIM_LoadAnim(anim_t *anim, uint8_t *buffer, int32_t length) +{ + if (memcmp(buffer, "LPF ", 4)) return -1; + + length -= sizeof(lpfileheader)+128+768; + if (length < 0) + return -1; + + anim->curlpnum = 0xffff; + anim->currentframe = -1; + + // this just modifies the data in-place instead of copying it elsewhere now + lpfileheader & lpheader = *(anim->lpheader = (lpfileheader *)(anim->buffer = buffer)); + + lpheader.id = LittleLong(lpheader.id); + lpheader.maxLps = LittleShort(lpheader.maxLps); + lpheader.nLps = LittleShort(lpheader.nLps); + lpheader.nRecords = LittleLong(lpheader.nRecords); + lpheader.maxRecsPerLp = LittleShort(lpheader.maxRecsPerLp); + lpheader.lpfTableOffset = LittleShort(lpheader.lpfTableOffset); + lpheader.contentType = LittleLong(lpheader.contentType); + lpheader.width = LittleShort(lpheader.width); + lpheader.height = LittleShort(lpheader.height); + lpheader.nFrames = LittleLong(lpheader.nFrames); + lpheader.framesPerSecond = LittleShort(lpheader.framesPerSecond); + + length -= lpheader.nLps * sizeof(lp_descriptor); + if (length < 0) + return -2; + + buffer += sizeof(lpfileheader)+128; + + // load the color palette + for (uint8_t * pal = anim->pal, * pal_end = pal+768; pal < pal_end; pal += 3, buffer += 4) + { + pal[2] = buffer[0]; + pal[1] = buffer[1]; + pal[0] = buffer[2]; + } + + // set up large page descriptors + anim->LpArray = (lp_descriptor *)buffer; + + // theoretically we should be able to play files with more than 256 frames now + // assuming the utilities to create them can make them that way + for (lp_descriptor * lp = anim->LpArray, * lp_end = lp+lpheader.nLps; lp < lp_end; ++lp) + { + lp->baseRecord = LittleShort(lp->baseRecord); + lp->nRecords = LittleShort(lp->nRecords); + lp->nBytes = LittleShort(lp->nBytes); + } + return ANIM_NumFrames(anim) <= 0 ? -1 : 0; +} + + +uint8_t * ANIM_DrawFrame(anim_t *anim, int32_t framenumber) +{ + uint32_t cnt = anim->currentframe; + + // handle first play and looping or rewinding + if (cnt > (uint32_t)framenumber) + cnt = 0; + + do drawframe(anim, cnt++); + while (cnt < (uint32_t)framenumber); + + anim->currentframe = framenumber; + return anim->imagebuffer; +} + + diff --git a/src/common/textures/animlib.h b/src/common/textures/animlib.h new file mode 100644 index 000000000..dd9415aa4 --- /dev/null +++ b/src/common/textures/animlib.h @@ -0,0 +1,144 @@ +//------------------------------------------------------------------------- +/* +Copyright (C) 1996, 2003 - 3D Realms Entertainment + +This file is part of Duke Nukem 3D version 1.5 - Atomic Edition + +Duke Nukem 3D 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 the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + +Original Source: 1996 - Todd Replogle +Prepared for public release: 03/21/2003 - Charlie Wiederhold, 3D Realms +Modifications for JonoF's port by Jonathon Fowler (jf@jonof.id.au) +*/ +//------------------------------------------------------------------------- + +#include +///////////////////////////////////////////////////////////////////////////// +// +// ANIMLIB.H +// +///////////////////////////////////////////////////////////////////////////// + +#pragma once + +#ifndef animlib_public_h_ +#define animlib_public_h_ + +# pragma pack(push,1) + +/* structure declarations for deluxe animate large page files, doesn't + need to be in the header because there are no exposed functions + that use any of this directly */ + +struct lpfileheader +{ + uint32_t id; /* 4 uint8_tacter ID == "LPF " */ + uint16_t maxLps; /* max # largePages allowed. 256 FOR NOW. */ + uint16_t nLps; /* # largePages in this file. */ + uint32_t nRecords; /* # records in this file. 65534 is current limit + ring */ + uint16_t maxRecsPerLp; /* # records permitted in an lp. 256 FOR NOW. */ + uint16_t lpfTableOffset; /* Absolute Seek position of lpfTable. 1280 FOR NOW. */ + uint32_t contentType; /* 4 character ID == "ANIM" */ + uint16_t width; /* Width of screen in pixels. */ + uint16_t height; /* Height of screen in pixels. */ + uint8_t variant; /* 0==ANIM. */ + uint8_t version; /* 0==frame rate in 18/sec, 1= 70/sec */ + uint8_t hasLastDelta; /* 1==Last record is a delta from last-to-first frame. */ + uint8_t lastDeltaValid; /* 0==Ignore ring frame. */ + uint8_t pixelType; /* 0==256 color. */ + uint8_t CompressionType; /* 1==(RunSkipDump) Only one used FOR NOW. */ + uint8_t otherRecsPerFrm; /* 0 FOR NOW. */ + uint8_t bitmaptype; /* 1==320x200, 256-color. Only one implemented so far. */ + uint8_t recordTypes[32]; /* Not yet implemented. */ + uint32_t nFrames; /* Number of actual frames in the file, includes ring frame. */ + uint16_t framesPerSecond; /* Number of frames to play per second. */ + uint16_t pad2[29]; /* 58 bytes of filler to round up to 128 bytes total. */ +}; + +// this is the format of a large page structure +struct lp_descriptor +{ + uint16_t baseRecord; // Number of first record in this large page. + uint16_t nRecords; // Number of records in lp. + // bit 15 of "nRecords" == "has continuation from previous lp". + // bit 14 of "nRecords" == "final record continues on next lp". + uint16_t nBytes; // Total number of bytes of contents, excluding header. +}; + +#pragma pack(pop) + +#define IMAGEBUFFERSIZE 0x10000 + +struct anim_t +{ + uint16_t framecount; // current frame of anim + lpfileheader * lpheader; // file header will be loaded into this structure + lp_descriptor * LpArray; // arrays of large page structs used to find frames + uint16_t curlpnum; // initialize to an invalid Large page number + lp_descriptor * curlp; // header of large page currently in memory + uint16_t * thepage; // buffer where current large page is loaded + uint8_t imagebuffer[IMAGEBUFFERSIZE]; // buffer where anim frame is decoded + uint8_t * buffer; + uint8_t pal[768]; + int32_t currentframe; +}; + +//**************************************************************************** +// +// ANIM_LoadAnim () +// +// Setup internal anim data structure +// +//**************************************************************************** + +int32_t ANIM_LoadAnim(anim_t *anim, uint8_t *buffer, int32_t length); + +//**************************************************************************** +// +// ANIM_NumFrames () +// +// returns the number of frames in the current anim +// +//**************************************************************************** + +inline int32_t ANIM_NumFrames(anim_t* anim) +{ + return anim->lpheader->nRecords; +} + +//**************************************************************************** +// +// ANIM_DrawFrame () +// +// Draw the frame to a returned buffer +// +//**************************************************************************** + +uint8_t * ANIM_DrawFrame(anim_t* anim, int32_t framenumber); + +//**************************************************************************** +// +// ANIM_GetPalette () +// +// return the palette of the anim +//**************************************************************************** + +inline uint8_t* ANIM_GetPalette(anim_t* anim) +{ + return anim->pal; +} + +#endif diff --git a/src/common/textures/animtexture.cpp b/src/common/textures/animtexture.cpp index b19efa167..6a96365a8 100644 --- a/src/common/textures/animtexture.cpp +++ b/src/common/textures/animtexture.cpp @@ -44,17 +44,44 @@ void AnimTexture::SetFrameSize(int format, int width, int height) { + pixelformat = format; FTexture::SetSize(width, height); Image.Resize(width * height * (format == Paletted ? 1 : 3)); - pixelformat = format; + memset(Image.Data(), 0, Image.Size()); + CleanHardwareTextures(); } void AnimTexture::SetFrame(const uint8_t* palette, const void* data_) { - memcpy(Palette, palette, 768); - memcpy(Image.Data(), data_, Width * Height * (pixelformat == Paletted ? 1 : 3)); + if (palette) memcpy(Palette, palette, 768); + if (data_) + { + if (pixelformat == YUV) + { + auto spix = (const uint8_t*)data_; + auto dpix = Image.Data(); + for (int i = 0; i < Width * Height; i++) + { + int p = i * 4; + int q = i * 3; + float y = spix[p] * (1 / 255.f); + float u = spix[p + 1] * (1 / 255.f) - 0.5f; + float v = spix[p + 2] * (1 / 255.f) - 0.5f; + + y = 1.1643f * (y - 0.0625f); + + float r = y + 1.5958f * v; + float g = y - 0.39173f * u - 0.81290f * v; + float b = y + 2.017f * u; + + dpix[q + 0] = (uint8_t)(clamp(r, 0.f, 1.f) * 255); + dpix[q + 1] = (uint8_t)(clamp(g, 0.f, 1.f) * 255); + dpix[q + 2] = (uint8_t)(clamp(b, 0.f, 1.f) * 255); + } + } + else memcpy(Image.Data(), data_, Width * Height * (pixelformat == Paletted ? 1 : 3)); + } CleanHardwareTextures(); - pixelformat = Paletted; } //=========================================================================== @@ -83,40 +110,18 @@ FBitmap AnimTexture::GetBgraBitmap(const PalEntry* remap, int* trans) dpix[p + 3] = 255; } } - else if (pixelformat == RGB) + else if (pixelformat == RGB || pixelformat == YUV) { for (int i = 0; i < Width * Height; i++) { int p = i * 4; - dpix[p + 0] = spix[p + 2]; - dpix[p + 1] = spix[p + 1]; - dpix[p + 2] = spix[p]; + int q = i * 3; + dpix[p + 0] = spix[q + 2]; + dpix[p + 1] = spix[q + 1]; + dpix[p + 2] = spix[q]; dpix[p + 3] = 255; } } - else if (pixelformat == YUV) - { - for (int i = 0; i < Width * Height; i++) - { - int p = i * 4; - float y = spix[p] * (1 / 255.f); - float u = spix[p + 1] * (1 / 255.f) - 0.5f; - float v = spix[p + 2] * (1 / 255.f) - 0.5f; - - y = 1.1643f * (y - 0.0625f); - - float r = y + 1.5958f * v; - float g = y - 0.39173f * u - 0.81290f * v; - float b = y + 2.017f * u; - - dpix[p + 0] = (uint8_t)(clamp(b, 0.f, 1.f) * 255); - dpix[p + 1] = (uint8_t)(clamp(g, 0.f, 1.f) * 255); - dpix[p + 2] = (uint8_t)(clamp(r, 0.f, 1.f) * 255); - dpix[p + 3] = 255; - } - return bmp; - - } return bmp; } @@ -135,8 +140,14 @@ AnimTextures::AnimTextures() AnimTextures::~AnimTextures() { - tex[0]->CleanHardwareData(true); - tex[1]->CleanHardwareData(true); + Clean(); +} + +void AnimTextures::Clean() +{ + if (tex[0]) tex[0]->CleanHardwareData(true); + if (tex[1]) tex[1]->CleanHardwareData(true); + tex[0] = tex[1] = nullptr; } void AnimTextures::SetSize(int format, int width, int height) diff --git a/src/common/textures/animtexture.h b/src/common/textures/animtexture.h index 5710d44b3..1f79d0af1 100644 --- a/src/common/textures/animtexture.h +++ b/src/common/textures/animtexture.h @@ -29,6 +29,7 @@ class AnimTextures public: AnimTextures(); ~AnimTextures(); + void Clean(); void SetSize(int format, int width, int height); void SetFrame(const uint8_t* palette, const void* data); FGameTexture* GetFrame() diff --git a/src/common/textures/formats/anmtexture.cpp b/src/common/textures/formats/anmtexture.cpp new file mode 100644 index 000000000..aa65dc693 --- /dev/null +++ b/src/common/textures/formats/anmtexture.cpp @@ -0,0 +1,171 @@ +/* +** anmtexture.cpp +** Texture class for reading the first frame of Build ANM files +** +**--------------------------------------------------------------------------- +** Copyright 2020 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 "files.h" +#include "filesystem.h" +#include "bitmap.h" +#include "imagehelpers.h" +#include "image.h" +#include "animlib.h" + +//========================================================================== +// +// +//========================================================================== + +class FAnmTexture : public FImageSource +{ + +public: + FAnmTexture (int lumpnum, int w, int h); + void ReadFrame(uint8_t *buffer, uint8_t *palette); + TArray CreatePalettedPixels(int conversion) override; + int CopyPixels(FBitmap *bmp, int conversion) override; +}; + + +//========================================================================== +// +// +// +//========================================================================== + +FImageSource *AnmImage_TryCreate(FileReader & file, int lumpnum) +{ + file.Seek(0, FileReader::SeekSet); + char check[4]; + file.Read(check, 4); + if (memcmp(check, "LPF ", 4)) return nullptr; + file.Seek(0, FileReader::SeekSet); + auto buffer = file.ReadPadded(1); + + anim_t anim; + if (ANIM_LoadAnim(&anim, buffer.Data(), buffer.Size() - 1) < 0) + { + return nullptr; + } + int numframes = ANIM_NumFrames(&anim); + if (numframes >= 1) + { + return new FAnmTexture(lumpnum, 320, 200); + } + + return nullptr; +} + +//========================================================================== +// +// +// +//========================================================================== + +FAnmTexture::FAnmTexture (int lumpnum, int w, int h) + : FImageSource(lumpnum) +{ + Width = w; + Height = h; + LeftOffset = 0; + TopOffset = 0; +} + +void FAnmTexture::ReadFrame(uint8_t *pixels, uint8_t *palette) +{ + FileData lump = fileSystem.ReadFile (SourceLump); + uint8_t *source = (uint8_t *)lump.GetMem(); + + anim_t anim; + if (ANIM_LoadAnim(&anim, source, (int)lump.GetSize()) >= 0) + { + int numframes = ANIM_NumFrames(&anim); + if (numframes >= 1) + { + memcpy(palette, ANIM_GetPalette(&anim), 768); + memcpy(pixels, ANIM_DrawFrame(&anim, 1), Width*Height); + return; + } + } + memset(pixels, 0, Width*Height); + memset(palette, 0, 768); +} + +//========================================================================== +// +// +// +//========================================================================== + +TArray FAnmTexture::CreatePalettedPixels(int conversion) +{ + TArray pixels(Width*Height, true); + uint8_t buffer[64000]; + uint8_t palette[768]; + uint8_t remap[256]; + + ReadFrame(buffer, palette); + for(int i=0;i<256;i++) + { + remap[i] = ColorMatcher.Pick(palette[i*3], palette[i*3+1], palette[i*3+2]); + } + ImageHelpers::FlipNonSquareBlockRemap (pixels.Data(), buffer, Width, Height, Width, remap); + return pixels; +} + +//========================================================================== +// +// +// +//========================================================================== + +int FAnmTexture::CopyPixels(FBitmap *bmp, int conversion) +{ + uint8_t buffer[64000]; + uint8_t palette[768]; + ReadFrame(buffer, palette); + + auto dpix = bmp->GetPixels(); + for (int i = 0; i < Width * Height; i++) + { + int p = i * 4; + int index = buffer[i]; + dpix[p + 0] = palette[index * 3 + 2]; + dpix[p + 1] = palette[index * 3 + 1]; + dpix[p + 2] = palette[index * 3]; + dpix[p + 3] = 255; + } + + return -1; +} + + \ No newline at end of file diff --git a/src/common/textures/formats/multipatchtexture.h b/src/common/textures/formats/multipatchtexture.h index d8f271333..48223ec5e 100644 --- a/src/common/textures/formats/multipatchtexture.h +++ b/src/common/textures/formats/multipatchtexture.h @@ -77,7 +77,7 @@ protected: int CopyPixels(FBitmap *bmp, int conversion) override; TArray CreatePalettedPixels(int conversion) override; void CopyToBlock(uint8_t *dest, int dwidth, int dheight, FImageSource *source, int xpos, int ypos, int rotate, const uint8_t *translation, int style); - void CollectForPrecache(PrecacheInfo &info, bool requiretruecolor); + void CollectForPrecache(PrecacheInfo &info, bool requiretruecolor) override; }; diff --git a/src/common/textures/formats/patchtexture.cpp b/src/common/textures/formats/patchtexture.cpp index 0a93adf50..f1f65291d 100644 --- a/src/common/textures/formats/patchtexture.cpp +++ b/src/common/textures/formats/patchtexture.cpp @@ -127,13 +127,19 @@ FImageSource *PatchImage_TryCreate(FileReader & file, int lumpnum) { bool isalpha; - if (!CheckIfPatch(file, isalpha)) return NULL; file.Seek(0, FileReader::SeekSet); int width = file.ReadUInt16(); int height = file.ReadUInt16(); int leftoffset = file.ReadInt16(); int topoffset = file.ReadInt16(); - return new FPatchTexture(lumpnum, width, height, leftoffset, topoffset, isalpha); + // quickly reject any lump which cannot be a texture without reading in all the data. + if (height > 0 && height <= 2048 && width > 0 && width <= 2048 && width < file.GetLength() / 4 && abs(leftoffset) < 4096 && abs(topoffset) < 4096) + { + if (!CheckIfPatch(file, isalpha)) return NULL; + file.Seek(8, FileReader::SeekSet); + return new FPatchTexture(lumpnum, width, height, leftoffset, topoffset, isalpha); + } + return nullptr; } //========================================================================== diff --git a/src/common/textures/gametexture.h b/src/common/textures/gametexture.h index 5f32e1fdb..ec013b3d5 100644 --- a/src/common/textures/gametexture.h +++ b/src/common/textures/gametexture.h @@ -58,6 +58,7 @@ enum EGameTexFlags GTexf_DisableFullbrightSprites = 64, // This texture will not be displayed as fullbright sprite GTexf_BrightmapChecked = 128, // Check for a colormap-based brightmap was already done. GTexf_AutoMaterialsAdded = 256, // AddAutoMaterials has been called on this texture. + GTexf_OffsetsNotForFont = 512, // The offsets must be ignored when using this texture in a font. }; // Refactoring helper to allow piece by piece adjustment of the API @@ -91,7 +92,7 @@ class FGameTexture SpritePositioningInfo* spi = nullptr; ISoftwareTexture* SoftwareTexture = nullptr; - FMaterial* Material[4] = { }; + FMaterial* Material[5] = { }; // Material properties FVector2 detailScale = { 1.f, 1.f }; @@ -156,6 +157,7 @@ public: void SetWorldPanning(bool on) { if (on) flags |= GTexf_WorldPanning; else flags &= ~GTexf_WorldPanning; } bool allowNoDecals() const { return !!(flags & GTexf_NoDecals); } void SetNoDecals(bool on) { if (on) flags |= GTexf_NoDecals; else flags &= ~GTexf_NoDecals; } + void SetOffsetsNotForFont() { flags |= GTexf_OffsetsNotForFont; } bool isValid() const { return UseType != ETextureType::Null; } int isWarped() { return warped; } @@ -208,12 +210,15 @@ public: float GetGlossiness() const { return Glossiness; } float GetSpecularLevel() const { return SpecularLevel; } - void CopySize(FGameTexture* BaseTexture) + void CopySize(FGameTexture* BaseTexture, bool forfont = false) { Base->CopySize(BaseTexture->Base.get()); SetDisplaySize(BaseTexture->GetDisplayWidth(), BaseTexture->GetDisplayHeight()); - SetOffsets(0, BaseTexture->GetTexelLeftOffset(0), BaseTexture->GetTexelTopOffset(0)); - SetOffsets(1, BaseTexture->GetTexelLeftOffset(1), BaseTexture->GetTexelTopOffset(1)); + if (!forfont || !(BaseTexture->flags & GTexf_OffsetsNotForFont)) + { + SetOffsets(0, BaseTexture->GetTexelLeftOffset(0), BaseTexture->GetTexelTopOffset(0)); + SetOffsets(1, BaseTexture->GetTexelLeftOffset(1), BaseTexture->GetTexelTopOffset(1)); + } } // Glowing is a pure material property that should not filter down to the actual texture objects. @@ -234,6 +239,7 @@ public: TexelWidth = x; TexelHeight = y; SetDisplaySize(float(x), float(y)); + GetTexture()->SetSize(x, y); } void SetDisplaySize(float w, float h) { diff --git a/src/common/textures/hw_material.cpp b/src/common/textures/hw_material.cpp index c4a1ba9d3..dabe738da 100644 --- a/src/common/textures/hw_material.cpp +++ b/src/common/textures/hw_material.cpp @@ -29,6 +29,13 @@ #include "c_cvars.h" #include "v_video.h" +static IHardwareTexture* (*layercallback)(int layer, int translation); + +void FMaterial::SetLayerCallback(IHardwareTexture* (*cb)(int layer, int translation)) +{ + layercallback = cb; +} + //=========================================================================== // // Constructor @@ -42,8 +49,9 @@ FMaterial::FMaterial(FGameTexture * tx, int scaleflags) auto imgtex = tx->GetTexture(); mTextureLayers.Push({ imgtex, scaleflags, -1 }); - if (tx->GetUseType() == ETextureType::SWCanvas && static_cast(imgtex)->GetColorFormat() == 0) + if ((tx->GetUseType() == ETextureType::SWCanvas && static_cast(imgtex)->GetColorFormat() == 0) || (scaleflags & CTF_Indexed)) { + mTextureLayers[0].scaleFlags |= CTF_Indexed; mShaderIndex = SHADER_Paletted; } else if (tx->isHardwareCanvas()) @@ -149,12 +157,25 @@ FMaterial::~FMaterial() // //=========================================================================== -IHardwareTexture *FMaterial::GetLayer(int i, int translation, MaterialLayerInfo **pLayer) const +IHardwareTexture* FMaterial::GetLayer(int i, int translation, MaterialLayerInfo** pLayer) const { - auto &layer = mTextureLayers[i]; - if (pLayer) *pLayer = &layer; - - if (layer.layerTexture) return layer.layerTexture->GetHardwareTexture(translation, layer.scaleFlags); + if (mShaderIndex == SHADER_Paletted && i > 0 && layercallback) + { + static MaterialLayerInfo deflayer = { nullptr, 0, CLAMP_XY }; + if (i == 1 || i == 2) + { + if (pLayer) *pLayer = &deflayer; + //This must be done with a user supplied callback because we cannot set up the rules for palette data selection here + return layercallback(i, translation); + } + } + else + { + auto& layer = mTextureLayers[i]; + if (pLayer) *pLayer = &layer; + if (mShaderIndex == SHADER_Paletted) translation = -1; + if (layer.layerTexture) return layer.layerTexture->GetHardwareTexture(translation, layer.scaleFlags); + } return nullptr; } @@ -169,6 +190,7 @@ FMaterial * FMaterial::ValidateTexture(FGameTexture * gtex, int scaleflags, bool { if (gtex && gtex->isValid()) { + if (scaleflags & CTF_Indexed) scaleflags = CTF_Indexed; if (!gtex->expandSprites()) scaleflags &= ~CTF_Expand; FMaterial *hwtex = gtex->Material[scaleflags]; diff --git a/src/common/textures/hw_material.h b/src/common/textures/hw_material.h index 75a77e35a..20c38b332 100644 --- a/src/common/textures/hw_material.h +++ b/src/common/textures/hw_material.h @@ -30,6 +30,8 @@ class FMaterial int mScaleFlags; public: + static void SetLayerCallback(IHardwareTexture* (*layercallback)(int layer, int translation)); + FGameTexture *sourcetex; // the owning texture. FMaterial(FGameTexture *tex, int scaleflags); diff --git a/src/common/textures/image.cpp b/src/common/textures/image.cpp index e70c76e39..73d6f5227 100644 --- a/src/common/textures/image.cpp +++ b/src/common/textures/image.cpp @@ -331,6 +331,7 @@ FImageSource *DDSImage_TryCreate(FileReader &, int lumpnum); FImageSource *PCXImage_TryCreate(FileReader &, int lumpnum); FImageSource *TGAImage_TryCreate(FileReader &, int lumpnum); FImageSource *StbImage_TryCreate(FileReader &, int lumpnum); +FImageSource *AnmImage_TryCreate(FileReader &, int lumpnum); FImageSource *RawPageImage_TryCreate(FileReader &, int lumpnum); FImageSource *FlatImage_TryCreate(FileReader &, int lumpnum); FImageSource *PatchImage_TryCreate(FileReader &, int lumpnum); @@ -350,6 +351,7 @@ FImageSource * FImageSource::GetImage(int lumpnum, bool isflat) { PCXImage_TryCreate, false }, { StbImage_TryCreate, false }, { TGAImage_TryCreate, false }, + { AnmImage_TryCreate, false }, { RawPageImage_TryCreate, false }, { FlatImage_TryCreate, true }, // flat detection is not reliable, so only consider this for real flats. { PatchImage_TryCreate, false }, diff --git a/src/common/textures/texture.cpp b/src/common/textures/texture.cpp index 85b98789b..503657f00 100644 --- a/src/common/textures/texture.cpp +++ b/src/common/textures/texture.cpp @@ -528,17 +528,15 @@ outl: IHardwareTexture* FTexture::GetHardwareTexture(int translation, int scaleflags) { - //if (UseType != ETextureType::Null) + int indexed = scaleflags & CTF_Indexed; + if (indexed) translation = -1; + IHardwareTexture* hwtex = SystemTextures.GetHardwareTexture(translation, scaleflags); + if (hwtex == nullptr) { - IHardwareTexture* hwtex = SystemTextures.GetHardwareTexture(translation, scaleflags); - if (hwtex == nullptr) - { - hwtex = screen->CreateHardwareTexture(4); - SystemTextures.AddHardwareTexture(translation, scaleflags, hwtex); + hwtex = screen->CreateHardwareTexture(indexed? 1 : 4); + SystemTextures.AddHardwareTexture(translation, scaleflags, hwtex); } - return hwtex; - } - return nullptr; + return hwtex; } diff --git a/src/common/textures/texturemanager.cpp b/src/common/textures/texturemanager.cpp index 60ed0c747..c396f3437 100644 --- a/src/common/textures/texturemanager.cpp +++ b/src/common/textures/texturemanager.cpp @@ -240,7 +240,7 @@ FTextureID FTextureManager::CheckForTexture (const char *name, ETextureType uset { // We intentionally only look for textures in subdirectories. // Any graphic being placed in the zip's root directory can not be found by this. - if (strchr(name, '/')) + if (strchr(name, '/') || (flags & TEXMAN_ForceLookup)) { FGameTexture *const NO_TEXTURE = (FGameTexture*)-1; int lump = fileSystem.CheckNumForFullName(name); diff --git a/src/common/textures/texturemanager.h b/src/common/textures/texturemanager.h index d9da9d20d..6001a1c0a 100644 --- a/src/common/textures/texturemanager.h +++ b/src/common/textures/texturemanager.h @@ -47,9 +47,9 @@ public: } // This only gets used in UI code so we do not need PALVERS handling. - FGameTexture* GetGameTextureByName(const char *name, bool animate = false) + FGameTexture* GetGameTextureByName(const char *name, bool animate = false, int flags = 0) { - FTextureID texnum = GetTextureID(name, ETextureType::MiscPatch); + FTextureID texnum = GetTextureID(name, ETextureType::MiscPatch, flags); return InternalGetTexture(texnum.GetIndex(), animate, true); } @@ -88,7 +88,8 @@ public: TEXMAN_AllowSkins = 8, TEXMAN_ShortNameOnly = 16, TEXMAN_DontCreate = 32, - TEXMAN_Localize = 64 + TEXMAN_Localize = 64, + TEXMAN_ForceLookup = 128 }; enum