From df1d9771aac6442da0dde55939211bbb6d6f740e Mon Sep 17 00:00:00 2001 From: sigonasr2 Date: Fri, 8 Dec 2023 15:50:40 -0600 Subject: [PATCH] Implemented hex color font coloring. --- Crawler/State_GameRun.cpp | 22 +++++- Crawler/State_GameRun.h | 1 + Crawler/Version.h | 2 +- Crawler/olcPGEX_TTF.h | 113 ++++++++++------------------ Crawler/olcPixelGameEngine.h | 142 +++++++++++++++++++++++++++++++++-- 5 files changed, 193 insertions(+), 87 deletions(-) diff --git a/Crawler/State_GameRun.cpp b/Crawler/State_GameRun.cpp index 085354f6..c5d6171f 100644 --- a/Crawler/State_GameRun.cpp +++ b/Crawler/State_GameRun.cpp @@ -50,7 +50,7 @@ void State_GameRun::OnStateChange(GameState*prevState){ Menu::CloseAllMenus(); } game->GetPlayer()->SetState(State::NORMAL); -}; +} void State_GameRun::OnUserUpdate(Crawler*game){ game->bossDisplayTimer=std::max(0.f,game->bossDisplayTimer-game->GetElapsedTime()); if(game->encounterStarted&&game->totalBossEncounterMobs>0){ @@ -72,7 +72,23 @@ void State_GameRun::OnUserUpdate(Crawler*game){ ItemDrop::UpdateDrops(game->GetElapsedTime()); game->UpdateBullets(game->GetElapsedTime()); game->UpdateCamera(game->GetElapsedTime()); -}; +} void State_GameRun::Draw(Crawler*game){ game->RenderHud(); -}; \ No newline at end of file + //FontTest(); //Enable to test font coloring. +} + +void State_GameRun::FontTest(){ + game->DrawStringDecal({8,8},"#000000This is a #000066test of hex"); + game->DrawStringDecal({8,16},"#FF0000This is a #BFA6F9test of hex"); + game->DrawStringDecal({8,24},"#00FF00This is a #E9A6F9test of hex"); + game->DrawStringDecal({8,32},"#0000FFThis is a #F9E8A6test of hex"); + game->DrawShadowStringDecal({8,40},"#000000This is a #000066test of hex"); + game->DrawShadowStringDecal({8,48},"#FF0000This is a #BFA6F9test of hex"); + game->DrawShadowStringDecal({8,56},"#00FF00This is a #E9A6F9test of hex"); + game->DrawShadowStringDecal({8,64},"#0000FFThis is a #F9E8A6test of hex"); + game->DrawShadowStringDecal(VisualNovel::font,{8,72},U"#000000This is a #000066test of hex"); + game->DrawShadowStringDecal(VisualNovel::font,{8,80},U"#FF0000This is a #BFA6F9test of hex"); + game->DrawShadowStringDecal(VisualNovel::font,{8,88},U"#00FF00This is a #E9A6F9test of hex"); + game->DrawShadowStringDecal(VisualNovel::font,{8,96},U"#0000FFThis is a #F9E8A6test of hex"); +} \ No newline at end of file diff --git a/Crawler/State_GameRun.h b/Crawler/State_GameRun.h index 240600ea..a8931edb 100644 --- a/Crawler/State_GameRun.h +++ b/Crawler/State_GameRun.h @@ -42,4 +42,5 @@ class State_GameRun:public GameState{ virtual void OnStateChange(GameState*prevState)override final; virtual void OnUserUpdate(Crawler*game)override final; virtual void Draw(Crawler*game)override final; + void FontTest(); }; \ No newline at end of file diff --git a/Crawler/Version.h b/Crawler/Version.h index 6415be00..507f8ce6 100644 --- a/Crawler/Version.h +++ b/Crawler/Version.h @@ -39,7 +39,7 @@ All rights reserved. #define VERSION_MAJOR 0 #define VERSION_MINOR 2 #define VERSION_PATCH 1 -#define VERSION_BUILD 3570 +#define VERSION_BUILD 3591 #define stringify(a) stringify_(a) #define stringify_(a) #a diff --git a/Crawler/olcPGEX_TTF.h b/Crawler/olcPGEX_TTF.h index b92deebb..b752f431 100644 --- a/Crawler/olcPGEX_TTF.h +++ b/Crawler/olcPGEX_TTF.h @@ -95,79 +95,6 @@ namespace olc { return *this; } - void DrawString(std::u32string string, int x, int y, - olc::Pixel color = olc::BLACK, float angle = 0.0f) { - FT_Matrix rotMat; - rotMat.xx = (FT_Fixed)(std::cos(angle) * 0x10000L); - rotMat.xy = (FT_Fixed)(-std::sin(angle) * 0x10000L); - rotMat.yx = (FT_Fixed)(std::sin(angle) * 0x10000L); - rotMat.yy = (FT_Fixed)(std::cos(angle) * 0x10000L); - - FT_Vector pen; - pen.x = x * 64; - pen.y = (pge->ScreenHeight() - y) * 64; - - olc::Pixel::Mode prevMode = pge->GetPixelMode(); - pge->SetPixelMode(olc::Pixel::ALPHA); - - Font *prevToUse = nullptr; - - for (size_t i = 0; i < string.size(); i++) { - char32_t chr = string[i]; - if(chr=='\r')continue; - - Font *toUse = this; - FT_UInt chrIndex = GetCharIndex(chr); - - if (chrIndex == 0) { - for (auto &font : fallbacks) { - FT_UInt fbChr = font.GetCharIndex(chr); - if (fbChr != 0) { - chrIndex = fbChr; - toUse = &font; - } - } - } - - if (prevToUse == toUse) { - FT_Vector kern; - FT_Get_Kerning(fontFace, string[i - 1], chr, - FT_KERNING_DEFAULT, &kern); - - pen.x += kern.x; - pen.y += kern.y; - } - - FT_Set_Transform(toUse->fontFace, &rotMat, &pen); - FT_Error error = FT_Load_Char(toUse->fontFace, chr, - FT_LOAD_RENDER | FT_LOAD_COLOR); - - FT_Bitmap bmp = toUse->fontFace->glyph->bitmap; - FT_GlyphSlot slot = toUse->fontFace->glyph; - - - pen.x += toUse->fontFace->glyph->advance.x; - pen.y += toUse->fontFace->glyph->advance.y; - - if(chr=='\n'){ - int scaled_line_spacing = fontFace->size->metrics.height; - pen.y -=scaled_line_spacing; - pen.x=x*64; - } else { - DrawBitmap(slot->bitmap_left, - pge->ScreenHeight() - slot->bitmap_top, bmp, color); - } - - prevToUse = toUse; - } - - pge->SetPixelMode(prevMode); - } - - void DrawString(std::u32string string, olc::vi2d pos, - olc::Pixel color = olc::BLACK, float angle = 0.0f) { - DrawString(string, pos.x, pos.y, color, angle); - } private: FontRect _GetStringBounds(std::u32string string, float angle = 0.0f) { FT_Matrix rotMat; @@ -190,11 +117,20 @@ namespace olc { int maxY = intMin; Font *prevToUse = nullptr; - + int skip=0; for (size_t i = 0; i < string.size(); i++) { char32_t chr = string[i]; + if(skip){ + skip--; + continue; + } if(chr=='\r')continue; else if(chr<0)continue; + else if (chr=='#') + { + skip=6; + continue; + } int prevmaxX=maxX; @@ -289,9 +225,17 @@ namespace olc { olc::Font *prevToUse = nullptr; Pixel textCol=color; - + int skip=0; for (size_t i = 0; i < string.size(); i++) { char32_t chr = string[i]; + const auto hexToNumber=[](char c){ + if(c<='9')return c-'0'; + return (c-'A')+10; + }; + if(skip){ + skip--; + continue; + } if(chr=='\r')continue; else if (chr>=-128&&chr<-105) { @@ -301,6 +245,25 @@ namespace olc { else if (chr==Color::Reset[0]) { textCol=color; + continue; + } + else if (chr=='#') + { + skip=6; + textCol=BLACK; + for(int j=1;j<7;j++){ + if(j<3){ + textCol.r*=16; + textCol.r+=hexToNumber(string[i+j]); + }else + if(j<5){ + textCol.g*=16; + textCol.g+=hexToNumber(string[i+j]); + }else{ + textCol.b*=16; + textCol.b+=hexToNumber(string[i+j]); + } + } continue; } Font *toUse = this; diff --git a/Crawler/olcPixelGameEngine.h b/Crawler/olcPixelGameEngine.h index 5355a4fd..76f5df28 100644 --- a/Crawler/olcPixelGameEngine.h +++ b/Crawler/olcPixelGameEngine.h @@ -3340,8 +3340,17 @@ namespace olc { olc::vf2d spos = { 0.0f, 0.0f }; Pixel textCol=col; - for (auto c : sText) + const auto hexToNumber=[](char c){ + if(c<='9')return c-'0'; + return (c-'A')+10; + }; + for (int skip=0,index=-1;auto c : sText) { + index++; + if(skip){ + skip--; + continue; + } if(c=='\r')continue; //Ignore carriage returns. Stupid Linux. if (c == '\n') { @@ -3355,9 +3364,23 @@ namespace olc { textCol={charToColor[c].r,charToColor[c].g,charToColor[c].b,col.a}; } - else if (c==Reset[0]) + else if (c=='#') { - textCol=col; + skip=6; + textCol=BLACK; + for(int i=1;i<7;i++){ + if(i<3){ + textCol.r*=16; + textCol.r+=hexToNumber(sText[index+i]); + }else + if(i<5){ + textCol.g*=16; + textCol.g+=hexToNumber(sText[index+i]); + }else{ + textCol.b*=16; + textCol.b+=hexToNumber(sText[index+i]); + } + } } else { @@ -3373,8 +3396,17 @@ namespace olc { olc::vf2d spos = { 0.0f, 0.0f }; Pixel textCol=col; - for (auto c : sText) + const auto hexToNumber=[](char c){ + if(c<='9')return c-'0'; + return (c-'A')+10; + }; + for (int skip=0,index=-1;auto c : sText) { + index++; + if(skip){ + skip--; + continue; + } if(c=='\r')continue; //Ignore carriage returns. Stupid Linux. if (c == '\n') { @@ -3392,6 +3424,24 @@ namespace olc { textCol=col; } + else if (c=='#') + { + skip=6; + textCol=BLACK; + for(int i=1;i<7;i++){ + if(i<3){ + textCol.r*=16; + textCol.r+=hexToNumber(sText[index+i]); + }else + if(i<5){ + textCol.g*=16; + textCol.g+=hexToNumber(sText[index+i]); + }else{ + textCol.b*=16; + textCol.b+=hexToNumber(sText[index+i]); + } + } + } else { int32_t ox = (c - 32) % 16; @@ -3405,6 +3455,8 @@ namespace olc void PixelGameEngine::DrawShadowStringDecal(const olc::vf2d& pos, const std::string& sText, const Pixel col, const Pixel shadowCol, const olc::vf2d& scale,const float shadowSizeFactor){ std::string strippedText=sText; std::erase_if(strippedText,[](char chr){return chr<0;}); + int skip=0; + std::erase_if(strippedText,[&](char chr){if(skip>0){skip--;return true;}if(chr=='#'){skip=6;return true;}return false;}); for(float y=-shadowSizeFactor;y<=shadowSizeFactor+0.1;y+=shadowSizeFactor/2){ for(float x=-shadowSizeFactor;x<=shadowSizeFactor+0.1;x+=shadowSizeFactor/2){ if(x!=0||y!=0){ @@ -3494,6 +3546,8 @@ namespace olc void PixelGameEngine::DrawShadowStringPropDecal(const olc::vf2d& pos, const std::string& sText, const Pixel col, const Pixel shadowCol, const olc::vf2d& scale,const float shadowSizeFactor){ std::string strippedText=sText; std::erase_if(strippedText,[](char chr){return chr<0;}); + int skip=0; + std::erase_if(strippedText,[&](char chr){if(skip>0){skip--;return true;}if(chr=='#'){skip=6;return true;}return false;}); for(float y=-shadowSizeFactor;y<=shadowSizeFactor+0.1;y+=shadowSizeFactor/2){ for(float x=-shadowSizeFactor;x<=shadowSizeFactor+0.1;x+=shadowSizeFactor/2){ if(x!=0||y!=0){ @@ -3602,12 +3656,21 @@ namespace olc { olc::vi2d size = { 0,1 }; olc::vi2d pos = { 0,1 }; - for (auto c : s) + for (int skip=0;auto c : s) { + if(skip){ + skip--; + continue; + } if(c=='\r')continue; //Ignore carriage returns. Stupid Linux. if (c == '\n') { pos.y++; pos.x = 0; } else if (c == '\t') { pos.x += nTabSizeInSpaces; } else if(c<0)continue; + else if (c=='#') + { + skip=6; + continue; + } else pos.x++; size.x = std::max(size.x, pos.x); size.y = std::max(size.y, pos.y); @@ -3630,8 +3693,17 @@ namespace olc if (col.a != 255) SetPixelMode(Pixel::ALPHA); else SetPixelMode(Pixel::MASK); } - for (auto c : sText) + const auto hexToNumber=[](char c){ + if(c<='9')return c-'0'; + return (c-'A')+10; + }; + for (int skip=0,index=-1;auto c : sText) { + index++; + if(skip){ + skip--; + continue; + } if(c=='\r')continue; //Ignore carriage returns. Stupid Linux. if (c == '\n') { @@ -3645,6 +3717,24 @@ namespace olc { textCol={charToColor[c].r,charToColor[c].g,charToColor[c].b,col.a}; } + else if (c=='#') + { + skip=6; + textCol=BLACK; + for(int i=1;i<7;i++){ + if(i<3){ + textCol.r*=16; + textCol.r+=hexToNumber(sText[index+i]); + }else + if(i<5){ + textCol.g*=16; + textCol.g+=hexToNumber(sText[index+i]); + }else{ + textCol.b*=16; + textCol.b+=hexToNumber(sText[index+i]); + } + } + } else if (c==Reset[0]) { textCol=col; @@ -3680,12 +3770,21 @@ namespace olc { olc::vi2d size = { 0,1 }; olc::vi2d pos = { 0,1 }; - for (auto c : s) + for (int skip=0;auto c : s) { + if(skip){ + skip--; + continue; + } if(c=='\r')continue; //Ignore carriage returns. Stupid Linux. if (c == '\n') { pos.y += 1; pos.x = 0; } else if (c == '\t') { pos.x += nTabSizeInSpaces * 8; } else if(c<0)continue; + else if (c=='#') + { + skip=6; + continue; + } else pos.x += vFontSpacing[c - 32].y; size.x = std::max(size.x, pos.x); size.y = std::max(size.y, pos.y); @@ -3704,13 +3803,22 @@ namespace olc int32_t sy = 0; Pixel::Mode m = nPixelMode; Pixel textCol=col; + const auto hexToNumber=[](char c){ + if(c<='9')return c-'0'; + return (c-'A')+10; + }; if (m != Pixel::CUSTOM) { if (col.a != 255) SetPixelMode(Pixel::ALPHA); else SetPixelMode(Pixel::MASK); } - for (auto c : sText) + for (int skip=0,index=-1;auto c : sText) { + index++; + if(skip){ + skip--; + continue; + } if(c=='\r')continue; //Ignore carriage returns. Stupid Linux. if (c == '\n') { @@ -3728,6 +3836,24 @@ namespace olc { textCol=col; } + else if (c=='#') + { + skip=6; + textCol=BLACK; + for(int i=1;i<7;i++){ + if(i<3){ + textCol.r*=16; + textCol.r+=hexToNumber(sText[index+i]); + }else + if(i<5){ + textCol.g*=16; + textCol.g+=hexToNumber(sText[index+i]); + }else{ + textCol.b*=16; + textCol.b+=hexToNumber(sText[index+i]); + } + } + } else { int32_t ox = (c - 32) % 16;