Skip to content

Commit

Permalink
use FTranslationID in the 2D drawer.
Browse files Browse the repository at this point in the history
  • Loading branch information
coelckers committed Nov 9, 2023
1 parent 752b67c commit 7a5d89c
Show file tree
Hide file tree
Showing 15 changed files with 58 additions and 43 deletions.
10 changes: 5 additions & 5 deletions source/common/2d/v_2ddrawer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -443,15 +443,15 @@ 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)
{
dg.mLightLevel = vertexcolor.Luminance();
vertexcolor = 0xffffffff;
}

if (!img->isHardwareCanvas() && parms.TranslationId != -1)
if (!img->isHardwareCanvas() && parms.TranslationId != INVALID_TRANSLATION)
{
dg.mTranslationId = parms.TranslationId;
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
Expand Down Expand Up @@ -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;

Expand Down
4 changes: 2 additions & 2 deletions source/common/2d/v_2ddrawer.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class F2DDrawer
int mIndexCount;

FGameTexture *mTexture;
int mTranslationId;
FTranslationID mTranslationId;
PalEntry mSpecialColormap[2];
int mScissor[4];
int mDesaturate;
Expand Down Expand Up @@ -216,7 +216,7 @@ class F2DDrawer
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);
Expand Down
4 changes: 2 additions & 2 deletions source/common/2d/v_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion source/common/2d/v_draw.h
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ struct DrawParms
double left;
float Alpha;
PalEntry fillcolor;
int TranslationId;
FTranslationID TranslationId;
PalEntry colorOverlay;
PalEntry color;
int alphaChannel;
Expand Down
10 changes: 5 additions & 5 deletions source/common/2d/v_drawtext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down Expand Up @@ -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;

Expand All @@ -282,15 +282,15 @@ 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;
boldcolor = normalcolor ? normalcolor - 1 : NumTextColors - 1;

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();
Expand Down
9 changes: 7 additions & 2 deletions source/common/engine/palettecontainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -187,6 +187,11 @@ class PaletteContainer
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)
Expand Down
8 changes: 4 additions & 4 deletions source/common/fonts/font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -711,10 +711,10 @@ int FFont::GetLuminosity (uint32_t *colorsused, TArray<double> &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)
Expand All @@ -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];
Expand Down Expand Up @@ -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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions source/common/fonts/hexfont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ class FHexFont : public FFont
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);
}
}
Expand Down Expand Up @@ -387,7 +387,7 @@ class FHexFont2 : public FFont
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);
}
}
Expand Down
2 changes: 1 addition & 1 deletion source/common/fonts/singlelumpfont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
4 changes: 2 additions & 2 deletions source/common/fonts/specialfont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -196,7 +196,7 @@ void FSpecialFont::LoadTranslations()
remap.Remap[i] = i;
}
}
trans = GPalette.StoreTranslation(TRANSLATION_Internal, &remap).index();
trans = GPalette.StoreTranslation(TRANSLATION_Internal, &remap);
}
}

Expand Down
23 changes: 13 additions & 10 deletions source/common/fonts/v_font.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
}
}
Expand Down
7 changes: 4 additions & 3 deletions source/common/fonts/v_font.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
#include "vectors.h"
#include "palentry.h"
#include "name.h"
#include "palettecontainer.h"

class FGameTexture;
struct FRemapTable;
Expand Down Expand Up @@ -104,7 +105,7 @@ class FFont

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; }
Expand Down Expand Up @@ -197,7 +198,7 @@ class FFont
int XMove = INT_MIN;
};
TArray<CharData> Chars;
TArray<int> Translations;
TArray<FTranslationID> Translations;

int Lump;
FName FontName = NAME_None;
Expand All @@ -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;

Expand Down
6 changes: 6 additions & 0 deletions source/common/rendering/hwrenderer/data/hw_renderstate.h
Original file line number Diff line number Diff line change
Expand Up @@ -635,6 +635,12 @@ class FRenderState
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;
Expand Down
4 changes: 2 additions & 2 deletions source/common/textures/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions source/core/automap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 7a5d89c

Please sign in to comment.