-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.pas
384 lines (324 loc) · 7.51 KB
/
app.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
unit app;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, SDL2, Window, Utility, Files, Options, World, Audio, GL,
GUI, Player;
type
//write own exception class
rAction = record
MousePressed,
Left, Right, Forwards, Backwards, Up, Down,
PitchDown, PitchUp,
YawLeft, YawRight,
RollLeft, RollRight,
WireFrame,
DrawFrame,
Paused: Boolean;
mx, my: single;
end;
//messes with gameloop
eGAState = (sMainMenu, sGameLoop, {sPauseLoop,} sShutDown);
eAppAction = (aHalt, aExitSublevel, aStartGame, aPauseGame);
{ tGameApp }
tGameApp = class
private
//Event: tSDL_Event;
Image: pSDL_Surface;
World: tWorld;
function LoadConfig: integer;
//procedure ProcessInput;
procedure ProcessAI;
procedure ProcessEconomy;
procedure ProcessGameLogic;
procedure ProcessPhysics;
procedure Render;
procedure RenderUI;
public//shed
{Done,} //ExitRequest: boolean;
Options: tOptions;
State: eGAState;
GameWindow: tGameWindow;
Menu: tGUI;
Player: tPlayer;
constructor Create;
function Initialize: integer;
procedure MainLoop;
procedure MenuLoop;
procedure GameLoop;
procedure PauseLoop;
procedure Finish;
destructor Destroy; override;
end;
const
MovRate = 0.02;
RotRate = 1;
ConfigName = 'init.cfg';
MainMenuFile = 'mm.uid'; //ui desc
PauseMenuFile = 'pm.uid';
var
GameApp: tGameApp;
implementation
{ tGameApp }
function tGameApp.LoadConfig: integer;
var
FS: tFileStream;
PrFN: string; //profile filename
begin
Result:= 0;
try
if FileExists(ConfigName) then
begin
WriteLog('Opening file ' + ConfigName);
try
FS:= tFileStream.Create(ConfigName, fmOpenRead);
PrFN:= FS.ReadAnsiString;
//WriteLog('Read filename ' + PrFN);
except
on E: Exception do
begin
Result:= -1;
WriteLog(emNoF + ConfigName + ': ' + E.Message);
//repair file
FS.Free;
FS:= tFileStream.Create(ConfigName, fmOpenWrite);
PrFN:= DefaultProfileName + ProfileExt;
FS.WriteAnsiString(PrFN);
//Options.WriteDefault(FS);
{ TODO : repair config? } //extensive check for compat?
end
end;
end
else //New File
begin
WriteLog('Creating file ' + ConfigName);
try
FS:= tFileStream.Create(ConfigName, fmCreate);
PrFN:= DefaultProfileName + ProfileExt;
FS.WriteAnsiString(PrFN);
//Options.WriteDefault(FS);
except
on E: Exception do
begin
Result:= -2;
WriteLog(emCrF + ConfigName + ': ' + E.Message);
end
end;
end;
finally
FS.Free;
Player:= tPlayer.Create(Options); //currently does nothing
Player.LoadProfile(PrFN);
end;
end;
procedure tGameApp.ProcessAI;
begin
end;
procedure tGameApp.ProcessEconomy;
begin
end;
procedure tGameApp.ProcessGameLogic;
begin
end;
procedure tGameApp.ProcessPhysics;
begin
with Player.Action, Options.GamePlay do
begin
with World.Camera.c do
begin
if left then x+= movrate;
if right then x-= movrate;
if forwards then z+= movrate;
if backwards then z-= movrate;
if up then y-= movrate;
if down then y+= movrate;
end;
with World.Camera.o, Options do
begin
if pitchup then x-= rotrate;
if pitchdown then x+= rotrate;
if yawright then y+= rotrate;
if yawleft then y-= rotrate;
if rollright then z+= rotrate;
if rollleft then z-= rotrate;
x+= my * MouseSensitivity;
y+= mx * MouseSensitivity;
end;
end;
end;
procedure tGameApp.Render;
begin
//World.Wireframe:= PlayerAction.WireFrame;
World.Render;
GameWindow.Update;
end;
procedure tGameApp.RenderUI;
begin
glClear(GL_COLOR_BUFFER_BIT {or GL_DEPTH_BUFFER_BIT});
{SDL_BlitSurface(Image, nil, GameWindow.Surface, nil);
SDL_UpdateWindowSurface(GameWindow.WindowHandle); }
Menu.Render;
GameWindow.Update;
end;
constructor tGameApp.Create;
begin
GameWindow:= tGameWindow.Create;
end;
function tGameApp.Initialize: integer;
begin
Options:= tOptions.Create;
{$IFOPT D+}
Options.System.KeepLog:= true;
Options.System.PrintToConsole:= true;
{$ENDIF}
Result:= LoadConfig;
if Result <> 0 then
exit;
//Options:= Player.Options;
{Menu:= tGUI.Create;
Result:= Menu.LoadContents(MainMenuFile);
if Result <> 0 then
exit;
Menu.Options:= Player.Options; }
State:= sMainMenu;
randomize;
try
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
Result:= SDL_Init(SDL_INIT_TIMER or SDL_INIT_AUDIO or SDL_INIT_VIDEO);
if Result < 0 then
WriteLog(SDL_GetError)
else
begin
Result:= GameWindow.Open('vx', false, 0, 0, Options.Graphics.XRes, Options.Graphics.XRes);
SDL_GL_SetSwapInterval(0);
if Result = 2 then
begin
//sdl_videoinit ??
Image:= LoadImage('Logo.bmp');
end;
end;
except
on e: exception do
writelog(e.message);
end;
AudioEngine.Create;
end;
procedure tGameApp.MainLoop;
begin
WriteLog('Entered Main Loop');
repeat
case State of
sMainMenu:
begin
MenuLoop;
end;
sGameLoop:
begin
GameLoop;
end;
end;
until State = sShutDown;
WriteLog('Left Main Loop');
end;
procedure tGameApp.MenuLoop;
var
Result: integer;
begin
SDL_SetRelativeMouseMode(SDL_FALSE);
with GameWindow do
begin
SDL_BlitSurface(Image, nil, Surface, nil);
SDL_UpdateWindowSurface(GameWindow.WindowHandle);
end;
Menu:= tGUI.Create;
Result:= Menu.LoadContents('');
//Menu.EscExits:= true;
Menu.Options:= Player.Options;
if Result <> 0 then
WriteLog(emNoF + '');
Menu.Render;
GameWindow.Update;
WriteLog('Entered Menu');
repeat
Menu.ProcessInput;
Menu.Render;
RenderUI;
until State <> sMainMenu;
WriteLog('Left Menu');
freeandnil(Menu);
end;
procedure tGameApp.GameLoop;
var
Result: integer;
begin
SDL_SetRelativeMouseMode(SDL_TRUE);
Menu:= tGUI.Create;
Result:= Menu.LoadContents(''); //this is the actual pause menu
// Menu.EscExits:= false;
if Result <> 0 then
WriteLog(emNoF + '');
//Menu.Render;
WriteLog('Setting up world');
//loading screen
World:=
//tWorld.LoadHM('map1024');
tWorld.CreateNew(random(100));
//writeln('done');
World.PassVoxels;
World.Camera.c.x:= 0;
World.Camera.c.y:= 0;
World.Camera.c.z:= 0;
World.Camera.o.x:= 0;
World.Camera.o.y:= 0;
World.Camera.o.z:= 0;
with Player, Player.Action do
begin
PitchDown:= false;
PitchUp:= false;
RollLeft:= false;
RollRight:= false;
YawLeft:= false;
YawRight:= false;
repeat
if Paused then
PauseLoop;
ProcessAI;
ProcessEconomy;
ProcessInput;
ProcessPhysics;
ProcessGameLogic;
//if PlayerAction.DrawFrame then
Render;
//PlayerAction.DrawFrame:= false;
until State <> sGameLoop;
Paused:= false;
end;
WriteLog('Destroying world');
freeandnil(World);
freeandnil(Menu);
end;
procedure tGameApp.PauseLoop;
begin
SDL_SetRelativeMouseMode(SDL_FALSE);
WriteLog('Paused');
repeat
Menu.ProcessInput;
RenderUI;
until not Player.Action.Paused;
WriteLog('Unpaused');
SDL_SetRelativeMouseMode(SDL_TRUE);
end;
procedure tGameApp.Finish;
begin
SDL_Quit;
World.Free;
Player.Free;
Menu.Free;
end;
destructor tGameApp.Destroy;
begin
GameWindow.Free;
AudioEngine.Free;
end;
end.