From f7875a6d2f129af47a203a5a31c2785b6e2fcf58 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Fri, 5 Feb 2021 13:32:04 -0500 Subject: Added speaker header --- src/input_system.cpp | 2 +- src/message_system.cpp | 5 +++-- src/message_system.h | 8 +++++++- src/renderer.cpp | 30 +++++++++++++++++++++++++++--- src/renderer.h | 15 +++++++++++---- 5 files changed, 49 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/input_system.cpp b/src/input_system.cpp index a0acc5e..e8b2bb9 100644 --- a/src/input_system.cpp +++ b/src/input_system.cpp @@ -33,7 +33,7 @@ void InputSystem::tick(double dt) { game_.getSystem().displayCutsceneBars(); }*/ //game_.getSystem().displayMessage("Some people always try to avoid fighting when there are enemies around. You know the type, right? They use the dash ability to zoom right by. I guess you could say they're followers of \"peace at any price\".", SpeakerType::Woman); - game_.getSystem().displayMessage("Go to sleep.\nIn the absolute darkness.\nDon't do aaaanything.\nDon't see anyone.\nJust sleep.\nIt'll be oh, so much fun......!\nOhohohoho!", SpeakerType::Woman); + game_.getSystem().displayMessage("Lucas. You're awful at hide-and-seek, you know that? Try harder.", "Kumatora", SpeakerType::Woman); } else if (e.key.keysym.sym == SDLK_b) { // TODO: Remove this, it's just for testing. game_.getSystem().advanceText(); diff --git a/src/message_system.cpp b/src/message_system.cpp index 5abb4b3..08ef799 100644 --- a/src/message_system.cpp +++ b/src/message_system.cpp @@ -76,12 +76,13 @@ void MessageSystem::hideCutsceneBars() { barsState_ = BarsState::Closing; } -void MessageSystem::displayMessage(std::string_view msg, SpeakerType speaker) { +void MessageSystem::displayMessage(std::string_view msg, std::string speakerName, SpeakerType speakerType) { if (!(barsState_ == BarsState::Opening || barsState_ == BarsState::Open)) { displayCutsceneBars(); } - speaker_ = speaker; + speaker_ = speakerType; + speakerName_ = speakerName; auto lineChunks = splitStr>(std::string(msg), "\n"); for (const std::string& text : lineChunks) { diff --git a/src/message_system.h b/src/message_system.h index 153805a..18dd299 100644 --- a/src/message_system.h +++ b/src/message_system.h @@ -33,7 +33,10 @@ public: void hideCutsceneBars(); - void displayMessage(std::string_view msg, SpeakerType speaker); + void displayMessage( + std::string_view msg, + std::string speakerName = "", + SpeakerType speakerType = SpeakerType::None); void advanceText(); @@ -48,6 +51,8 @@ public: const std::list& getLines() const { return linesToShow_; } + const std::string& getSpeaker() const { return speakerName_; } + private: enum class BarsState { @@ -62,6 +67,7 @@ private: double accum_ = 0.0; double length_ = 1000.0/8; SpeakerType speaker_ = SpeakerType::None; + std::string speakerName_; std::list lines_; std::list linesToShow_; Timer textAdvTimer_ { 10 }; diff --git a/src/renderer.cpp b/src/renderer.cpp index b22c316..ae18851 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -157,10 +157,35 @@ void Renderer::render(Game& game) { if (game.getSystem().getCutsceneBarsProgress() == 1.0 && !game.getSystem().getLines().empty()) { + if (!game.getSystem().getSpeaker().empty()) { + if (speakerHeaderTex_ == -1) { + speakerHeaderTex_ = loadImageFromFile("../res/speaker_header.png"); + } + + { + SDL_Rect destRect { 0, 111, 57, 13 }; + SDL_RenderCopy(ren_.get(), textures_.at(speakerHeaderTex_).get(), nullptr, &destRect); + } + + if (speakerHeaderLine_.line != game.getSystem().getSpeaker()) { + renderMessageLine(speakerHeaderLine_, game.getSystem().getSpeaker(), game); + } + + { + SDL_Rect destRect { + 4, + 112, + MESSAGE_TEXT_WIDTH, + game.getFont().getCharacterHeight() }; + + SDL_RenderCopy(ren_.get(), speakerHeaderLine_.renderedTex.get(), nullptr, &destRect); + } + } + int lineIndex = 0; for (const MessageSystem::MessageLine& line : game.getSystem().getLines()) { if (messageLines_[lineIndex].line != line.text) { - renderMessageLine(lineIndex, line.text, game); + renderMessageLine(messageLines_[lineIndex], line.text, game); } // 18x, 127y1, 143y2 @@ -204,8 +229,7 @@ int Renderer::loadImageFromFile(std::string_view filename) { return texId; } -void Renderer::renderMessageLine(int lineIndex, const std::string& text, Game& game) { - MessageCache& line = messageLines_[lineIndex]; +void Renderer::renderMessageLine(MessageCache& line, const std::string& text, Game& game) { line.line = text; line.renderedTex.reset( diff --git a/src/renderer.h b/src/renderer.h index fd6f707..4195d11 100644 --- a/src/renderer.h +++ b/src/renderer.h @@ -123,26 +123,33 @@ public: private: - texture_ptr renderMapLayer(const Map& map, int layer); - - void renderMessageLine(int lineIndex, const std::string& text, Game& game); - + // Important wrappers sdl_wrapper sdl_; img_wrapper img_; window_ptr win_; renderer_ptr ren_; + // Textures loaded from files std::vector textures_; + + // Map rendering + texture_ptr renderMapLayer(const Map& map, int layer); + texture_ptr renLay0_; texture_ptr renLay1_; + // Text rendering struct MessageCache { texture_ptr renderedTex; std::vector charIndexToWidth; std::string line; }; + void renderMessageLine(MessageCache& line, const std::string& text, Game& game); + MessageCache messageLines_[2]; + int speakerHeaderTex_ = -1; + MessageCache speakerHeaderLine_; }; #endif /* end of include guard: RENDERER_H_6A58EC30 */ -- cgit 1.4.1