diff options
Diffstat (limited to 'src/menu.cpp')
-rw-r--r-- | src/menu.cpp | 109 |
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 | |||
5 | Menu::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 | |||
22 | void Menu::open(Game& game) { | ||
23 | menuState = MenuState::Opening; | ||
24 | menuDisplayProgress.start(200); | ||
25 | game.muxer.playSound("openmenu"); | ||
26 | } | ||
27 | |||
28 | void Menu::close() { | ||
29 | menuState = MenuState::Closing; | ||
30 | menuDisplayProgress.start(200); | ||
31 | } | ||
32 | |||
33 | void 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 | } | ||