From b2f0190f6b2a227a21dd4909476171f7cc371a2d Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Sat, 19 Mar 2022 16:46:19 -0400 Subject: menu! --- src/menu.cpp | 109 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 109 insertions(+) create mode 100644 src/menu.cpp (limited to 'src/menu.cpp') diff --git a/src/menu.cpp b/src/menu.cpp new file mode 100644 index 0000000..b9a3ace --- /dev/null +++ b/src/menu.cpp @@ -0,0 +1,109 @@ +#include "menu.h" +#include "game.h" +#include "renderer.h" + +Menu::Menu() { + items.push_back({.text = "New Game", .activationFunction = [this] (Game& game) { + close(); + game.losing = LoseState::PoppingLamps; + game.muxer.stopMusic(); + }}); + items.push_back({.text = "Toggle Fullscreen", .activationFunction = [] (Game& game) { + game.renderer.toggleFullscreen(); + }}); + items.push_back({.text = "Quit", .activationFunction = [this] (Game& game) { + close(); + game.losing = LoseState::PoppingLamps; + game.quitting = true; + game.muxer.stopMusic(); + }}); +} + +void Menu::open(Game& game) { + menuState = MenuState::Opening; + menuDisplayProgress.start(200); + game.muxer.playSound("openmenu"); +} + +void Menu::close() { + menuState = MenuState::Closing; + menuDisplayProgress.start(200); +} + +void Menu::update(size_t dt, Game& game) { + if (menuState != MenuState::Open) { + SDL_Event e; + + while (SDL_PollEvent(&e)) { + if (e.type == SDL_QUIT) { + game.quit = true; + } + } + } + + switch (menuState) { + case MenuState::Closed: { + // Shouldn't happen. + break; + } + case MenuState::Opening: { + menuDisplayProgress.tick(dt); + if (menuDisplayProgress.isComplete()) { + menuState = MenuState::Open; + } + + break; + } + case MenuState::Closing: { + menuDisplayProgress.tick(dt); + if (menuDisplayProgress.isComplete()) { + menuState = MenuState::Closed; + } + + break; + } + case MenuState::Open: { + SDL_Event e; + + while (SDL_PollEvent(&e)) { + if (e.type == SDL_QUIT) { + game.quit = true; + } else if (e.type == SDL_KEYDOWN) { + switch (e.key.keysym.sym) + { + case SDLK_ESCAPE: { + close(); + game.muxer.playSound("closemenu"); + break; + } + case SDLK_SPACE: { + items[cursor].activationFunction(game); + game.muxer.playSound("menuselect"); + break; + } + case SDLK_UP: + case SDLK_w: { + cursor--; + if (cursor < 0) { + cursor = items.size() - 1; + } + game.muxer.playSound("menucursor"); + break; + } + case SDLK_DOWN: + case SDLK_s: { + cursor++; + if (cursor >= items.size()) { + cursor = 0; + } + game.muxer.playSound("menucursor"); + break; + } + } + } + } + + break; + } + } +} -- cgit 1.4.1