-
Notifications
You must be signed in to change notification settings - Fork 0
/
OptionsVideoState.cpp
215 lines (175 loc) · 5.18 KB
/
OptionsVideoState.cpp
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include "OptionsVideoState.h"
OptionsVideoState::OptionsVideoState()
{
stateName = "OptionsVideo";
}
void OptionsVideoState::Enter()
{
stateManager = this->GetStateManager();
resManager = stateManager->GetResourceManager();
screen = stateManager->GetGpuTarget();
settings = stateManager->GetSettings();
//
// Load resources
//
// Font
fontSharedBig = resManager->GetSharedFontResourceIdByName("Data\\Fonts\\OpenSans-Regular-64.fnt");
// Button
buttonSave = new Button(stateManager, fontSharedBig, "Save", 500.0f, 700.0f, true);
buttonBack = new Button(stateManager, fontSharedBig, "Back", 500.0f, 800.0f, true);
// CheckBox
checkBoxFullscreen = new CheckBox(stateManager, fontSharedBig, "Fullscreen", 500.0f, 400.0f, true);
checkBoxFullscreen->SetOn(settings->GetFullscreen());
checkBoxVSync = new CheckBox(stateManager, fontSharedBig, "Vertical Sync", 500.0f, 500.0f, true);
checkBoxVSync->SetOn(settings->GetVSync());
checkBoxFastGPU = new CheckBox(stateManager, fontSharedBig, "Fast GPU", 500.0f, 600.0f, true);
checkBoxFastGPU->SetOn(settings->GetFastGpu());
// DropList
dropListResolution = new DropList(stateManager, fontSharedBig, "Resolution", 500.0f, 300.0f, true);
//
// Inputs
//
keyEsc = false;
// Menu
needRestartApplication = false;
// Fill data items into drop list
dropListResolution->AddItem("1024x768");
dropListResolution->AddItem("1280x720");
dropListResolution->AddItem("1280x800");
dropListResolution->AddItem("1280x1024");
dropListResolution->AddItem("1360x768");
dropListResolution->AddItem("1366x768");
dropListResolution->AddItem("1440x900");
dropListResolution->AddItem("1600x900");
dropListResolution->AddItem("1680x1050");
dropListResolution->AddItem("1920x1080");
dropListResolution->AddItem("1920x1200");
dropListResolution->SetItemSelectedByText(std::to_string(settings->GetWindowWidth()) + "x" + std::to_string(settings->GetWindowHeight()));
}
void OptionsVideoState::Exit()
{
resManager->FreeResources();
fontSharedBig = 0;
if (checkBoxFullscreen)
delete checkBoxFullscreen;
checkBoxFullscreen = nullptr;
if (checkBoxVSync)
delete checkBoxVSync;
checkBoxVSync = nullptr;
if (checkBoxFastGPU)
delete checkBoxFastGPU;
checkBoxFastGPU = nullptr;
if (buttonSave)
delete buttonSave;
buttonSave = nullptr;
if (buttonBack)
delete buttonBack;
buttonBack = nullptr;
if (dropListResolution)
delete dropListResolution;
dropListResolution = nullptr;
resManager = nullptr;
stateManager = nullptr;
settings = nullptr;
screen = nullptr;
}
GotoState OptionsVideoState::Update()
{
// **** INPUT ****
while (SDL_PollEvent(&e)) // e for event
{
if (e.type == SDL_QUIT)
{
return ToState::Quit;
}
else if (e.type == SDL_KEYDOWN)
{
switch (e.key.keysym.sym)
{
case SDLK_ESCAPE:
if (!dropListResolution->EscIsPressed())
return ToState::Options;
}
}
else if (e.type == SDL_KEYUP)
{
switch (e.key.keysym.sym)
{
case SDLK_ESCAPE:
keyEsc = false;
break;
}
}
// Check "Resolution"
dropListResolution->Event(e);
if (dropListResolution->GetClick())
{
dropListResolution->SetClick(false);
// Obtain wanted resolution from the drop list.
std::string tItem = dropListResolution->GetItemSelected();
settings->SetWindowWidth(std::stoi(tItem.substr(0, tItem.find_first_of('x'))));
settings->SetWindowHeight(std::stoi(tItem.substr(tItem.find_first_of('x')+1)));
}
// Check "Fullscreen"
checkBoxFullscreen->Event(e);
if (checkBoxFullscreen->GetClick())
{
checkBoxFullscreen->SetClick(false);
settings->SetFullscreen(!settings->GetFullscreen());
}
// Check "VSync"
checkBoxVSync->Event(e);
if (checkBoxVSync->GetClick())
{
checkBoxVSync->SetClick(false);
settings->SetVSync(!settings->GetVSync());
}
// Check "FastGPU"
checkBoxFastGPU->Event(e);
if (checkBoxFastGPU->GetClick())
{
checkBoxFastGPU->SetClick(false);
settings->SetFastGpu(!settings->GetFastGpu());
}
// Check "Save"
buttonSave->Event(e);
if (buttonSave->GetClick())
{
buttonSave->SetClick(false);
// Write to file
settings->Save();
needRestartApplication = true;
}
// Check "Back"
buttonBack->Event(e);
if (buttonBack->GetClick())
{
buttonBack->SetClick(false);
return ToState::Options;
}
}
// **** RENDER ****
// Clear the screen
GPU_Clear(screen);
//
resManager->GetSharedFontResource(fontSharedBig)->SetColor(160, 160, 160, 255);
resManager->GetSharedFontResource(fontSharedBig)->DrawText("VIDEO OPTIONS", 500.0f, 200.0f);
//
if (needRestartApplication)
{
resManager->GetSharedFontResource(fontSharedBig)->SetColor(160, 160, 160, 255);
resManager->GetSharedFontResource(fontSharedBig)->DrawText("Need to restart to change resolution", 100.0f, 100.0f);
}
// Button
buttonSave->Draw();
buttonBack->Draw();
// CheckBox
checkBoxFullscreen->Draw();
checkBoxVSync->Draw();
checkBoxFastGPU->Draw();
// DropList
dropListResolution->Draw();
// Show the result in the window
GPU_Flip(screen);
return ToState::Run;
}