From 2c75d95ddf849996bfc18267a9eecb4d0f4e1916 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Wed, 16 Mar 2022 15:30:37 -0400 Subject: signs can be read now! --- src/renderer.cpp | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 96 insertions(+) (limited to 'src/renderer.cpp') diff --git a/src/renderer.cpp b/src/renderer.cpp index 2be36ae..c8c1746 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -1,4 +1,5 @@ #include "renderer.h" +#include #include "game.h" Renderer::Renderer() @@ -113,6 +114,11 @@ Renderer::Renderer() loadTextureFromFile("../res/title0.png", titles_[0]); SDL_QueryTexture(titles_[0].get(), nullptr, nullptr, &titleWidths_[0], &titleHeights_[0]); + + font_ = font_ptr(TTF_OpenFont("../res/softsquare.ttf", 45)); + if (!font_) { + throw ttf_error(); + } } void Renderer::renderGame( @@ -389,6 +395,55 @@ void Renderer::renderGame( SDL_RenderCopy(ren_.get(), readInstruction_.get(), nullptr, nullptr); } + if (game.sign.signDisplayState != SignInstructionState::Hidden) { + int opacity = 255; + if (game.sign.signDisplayState == SignInstructionState::FadingIn) { + opacity = game.sign.signDisplayFade.getProgress(0, 255); + } else if (game.sign.signDisplayState == SignInstructionState::FadingOut) { + opacity = game.sign.signDisplayFade.getProgress(255, 0); + } + + SDL_Rect signRect { + 0, + GAME_HEIGHT / 2 - (45 * 2 + 1 + MESSAGE_MARGIN * 2) / 2, + GAME_WIDTH, + 45 * 2 + 1 + MESSAGE_MARGIN * 2 + }; + + SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_BLEND); + SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, opacity); + SDL_RenderFillRect(ren_.get(), &signRect); + + if (game.sign.signDisplayState == SignInstructionState::Visible) { + int lineIndex = 0; + for (const SignLine& line : game.sign.linesToShow) { + if (messageLines_[lineIndex].line != line.text) { + renderMessageLine(messageLines_[lineIndex], line.text, game); + } + + if (line.charsRevealed > 0) { + { + SDL_Rect srcRect { + 0, 0, messageLines_[lineIndex].charIndexToWidth[line.charsRevealed], + TTF_FontHeight(font_.get()) + }; + SDL_Rect destRect { + MESSAGE_MARGIN, + GAME_HEIGHT / 2 - (45 * 2 + 1 + MESSAGE_MARGIN * 2) / 2 + MESSAGE_MARGIN + (45 + 1) * lineIndex, + srcRect.w, + srcRect.h }; + + SDL_SetRenderTarget(ren_.get(), nullptr); + SDL_RenderCopy(ren_.get(), messageLines_[lineIndex].renderedTex.get(), &srcRect, &destRect); + //std::cout << line.charsRevealed << " (" << messageLines_[lineIndex].charIndexToWidth[line.charsRevealed] << "): " << messageLines_[lineIndex].line.substr(0,line.charsRevealed) << std::endl; + } + } + + lineIndex++; + } + } + } + SDL_RenderPresent(ren_.get()); } @@ -443,3 +498,44 @@ void Renderer::loadTextureFromFile(std::string_view path, texture_ptr& texture) texture = texture_ptr(SDL_CreateTextureFromSurface(ren_.get(), pfs.get())); SDL_SetTextureBlendMode(texture.get(), SDL_BLENDMODE_BLEND); } + +void Renderer::renderMessageLine(MessageCache& line, const std::string& text, const Game& game) { + line.line = text; + + line.renderedTex.reset( + SDL_CreateTexture( + ren_.get(), + SDL_PIXELFORMAT_RGBA8888, + SDL_TEXTUREACCESS_TARGET, + MESSAGE_TEXT_WIDTH, + TTF_FontHeight(font_.get()))); + + SDL_SetTextureBlendMode(line.renderedTex.get(), SDL_BLENDMODE_BLEND); + + SDL_SetRenderTarget(ren_.get(), line.renderedTex.get()); + SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_BLEND); + SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 0); + SDL_RenderClear(ren_.get()); + + line.charIndexToWidth.clear(); + line.charIndexToWidth.push_back(0); + + for (int i=0; i