From 7a5d89c8516ce5bc4e3343a86cefccc9aa613ab1 Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Thu, 9 Nov 2023 23:00:36 +0100 Subject: [PATCH] use FTranslationID in the 2D drawer. --- source/common/2d/v_2ddrawer.cpp | 10 ++++---- source/common/2d/v_2ddrawer.h | 4 ++-- source/common/2d/v_draw.cpp | 4 ++-- source/common/2d/v_draw.h | 2 +- source/common/2d/v_drawtext.cpp | 10 ++++---- source/common/engine/palettecontainer.h | 9 ++++++-- source/common/fonts/font.cpp | 8 +++---- source/common/fonts/hexfont.cpp | 4 ++-- source/common/fonts/singlelumpfont.cpp | 2 +- source/common/fonts/specialfont.cpp | 4 ++-- source/common/fonts/v_font.cpp | 23 +++++++++++-------- source/common/fonts/v_font.h | 7 +++--- .../hwrenderer/data/hw_renderstate.h | 6 +++++ source/common/textures/texture.cpp | 4 ++-- source/core/automap.cpp | 4 ++-- 15 files changed, 58 insertions(+), 43 deletions(-) diff --git a/source/common/2d/v_2ddrawer.cpp b/source/common/2d/v_2ddrawer.cpp index e12c5a61c..717d607c6 100644 --- a/source/common/2d/v_2ddrawer.cpp +++ b/source/common/2d/v_2ddrawer.cpp @@ -443,7 +443,7 @@ void F2DDrawer::AddTexture(FGameTexture* img, DrawParms& parms) if (img->isWarped()) dg.mFlags |= DTF_Wrap; if (parms.indexed) dg.mFlags |= DTF_Indexed; - dg.mTranslationId = 0; + dg.mTranslationId = NO_TRANSLATION; SetStyle(img, parms, vertexcolor, dg); if (parms.indexed) { @@ -451,7 +451,7 @@ void F2DDrawer::AddTexture(FGameTexture* img, DrawParms& parms) vertexcolor = 0xffffffff; } - if (!img->isHardwareCanvas() && parms.TranslationId != -1) + if (!img->isHardwareCanvas() && parms.TranslationId != INVALID_TRANSLATION) { dg.mTranslationId = parms.TranslationId; } @@ -607,7 +607,7 @@ void F2DDrawer::AddShape(FGameTexture* img, DShape2D* shape, DrawParms& parms) dg.mFlags |= DTF_Wrap; dg.mTexture = img; - dg.mTranslationId = 0; + dg.mTranslationId = NO_TRANSLATION; SetStyle(img, parms, vertexcolor, dg); if (shape->lastParms == nullptr) { @@ -624,7 +624,7 @@ void F2DDrawer::AddShape(FGameTexture* img, DShape2D* shape, DrawParms& parms) shape->lastParms = new DrawParms(parms); } - if (!(img != nullptr && img->isHardwareCanvas()) && parms.TranslationId != -1) + if (!(img != nullptr && img->isHardwareCanvas()) && parms.TranslationId != INVALID_TRANSLATION) dg.mTranslationId = parms.TranslationId; auto osave = offset; @@ -796,7 +796,7 @@ void F2DDrawer::AddPoly(FGameTexture *texture, FVector2 *points, int npoints, // //========================================================================== -void F2DDrawer::AddPoly(FGameTexture* img, FVector4* vt, size_t vtcount, const unsigned int* ind, size_t idxcount, int translation, PalEntry color, FRenderStyle style, const IntRect* clip) +void F2DDrawer::AddPoly(FGameTexture* img, FVector4* vt, size_t vtcount, const unsigned int* ind, size_t idxcount, FTranslationID translation, PalEntry color, FRenderStyle style, const IntRect* clip) { RenderCommand dg; diff --git a/source/common/2d/v_2ddrawer.h b/source/common/2d/v_2ddrawer.h index fc2970b15..3567f2bff 100644 --- a/source/common/2d/v_2ddrawer.h +++ b/source/common/2d/v_2ddrawer.h @@ -131,7 +131,7 @@ public: int mIndexCount; FGameTexture *mTexture; - int mTranslationId; + FTranslationID mTranslationId; PalEntry mSpecialColormap[2]; int mScissor[4]; int mDesaturate; @@ -216,7 +216,7 @@ public: void AddPoly(FGameTexture *texture, FVector2 *points, int npoints, double originx, double originy, double scalex, double scaley, DAngle rotation, const FColormap &colormap, PalEntry flatcolor, double lightlevel, uint32_t *indices, size_t indexcount); - void AddPoly(FGameTexture* img, FVector4 *vt, size_t vtcount, const unsigned int *ind, size_t idxcount, int translation, PalEntry color, FRenderStyle style, const IntRect* clip); + void AddPoly(FGameTexture* img, FVector4 *vt, size_t vtcount, const unsigned int *ind, size_t idxcount, FTranslationID translation, PalEntry color, FRenderStyle style, const IntRect* clip); void FillPolygon(int* rx1, int* ry1, int* xb1, int32_t npoints, int pic, int palette, int shade, int props, const FVector2& xtex, const FVector2& ytex, const FVector2& otex, int clipx1, int clipy1, int clipx2, int clipy2); void AddFlatFill(int left, int top, int right, int bottom, FGameTexture *src, int local_origin = false, double flatscale = 1.0, PalEntry color = 0xffffffff, ERenderStyle rs = STYLE_Normal); diff --git a/source/common/2d/v_draw.cpp b/source/common/2d/v_draw.cpp index e7ea8d3ef..247a2916d 100644 --- a/source/common/2d/v_draw.cpp +++ b/source/common/2d/v_draw.cpp @@ -866,7 +866,7 @@ bool ParseDrawTextureTags(F2DDrawer *drawer, FGameTexture *img, double x, double parms->destheight = INT_MAX; parms->Alpha = type == DrawTexture_Fill ? (float)fillalpha : 1.f; parms->fillcolor = type == DrawTexture_Fill ? fill : PalEntry(~0u); - parms->TranslationId = -1; + parms->TranslationId = INVALID_TRANSLATION; parms->colorOverlay = 0; parms->alphaChannel = false; parms->flipX = false; @@ -1090,7 +1090,7 @@ bool ParseDrawTextureTags(F2DDrawer *drawer, FGameTexture *img, double x, double break; case DTA_TranslationIndex: - parms->TranslationId = ListGetInt(tags); + parms->TranslationId = FTranslationID::fromInt(ListGetInt(tags)); break; case DTA_ColorOverlay: diff --git a/source/common/2d/v_draw.h b/source/common/2d/v_draw.h index 97668d97c..e98ca9245 100644 --- a/source/common/2d/v_draw.h +++ b/source/common/2d/v_draw.h @@ -176,7 +176,7 @@ struct DrawParms double left; float Alpha; PalEntry fillcolor; - int TranslationId; + FTranslationID TranslationId; PalEntry colorOverlay; PalEntry color; int alphaChannel; diff --git a/source/common/2d/v_drawtext.cpp b/source/common/2d/v_drawtext.cpp index ea760b4b6..22cfb476e 100644 --- a/source/common/2d/v_drawtext.cpp +++ b/source/common/2d/v_drawtext.cpp @@ -185,7 +185,7 @@ void DrawChar(F2DDrawer *drawer, FFont* font, int normalcolor, double x, double { return; } - bool palettetrans = (normalcolor == CR_NATIVEPAL && parms.TranslationId != 0); + bool palettetrans = (normalcolor == CR_NATIVEPAL && parms.TranslationId != NO_TRANSLATION); PalEntry color = 0xffffffff; if (!palettetrans) parms.TranslationId = font->GetColorTranslation((EColorRange)normalcolor, &color); parms.color = PalEntry((color.a * parms.color.a) / 255, (color.r * parms.color.r) / 255, (color.g * parms.color.g) / 255, (color.b * parms.color.b) / 255); @@ -210,7 +210,7 @@ void DrawChar(F2DDrawer *drawer, FFont *font, int normalcolor, double x, double uint32_t tag = ListGetInt(args); bool res = ParseDrawTextureTags(drawer, pic, x, y, tag, args, &parms, DrawTexture_Normal); if (!res) return; - bool palettetrans = (normalcolor == CR_NATIVEPAL && parms.TranslationId != 0); + bool palettetrans = (normalcolor == CR_NATIVEPAL && parms.TranslationId != NO_TRANSLATION); PalEntry color = 0xffffffff; if (!palettetrans) parms.TranslationId = font->GetColorTranslation((EColorRange)normalcolor, &color); parms.color = PalEntry((color.a * parms.color.a) / 255, (color.r * parms.color.r) / 255, (color.g * parms.color.g) / 255, (color.b * parms.color.b) / 255); @@ -272,7 +272,7 @@ void DrawTextCommon(F2DDrawer *drawer, FFont *font, int normalcolor, double x, d double cx; double cy; int boldcolor; - int trans = -1; + FTranslationID trans = INVALID_TRANSLATION; int kerning; FGameTexture *pic; @@ -282,7 +282,7 @@ void DrawTextCommon(F2DDrawer *drawer, FFont *font, int normalcolor, double x, d if (parms.celly == 0) parms.celly = font->GetHeight() + 1; parms.celly = int (parms.celly * scaley); - bool palettetrans = (normalcolor == CR_NATIVEPAL && parms.TranslationId != 0); + bool palettetrans = (normalcolor == CR_NATIVEPAL && parms.TranslationId != NO_TRANSLATION); if (normalcolor >= NumTextColors) normalcolor = CR_UNTRANSLATED; @@ -290,7 +290,7 @@ void DrawTextCommon(F2DDrawer *drawer, FFont *font, int normalcolor, double x, d PalEntry colorparm = parms.color; PalEntry color = 0xffffffff; - trans = palettetrans? -1 : font->GetColorTranslation((EColorRange)normalcolor, &color); + trans = palettetrans? INVALID_TRANSLATION : font->GetColorTranslation((EColorRange)normalcolor, &color); parms.color = PalEntry(colorparm.a, (color.r * colorparm.r) / 255, (color.g * colorparm.g) / 255, (color.b * colorparm.b) / 255); kerning = font->GetDefaultKerning(); diff --git a/source/common/engine/palettecontainer.h b/source/common/engine/palettecontainer.h index b7869e276..3c6da1a36 100644 --- a/source/common/engine/palettecontainer.h +++ b/source/common/engine/palettecontainer.h @@ -114,10 +114,10 @@ inline constexpr FTranslationID TRANSLATION(uint8_t a, uint32_t b) { return FTranslationID::fromInt((a << TRANSLATION_SHIFT) | b); } -inline constexpr int MakeLuminosityTranslation(int range, uint8_t min, uint8_t max) +inline constexpr FTranslationID MakeLuminosityTranslation(int range, uint8_t min, uint8_t max) { // ensure that the value remains positive. - return ( (1 << 30) | ((range&0x3fff) << 16) | (min << 8) | max ); + return FTranslationID::fromInt( (1 << 30) | ((range&0x3fff) << 16) | (min << 8) | max ); } inline constexpr bool IsLuminosityTranslation(FTranslationID trans) @@ -187,6 +187,11 @@ public: void CopyTranslation(FTranslationID dest, FTranslationID src); FTranslationID StoreTranslation(int slot, FRemapTable* remap); FRemapTable* TranslationToTable(int translation) const; + FRemapTable* TranslationToTable(FTranslationID translation) const + { + return TranslationToTable(translation.index()); + } + void GenerateGlobalBrightmapFromColormap(const uint8_t* cmapdata, int numlevels); void PushIdentityTable(int slot) diff --git a/source/common/fonts/font.cpp b/source/common/fonts/font.cpp index c622eab3b..c0a9758be 100644 --- a/source/common/fonts/font.cpp +++ b/source/common/fonts/font.cpp @@ -711,10 +711,10 @@ int FFont::GetLuminosity (uint32_t *colorsused, TArray &Luminosity, int* // //========================================================================== -int FFont::GetColorTranslation (EColorRange range, PalEntry *color) const +FTranslationID FFont::GetColorTranslation (EColorRange range, PalEntry *color) const { // Single pic fonts do not set up their translation table and must always return 0. - if (Translations.Size() == 0) return 0; + if (Translations.Size() == 0) return NO_TRANSLATION; assert(Translations.Size() == (unsigned)NumTextColors); if (noTranslate) @@ -728,7 +728,7 @@ int FFont::GetColorTranslation (EColorRange range, PalEntry *color) const if (color != nullptr) *color = retcolor; } if (range == CR_UNDEFINED) - return -1; + return INVALID_TRANSLATION; else if (range >= NumTextColors) range = CR_UNTRANSLATED; return Translations[range]; @@ -1059,7 +1059,7 @@ void FFont::LoadTranslations() Translations.Resize(NumTextColors); for (int i = 0; i < NumTextColors; i++) { - if (i == CR_UNTRANSLATED) Translations[i] = 0; + if (i == CR_UNTRANSLATED) Translations[i] = NO_TRANSLATION; else Translations[i] = MakeLuminosityTranslation(i*2 + TranslationType, minlum, maxlum); } } diff --git a/source/common/fonts/hexfont.cpp b/source/common/fonts/hexfont.cpp index d72c50e8a..95b7999bf 100644 --- a/source/common/fonts/hexfont.cpp +++ b/source/common/fonts/hexfont.cpp @@ -323,7 +323,7 @@ public: Translations.Resize(NumTextColors); for (int i = 0; i < NumTextColors; i++) { - if (i == CR_UNTRANSLATED) Translations[i] = 0; + if (i == CR_UNTRANSLATED) Translations[i] = NO_TRANSLATION; else Translations[i] = MakeLuminosityTranslation(i * 2 + 1, minlum, maxlum); } } @@ -387,7 +387,7 @@ public: Translations.Resize(NumTextColors); for (int i = 0; i < NumTextColors; i++) { - if (i == CR_UNTRANSLATED) Translations[i] = 0; + if (i == CR_UNTRANSLATED) Translations[i] = NO_TRANSLATION; else Translations[i] = MakeLuminosityTranslation(i * 2, minlum, maxlum); } } diff --git a/source/common/fonts/singlelumpfont.cpp b/source/common/fonts/singlelumpfont.cpp index ca40519da..0a8a099e4 100644 --- a/source/common/fonts/singlelumpfont.cpp +++ b/source/common/fonts/singlelumpfont.cpp @@ -194,7 +194,7 @@ void FSingleLumpFont::LoadTranslations() Translations.Resize(NumTextColors); for (int i = 0; i < NumTextColors; i++) { - if (i == CR_UNTRANSLATED) Translations[i] = 0; + if (i == CR_UNTRANSLATED) Translations[i] = NO_TRANSLATION; else Translations[i] = MakeLuminosityTranslation(i * 2 + (FontType == FONT1 ? 1 : 0), minlum, maxlum); } } diff --git a/source/common/fonts/specialfont.cpp b/source/common/fonts/specialfont.cpp index 6547778c0..9ab3d4f38 100644 --- a/source/common/fonts/specialfont.cpp +++ b/source/common/fonts/specialfont.cpp @@ -182,7 +182,7 @@ void FSpecialFont::LoadTranslations() workpal[i * 4 + 2] = GPalette.BaseColors[i].r; workpal[i * 4 + 3] = GPalette.BaseColors[i].a; } - V_ApplyLuminosityTranslation(trans, workpal, 256); + V_ApplyLuminosityTranslation(LuminosityTranslationDesc::fromID(trans), workpal, 256); for (int i = 0; i < 256; i++) { if (!notranslate[i]) @@ -196,7 +196,7 @@ void FSpecialFont::LoadTranslations() remap.Remap[i] = i; } } - trans = GPalette.StoreTranslation(TRANSLATION_Internal, &remap).index(); + trans = GPalette.StoreTranslation(TRANSLATION_Internal, &remap); } } diff --git a/source/common/fonts/v_font.cpp b/source/common/fonts/v_font.cpp index f516a2091..d838fb181 100644 --- a/source/common/fonts/v_font.cpp +++ b/source/common/fonts/v_font.cpp @@ -667,12 +667,12 @@ static void CreateLuminosityTranslationRanges() // //========================================================================== -void V_ApplyLuminosityTranslation(int translation, uint8_t* pixel, int size) +void V_ApplyLuminosityTranslation(const LuminosityTranslationDesc& lum, uint8_t* pixel, int size) { - int colorrange = (translation >> 16) & 0x3fff; + int colorrange = lum.colorrange; if (colorrange >= NumTextColors * 2) return; - int lum_min = (translation >> 8) & 0xff; - int lum_max = translation & 0xff; + int lum_min = lum.lum_min; + int lum_max = lum.lum_max; int lum_range = (lum_max - lum_min + 1); PalEntry* remap = paletteptr + colorrange * 256; @@ -892,10 +892,11 @@ void V_LoadTranslations() CalcDefaultTranslation(BigFont, CR_UNTRANSLATED * 2 + 1); if (OriginalBigFont != nullptr && OriginalBigFont != BigFont) { - int sometrans = OriginalBigFont->Translations[0]; + assert(IsLuminosityTranslation(OriginalBigFont->Translations[0])); + int sometrans = OriginalBigFont->Translations[0].index(); sometrans &= ~(0x3fff << 16); sometrans |= (CR_UNTRANSLATED * 2 + 1) << 16; - OriginalBigFont->Translations[CR_UNTRANSLATED] = sometrans; + OriginalBigFont->Translations[CR_UNTRANSLATED] = FTranslationID::fromInt(sometrans); OriginalBigFont->forceremap = true; } } @@ -904,18 +905,20 @@ void V_LoadTranslations() CalcDefaultTranslation(SmallFont, CR_UNTRANSLATED * 2); if (OriginalSmallFont != nullptr && OriginalSmallFont != SmallFont) { - int sometrans = OriginalSmallFont->Translations[0]; + assert(IsLuminosityTranslation(OriginalSmallFont->Translations[0])); + int sometrans = OriginalSmallFont->Translations[0].index(); sometrans &= ~(0x3fff << 16); sometrans |= (CR_UNTRANSLATED * 2) << 16; - OriginalSmallFont->Translations[CR_UNTRANSLATED] = sometrans; + OriginalSmallFont->Translations[CR_UNTRANSLATED] = FTranslationID::fromInt(sometrans); OriginalSmallFont->forceremap = true; } if (NewSmallFont != nullptr) { - int sometrans = NewSmallFont->Translations[0]; + assert(IsLuminosityTranslation(NewSmallFont->Translations[0])); + int sometrans = NewSmallFont->Translations[0].index(); sometrans &= ~(0x3fff << 16); sometrans |= (CR_UNTRANSLATED * 2) << 16; - NewSmallFont->Translations[CR_UNTRANSLATED] = sometrans; + NewSmallFont->Translations[CR_UNTRANSLATED] = FTranslationID::fromInt(sometrans); NewSmallFont->forceremap = true; } } diff --git a/source/common/fonts/v_font.h b/source/common/fonts/v_font.h index 0b5ce1338..4e7596ef8 100644 --- a/source/common/fonts/v_font.h +++ b/source/common/fonts/v_font.h @@ -37,6 +37,7 @@ #include "vectors.h" #include "palentry.h" #include "name.h" +#include "palettecontainer.h" class FGameTexture; struct FRemapTable; @@ -104,7 +105,7 @@ public: virtual FGameTexture *GetChar (int code, int translation, int *const width) const; virtual int GetCharWidth (int code) const; - int GetColorTranslation (EColorRange range, PalEntry *color = nullptr) const; + FTranslationID GetColorTranslation (EColorRange range, PalEntry *color = nullptr) const; int GetLump() const { return Lump; } int GetSpaceWidth () const { return SpaceWidth; } int GetHeight () const { return FontHeight; } @@ -197,7 +198,7 @@ protected: int XMove = INT_MIN; }; TArray Chars; - TArray Translations; + TArray Translations; int Lump; FName FontName = NAME_None; @@ -219,7 +220,7 @@ PalEntry V_LogColorFromColorRange (EColorRange range); EColorRange V_ParseFontColor (const uint8_t *&color_value, int normalcolor, int boldcolor); void V_InitFontColors(); char* CleanseString(char* str); -void V_ApplyLuminosityTranslation(int translation, uint8_t* pixel, int size); +void V_ApplyLuminosityTranslation(const LuminosityTranslationDesc& lum, uint8_t* pixel, int size); void V_LoadTranslations(); class FBitmap; diff --git a/source/common/rendering/hwrenderer/data/hw_renderstate.h b/source/common/rendering/hwrenderer/data/hw_renderstate.h index 1f5e08d4c..05fd6bcbf 100644 --- a/source/common/rendering/hwrenderer/data/hw_renderstate.h +++ b/source/common/rendering/hwrenderer/data/hw_renderstate.h @@ -635,6 +635,12 @@ public: SetMaterial(mat, clampmode, translation, overrideshader); } + void SetMaterial(FGameTexture* tex, EUpscaleFlags upscalemask, int scaleflags, int clampmode, FTranslationID translation, int overrideshader) + { + SetMaterial(tex, upscalemask, scaleflags, clampmode, translation.index(), overrideshader); + } + + void SetClipSplit(float bottom, float top) { mClipSplit[0] = bottom; diff --git a/source/common/textures/texture.cpp b/source/common/textures/texture.cpp index db922a7b5..dd693d087 100644 --- a/source/common/textures/texture.cpp +++ b/source/common/textures/texture.cpp @@ -50,6 +50,7 @@ #include "c_cvars.h" #include "imagehelpers.h" #include "v_video.h" +#include "v_font.h" // Wrappers to keep the definitions of these classes out of here. IHardwareTexture* CreateHardwareTexture(int numchannels); @@ -321,7 +322,6 @@ bool FTexture::ProcessData(unsigned char* buffer, int w, int h, bool ispatch) // Initializes the buffer for the texture data // //=========================================================================== -void V_ApplyLuminosityTranslation(int translation, uint8_t *buffer, int size); FTextureBuffer FTexture::CreateTexBuffer(int translation, int flags) { @@ -367,7 +367,7 @@ FTextureBuffer FTexture::CreateTexBuffer(int translation, int flags) bmp.Blit(exx, exx, Pixels); if (IsLuminosityTranslation(translation)) { - V_ApplyLuminosityTranslation(translation, buffer, W * H); + V_ApplyLuminosityTranslation(LuminosityTranslationDesc::fromInt(translation), buffer, W * H); } if (remap == nullptr) diff --git a/source/core/automap.cpp b/source/core/automap.cpp index 1cef14907..524c30d0a 100644 --- a/source/core/automap.cpp +++ b/source/core/automap.cpp @@ -527,7 +527,7 @@ static void renderDrawMapView(const DVector2& cpos, const DVector2& cangvect, co auto flortex = sect->floortexture; if (!flortex.isValid()) continue; - int translation = TRANSLATION(Translation_Remap + curbasepal, sector[i].floorpal).index(); + auto translation = TRANSLATION(Translation_Remap + curbasepal, sector[i].floorpal); PalEntry light = shadeToLight(sector[i].floorshade); for (auto section : sectionsPerSector[i]) @@ -580,7 +580,7 @@ static void renderDrawMapView(const DVector2& cpos, const DVector2& cangvect, co color.a = uint8_t(alpha * 255); } - int translation = TRANSLATION(Translation_Remap + curbasepal, actor->spr.pal).index(); + auto translation = TRANSLATION(Translation_Remap + curbasepal, actor->spr.pal); const static unsigned indices[] = { 0, 1, 2, 0, 2, 3 }; twod->AddPoly(TexMan.GetGameTexture(actor->spr.spritetexture(), true), vertices.Data(), vertices.Size(), indices, 6, translation, color, rs, &viewport3d); }