From 72709189edf800d52fe29eea59880bce28becc82 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sat, 13 Mar 2021 08:42:18 -0500 Subject: Rendered some menu options #7 --- CMakeLists.txt | 1 + src/menu.cpp | 12 ++++++++++++ src/menu.h | 37 +++++++++++++++++++++++++++++++++++++ src/menu_system.cpp | 6 ++++++ src/menu_system.h | 7 ++++++- src/renderer.cpp | 26 ++++++++++++++++++++++++++ 6 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/menu.cpp create mode 100644 src/menu.h diff --git a/CMakeLists.txt b/CMakeLists.txt index c8a1753..7e5c0ca 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,6 +47,7 @@ add_executable(tanetane src/map.cpp src/font.cpp src/interpolation.cpp + src/menu.cpp src/transform_system.cpp src/camera_system.cpp src/animation_system.cpp diff --git a/src/menu.cpp b/src/menu.cpp new file mode 100644 index 0000000..4da4574 --- /dev/null +++ b/src/menu.cpp @@ -0,0 +1,12 @@ +#include "menu.h" + +std::vector CreateMenu(const std::vector& builders) { + std::vector result; + result.reserve(builders.size()); + + for (const MenuBuilder& builder : builders) { + result.push_back(builder.Build()); + } + + return result; +} diff --git a/src/menu.h b/src/menu.h new file mode 100644 index 0000000..1b91b88 --- /dev/null +++ b/src/menu.h @@ -0,0 +1,37 @@ +#ifndef MENU_H_3F6E62B3 +#define MENU_H_3F6E62B3 + +#include +#include +#include + +enum class MenuType { + Command +}; + +struct MenuItem { + MenuType type = MenuType::Command; + std::string text; +}; + +class MenuBuilder { +public: + + MenuBuilder& Command(std::string text) { + result_.type = MenuType::Command; + result_.text = std::move(text); + return *this; + } + + MenuItem Build() const { + return result_; + } + +private: + + MenuItem result_; +}; + +std::vector CreateMenu(const std::vector& builders); + +#endif /* end of include guard: MENU_H_3F6E62B3 */ diff --git a/src/menu_system.cpp b/src/menu_system.cpp index 7c7d162..00c4849 100644 --- a/src/menu_system.cpp +++ b/src/menu_system.cpp @@ -20,6 +20,12 @@ void MenuSystem::openPauseMenu() { openState_ = OpenState::Animating; game_.pauseGameplay(); + + menu_ = CreateMenu({ + MenuBuilder().Command("Settings"), + MenuBuilder().Command("Return to Main Menu"), + MenuBuilder().Command("Resume Game") + }); } void MenuSystem::closePauseMenu() { diff --git a/src/menu_system.h b/src/menu_system.h index ded57b5..56f84d5 100644 --- a/src/menu_system.h +++ b/src/menu_system.h @@ -1,8 +1,10 @@ #ifndef MENU_SYSTEM_H_205861EC #define MENU_SYSTEM_H_205861EC -#include "system.h" +#include #include "interpolation.h" +#include "menu.h" +#include "system.h" class Game; @@ -27,6 +29,8 @@ public: bool isMenuOpen() const { return openState_ == OpenState::Open; } + const std::vector& getMenu() const { return menu_; } + private: enum class OpenState { @@ -38,6 +42,7 @@ private: Game& game_; Interpolation pauseAnimation_; OpenState openState_ = OpenState::Closed; + std::vector menu_; }; #endif /* end of include guard: MENU_SYSTEM_H_205861EC */ diff --git a/src/renderer.cpp b/src/renderer.cpp index 314dd4f..81e6b9c 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -500,6 +500,32 @@ void Renderer::render(Game& game) { SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE); SDL_SetRenderDrawColor(ren_.get(), 0, 0, 0, 255); SDL_RenderFillRect(ren_.get(), nullptr); + + const int lineHeight = 16; + int totalHeight = menus.getMenu().size() * lineHeight; + + int index = 0; + for (const MenuItem& menuItem : menus.getMenu()) { + switch (menuItem.type) { + case MenuType::Command: { + MessageCache output; + renderMessageLine(output, menuItem.text, game); + + SDL_Rect dest { + (CANVAS_WIDTH - output.charIndexToWidth.back()) / 2, + (CANVAS_HEIGHT - totalHeight) / 2 + lineHeight * index, + MESSAGE_TEXT_WIDTH, + game.getFont().getCharacterHeight() + }; + + SDL_SetRenderTarget(ren_.get(), cameraTex.get()); + SDL_RenderCopy(ren_.get(), output.renderedTex.get(), nullptr, &dest); + + break; + } + } + index++; + } } else if (menus.getPauseAnimationProgress() > 0.0) { SDL_SetRenderTarget(ren_.get(), cameraTex.get()); SDL_SetRenderDrawBlendMode(ren_.get(), SDL_BLENDMODE_NONE); -- cgit 1.4.1