- use TArrays for most buffers being used in the font class.

This commit is contained in:
Christoph Oelckers 2018-12-15 11:55:21 +01:00
parent cd25b4be4f
commit 48d87e3dcf
2 changed files with 35 additions and 63 deletions

View file

@ -310,16 +310,14 @@ FFont::FFont (const char *name, const char *nametemplate, int first, int count,
int i; int i;
FTextureID lump; FTextureID lump;
char buffer[12]; char buffer[12];
TArray<FTexture*> charLumps; TArray<FTexture*> charLumps(count, true);
int maxyoffs; int maxyoffs;
bool doomtemplate = gameinfo.gametype & GAME_DoomChex ? strncmp (nametemplate, "STCFN", 5) == 0 : false; bool doomtemplate = gameinfo.gametype & GAME_DoomChex ? strncmp (nametemplate, "STCFN", 5) == 0 : false;
bool stcfn121 = false; bool stcfn121 = false;
noTranslate = notranslate; noTranslate = notranslate;
Lump = fdlump; Lump = fdlump;
Chars = new CharData[count]; Chars.Resize(count);
charLumps.Resize(count);
PatchRemap = new uint8_t[256];
FirstChar = first; FirstChar = first;
LastChar = first + count - 1; LastChar = first + count - 1;
FontHeight = 0; FontHeight = 0;
@ -422,19 +420,6 @@ FFont::FFont (const char *name, const char *nametemplate, int first, int count,
FFont::~FFont () FFont::~FFont ()
{ {
if (Chars)
{
int count = LastChar - FirstChar + 1;
delete[] Chars;
Chars = NULL;
}
if (PatchRemap)
{
delete[] PatchRemap;
PatchRemap = NULL;
}
FFont **prev = &FirstFont; FFont **prev = &FirstFont;
FFont *font = *prev; FFont *font = *prev;
@ -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; double min, max, diver;
int i, j; int i, j;
@ -555,26 +540,26 @@ int FFont::SimpleTranslation (uint8_t *colorsused, uint8_t *translation, uint8_t
qsort (reverse+1, j-1, 1, compare); qsort (reverse+1, j-1, 1, compare);
*luminosity = new double[j]; Luminosity.Resize(j);
(*luminosity)[0] = 0.0; // [BL] Prevent uninitalized memory Luminosity[0] = 0.0; // [BL] Prevent uninitalized memory
max = 0.0; max = 0.0;
min = 100000000.0; min = 100000000.0;
for (i = 1; i < j; i++) for (i = 1; i < j; i++)
{ {
translation[reverse[i]] = 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 + GPART(GPalette.BaseColors[reverse[i]]) * 0.587 +
BPART(GPalette.BaseColors[reverse[i]]) * 0.114; BPART(GPalette.BaseColors[reverse[i]]) * 0.114;
if ((*luminosity)[i] > max) if (Luminosity[i] > max)
max = (*luminosity)[i]; max = Luminosity[i];
if ((*luminosity)[i] < min) if (Luminosity[i] < min)
min = (*luminosity)[i]; min = Luminosity[i];
} }
diver = 1.0 / (max - min); diver = 1.0 / (max - min);
for (i = 1; i < j; i++) for (i = 1; i < j; i++)
{ {
(*luminosity)[i] = ((*luminosity)[i] - min) * diver; Luminosity[i] = (Luminosity[i] - min) * diver;
} }
return j; return j;
@ -858,7 +843,7 @@ void FFont::LoadTranslations()
{ {
unsigned int count = LastChar - FirstChar + 1; unsigned int count = LastChar - FirstChar + 1;
uint8_t usedcolors[256], identity[256]; uint8_t usedcolors[256], identity[256];
double *luminosity; TArray<double> Luminosity;
memset (usedcolors, 0, 256); memset (usedcolors, 0, 256);
for (unsigned int i = 0; i < count; i++) for (unsigned int i = 0; i < count; i++)
@ -876,7 +861,7 @@ void FFont::LoadTranslations()
// Fixme: This needs to build a translation based on the source palette, not some intermediate 'ordered' table. // 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++) for (unsigned int i = 0; i < count; i++)
{ {
@ -884,9 +869,7 @@ void FFont::LoadTranslations()
static_cast<FFontChar1 *>(Chars[i].Pic->GetImage())->SetSourceRemap(PatchRemap); static_cast<FFontChar1 *>(Chars[i].Pic->GetImage())->SetSourceRemap(PatchRemap);
} }
BuildTranslations (luminosity, identity, &TranslationParms[0][0], ActiveColors, NULL); BuildTranslations (Luminosity.Data(), identity, &TranslationParms[0][0], ActiveColors, NULL);
delete[] luminosity;
} }
//========================================================================== //==========================================================================
@ -898,8 +881,6 @@ void FFont::LoadTranslations()
FFont::FFont (int lump) FFont::FFont (int lump)
{ {
Lump = lump; Lump = lump;
Chars = NULL;
PatchRemap = NULL;
FontName = NAME_None; FontName = NAME_None;
Cursor = '_'; Cursor = '_';
noTranslate = false; noTranslate = false;
@ -964,8 +945,8 @@ void FSingleLumpFont::CreateFontFromPic (FTextureID picnum)
GlobalKerning = 0; GlobalKerning = 0;
FirstChar = LastChar = 'A'; FirstChar = LastChar = 'A';
Chars = new CharData[1]; Chars.Resize(1);
Chars->Pic = pic; Chars[0].Pic = pic;
// Only one color range. Don't bother with the others. // Only one color range. Don't bother with the others.
ActiveColors = 0; ActiveColors = 0;
@ -1030,7 +1011,7 @@ void FSingleLumpFont::LoadFON1 (int lump, const uint8_t *data)
{ {
int w, h; int w, h;
Chars = new CharData[256]; Chars.Resize(256);
w = data[4] + data[5]*256; w = data[4] + data[5]*256;
h = data[6] + data[7]*256; h = data[6] + data[7]*256;
@ -1042,10 +1023,9 @@ void FSingleLumpFont::LoadFON1 (int lump, const uint8_t *data)
LastChar = 255; LastChar = 255;
GlobalKerning = 0; GlobalKerning = 0;
translateUntranslated = true; translateUntranslated = true;
PatchRemap = new uint8_t[256];
for(unsigned int i = 0;i < 256;++i) for(unsigned int i = 0;i < 256;++i)
Chars[i].Pic = NULL; Chars[i].Pic = nullptr;
LoadTranslations(); LoadTranslations();
} }
@ -1062,7 +1042,6 @@ void FSingleLumpFont::LoadFON1 (int lump, const uint8_t *data)
void FSingleLumpFont::LoadFON2 (int lump, const uint8_t *data) void FSingleLumpFont::LoadFON2 (int lump, const uint8_t *data)
{ {
int count, i, totalwidth; int count, i, totalwidth;
int *widths2;
uint16_t *widths; uint16_t *widths;
const uint8_t *palette; const uint8_t *palette;
const uint8_t *data_p; const uint8_t *data_p;
@ -1072,12 +1051,11 @@ void FSingleLumpFont::LoadFON2 (int lump, const uint8_t *data)
FirstChar = data[6]; FirstChar = data[6];
LastChar = data[7]; LastChar = data[7];
ActiveColors = data[10]+1; ActiveColors = data[10]+1;
PatchRemap = NULL;
RescalePalette = data[9] == 0; RescalePalette = data[9] == 0;
count = LastChar - FirstChar + 1; count = LastChar - FirstChar + 1;
Chars = new CharData[count]; Chars.Resize(count);
widths2 = new int[count]; TArray<int> widths2(count, true);
if (data[11] & 1) if (data[11] & 1)
{ // Font specifies a kerning value. { // Font specifies a kerning value.
GlobalKerning = LittleShort(*(int16_t *)&data[12]); GlobalKerning = LittleShort(*(int16_t *)&data[12]);
@ -1162,7 +1140,6 @@ void FSingleLumpFont::LoadFON2 (int lump, const uint8_t *data)
} }
LoadTranslations(); LoadTranslations();
delete[] widths2;
} }
//========================================================================== //==========================================================================
@ -1221,7 +1198,7 @@ void FSingleLumpFont::LoadBMF(int lump, const uint8_t *data)
I_FatalError("BMF font defines no characters"); I_FatalError("BMF font defines no characters");
} }
count = LastChar - FirstChar + 1; count = LastChar - FirstChar + 1;
Chars = new CharData[count]; Chars.Resize(count);
for (i = 0; i < count; ++i) for (i = 0; i < count; ++i)
{ {
Chars[i].Pic = NULL; Chars[i].Pic = NULL;
@ -1247,7 +1224,6 @@ void FSingleLumpFont::LoadBMF(int lump, const uint8_t *data)
qsort(sort_palette + 1, ActiveColors - 1, sizeof(PalEntry), BMFCompare); qsort(sort_palette + 1, ActiveColors - 1, sizeof(PalEntry), BMFCompare);
// Create the PatchRemap table from the sorted "alpha" values. // Create the PatchRemap table from the sorted "alpha" values.
PatchRemap = new uint8_t[ActiveColors];
PatchRemap[0] = 0; PatchRemap[0] = 0;
for (i = 1; i < ActiveColors; ++i) for (i = 1; i < ActiveColors; ++i)
{ {
@ -1515,10 +1491,11 @@ 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; int i;
FTexture **charlumps; TArray<FTexture *> charlumps(count, true);
int maxyoffs; int maxyoffs;
FTexture *pic; FTexture *pic;
@ -1526,9 +1503,7 @@ FSpecialFont::FSpecialFont (const char *name, int first, int count, FTexture **l
noTranslate = donttranslate; noTranslate = donttranslate;
FontName = name; FontName = name;
Chars = new CharData[count]; Chars.Resize(count);
charlumps = new FTexture*[count];
PatchRemap = new uint8_t[256];
FirstChar = first; FirstChar = first;
LastChar = first + count - 1; LastChar = first + count - 1;
FontHeight = 0; FontHeight = 0;
@ -1541,7 +1516,7 @@ FSpecialFont::FSpecialFont (const char *name, int first, int count, FTexture **l
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
{ {
pic = charlumps[i] = lumplist[i]; pic = charlumps[i] = lumplist[i];
if (pic != NULL) if (pic != nullptr)
{ {
int height = pic->GetDisplayHeight(); int height = pic->GetDisplayHeight();
int yoffs = pic->GetDisplayTopOffset(); int yoffs = pic->GetDisplayTopOffset();
@ -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) if (!noTranslate)
{ {
@ -1594,8 +1569,6 @@ FSpecialFont::FSpecialFont (const char *name, int first, int count, FTexture **l
{ {
LoadTranslations(); LoadTranslations();
} }
delete[] charlumps;
} }
//========================================================================== //==========================================================================
@ -1608,7 +1581,7 @@ void FSpecialFont::LoadTranslations()
{ {
int count = LastChar - FirstChar + 1; int count = LastChar - FirstChar + 1;
uint8_t usedcolors[256], identity[256]; uint8_t usedcolors[256], identity[256];
double *luminosity; TArray<double> Luminosity;
int TotalColors; int TotalColors;
int i, j; int i, j;
@ -1631,7 +1604,7 @@ void FSpecialFont::LoadTranslations()
if (notranslate[i]) if (notranslate[i])
usedcolors[i] = false; 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 // Map all untranslated colors into the table of used colors
for (i = 0; i < 256; i++) for (i = 0; i < 256; i++)
@ -1650,7 +1623,7 @@ void FSpecialFont::LoadTranslations()
static_cast<FFontChar1 *>(Chars[i].Pic->GetImage())->SetSourceRemap(PatchRemap); 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 // add the untranslated colors to the Ranges tables
if (ActiveColors < TotalColors) if (ActiveColors < TotalColors)
@ -1667,8 +1640,6 @@ void FSpecialFont::LoadTranslations()
} }
} }
ActiveColors = TotalColors; ActiveColors = TotalColors;
delete[] luminosity;
} }
//========================================================================== //==========================================================================

View file

@ -82,7 +82,7 @@ public:
FFont (const char *fontname, const char *nametemplate, int first, int count, int base, int fdlump, int spacewidth=-1, bool notranslate = false); FFont (const char *fontname, const char *nametemplate, int first, int count, int base, int fdlump, int spacewidth=-1, bool notranslate = false);
virtual ~FFont (); 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; virtual int GetCharWidth (int code) const;
FRemapTable *GetColorTranslation (EColorRange range, PalEntry *color = nullptr) const; FRemapTable *GetColorTranslation (EColorRange range, PalEntry *color = nullptr) const;
int GetLump() const { return Lump; } int GetLump() const { return Lump; }
@ -112,7 +112,7 @@ protected:
void FixXMoves(); void FixXMoves();
static int SimpleTranslation (uint8_t *colorsused, uint8_t *translation, static int SimpleTranslation (uint8_t *colorsused, uint8_t *translation,
uint8_t *identity, double **luminosity); uint8_t *identity, TArray<double> &Luminosity);
int FirstChar, LastChar; int FirstChar, LastChar;
int SpaceWidth; int SpaceWidth;
@ -125,10 +125,11 @@ protected:
{ {
FTexture *Pic; FTexture *Pic;
int XMove; int XMove;
} *Chars; };
TArray<CharData> Chars;
int ActiveColors; int ActiveColors;
TArray<FRemapTable> Ranges; TArray<FRemapTable> Ranges;
uint8_t *PatchRemap; uint8_t PatchRemap[256];
int Lump; int Lump;
FName FontName = NAME_None; FName FontName = NAME_None;