-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathd2dApplication.pas
340 lines (306 loc) · 7.31 KB
/
d2dApplication.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
//---------------------------------------------------------------------------
// The contents of this file are subject to the Mozilla Public License
// Version 1.1 (the "License"); you may not use this file except in
// compliance with the License. You may obtain a copy of the License at
// http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS"
// basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
// License for the specific language governing rights and limitations
// under the License.
//---------------------------------------------------------------------------
unit d2dApplication;
interface
uses
Classes,
d2dTypes;
type
Td2dScene = class;
Td2dSceneClass = class of Td2dScene;
Td2dApplication = class
private
f_CurrentScene: string;
f_CurrentSceneObj: Td2dScene;
f_Finished: Boolean;
f_Scenes: TStringList;
procedure pm_SetCurrentScene(const Value: string);
protected
procedure FrameFunc(aDelta: Single; var theFinish: Boolean);
function Init: Boolean; virtual;
procedure LoadGlobalResources; virtual;
procedure RenderFunc;
procedure UnloadGlobalResources; virtual;
public
constructor Create(const aResWidth, aResHeight: Word; const aWindowed: Boolean = True; aTitle: string = '');
destructor Destroy; override;
procedure AddScene(aKey: string; aScene: Td2dScene);
procedure Run;
property CurrentScene: string read f_CurrentScene write pm_SetCurrentScene;
property Finished: Boolean read f_Finished write f_Finished;
end;
Td2dScene = class
private
f_Application: Td2dApplication;
f_Loaded: Boolean;
f_Child : Td2dScene;
f_Parent: Td2dScene;
f_RunningAsChild: Boolean;
function pm_GetHasRunningChild: Boolean;
protected
procedure DoLoad; virtual;
procedure DoUnload; virtual;
procedure Frame(aDelta: Single);
procedure DoFrame(aDelta: Single); virtual;
procedure ProcessEvent(var theEvent: Td2dInputEvent);
procedure DoProcessEvent(var theEvent: Td2dInputEvent); virtual;
procedure Render;
procedure DoRender; virtual; abstract;
procedure StartChild(const aScene: Td2dScene);
procedure StopChild; // îñòàíîâèòü âûïîëíåíèå äî÷åðíåé ñöåíû
procedure StopAsChild;
procedure BeforeStoppingChild; virtual;
property HasRunningChild: Boolean read pm_GetHasRunningChild;
public
constructor Create(aApplication: Td2dApplication);
destructor Destroy; override;
procedure Load;
procedure Unload;
property Application: Td2dApplication read f_Application;
end;
implementation
uses
SysUtils,
{$IFDEF TRACE_STACK}
JclDebug,
{$ENDIF}
d2dCore;
constructor Td2dApplication.Create(const aResWidth, aResHeight: Word;
const aWindowed: Boolean = True;
aTitle: string = '');
begin
inherited Create;
f_Scenes := TStringList.Create;
f_Scenes.Sorted := True;
f_Scenes.CaseSensitive := False;
f_Scenes.Duplicates := dupError;
D2DInit(aResWidth, aResHeight, aWindowed, aTitle);
f_Finished := False;
gD2DE.OnFrame := FrameFunc;
gD2DE.OnRender := RenderFunc;
end;
destructor Td2dApplication.Destroy;
begin
f_Scenes.Free;
D2DDone;
inherited;
end;
procedure Td2dApplication.AddScene(aKey: string; aScene: Td2dScene);
begin
f_Scenes.AddObject(aKey, aScene);
end;
procedure Td2dApplication.FrameFunc(aDelta: Single; var theFinish: Boolean);
var
l_Ev: Td2dInputEvent;
begin
if f_CurrentSceneObj <> nil then
begin
while gD2DE.Input_GetEvent(l_Ev) do
begin
f_CurrentSceneObj.ProcessEvent(l_Ev);
end;
f_CurrentSceneObj.Frame(aDelta);
end;
theFinish := f_Finished;
end;
function Td2dApplication.Init: Boolean;
begin
Result := True;
// here we supposed to register scenes and initialize all needed data
end;
procedure Td2dApplication.LoadGlobalResources;
begin
// here we load application-wide resources
end;
procedure Td2dApplication.pm_SetCurrentScene(const Value: string);
var
l_Idx: Integer;
begin
if (f_CurrentSceneObj <> nil) and (gD2DE.D3DDevice <> nil) then
f_CurrentSceneObj.Unload;
l_Idx := f_Scenes.IndexOf(Value);
if l_Idx >= 0 then
begin
f_CurrentSceneObj := Td2dScene(f_Scenes.Objects[l_Idx]);
if gD2DE.D3DDevice <> nil then
f_CurrentSceneObj.Load;
f_CurrentScene := Value;
end
else
begin
gD2DE.System_Log('Can''t find scene "%s". Terminating application.', [Value]);
f_Finished := True;
end;
end;
procedure Td2dApplication.RenderFunc;
begin
gD2DE.Gfx_BeginScene;
try
if f_CurrentSceneObj <> nil then
f_CurrentSceneObj.Render
else
gD2DE.Gfx_Clear(0);
finally
gD2DE.Gfx_EndScene;
end;
end;
procedure Td2dApplication.Run;
var
I: Integer;
begin
if not Init then
Exit;
if gD2DE.System_Start then
begin
try
if f_Scenes.Count > 0 then
begin
LoadGlobalResources;
try
if f_CurrentSceneObj = nil then
CurrentScene := f_Scenes[0]
else
f_CurrentSceneObj.Load;
try
gD2DE.System_Run;
except
on E: Exception do
begin
gD2DE.System_Log('ÎØÈÁÊÀ: '+E.Message);
{$IFDEF TRACE_STACK}
l_List := TStringList.Create;
try
JclLastExceptStackListToStrings(l_List);
gD2DE.System_Log(l_List.Text);
finally
FreeAndNil(l_List);
end;
{$ENDIF}
end;
end;
finally
for I := 0 to f_Scenes.Count-1 do
f_Scenes.Objects[I].Free;
f_Scenes.Clear;
UnloadGlobalResources;
end;
end
else
gD2DE.System_Log('No scenes defined!');
finally
gD2DE.System_Shutdown;
end;
end;
end;
procedure Td2dApplication.UnloadGlobalResources;
begin
// here we unload application-wide resources
end;
constructor Td2dScene.Create(aApplication: Td2dApplication);
begin
inherited Create;
f_Application := aApplication;
end;
destructor Td2dScene.Destroy;
begin
StopChild;
Unload;
inherited;
end;
procedure Td2dScene.BeforeStoppingChild;
begin
// does nothing in base class
end;
procedure Td2dScene.DoLoad;
begin
// does nothing in base class
end;
procedure Td2dScene.DoUnload;
begin
// does nothing in base class
end;
procedure Td2dScene.DoFrame(aDelta: Single);
begin
// does nothing in base class
end;
procedure Td2dScene.Load;
begin
if not f_Loaded then
begin
DoLoad;
f_Loaded := True;
end;
end;
procedure Td2dScene.DoProcessEvent(var theEvent: Td2dInputEvent);
begin
// does nothing in base class
end;
procedure Td2dScene.Frame(aDelta: Single);
begin
if HasRunningChild then
f_Child.Frame(aDelta)
else
DoFrame(aDelta);
end;
function Td2dScene.pm_GetHasRunningChild: Boolean;
begin
Result := f_Child <> nil;
end;
procedure Td2dScene.ProcessEvent(var theEvent: Td2dInputEvent);
begin
if HasRunningChild then
begin
f_Child.ProcessEvent(theEvent);
if not f_Child.f_RunningAsChild then
StopChild;
end
else
DoProcessEvent(theEvent);
end;
procedure Td2dScene.Render;
begin
DoRender;
if HasRunningChild then
f_Child.Render;
end;
procedure Td2dScene.StartChild(const aScene: Td2dScene);
begin
StopChild;
f_Child := aScene;
f_Child.f_Parent := Self;
f_Child.f_RunningAsChild := True;
f_Child.Load;
end;
procedure Td2dScene.StopAsChild;
begin
if f_Parent <> nil then
f_RunningAsChild := False;
end;
procedure Td2dScene.StopChild;
begin
if HasRunningChild then
begin
BeforeStoppingChild;
f_Child.Unload;
f_Child.f_Parent := nil;
f_Child := nil;
end;
end;
procedure Td2dScene.Unload;
begin
if f_Loaded then
begin
DoUnload;
f_Loaded := False;
end;
end;
end.