From 03c50512124cdf8e1a3b1a08b5872f3f1832baa5 Mon Sep 17 00:00:00 2001 From: Kapitan Date: Wed, 15 Dec 2021 18:55:13 +0100 Subject: [PATCH] Zadanie 2 - fix text, change bindings --- 2dgk_7/2dgk_7/KFont.cpp | 34 ++++++++++++++++++++++++++++++++++ 2dgk_7/2dgk_7/KFont.h | 1 + 2dgk_7/2dgk_7/KGame.cpp | 6 +++--- 2dgk_7/2dgk_7/config.json | 8 ++++---- 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/2dgk_7/2dgk_7/KFont.cpp b/2dgk_7/2dgk_7/KFont.cpp index aa92608..520fede 100644 --- a/2dgk_7/2dgk_7/KFont.cpp +++ b/2dgk_7/2dgk_7/KFont.cpp @@ -60,4 +60,38 @@ namespace KapitanGame } return texture; } + + KTexture KFont::GetTextWithOutlineTexture(const std::string& text, const SDL_Color fgColor, const SDL_Color bgColor, + const int outlineSize, SDL_Renderer* renderer) const + { + KTexture texture; + const int originalOutlineSize = TTF_GetFontOutline(Font); + + if (SDL_Surface* fgSurface = TTF_RenderText_Blended(Font, text.c_str(), fgColor); fgSurface == nullptr) + { + printf("Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError()); + } + else + { + TTF_SetFontOutline(Font, outlineSize); + SDL_Surface* bgSurface = TTF_RenderText_Blended(Font, text.c_str(), bgColor); + TTF_SetFontOutline(Font, originalOutlineSize); + if (bgSurface == nullptr) + { + printf("Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError()); + } + else + { + SDL_Rect rect = { outlineSize, outlineSize, fgSurface->w, fgSurface->h }; + + /* blit text onto its outline */ + SDL_SetSurfaceBlendMode(fgSurface, SDL_BLENDMODE_BLEND); + SDL_BlitSurface(fgSurface, nullptr, bgSurface, &rect); + texture.LoadFromSurface(bgSurface, renderer); + SDL_FreeSurface(bgSurface); + } + SDL_FreeSurface(fgSurface); + } + return texture; + } } diff --git a/2dgk_7/2dgk_7/KFont.h b/2dgk_7/2dgk_7/KFont.h index 05aaa20..20e8e92 100644 --- a/2dgk_7/2dgk_7/KFont.h +++ b/2dgk_7/2dgk_7/KFont.h @@ -26,6 +26,7 @@ namespace KapitanGame { void Free(); KTexture GetTextTexture(const std::string& text, SDL_Color textColor, SDL_Renderer* renderer) const; + KTexture GetTextWithOutlineTexture(const std::string& text, SDL_Color fgColor, SDL_Color bgColor, int outlineSize, SDL_Renderer* renderer) const; private: //The actual hardware font diff --git a/2dgk_7/2dgk_7/KGame.cpp b/2dgk_7/2dgk_7/KGame.cpp index 2ed17d7..77ea30e 100644 --- a/2dgk_7/2dgk_7/KGame.cpp +++ b/2dgk_7/2dgk_7/KGame.cpp @@ -89,7 +89,7 @@ namespace KapitanGame { pc->UnPossess(); } Textures.erase("Text_Score"); - Textures.emplace("Text_Score", Fonts["Roboto-Thin"].GetTextTexture(Utils::StringFormat("%d:%d", Scores[0], Scores[1]), { 0,0,0,0xFF }, Renderer)); + Textures.emplace("Text_Score", Fonts["Roboto-Thin"].GetTextWithOutlineTexture(Utils::StringFormat("%d:%d", Scores[0], Scores[1]), { 0xFF,0xFF,0xFF,0xFF }, { 0,0,0,0xFF }, 1, Renderer)); if (++LvlCounter > 3) { int max = Scores[0]; @@ -236,7 +236,7 @@ namespace KapitanGame { Fonts.erase("Roboto-Thin"); success = false; } - Textures.emplace("Text_Score", Fonts["Roboto-Thin"].GetTextTexture("0:0", { 0,0,0,0xFF }, Renderer)); + Textures.emplace("Text_Score", Fonts["Roboto-Thin"].GetTextWithOutlineTexture("0:0", { 0xFF,0xFF,0xFF,0xFF }, { 0,0,0,0xFF }, 1, Renderer)); Textures.emplace("Text_Winner", KTexture()); std::ifstream configFile("config.json"); @@ -364,7 +364,7 @@ namespace KapitanGame { Scores[static_cast(i)] = 0; } Textures.erase("Text_Score"); - Textures.emplace("Text_Score", Fonts["Roboto-Thin"].GetTextTexture(Utils::StringFormat("%d:%d", Scores[0], Scores[1]), { 0,0,0,0xFF }, Renderer)); + Textures.emplace("Text_Score", Fonts["Roboto-Thin"].GetTextWithOutlineTexture(Utils::StringFormat("%d:%d", Scores[0], Scores[1]), { 0xFF,0xFF,0xFF,0xFF }, { 0,0,0,0xFF }, 1, Renderer)); ShowWinner = false; } Playing = true; diff --git a/2dgk_7/2dgk_7/config.json b/2dgk_7/2dgk_7/config.json index b6ff979..44763f4 100644 --- a/2dgk_7/2dgk_7/config.json +++ b/2dgk_7/2dgk_7/config.json @@ -40,22 +40,22 @@ { "AxisName": "MoveY", "Scale": -1.000000, - "Key": "I" + "Key": "Up" }, { "AxisName": "MoveY", "Scale": 1.000000, - "Key": "K" + "Key": "Down" }, { "AxisName": "MoveX", "Scale": -1.000000, - "Key": "J" + "Key": "Left" }, { "AxisName": "MoveX", "Scale": 1.000000, - "Key": "L" + "Key": "Right" }, { "AxisName": "MoveY",