Skip to content

Commit

Permalink
- use TArrays for most buffers being used in the font class.
Browse files Browse the repository at this point in the history
  • Loading branch information
coelckers committed Dec 15, 2018
1 parent cd25b4b commit 48d87e3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 63 deletions.
89 changes: 30 additions & 59 deletions src/v_font.cpp
Expand Up @@ -310,16 +310,14 @@ FFont::FFont (const char *name, const char *nametemplate, int first, int count,
int i;
FTextureID lump;
char buffer[12];
TArray<FTexture*> charLumps;
TArray<FTexture*> charLumps(count, true);
int maxyoffs;
bool doomtemplate = gameinfo.gametype & GAME_DoomChex ? strncmp (nametemplate, "STCFN", 5) == 0 : false;
bool stcfn121 = false;

noTranslate = notranslate;
Lump = fdlump;
Chars = new CharData[count];
charLumps.Resize(count);
PatchRemap = new uint8_t[256];
Chars.Resize(count);
FirstChar = first;
LastChar = first + count - 1;
FontHeight = 0;
Expand Down Expand Up @@ -422,19 +420,6 @@ FFont::FFont (const char *name, const char *nametemplate, int first, int count,

FFont::~FFont ()
{
if (Chars)
{
int count = LastChar - FirstChar + 1;

delete[] Chars;
Chars = NULL;
}
if (PatchRemap)
{
delete[] PatchRemap;
PatchRemap = NULL;
}

FFont **prev = &FirstFont;
FFont *font = *prev;

Expand Down Expand Up @@ -537,7 +522,7 @@ static int compare (const void *arg1, const void *arg2)
//
//==========================================================================

int FFont::SimpleTranslation (uint8_t *colorsused, uint8_t *translation, uint8_t *reverse, double **luminosity)
int FFont::SimpleTranslation (uint8_t *colorsused, uint8_t *translation, uint8_t *reverse, TArray<double> &Luminosity)
{
double min, max, diver;
int i, j;
Expand All @@ -555,26 +540,26 @@ int FFont::SimpleTranslation (uint8_t *colorsused, uint8_t *translation, uint8_t

qsort (reverse+1, j-1, 1, compare);

*luminosity = new double[j];
(*luminosity)[0] = 0.0; // [BL] Prevent uninitalized memory
Luminosity.Resize(j);
Luminosity[0] = 0.0; // [BL] Prevent uninitalized memory
max = 0.0;
min = 100000000.0;
for (i = 1; i < j; i++)
{
translation[reverse[i]] = i;

(*luminosity)[i] = RPART(GPalette.BaseColors[reverse[i]]) * 0.299 +
Luminosity[i] = RPART(GPalette.BaseColors[reverse[i]]) * 0.299 +
GPART(GPalette.BaseColors[reverse[i]]) * 0.587 +
BPART(GPalette.BaseColors[reverse[i]]) * 0.114;
if ((*luminosity)[i] > max)
max = (*luminosity)[i];
if ((*luminosity)[i] < min)
min = (*luminosity)[i];
if (Luminosity[i] > max)
max = Luminosity[i];
if (Luminosity[i] < min)
min = Luminosity[i];
}
diver = 1.0 / (max - min);
for (i = 1; i < j; i++)
{
(*luminosity)[i] = ((*luminosity)[i] - min) * diver;
Luminosity[i] = (Luminosity[i] - min) * diver;
}

return j;
Expand Down Expand Up @@ -858,7 +843,7 @@ void FFont::LoadTranslations()
{
unsigned int count = LastChar - FirstChar + 1;
uint8_t usedcolors[256], identity[256];
double *luminosity;
TArray<double> Luminosity;

memset (usedcolors, 0, 256);
for (unsigned int i = 0; i < count; i++)
Expand All @@ -876,17 +861,15 @@ void FFont::LoadTranslations()

// Fixme: This needs to build a translation based on the source palette, not some intermediate 'ordered' table.

ActiveColors = SimpleTranslation (usedcolors, PatchRemap, identity, &luminosity);
ActiveColors = SimpleTranslation (usedcolors, PatchRemap, identity, Luminosity);

for (unsigned int i = 0; i < count; i++)
{
if(Chars[i].Pic)
static_cast<FFontChar1 *>(Chars[i].Pic->GetImage())->SetSourceRemap(PatchRemap);
}

BuildTranslations (luminosity, identity, &TranslationParms[0][0], ActiveColors, NULL);

delete[] luminosity;
BuildTranslations (Luminosity.Data(), identity, &TranslationParms[0][0], ActiveColors, NULL);
}

//==========================================================================
Expand All @@ -898,8 +881,6 @@ void FFont::LoadTranslations()
FFont::FFont (int lump)
{
Lump = lump;
Chars = NULL;
PatchRemap = NULL;
FontName = NAME_None;
Cursor = '_';
noTranslate = false;
Expand Down Expand Up @@ -964,8 +945,8 @@ void FSingleLumpFont::CreateFontFromPic (FTextureID picnum)
GlobalKerning = 0;

FirstChar = LastChar = 'A';
Chars = new CharData[1];
Chars->Pic = pic;
Chars.Resize(1);
Chars[0].Pic = pic;

// Only one color range. Don't bother with the others.
ActiveColors = 0;
Expand Down Expand Up @@ -1030,7 +1011,7 @@ void FSingleLumpFont::LoadFON1 (int lump, const uint8_t *data)
{
int w, h;

Chars = new CharData[256];
Chars.Resize(256);

w = data[4] + data[5]*256;
h = data[6] + data[7]*256;
Expand All @@ -1042,10 +1023,9 @@ void FSingleLumpFont::LoadFON1 (int lump, const uint8_t *data)
LastChar = 255;
GlobalKerning = 0;
translateUntranslated = true;
PatchRemap = new uint8_t[256];

for(unsigned int i = 0;i < 256;++i)
Chars[i].Pic = NULL;
Chars[i].Pic = nullptr;

LoadTranslations();
}
Expand All @@ -1062,7 +1042,6 @@ void FSingleLumpFont::LoadFON1 (int lump, const uint8_t *data)
void FSingleLumpFont::LoadFON2 (int lump, const uint8_t *data)
{
int count, i, totalwidth;
int *widths2;
uint16_t *widths;
const uint8_t *palette;
const uint8_t *data_p;
Expand All @@ -1072,12 +1051,11 @@ void FSingleLumpFont::LoadFON2 (int lump, const uint8_t *data)
FirstChar = data[6];
LastChar = data[7];
ActiveColors = data[10]+1;
PatchRemap = NULL;
RescalePalette = data[9] == 0;

count = LastChar - FirstChar + 1;
Chars = new CharData[count];
widths2 = new int[count];
Chars.Resize(count);
TArray<int> widths2(count, true);
if (data[11] & 1)
{ // Font specifies a kerning value.
GlobalKerning = LittleShort(*(int16_t *)&data[12]);
Expand Down Expand Up @@ -1162,7 +1140,6 @@ void FSingleLumpFont::LoadFON2 (int lump, const uint8_t *data)
}

LoadTranslations();
delete[] widths2;
}

//==========================================================================
Expand Down Expand Up @@ -1221,7 +1198,7 @@ void FSingleLumpFont::LoadBMF(int lump, const uint8_t *data)
I_FatalError("BMF font defines no characters");
}
count = LastChar - FirstChar + 1;
Chars = new CharData[count];
Chars.Resize(count);
for (i = 0; i < count; ++i)
{
Chars[i].Pic = NULL;
Expand All @@ -1247,7 +1224,6 @@ void FSingleLumpFont::LoadBMF(int lump, const uint8_t *data)
qsort(sort_palette + 1, ActiveColors - 1, sizeof(PalEntry), BMFCompare);

// Create the PatchRemap table from the sorted "alpha" values.
PatchRemap = new uint8_t[ActiveColors];
PatchRemap[0] = 0;
for (i = 1; i < ActiveColors; ++i)
{
Expand Down Expand Up @@ -1515,20 +1491,19 @@ int FSinglePicFont::GetCharWidth (int code) const
//
//==========================================================================

FSpecialFont::FSpecialFont (const char *name, int first, int count, FTexture **lumplist, const bool *notranslate, int lump, bool donttranslate) : FFont(lump)
FSpecialFont::FSpecialFont (const char *name, int first, int count, FTexture **lumplist, const bool *notranslate, int lump, bool donttranslate)
: FFont(lump)
{
int i;
FTexture **charlumps;
TArray<FTexture *> charlumps(count, true);
int maxyoffs;
FTexture *pic;

memcpy(this->notranslate, notranslate, 256*sizeof(bool));

noTranslate = donttranslate;
FontName = name;
Chars = new CharData[count];
charlumps = new FTexture*[count];
PatchRemap = new uint8_t[256];
Chars.Resize(count);
FirstChar = first;
LastChar = first + count - 1;
FontHeight = 0;
Expand All @@ -1541,7 +1516,7 @@ FSpecialFont::FSpecialFont (const char *name, int first, int count, FTexture **l
for (i = 0; i < count; i++)
{
pic = charlumps[i] = lumplist[i];
if (pic != NULL)
if (pic != nullptr)
{
int height = pic->GetDisplayHeight();
int yoffs = pic->GetDisplayTopOffset();
Expand All @@ -1557,7 +1532,7 @@ FSpecialFont::FSpecialFont (const char *name, int first, int count, FTexture **l
}
}

if (charlumps[i] != NULL)
if (charlumps[i] != nullptr)
{
if (!noTranslate)
{
Expand Down Expand Up @@ -1594,8 +1569,6 @@ FSpecialFont::FSpecialFont (const char *name, int first, int count, FTexture **l
{
LoadTranslations();
}

delete[] charlumps;
}

//==========================================================================
Expand All @@ -1608,7 +1581,7 @@ void FSpecialFont::LoadTranslations()
{
int count = LastChar - FirstChar + 1;
uint8_t usedcolors[256], identity[256];
double *luminosity;
TArray<double> Luminosity;
int TotalColors;
int i, j;

Expand All @@ -1631,7 +1604,7 @@ void FSpecialFont::LoadTranslations()
if (notranslate[i])
usedcolors[i] = false;

TotalColors = ActiveColors = SimpleTranslation (usedcolors, PatchRemap, identity, &luminosity);
TotalColors = ActiveColors = SimpleTranslation (usedcolors, PatchRemap, identity, Luminosity);

// Map all untranslated colors into the table of used colors
for (i = 0; i < 256; i++)
Expand All @@ -1650,7 +1623,7 @@ void FSpecialFont::LoadTranslations()
static_cast<FFontChar1 *>(Chars[i].Pic->GetImage())->SetSourceRemap(PatchRemap);
}

BuildTranslations (luminosity, identity, &TranslationParms[0][0], TotalColors, NULL);
BuildTranslations (Luminosity.Data(), identity, &TranslationParms[0][0], TotalColors, NULL);

// add the untranslated colors to the Ranges tables
if (ActiveColors < TotalColors)
Expand All @@ -1667,8 +1640,6 @@ void FSpecialFont::LoadTranslations()
}
}
ActiveColors = TotalColors;

delete[] luminosity;
}

//==========================================================================
Expand Down
9 changes: 5 additions & 4 deletions src/v_font.h
Expand Up @@ -82,7 +82,7 @@ class FFont
FFont (const char *fontname, const char *nametemplate, int first, int count, int base, int fdlump, int spacewidth=-1, bool notranslate = false);
virtual ~FFont ();

virtual FTexture *GetChar (int code, int *const width) const;
FTexture *GetChar (int code, int *const width) const;
virtual int GetCharWidth (int code) const;
FRemapTable *GetColorTranslation (EColorRange range, PalEntry *color = nullptr) const;
int GetLump() const { return Lump; }
Expand Down Expand Up @@ -112,7 +112,7 @@ class FFont
void FixXMoves();

static int SimpleTranslation (uint8_t *colorsused, uint8_t *translation,
uint8_t *identity, double **luminosity);
uint8_t *identity, TArray<double> &Luminosity);

int FirstChar, LastChar;
int SpaceWidth;
Expand All @@ -125,10 +125,11 @@ class FFont
{
FTexture *Pic;
int XMove;
} *Chars;
};
TArray<CharData> Chars;
int ActiveColors;
TArray<FRemapTable> Ranges;
uint8_t *PatchRemap;
uint8_t PatchRemap[256];

int Lump;
FName FontName = NAME_None;
Expand Down

0 comments on commit 48d87e3

Please sign in to comment.