forked from Vogtinator/crafti
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsettingstask.cpp
201 lines (157 loc) · 5.25 KB
/
settingstask.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
#include "settingstask.h"
#include "font.h"
#include "texturetools.h"
#include "worldtask.h"
#include "textures/selection.h"
SettingsTask settings_task;
const char *leaves_values[] = {
"Opaque",
"Transparent"
};
const char *speed_values[] = {
"Slow",
"Normal",
"Fast"
};
const char *fastmode_values[] = {
"Off",
"On"
};
const char *world_static_values[] = {
"Static (no ticks)",
"Dynamic"
};
SettingsTask::SettingsTask()
{
//Must have the same order as the "Settings" enum
settings.push_back({"Leaves", leaves_values, 2, 0, 0, 1});
settings.push_back({"Speed", speed_values, 3, 1, 0, 1});
settings.push_back({"Distance", nullptr, 10, 2, 1, 1});
settings.push_back({"Fast mode", fastmode_values, 2, 0, 0, 1});
settings.push_back({"Near plane", nullptr, 500, 256, 120, 16});
settings.push_back({"World", world_static_values, 2, 1, 0, 1});
settings.push_back({"Show FPS", fastmode_values, 2, 0, 0, 1});
background = newTexture(background_width, background_height, 0, false);
}
SettingsTask::~SettingsTask()
{
deleteTexture(background);
}
void SettingsTask::makeCurrent()
{
if(!background_saved)
saveBackground();
settings[DISTANCE].current_value = world.fieldOfView();
changed_something = false;
Task::makeCurrent();
}
void SettingsTask::render()
{
drawBackground();
const unsigned int x = (SCREEN_WIDTH - background->width) / 2;
unsigned int y = (SCREEN_HEIGHT - background->height) / 2;
drawTextureOverlay(*background, 0, 0, *screen, x, y, background->width, background->height);
drawString("Settings", 0xFFFF, *screen, x, y - fontHeight());
y += 8;
for(unsigned int i = 0; i < settings.size(); ++i)
{
SettingsEntry &e = settings[i];
if(i == current_selection)
drawTexture(selection, *screen, 0, 0, selection.width, selection.height, x + 5, y, selection.width, selection.height);
drawString(e.name, 0xFFFF, *screen, x + selection.width + 10, y);
//Print the numeric value
if(e.values == nullptr)
{
char number[10];
snprintf(number, sizeof(number), "%d", e.current_value);
drawString(number, 0xFFFF, *screen, x + 100, y);
}
else
drawString(e.values[e.current_value], 0xFFFF, *screen, x + 100, y);
y += fontHeight() + 5;
}
}
void SettingsTask::logic()
{
if(key_held_down)
key_held_down = keyPressed(KEY_NSPIRE_ESC) || keyPressed(KEY_NSPIRE_UP) || keyPressed(KEY_NSPIRE_DOWN) || keyPressed(KEY_NSPIRE_2) || keyPressed(KEY_NSPIRE_8) || keyPressed(KEY_NSPIRE_LEFT) || keyPressed(KEY_NSPIRE_4) || keyPressed(KEY_NSPIRE_RIGHT) || keyPressed(KEY_NSPIRE_6);
else if(keyPressed(KEY_NSPIRE_ESC))
{
world_task.makeCurrent();
if(changed_something)
{
world.setDirty();
world.setFieldOfView(settings[DISTANCE].current_value);
nglSetNearPlane(settings[NEARPLANE_Z].current_value);
}
key_held_down = true;
}
else if(keyPressed(KEY_NSPIRE_UP) || keyPressed(KEY_NSPIRE_8))
{
if(current_selection == 0)
current_selection = settings.size() - 1;
else
--current_selection;
key_held_down = true;
}
else if(keyPressed(KEY_NSPIRE_DOWN) || keyPressed(KEY_NSPIRE_2))
{
++current_selection;
if(current_selection >= settings.size())
current_selection = 0;
key_held_down = true;
}
else if(keyPressed(KEY_NSPIRE_LEFT) || keyPressed(KEY_NSPIRE_4))
{
SettingsEntry &entry = settings[current_selection];
if(entry.current_value < entry.min_value + entry.step)
entry.current_value = entry.values_count - 1;
else
entry.current_value -= entry.step;
changed_something = true;
key_held_down = true;
}
else if(keyPressed(KEY_NSPIRE_RIGHT) || keyPressed(KEY_NSPIRE_6))
{
SettingsEntry &entry = settings[current_selection];
entry.current_value += entry.step;
if(entry.current_value >= entry.values_count)
entry.current_value = entry.min_value;
changed_something = true;
key_held_down = true;
}
}
unsigned int SettingsTask::getValue(unsigned int entry) const
{
return settings[entry].current_value;
}
bool SettingsTask::loadFromFile(FILE *file)
{
//World doesn't care about DISTANCE being saved and loaded here as well
unsigned int size;
if(fread(&size, sizeof(size), 1, file) != 1)
return false;
//For backwards compatibility
if(size != settings.size())
return fseek(file, size * sizeof(unsigned int), SEEK_CUR) == 0;
for(unsigned int i = 0; i < size; ++i)
{
if(fread(&settings[i].current_value, sizeof(unsigned int), 1, file) != 1)
return false;
}
world.setDirty();
nglSetNearPlane(settings[NEARPLANE_Z].current_value);
return true;
}
bool SettingsTask::saveToFile(FILE *file)
{
unsigned int size = settings.size();
if(fwrite(&size, sizeof(size), 1, file) != 1)
return false;
for(unsigned int i = 0; i < size; ++i)
{
if(fwrite(&settings[i].current_value, sizeof(unsigned int), 1, file) != 1)
return false;
}
return true;
}