-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOptions.pas
246 lines (193 loc) · 4.72 KB
/
Options.pas
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
unit Options;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, CustomXML;
const
KeyCount = 32;
type
pBool= ^boolean;
eGraphicsOptions = (grDrawFrame, grWireFrame); { TODO 2 -cFeature : variant array with enumerators for every option }
ePlayerAction = (aEsc = -2, aNone = -1, aLeft, aRight, aForward, aBackward, aUpward, aDownward,
aPitchDown, aPitchUp, aYawLeft, aYawRight, aRollLeft, aRollRight);
{$INCLUDE keys.inc}
type
rGraphicsOptions = record
XRes, YRes: longword;
//FOV
DrawFrame, WireFrame: boolean;
end;
rGamePlayOptions = record
MouseSensitivity: single;
end;
rSystemOptions = record
KeepLog: boolean;
PrintToConsole: boolean;
end;
{rControlScheme = record
m: array[0..KeyCount - 1] of pBool;
end; }
{ tOptions }
tOptions = class
CS: array[low(eScanCode)..high(eScanCode)] of ePlayerAction; // Control Scheme
Graphics: rGraphicsOptions;
GamePlay: rGamePlayOptions;
System: rSystemOptions;
function Load(FileName: string): integer;
function Save(FileName: string): integer;
function WriteDefault(FileName: string): integer;
end;
implementation
uses
SDL2, Utility;
{ tOptions }
function tOptions.Load(FileName: string): integer;
var
F: tXMLFile;
a: ePlayerAction;
KeyCode: eScanCode;
e: integer;
s: string;
begin
F:= tXMLFile.Open(FileName);
if F = nil then
begin
WriteLog('Failed to load config');
exit(-1);
end;
for KeyCode:= low(eScanCode) to high(eScanCode) do
begin
CS[KeyCode]:= aNone;
end;
F.FindNode('keybindings');
while F.IterateNodesWithName('keyb') do
begin
s:= F.GetValue('action');
WriteLog('Read string ' + s);
val(s, a, e);
if e <> 0 then
continue;
s:= F.GetValue('keyprim'); { TODO : make all these into enums }
WriteLog('Read string ' + s);
val(s, KeyCode, e);
if e = 0 then
CS[KeyCode]:= a;
end;
CS[scESCAPE]:= aEsc;
F.BackToRoot;
F.FindNode('graphicssettings');
while F.IterateNodesWithName('grs') do
begin
s:= F.GetValue('name');
case s of
'xres': Graphics.XRes:= F.GetValue('value');
'yres': Graphics.YRes:= F.GetValue('value');
else Writelog('Warning: unrecognised setting name in ' + FileName + ': ' + s);
end;
end;
F.BackToRoot;
F.FindNode('gameplaysettings');
while F.IterateNodesWithName('gms') do
begin
s:= F.GetValue('name');
writelog(s);
case s of
'mousesens': GamePlay.MouseSensitivity:= F.GetValue('value');
else Writelog('Warning: unrecognised setting name in ' + FileName + ': ' + s);
end;
end;
F.Free;
end;
function tOptions.Save(FileName: string): integer;
var
F: tXMLFile;
a, v: string;
KeyCode: eScanCode;
begin
F:= tXMLFile.CreateNew(FileName, true);
if F = nil then
begin
WriteLog('Failed to save config');
exit(-1);
end;
F.AddNode('keybindings');
for KeyCode:= low(eScanCode) to high(eScanCode) do
begin
if CS[KeyCode] <> aNone then
begin
str(CS[KeyCode], a);
str(KeyCode, v);
F.AddNode('keyb');
F.SetValue('action', a);
F.SetValue('keyprim', v);
F.Back;
end;
end;
F.Back;
F.AddNode('graphicssettings');
F.AddNode('grs');
F.SetValue('name', 'xres');
F.SetValue('value', Graphics.XRes);
F.Back;
F.AddNode('grs');
F.SetValue('name', 'yres');
F.SetValue('value', Graphics.YRes);
F.Back;
F.Back;
F.AddNode('gameplaysettings');
F.AddNode('gms');
F.SetValue('name', 'mousesens');
F.SetValue('value', GamePlay.MouseSensitivity);
F.Back;
F.Back;
F.Free;
Result:= 0;
end;
function tOptions.WriteDefault(FileName: string): integer;
var
F: tXMLFile;
i: ePlayerAction;
a, v: string;
begin
GamePlay.MouseSensitivity:= 0.8;
Graphics.XRes:= 1000;
Graphics.YRes:= 1000;
System.KeepLog:= true;
WriteLog('Writing default config to ' + Filename);
F:= tXMLFile.CreateNew(FileName);
if F = nil then
begin
WriteLog('Failed to create default config');
exit(-1);
end;
F.AddNode('keybindings');
for i:= aLeft to high(ePlayerAction) do //Has to start from aLeft, low(..) is -1!
begin
str(i, a);
str(DefaultBinds[i], v);
F.AddNode('keyb');
F.SetValue('action', a);
F.SetValue('keyprim', v);
F.Back;
end;
F.Back;
F.AddNode('graphicssettings');
F.AddNode('grs');
F.SetValue('name', 'xres');
F.SetValue('value', Graphics.XRes);
F.Back;
F.AddNode('grs');
F.SetValue('name', 'yres');
F.SetValue('value', Graphics.YRes);
F.Back;
F.Back;
F.AddNode('gameplaysettings');
F.AddNode('gms');
F.SetValue('name', 'mousesens');
F.SetValue('value', GamePlay.MouseSensitivity);
F.Back;
F.Back;
F.Free;
Result:= 0;
end;
end.