#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; } } }