Debian Codegraph @

examples/deprecated_library/lazarus_library_tester/cge_dynlib_tester_form.pas

raw
1 {
2 Copyright 2008-2018 Jan Adamec, Michalis Kamburelis.
3
4 This file is part of "Castle Game Engine".
5
6 "Castle Game Engine" is free software; see the file COPYING.txt,
7 included in this distribution, for details about the copyright.
8
9 "Castle Game Engine" is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12
13 ----------------------------------------------------------------------------
14
15 This is a test project for our library in src/deprecated_library/.
16 It uses library API (through the castlelib_dynloader unit),
17 and uses a compiled dynamic library with the engine.
18
19 THIS IS *NOT* HOW YOU SHOULD USUALLY USE THE ENGINE from FPC/Lazarus.
20 If you use FPC/Lazarus to make your game, then usually you want to simply
21 use the engine units (and Lazarus packages). For example, unit CastleControl
22 gives you a nice Lazarus component readily integrated with the engine,
23 CastleWindow gives you a window (without LCL dependency) integrated with
24 engine. Countless other engine units give you useful things
25 (like CastleScene, CastleViewport, CastleVectors... see the engine manual).
26
27 Using the engine units directly gives you a complete object-oriented API
28 in ObjectPascal to do everything :) The C library API (exposed in
29 castlelib_dynloader) offers only a small subset of engine functionality.
30 The library is useful to access the engine from other programming languages.
31
32 HOW TO RUN THIS: prior to running this project,
33 compile and copy the shared library
34 (in src/deprecated_library/) to a place where it can be loaded:
35
36 - Windows: copy castleengine.dll to this project folder
37 (where the executable file is generated), or anywhere on $PATH.
38
39 - Unix: copy libcastleengine.so (or libcastleengine.dylib on macOS)
40 to any directory listed on $LD_LIBRARY_PATH.
41 For example you can set LD_LIBRARY_PATH to contain empty directory,
42 and then you can just copy to the current directory (from which you
43 run the program).
44 Or you can explicitly list the directory with libcastleengine.so,
45 by doing this (in your shell, or even in your ~/.bashrc or similar file):
46
47 export LD_LIBRARY_PATH="$LD_LIBRARY_PATH":"$CASTLE_ENGINE_PATH"/src/deprecated_library/
48 }
49 unit cge_dynlib_tester_form;
50
51 {$mode objfpc}{$H+}
52
53 interface
54
55 uses
56 Classes, SysUtils, FileUtil, OpenGLContext, Forms, Controls, Graphics,
57 Dialogs, StdCtrls, ExtCtrls, Types;
58
59 type
60
61 { TForm1 }
62
63 TForm1 = class(TForm)
64 BtnNavWalk: TToggleBox;
65 BtnNavFly: TToggleBox;
66 BtnNavExamine: TToggleBox;
67 BtnNavTurntable: TToggleBox;
68 BtnScreenshot: TButton;
69 BtnOpen: TButton;
70 CbViewpoints: TComboBox;
71 OpenDialog1: TOpenDialog;
72 OpenGLControl1: TOpenGLControl;
73 Panel1: TPanel;
74 procedure BtnOpenClick(Sender: TObject);
75 procedure BtnScreenshotClick(Sender: TObject);
76 procedure BtnWalkClick(Sender: TObject);
77 procedure CbViewpointsChange(Sender: TObject);
78 procedure FormCreate(Sender: TObject);
79 procedure FormDestroy(Sender: TObject);
80 procedure FormResize(Sender: TObject);
81 procedure OpenGLControl1KeyDown(Sender: TObject; var Key: Word;
82 Shift: TShiftState);
83 procedure OpenGLControl1KeyUp(Sender: TObject; var Key: Word;
84 Shift: TShiftState);
85 procedure OpenGLControl1MouseDown(Sender: TObject; Button: Controls.TMouseButton;
86 Shift: TShiftState; X, Y: Integer);
87 procedure OpenGLControl1MouseMove(Sender: TObject; Shift: TShiftState; X,
88 Y: Integer);
89 procedure OpenGLControl1MouseUp(Sender: TObject; Button: Controls.TMouseButton;
90 Shift: TShiftState; X, Y: Integer);
91 procedure OpenGLControl1MouseWheel(Sender: TObject; Shift: TShiftState;
92 WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
93 procedure OpenGLControl1Paint(Sender: TObject);
94 procedure IdleFunc(Sender: TObject; var Done: Boolean);
95 private
96 { private declarations }
97 procedure UpdateUIAfterOpen;
98 procedure UpdateNavigationButtons;
99 procedure FillViewpoints;
100 public
101 { public declarations }
102 end;
103
104 var
105 Form1: TForm1;
106
107 implementation
108
109 uses
110 LCLType, castlelib_dynloader, ctypes;
111
112 var
113 bIgnoreNotifications: boolean;
114
115 {$R *.lfm}
116
117 function OpenGlLibraryCallback(eCode, iParam1, iParam2: cInt32; szParam: pcchar):cInt32; cdecl;
118 begin
119 case eCode of
120 ecgelibNeedsDisplay: Form1.OpenGLControl1.Invalidate;
121 ecgelibSetMouseCursor:
122 case iParam1 of
123 ecgecursorNone: Form1.OpenGLControl1.Cursor := crNone;
124 ecgecursorWait: Form1.OpenGLControl1.Cursor := crHourGlass;
125 ecgecursorHand: Form1.OpenGLControl1.Cursor := crHandPoint;
126 ecgecursorText: Form1.OpenGLControl1.Cursor := crIBeam;
127 else Form1.OpenGLControl1.Cursor := crDefault;
128 end;
129 ecgelibNavigationTypeChanged: Form1.UpdateNavigationButtons;
130 end;
131 Result := 0;
132 end;
133
134 { TForm1 }
135
136 procedure TForm1.FormCreate(Sender: TObject);
137 var
138 sFile: String;
139 begin
140 bIgnoreNotifications := false;
141
142 OpenGLControl1.MakeCurrent();
143 Application.OnIdle := @IdleFunc;
144 CGE_Initialize(PCChar(PChar(GetAppConfigDir(false))));
145 CGE_Open(ecgeofLog, OpenGLControl1.Width, OpenGLControl1.Height, 96);
146 CGE_SetLibraryCallbackProc(@OpenGlLibraryCallback);
147 CGE_SetUserInterface(true);
148 sFile := 'data/bridge_level/bridge_final.x3dv';
149 CGE_LoadSceneFromFile(@sFile[1]);
150 UpdateUIAfterOpen;
151 end;
152
153 procedure TForm1.FormDestroy(Sender: TObject);
154 begin
155 CGE_Close();
156 CGE_Finalize();
157 end;
158
159 procedure TForm1.FormResize(Sender: TObject);
160 begin
161 OpenGLControl1.Width := Width-OpenGLControl1.Left*2;
162 OpenGLControl1.Height := Height-OpenGLControl1.Top*2;
163 CGE_Resize(OpenGLControl1.Width, OpenGLControl1.Height);
164 end;
165
166 { Convert Key (Lazarus key code) to Castle Game Engine library kcge_Xxx constant.
167 Returns kcge_None if not possible. }
168 function KeyToCastleLibrary(const Key: Word; const Shift: TShiftState): CInt32;
169 begin
170 { Note: We have almost the same conversion (LCL -> CGE TKey) already implemented
171 in KeyToCastle in CastleLCLUtils.
172 But this example application deliberately does not use any CGE units,
173 it only uses CGE as a shared library through the API in castlelib_dynloader.
174 So we duplicate here logic of KeyToCastle. }
175
176 Result := kcge_None;
177 case Key of
178 VK_BACK: Result := kcge_BackSpace;
179 VK_TAB: Result := kcge_Tab;
180 VK_RETURN: Result := kcge_Enter;
181 VK_SHIFT: Result := kcge_Shift;
182 VK_CONTROL: Result := kcge_Ctrl;
183 VK_MENU: Result := kcge_Alt;
184 VK_ESCAPE: Result := kcge_Escape;
185 VK_SPACE: Result := kcge_Space;
186 VK_PRIOR: Result := kcge_PageUp;
187 VK_NEXT: Result := kcge_PageDown;
188 VK_END: Result := kcge_End;
189 VK_HOME: Result := kcge_Home;
190 VK_LEFT: Result := kcge_Left;
191 VK_UP: Result := kcge_Up;
192 VK_RIGHT: Result := kcge_Right;
193 VK_DOWN: Result := kcge_Down;
194 VK_INSERT: Result := kcge_Insert;
195 VK_DELETE: Result := kcge_Delete;
196 VK_ADD: Result := kcge_Numpad_Plus;
197 VK_SUBTRACT: Result := kcge_Numpad_Minus;
198 VK_SNAPSHOT: Result := kcge_PrintScreen;
199 VK_NUMLOCK: Result := kcge_NumLock;
200 VK_SCROLL: Result := kcge_ScrollLock;
201 VK_CAPITAL: Result := kcge_CapsLock;
202 VK_PAUSE: Result := kcge_Pause;
203 VK_OEM_COMMA: Result := kcge_Comma;
204 VK_OEM_PERIOD: Result := kcge_Period;
205 VK_NUMPAD0: Result := kcge_Numpad_0;
206 VK_NUMPAD1: Result := kcge_Numpad_1;
207 VK_NUMPAD2: Result := kcge_Numpad_2;
208 VK_NUMPAD3: Result := kcge_Numpad_3;
209 VK_NUMPAD4: Result := kcge_Numpad_4;
210 VK_NUMPAD5: Result := kcge_Numpad_5;
211 VK_NUMPAD6: Result := kcge_Numpad_6;
212 VK_NUMPAD7: Result := kcge_Numpad_7;
213 VK_NUMPAD8: Result := kcge_Numpad_8;
214 VK_NUMPAD9: Result := kcge_Numpad_9;
215 VK_CLEAR: Result := kcge_Numpad_Begin;
216 VK_MULTIPLY: Result := kcge_Numpad_Multiply;
217 VK_DIVIDE: Result := kcge_Numpad_Divide;
218 VK_OEM_MINUS: Result := kcge_Minus;
219 VK_OEM_PLUS:
220 if ssShift in Shift then
221 Result := kcge_Plus
222 else
223 Result := kcge_Equal;
224 Ord('0') .. Ord('9'):
225 Result := Ord(kcge_0) + Ord(Key) - Ord('0');
226 Ord('A') .. Ord('Z'):
227 Result := Ord(kcge_A) + Ord(Key) - Ord('A');
228 VK_F1 .. VK_F12:
229 Result := Ord(kcge_F1) + Ord(Key) - VK_F1;
230 end;
231 end;
232
233 procedure TForm1.OpenGLControl1KeyDown(Sender: TObject; var Key: Word;
234 Shift: TShiftState);
235 var
236 KeyCge: CInt32;
237 begin
238 { Test CGE_MoveToViewpoint by switching to 1st, 2nd or 3rd viewpoint
239 when you press 1,2,3 keys.
240 Hold Shift to switch with smooth animation instead of instantly.
241
242 Example test model for this: demo-models/navigation/viewpoints_various_tests.x3dv
243 (switch between 1st and 3rd viewpoints there, note that 2nd viewpoint there equals 1st). }
244 case Key of
245 VK_1: CGE_MoveToViewpoint(0, ssShift in Shift);
246 VK_2: CGE_MoveToViewpoint(1, ssShift in Shift);
247 VK_3: CGE_MoveToViewpoint(2, ssShift in Shift);
248 end;
249
250 KeyCge := KeyToCastleLibrary(Key, Shift);
251 if KeyCge <> kcge_None then
252 CGE_KeyDown(KeyCge);
253 end;
254
255 procedure TForm1.OpenGLControl1KeyUp(Sender: TObject; var Key: Word;
256 Shift: TShiftState);
257 var
258 KeyCge: CInt32;
259 begin
260 KeyCge := KeyToCastleLibrary(Key, Shift);
261 if KeyCge <> kcge_None then
262 CGE_KeyUp(KeyCge);
263 end;
264
265 const
266 { Because of deprecated CastleKeysMouse conflict (deprecated CastleKeysMouse.mbLeft),
267 we need to write "Controls.mbLeft" not just "mbLeft".
268 But we cannot use inside TForm1 method, as there it is a property that calls GetControl. }
269 LeftMouseButton = Controls.mbLeft;
270
271 procedure TForm1.OpenGLControl1MouseDown(Sender: TObject; Button: Controls.TMouseButton;
272 Shift: TShiftState; X, Y: Integer);
273 begin
274 CGE_MouseDown(x, OpenGLControl1.Height - 1 - y, Button=LeftMouseButton, 0);
275 end;
276
277 procedure TForm1.OpenGLControl1MouseMove(Sender: TObject; Shift: TShiftState;
278 X, Y: Integer);
279 begin
280 CGE_Motion(x, OpenGLControl1.Height - 1 - y, 0);
281 end;
282
283 procedure TForm1.OpenGLControl1MouseUp(Sender: TObject; Button: Controls.TMouseButton;
284 Shift: TShiftState; X, Y: Integer);
285 begin
286 CGE_MouseUp(x, OpenGLControl1.Height - 1 - y, Button=LeftMouseButton, 0, true);
287 end;
288
289 procedure TForm1.OpenGLControl1MouseWheel(Sender: TObject; Shift: TShiftState;
290 WheelDelta: Integer; MousePos: TPoint; var Handled: Boolean);
291 begin
292 CGE_MouseWheel(WheelDelta, true);
293 Handled := true;
294 end;
295
296 procedure TForm1.IdleFunc(Sender: TObject; var Done: Boolean);
297 begin
298 CGE_Update();
299 Done:=false;
300 end;
301
302 procedure TForm1.OpenGLControl1Paint(Sender: TObject);
303 begin
304 OpenGLControl1.MakeCurrent();
305 CGE_Render();
306 OpenGLControl1.SwapBuffers;
307 end;
308
309 procedure TForm1.BtnScreenshotClick(Sender: TObject);
310 var
311 sFile: string;
312 csFile: array[0..260] of char;
313 begin
314 sFile := ExtractFilePath(Application.ExeName) + 'cge_test_screenshot.png';
315 StrPCopy(csFile, sFile);
316 CGE_SaveScreenshotToFile(@csFile[0]);
317 end;
318
319 procedure TForm1.BtnOpenClick(Sender: TObject);
320 var
321 csFile: array[0..260] of char;
322 begin
323 { Note about OpenDialog1 (TOpenDialog) usage:
324 In a "real" Castle Game Engine application using LCL, we recommend to use
325 component TCastleOpenSceneDialog (from castle_components.lpk)
326 to have a dialog box to select a file to load in TCastleScene.
327 However, in case of this application, it deliberately *does not* use CGE in a normal
328 way (through Lazarus packages or Pascal units), it only accesses CGE as a shared library.
329 That's why we decided to define OpenDialog1 as TOpenDialog, not TCastleOpenSceneDialog. }
330
331 if OpenDialog1.Execute then
332 begin
333 StrPCopy(csFile, OpenDialog1.Filename);
334
335 CGE_LoadSceneFromFile(@csFile[0]);
336
337 UpdateUIAfterOpen;
338 end;
339 end;
340
341 procedure TForm1.UpdateUIAfterOpen;
342 begin
343 OpenGLControl1.Invalidate;
344 FillViewpoints;
345 ActiveControl := OpenGLControl1; // set focus in order to receive keydowns
346 UpdateNavigationButtons;
347 end;
348
349 procedure TForm1.BtnWalkClick(Sender: TObject);
350 begin
351 if bIgnoreNotifications then exit;
352
353 CGE_SetNavigationType((Sender as TToggleBox).Tag);
354 UpdateNavigationButtons;
355 end;
356
357 procedure TForm1.UpdateNavigationButtons;
358 var
359 iType: integer;
360 bOldIgnore: boolean;
361 begin
362 bOldIgnore := bIgnoreNotifications;
363 bIgnoreNotifications := true;
364
365 iType := CGE_GetNavigationType();
366 BtnNavWalk.Checked := (iType = ecgenavWalk);
367 BtnNavFly.Checked := (iType = ecgenavFly);
368 BtnNavExamine.Checked := (iType = ecgenavExamine);
369 BtnNavTurntable.Checked := (iType = ecgenavTurntable);
370
371 bIgnoreNotifications := bOldIgnore;
372 end;
373
374 procedure TForm1.FillViewpoints;
375 var
376 i, nCount: integer;
377 csName: array[0..260] of char;
378 bOldIgnore: boolean;
379 begin
380 bOldIgnore := bIgnoreNotifications;
381 bIgnoreNotifications := true;
382
383 CbViewpoints.Items.Clear;
384 nCount := CGE_GetViewpointsCount();
385 for i := 0 to nCount-1 do
386 begin
387 CGE_GetViewpointName(i, @csName[0], 260);
388 CbViewpoints.Items.Add(csName);
389 end;
390
391 bIgnoreNotifications := bOldIgnore;
392 end;
393
394 procedure TForm1.CbViewpointsChange(Sender: TObject);
395 begin
396 if bIgnoreNotifications then exit;
397
398 CGE_MoveToViewpoint(CbViewpoints.ItemIndex, true);
399 end;
400
401 end.