From 9102fb86a5a648cf1e02dc735f5e85ad5c9f0b8c Mon Sep 17 00:00:00 2001 From: Christoph Oelckers Date: Sun, 17 Feb 2019 12:53:12 +0100 Subject: [PATCH] - moved FSpecialFont to its own file. --- src/CMakeLists.txt | 1 + src/gamedata/fonts/fontinternals.h | 2 + src/gamedata/fonts/specialfont.cpp | 221 +++++++++++++++++++++++++++++ src/gamedata/fonts/v_font.cpp | 189 ++---------------------- src/gamedata/textures/textures.h | 1 - 5 files changed, 232 insertions(+), 182 deletions(-) create mode 100644 src/gamedata/fonts/specialfont.cpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 0bf300a18..9005c99db 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1127,6 +1127,7 @@ set (PCH_SOURCES gamedata/textures/hires/hirestex.cpp gamedata/fonts/singlelumpfont.cpp gamedata/fonts/singlepicfont.cpp + gamedata/fonts/specialfont.cpp gamedata/fonts/v_font.cpp gamedata/fonts/v_text.cpp gamedata/p_xlat.cpp diff --git a/src/gamedata/fonts/fontinternals.h b/src/gamedata/fonts/fontinternals.h index c64dd54ea..b74220c38 100644 --- a/src/gamedata/fonts/fontinternals.h +++ b/src/gamedata/fonts/fontinternals.h @@ -14,3 +14,5 @@ struct TranslationParm extern TArray TranslationParms[2]; + +void RecordTextureColors (FImageSource *pic, uint8_t *usedcolors); diff --git a/src/gamedata/fonts/specialfont.cpp b/src/gamedata/fonts/specialfont.cpp new file mode 100644 index 000000000..d90cb330b --- /dev/null +++ b/src/gamedata/fonts/specialfont.cpp @@ -0,0 +1,221 @@ +/* +** v_font.cpp +** Font management +** +**--------------------------------------------------------------------------- +** Copyright 1998-2016 Randy Heit +** Copyright 2005-2019 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 "v_font.h" +#include "textures.h" +#include "image.h" +#include "textures/formats/fontchars.h" + +#include "fontinternals.h" + +// Essentially a normal multilump font but with an explicit list of character patches +class FSpecialFont : public FFont +{ +public: + FSpecialFont (const char *name, int first, int count, FTexture **lumplist, const bool *notranslate, int lump, bool donttranslate); + + void LoadTranslations(); + +protected: + bool notranslate[256]; +}; + + +//========================================================================== +// +// FSpecialFont :: FSpecialFont +// +//========================================================================== + +FSpecialFont::FSpecialFont (const char *name, int first, int count, FTexture **lumplist, const bool *notranslate, int lump, bool donttranslate) + : FFont(lump) +{ + int i; + TArray charlumps(count, true); + int maxyoffs; + FTexture *pic; + + memcpy(this->notranslate, notranslate, 256*sizeof(bool)); + + noTranslate = donttranslate; + FontName = name; + Chars.Resize(count); + FirstChar = first; + LastChar = first + count - 1; + FontHeight = 0; + GlobalKerning = false; + Next = FirstFont; + FirstFont = this; + + maxyoffs = 0; + + for (i = 0; i < count; i++) + { + pic = charlumps[i] = lumplist[i]; + if (pic != nullptr) + { + int height = pic->GetDisplayHeight(); + int yoffs = pic->GetDisplayTopOffset(); + + if (yoffs > maxyoffs) + { + maxyoffs = yoffs; + } + height += abs (yoffs); + if (height > FontHeight) + { + FontHeight = height; + } + } + + if (charlumps[i] != nullptr) + { + charlumps[i]->SetUseType(ETextureType::FontChar); + + Chars[i].OriginalPic = charlumps[i]; + if (!noTranslate) + { + Chars[i].TranslatedPic = new FImageTexture(new FFontChar1 (charlumps[i]->GetImage()), ""); + Chars[i].TranslatedPic->CopySize(charlumps[i]); + Chars[i].TranslatedPic->SetUseType(ETextureType::FontChar); + TexMan.AddTexture(Chars[i].TranslatedPic); + } + else Chars[i].TranslatedPic = charlumps[i]; + Chars[i].XMove = Chars[i].TranslatedPic->GetDisplayWidth(); + } + else + { + Chars[i].TranslatedPic = nullptr; + Chars[i].XMove = INT_MIN; + } + } + + // Special fonts normally don't have all characters so be careful here! + if ('N'-first >= 0 && 'N'-first < count && Chars['N' - first].TranslatedPic != nullptr) + { + SpaceWidth = (Chars['N' - first].XMove + 1) / 2; + } + else + { + SpaceWidth = 4; + } + + FixXMoves(); + + if (noTranslate) + { + ActiveColors = 0; + } + else + { + LoadTranslations(); + } +} + +//========================================================================== +// +// FSpecialFont :: LoadTranslations +// +//========================================================================== + +void FSpecialFont::LoadTranslations() +{ + int count = LastChar - FirstChar + 1; + uint8_t usedcolors[256], identity[256]; + TArray Luminosity; + int TotalColors; + int i, j; + + memset (usedcolors, 0, 256); + for (i = 0; i < count; i++) + { + if (Chars[i].TranslatedPic) + { + FFontChar1 *pic = static_cast(Chars[i].TranslatedPic->GetImage()); + if (pic) + { + pic->SetSourceRemap(nullptr); // Force the FFontChar1 to return the same pixels as the base texture + RecordTextureColors(pic, usedcolors); + } + } + } + + // exclude the non-translated colors from the translation calculation + for (i = 0; i < 256; i++) + if (notranslate[i]) + usedcolors[i] = false; + + TotalColors = ActiveColors = SimpleTranslation (usedcolors, PatchRemap, identity, Luminosity); + + // Map all untranslated colors into the table of used colors + for (i = 0; i < 256; i++) + { + if (notranslate[i]) + { + PatchRemap[i] = TotalColors; + identity[TotalColors] = i; + TotalColors++; + } + } + + for (i = 0; i < count; i++) + { + if(Chars[i].TranslatedPic) + static_cast(Chars[i].TranslatedPic->GetImage())->SetSourceRemap(PatchRemap); + } + + BuildTranslations (Luminosity.Data(), identity, &TranslationParms[0][0], TotalColors, nullptr); + + // add the untranslated colors to the Ranges tables + if (ActiveColors < TotalColors) + { + for (i = 0; i < NumTextColors; i++) + { + FRemapTable *remap = &Ranges[i]; + for (j = ActiveColors; j < TotalColors; ++j) + { + remap->Remap[j] = identity[j]; + remap->Palette[j] = GPalette.BaseColors[identity[j]]; + remap->Palette[j].a = 0xff; + } + } + } + ActiveColors = TotalColors; +} + +FFont *CreateSpecialFont (const char *name, int first, int count, FTexture **lumplist, const bool *notranslate, int lump, bool donttranslate) +{ + return new FSpecialFont(name, first, count, lumplist, notranslate, lump, donttranslate); +} diff --git a/src/gamedata/fonts/v_font.cpp b/src/gamedata/fonts/v_font.cpp index 6a1dc1f70..fc7cb44fb 100644 --- a/src/gamedata/fonts/v_font.cpp +++ b/src/gamedata/fonts/v_font.cpp @@ -65,24 +65,6 @@ // TYPES ------------------------------------------------------------------- void RecordTextureColors (FImageSource *pic, uint8_t *colorsused); -struct TranslationMap -{ - FName Name; - int Number; -}; - -// Essentially a normal multilump font but with an explicit list of character patches -class FSpecialFont : public FFont -{ -public: - FSpecialFont (const char *name, int first, int count, FTexture **lumplist, const bool *notranslate, int lump, bool donttranslate); - - void LoadTranslations(); - -protected: - bool notranslate[256]; -}; - struct TempParmInfo { unsigned int StartParm[2]; @@ -96,6 +78,12 @@ struct TempColorInfo PalEntry LogColor; }; +struct TranslationMap +{ + FName Name; + int Number; +}; + // EXTERNAL FUNCTION PROTOTYPES -------------------------------------------- // PUBLIC FUNCTION PROTOTYPES ---------------------------------------------- @@ -1652,168 +1640,6 @@ FFont::FFont (int lump) for (auto &p : PatchRemap) p = pp++; } -//========================================================================== -// -// FSpecialFont :: FSpecialFont -// -//========================================================================== - -FSpecialFont::FSpecialFont (const char *name, int first, int count, FTexture **lumplist, const bool *notranslate, int lump, bool donttranslate) - : FFont(lump) -{ - int i; - TArray charlumps(count, true); - int maxyoffs; - FTexture *pic; - - memcpy(this->notranslate, notranslate, 256*sizeof(bool)); - - noTranslate = donttranslate; - FontName = name; - Chars.Resize(count); - FirstChar = first; - LastChar = first + count - 1; - FontHeight = 0; - GlobalKerning = false; - Next = FirstFont; - FirstFont = this; - - maxyoffs = 0; - - for (i = 0; i < count; i++) - { - pic = charlumps[i] = lumplist[i]; - if (pic != nullptr) - { - int height = pic->GetDisplayHeight(); - int yoffs = pic->GetDisplayTopOffset(); - - if (yoffs > maxyoffs) - { - maxyoffs = yoffs; - } - height += abs (yoffs); - if (height > FontHeight) - { - FontHeight = height; - } - } - - if (charlumps[i] != nullptr) - { - charlumps[i]->SetUseType(ETextureType::FontChar); - - Chars[i].OriginalPic = charlumps[i]; - if (!noTranslate) - { - Chars[i].TranslatedPic = new FImageTexture(new FFontChar1 (charlumps[i]->GetImage()), ""); - Chars[i].TranslatedPic->CopySize(charlumps[i]); - Chars[i].TranslatedPic->SetUseType(ETextureType::FontChar); - TexMan.AddTexture(Chars[i].TranslatedPic); - } - else Chars[i].TranslatedPic = charlumps[i]; - Chars[i].XMove = Chars[i].TranslatedPic->GetDisplayWidth(); - } - else - { - Chars[i].TranslatedPic = nullptr; - Chars[i].XMove = INT_MIN; - } - } - - // Special fonts normally don't have all characters so be careful here! - if ('N'-first >= 0 && 'N'-first < count && Chars['N' - first].TranslatedPic != nullptr) - { - SpaceWidth = (Chars['N' - first].XMove + 1) / 2; - } - else - { - SpaceWidth = 4; - } - - FixXMoves(); - - if (noTranslate) - { - ActiveColors = 0; - } - else - { - LoadTranslations(); - } -} - -//========================================================================== -// -// FSpecialFont :: LoadTranslations -// -//========================================================================== - -void FSpecialFont::LoadTranslations() -{ - int count = LastChar - FirstChar + 1; - uint8_t usedcolors[256], identity[256]; - TArray Luminosity; - int TotalColors; - int i, j; - - memset (usedcolors, 0, 256); - for (i = 0; i < count; i++) - { - if (Chars[i].TranslatedPic) - { - FFontChar1 *pic = static_cast(Chars[i].TranslatedPic->GetImage()); - if (pic) - { - pic->SetSourceRemap(nullptr); // Force the FFontChar1 to return the same pixels as the base texture - RecordTextureColors(pic, usedcolors); - } - } - } - - // exclude the non-translated colors from the translation calculation - for (i = 0; i < 256; i++) - if (notranslate[i]) - usedcolors[i] = false; - - TotalColors = ActiveColors = SimpleTranslation (usedcolors, PatchRemap, identity, Luminosity); - - // Map all untranslated colors into the table of used colors - for (i = 0; i < 256; i++) - { - if (notranslate[i]) - { - PatchRemap[i] = TotalColors; - identity[TotalColors] = i; - TotalColors++; - } - } - - for (i = 0; i < count; i++) - { - if(Chars[i].TranslatedPic) - static_cast(Chars[i].TranslatedPic->GetImage())->SetSourceRemap(PatchRemap); - } - - BuildTranslations (Luminosity.Data(), identity, &TranslationParms[0][0], TotalColors, nullptr); - - // add the untranslated colors to the Ranges tables - if (ActiveColors < TotalColors) - { - for (i = 0; i < NumTextColors; i++) - { - FRemapTable *remap = &Ranges[i]; - for (j = ActiveColors; j < TotalColors; ++j) - { - remap->Remap[j] = identity[j]; - remap->Palette[j] = GPalette.BaseColors[identity[j]]; - remap->Palette[j].a = 0xff; - } - } - } - ActiveColors = TotalColors; -} - //========================================================================== // // FFont :: FixXMoves @@ -1997,7 +1823,8 @@ void V_InitCustomFonts() } if (count > 0) { - FFont *fnt = new FSpecialFont (namebuffer, first, count, &lumplist[first], notranslate, llump, donttranslate); + FFont *CreateSpecialFont (const char *name, int first, int count, FTexture **lumplist, const bool *notranslate, int lump, bool donttranslate); + FFont *fnt = CreateSpecialFont (namebuffer, first, count, &lumplist[first], notranslate, llump, donttranslate); fnt->SetCursor(cursor); } } diff --git a/src/gamedata/textures/textures.h b/src/gamedata/textures/textures.h index 42affa036..7ecb80621 100644 --- a/src/gamedata/textures/textures.h +++ b/src/gamedata/textures/textures.h @@ -300,7 +300,6 @@ class FTexture friend class FBrightmapTexture; friend class FFont; friend class FSpecialFont; - friend void RecordTextureColors (FTexture *pic, uint8_t *usedcolors); public: