summary refs log tree commit diff stats
path: root/src/menu.cpp
diff options
context:
space:
mode:
authorStar Rauchenberger <fefferburbia@gmail.com>2022-03-19 16:46:19 -0400
committerStar Rauchenberger <fefferburbia@gmail.com>2022-03-19 16:46:19 -0400
commitb2f0190f6b2a227a21dd4909476171f7cc371a2d (patch)
tree0b12cdc515198a6daf5d0a39ed40f581e410cfba /src/menu.cpp
parent81e8c1dae2e7e4be632d6e81d765b5dd43ea3927 (diff)
downloadether-b2f0190f6b2a227a21dd4909476171f7cc371a2d.tar.gz
ether-b2f0190f6b2a227a21dd4909476171f7cc371a2d.tar.bz2
ether-b2f0190f6b2a227a21dd4909476171f7cc371a2d.zip
menu!
Diffstat (limited to 'src/menu.cpp')
-rw-r--r--src/menu.cpp109
1 files changed, 109 insertions, 0 deletions
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 @@
1#include "menu.h"
2#include "game.h"
3#include "renderer.h"
4
5Menu::Menu() {
6 items.push_back({.text = "New Game", .activationFunction = [this] (Game& game) {
7 close();
8 game.losing = LoseState::PoppingLamps;
9 game.muxer.stopMusic();
10 }});
11 items.push_back({.text = "Toggle Fullscreen", .activationFunction = [] (Game& game) {
12 game.renderer.toggleFullscreen();
13 }});
14 items.push_back({.text = "Quit", .activationFunction = [this] (Game& game) {
15 close();
16 game.losing = LoseState::PoppingLamps;
17 game.quitting = true;
18 game.muxer.stopMusic();
19 }});
20}
21
22void Menu::open(Game& game) {
23 menuState = MenuState::Opening;
24 menuDisplayProgress.start(200);
25 game.muxer.playSound("openmenu");
26}
27
28void Menu::close() {
29 menuState = MenuState::Closing;
30 menuDisplayProgress.start(200);
31}
32
33void Menu::update(size_t dt, Game& game) {
34 if (menuState != MenuState::Open) {
35 SDL_Event e;
36
37 while (SDL_PollEvent(&e)) {
38 if (e.type == SDL_QUIT) {
39 game.quit = true;
40 }
41 }
42 }
43
44 switch (menuState) {
45 case MenuState::Closed: {
46 // Shouldn't happen.
47 break;
48 }
49 case MenuState::Opening: {
50 menuDisplayProgress.tick(dt);
51 if (menuDisplayProgress.isComplete()) {
52 menuState = MenuState::Open;
53 }
54
55 break;
56 }
57 case MenuState::Closing: {
58 menuDisplayProgress.tick(dt);
59 if (menuDisplayProgress.isComplete()) {
60 menuState = MenuState::Closed;
61 }
62
63 break;
64 }
65 case MenuState::Open: {
66 SDL_Event e;
67
68 while (SDL_PollEvent(&e)) {
69 if (e.type == SDL_QUIT) {
70 game.quit = true;
71 } else if (e.type == SDL_KEYDOWN) {
72 switch (e.key.keysym.sym)
73 {
74 case SDLK_ESCAPE: {
75 close();
76 game.muxer.playSound("closemenu");
77 break;
78 }
79 case SDLK_SPACE: {
80 items[cursor].activationFunction(game);
81 game.muxer.playSound("menuselect");
82 break;
83 }
84 case SDLK_UP:
85 case SDLK_w: {
86 cursor--;
87 if (cursor < 0) {
88 cursor = items.size() - 1;
89 }
90 game.muxer.playSound("menucursor");
91 break;
92 }
93 case SDLK_DOWN:
94 case SDLK_s: {
95 cursor++;
96 if (cursor >= items.size()) {
97 cursor = 0;
98 }
99 game.muxer.playSound("menucursor");
100 break;
101 }
102 }
103 }
104 }
105
106 break;
107 }
108 }
109}