forked from DKU-EmbeddedSystem-Lab/2025_DKU_OpenSourceBasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExMainoptionState.cpp
More file actions
149 lines (126 loc) · 5.53 KB
/
ExMainoptionState.cpp
File metadata and controls
149 lines (126 loc) · 5.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include "mainoptionstate.hpp"
#include "config.hpp"
#include "game.hpp"
#include "selectioninputhandler.hpp"
MainMenuOptionState::MainMenuOptionState(InputManager* inputManager)
: OptionState(inputManager) {}
MainMenuOptionState::~MainMenuOptionState() {
exit();
}
void MainMenuOptionState::initialize() {
index = 0;
for (int i = 0; i < config::possible_resolution_scalings; i++) {
if (config::resolution_scaling == config::available_resolution_scalings[i]) {
resolution_scaling_index = i;
break;
}
}
title_text = new Texture();
title_text->loadFromText("Options", Game::getInstance()->mRenderer->bigFont, config::default_text_color);
#if defined(WIN32)
OKButton = new Button("../../assets/button-ok.png", [](){
Game::getInstance()->popState();
}, (config::logical_window_width - 80) / 2, 280);
#else
OKButton = new Button("../assets/button-ok.png", [](){
Game::getInstance()->popState();
}, (config::logical_window_width - 80) / 2, 280);
#endif
mButtons.clear();
addButton(new Button("Resolution", nullptr, 50, 100, 200, 40));
addButton(new Button("Ghost Block", nullptr, 50, 180, 200, 40));
addButton(OKButton);
resolution_setting_text = new Texture();
resolution_text = new Texture();
ghost_block_setting_text = new Texture();
left_arrow = new Texture();
right_arrow = new Texture();
texture_on_on = new Texture();
texture_on_off = new Texture();
texture_off_on = new Texture();
texture_off_off = new Texture();
#if defined(WIN32)
left_arrow->loadFromImage("../../assets/arrow-left.png");
right_arrow->loadFromImage("../../assets/arrow-right.png");
texture_on_on->loadFromImage("../../assets/button-on-on.png");
texture_on_off->loadFromImage("../../assets/button-on-off.png");
texture_off_on->loadFromImage("../../assets/button-off-on.png");
texture_off_off->loadFromImage("../../assets/button-off-off.png");
#else
left_arrow->loadFromImage("../assets/arrow-left.png");
right_arrow->loadFromImage("../assets/arrow-right.png");
texture_on_on->loadFromImage("../assets/button-on-on.png");
texture_on_off->loadFromImage("../assets/button-on-off.png");
texture_off_on->loadFromImage("../assets/button-off-on.png");
texture_off_off->loadFromImage("../assets/button-off-off.png");
#endif
}
void MainMenuOptionState::update() {
SelectionInputHandler::handle(mInputManager, mButtons, index, nextStateID);
while (mInputManager->pollAction()) {
switch (mInputManager->getAction()) {
case Action::move_left:
if (index == 0) changeResolution(SettingChange::left);
else if (index == 1) changeGhostBlock(SettingChange::left);
break;
case Action::move_right:
if (index == 0) changeResolution(SettingChange::right);
else if (index == 1) changeGhostBlock(SettingChange::right);
break;
default:
break;
}
}
}
void MainMenuOptionState::exit() {
delete title_text;
delete resolution_text;
delete resolution_setting_text;
delete ghost_block_setting_text;
delete texture_on_on;
delete texture_on_off;
delete texture_off_on;
delete texture_off_off;
delete left_arrow;
delete right_arrow;
delete OKButton;
for (auto btn : mButtons) delete btn;
mButtons.clear();
}
void MainMenuOptionState::drawOptions() {
title_text->renderCentered(config::logical_window_width / 2, 50);
resolution_setting_text->loadFromText("Resolution", Game::getInstance()->mRenderer->mediumFont, config::default_text_color);
resolution_setting_text->render(50, 100);
ghost_block_setting_text->loadFromText("Ghost Block", Game::getInstance()->mRenderer->mediumFont, config::default_text_color);
ghost_block_setting_text->render(50, 180);
std::string resolution_string = std::to_string(int(config::logical_window_width * config::resolution_scaling)) + " x " +
std::to_string(int(config::logical_window_height * config::resolution_scaling));
resolution_text->loadFromText(resolution_string, Game::getInstance()->mRenderer->mediumFont, config::default_text_color);
resolution_text->render(400 + (200 - resolution_text->getWidth()) / 2, 101);
left_arrow->render(383, 105);
right_arrow->render(590, 105);
if (config::ghost_piece_enabled) {
texture_off_off->render(400, 188);
texture_on_on->render(510, 188);
} else {
texture_off_on->render(400, 188);
texture_on_off->render(510, 188);
}
}
void MainMenuOptionState::changeResolution(SettingChange s) {
if (s == SettingChange::left && resolution_scaling_index > 0)
--resolution_scaling_index;
else if (s == SettingChange::right && resolution_scaling_index < config::possible_resolution_scalings - 1)
++resolution_scaling_index;
config::resolution_scaling = config::available_resolution_scalings[resolution_scaling_index];
SDL_SetWindowSize(Game::getInstance()->mWindow,
config::logical_window_width * config::resolution_scaling,
config::logical_window_height * config::resolution_scaling);
SDL_SetWindowPosition(Game::getInstance()->mWindow, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
}
void MainMenuOptionState::changeGhostBlock(SettingChange s) {
if ((s == SettingChange::left && config::ghost_piece_enabled) ||
(s == SettingChange::right && !config::ghost_piece_enabled)) {
config::ghost_piece_enabled = !config::ghost_piece_enabled;
}
}