Debian Codegraph @

src/deprecated_library/castleengine.lpr

raw
1 { -*- compile-command: "./castleengine_compile.sh" -*- }
2 {
3 Copyright 2013-2024 Jan Adamec, Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { The castleengine library main source code.
18 This compiles to castleengine.dll / libcastleengine.so / libcastleengine.dylib.
19
20 Development notes:
21
22 Adapted from Jonas Maebe's example project :
23 http://users.elis.ugent.be/~jmaebe/fpc/FPC_Objective-C_Cocoa.tbz
24 http://julien.marcel.free.fr/ObjP_Part7.html
25
26 To make c-compatible (and Xcode-compatible) libraries, you must :
27 1- use c-types arguments
28 2- add a "cdecl" declaration after your functions declarations
29 3- export your functions
30
31 See FPC CTypes unit (source rtl/unix/ctypes.inc) for a full list of c-types.
32 }
33
34 library castleengine;
35
36 uses CTypes, Math, SysUtils, CastleUtils,
37 Classes, CastleKeysMouse, CastleCameras, CastleVectors, CastleGLUtils, CastleGLVersion,
38 CastleImages, CastleSceneCore, CastleUIControls, X3DNodes, X3DFields, CastleLog,
39 CastleBoxes, CastleControls, CastleInputs, CastleApplicationProperties,
40 CastleWindow, CastleViewport, CastleScene, CastleTransform;
41
42 type
43 TCrosshairManager = class(TObject)
44 public
45 CrosshairCtl: TCastleCrosshair;
46 CrosshairActive: boolean;
47
48 constructor Create;
49 destructor Destroy; override;
50
51 procedure UpdateCrosshairImage;
52 procedure OnPointingDeviceSensorsChange(Sender: TObject);
53 end;
54
55 var
56 Window: TCastleWindow;
57 { Using TCastleAutoNavigationViewport in this case is justified,
58 it is the most straightforward solution to make viewport navigation
59 follow X3D navigation. }
60 {$warnings off}
61 Viewport: TCastleAutoNavigationViewport;
62 {$warnings on}
63 MainScene: TCastleScene; //< Always equal to Viewport.Items.MainScene
64 PreviousNavigationType: TNavigationType;
65 TouchNavigation: TCastleTouchNavigation;
66 Crosshair: TCrosshairManager;
67
68 { Check that CGE_Open was called, and at least Window and Viewport are created. }
69 function CGE_VerifyWindow(const FromFunc: string): boolean;
70 begin
71 Result :=
72 (Window <> nil) and
73 (Viewport <> nil);
74 if not Result then
75 WarningWrite(FromFunc + ' : CGE window not initialized (CGE_Open not called)');
76 end;
77
78 { Check that CGE_LoadSceneFromFile was called,
79 and at least Window and Viewport and MainScene are created. }
80 function CGE_VerifyScene(const FromFunc: string): boolean;
81 begin
82 Result :=
83 (Window <> nil) and
84 (Viewport <> nil) and
85 (MainScene <> nil);
86 {$warnings off} // using Viewport.Items.MainScene is this case is justified
87 Assert((not Result) or (Viewport.Items.MainScene = MainScene));
88 if not Result then
89 WarningWrite(FromFunc + ': CGE scene not initialized (CGE_LoadSceneFromFile not called)');
90 end;
91
92 procedure CGE_Initialize(ApplicationConfigDirectory: PChar); cdecl;
93 begin
94 CGEApp_Initialize(ApplicationConfigDirectory);
95 end;
96
97 procedure CGE_Finalize(); cdecl;
98 begin
99 CGEApp_Finalize();
100 end;
101
102 procedure CGE_Open(flags: cUInt32; InitialWidth, InitialHeight, Dpi: cUInt32); cdecl;
103 begin
104 try
105 if (flags and 1 {ecgeofSaveMemory}) > 0 then
106 begin
107 DefaultTriangulationSlices := 16;
108 DefaultTriangulationStacks := 16;
109 end;
110 if (flags and 2 {ecgeofLog}) > 0 then
111 InitializeLog;
112
113 Window := TCastleWindow.Create(nil);
114 Application.MainWindow := Window;
115
116 Viewport := TCastleAutoNavigationViewport.Create(Window);
117 Viewport.FullSize := true;
118 { AutoCamera is necessary for Viewport.Camera to follow X3D file camera,
119 not only at initialization (this is done by AssignDefaultCamera) but also
120 when new viewpoint node is bound using X3D events or
121 TCastleSceneCore.MoveToViewpoint call. }
122 Viewport.AutoCamera := true;
123 { AutoNavigation is necessary for navigation to follow routes in X3D file.
124 For example when changing navigation by X3D events in
125 demo-models/navigation/navigation_info_bind.x3dv , to make it affect actual
126 CGE navigation. }
127 Viewport.AutoNavigation := true;
128 Window.Controls.InsertFront(Viewport);
129
130 TouchNavigation := TCastleTouchNavigation.Create(nil);
131 TouchNavigation.FullSize := true;
132 TouchNavigation.Viewport := Viewport;
133 Viewport.InsertFront(TouchNavigation);
134
135 PreviousNavigationType := Viewport.NavigationType;
136
137 CGEApp_Open(InitialWidth, InitialHeight, 0, Dpi);
138
139 Crosshair := TCrosshairManager.Create;
140 except
141 on E: TObject do WritelnWarning('Window', 'CGE_Open: ' + ExceptMessage(E));
142 end;
143 end;
144
145 procedure CGE_Close(QuitWhenNoOpenWindows: CBool); cdecl;
146 begin
147 try
148 if not CGE_VerifyWindow('CGE_Close') then Exit;
149
150 if MainScene <> nil then
151 MainScene.OnPointingDeviceSensorsChange := nil;
152 FreeAndNil(Crosshair);
153
154 CGEApp_Close(QuitWhenNoOpenWindows);
155 FreeAndNil(Window);
156 MainScene := nil;
157 except
158 on E: TObject do WritelnWarning('Window', 'CGE_Close: ' + ExceptMessage(E));
159 end;
160 end;
161
162 procedure CGE_GetOpenGLInformation(szBuffer: pchar; nBufSize: cInt32); cdecl;
163 var
164 sText: string;
165 begin
166 try
167 sText := GLInformationString;
168 StrPLCopy(szBuffer, sText, nBufSize-1);
169 except
170 on E: TObject do WritelnWarning('Window', 'CGE_GetOpenGLInformation: ' + ExceptMessage(E));
171 end;
172 end;
173
174 procedure CGE_Resize(uiViewWidth, uiViewHeight: cUInt32); cdecl;
175 begin
176 try
177 if not CGE_VerifyWindow('CGE_Resize') then exit;
178 CGEApp_Resize(uiViewWidth, uiViewHeight, 0);
179 except
180 on E: TObject do WritelnWarning('Window', 'CGE_Resize: ' + ExceptMessage(E));
181 end;
182 end;
183
184 procedure CGE_Render; cdecl;
185 begin
186 try
187 if not CGE_VerifyWindow('CGE_Render') then exit;
188 CGEApp_Render;
189 except
190 on E: TObject do WritelnWarning('Window', 'CGE_Render: ' + ExceptMessage(E));
191 end;
192 end;
193
194 procedure CGE_SaveScreenshotToFile(szFile: pcchar); cdecl;
195 var
196 Image: TRGBImage;
197 begin
198 try
199 if not CGE_VerifyWindow('CGE_SaveScreenshotToFile') then exit;
200
201 // hide touch controls
202 TouchNavigation.Exists := false;
203
204 // make screenshot
205 Image := Window.SaveScreen;
206 try
207 SaveImage(Image, StrPas(PChar(szFile)));
208 finally FreeAndNil(Image) end;
209
210 // restore hidden controls
211 TouchNavigation.Exists := true;
212 except
213 on E: TObject do WritelnWarning('Window', 'CGE_SaveScreenshotToFile: ' + ExceptMessage(E));
214 end;
215 end;
216
217 procedure CGE_SetLibraryCallbackProc(aProc: TLibraryCallbackProc); cdecl;
218 begin
219 if Window = nil then exit;
220 CGEApp_SetLibraryCallbackProc(aProc);
221 end;
222
223 procedure CGE_Update; cdecl;
224 begin
225 try
226 if not CGE_VerifyWindow('CGE_Update') then exit;
227
228 { Call LibraryCallbackProc(ecgelibNavigationTypeChanged,...) when necessary.
229 For this, we just query the Viewport.NavigationType every frame. }
230 if PreviousNavigationType <> Viewport.NavigationType then
231 begin
232 PreviousNavigationType := Viewport.NavigationType;
233 if Assigned(LibraryCallbackProc) then
234 begin
235 case Viewport.NavigationType of
236 ntWalk : LibraryCallbackProc(ecgelibNavigationTypeChanged, ecgenavWalk , 0, nil);
237 ntFly : LibraryCallbackProc(ecgelibNavigationTypeChanged, ecgenavFly , 0, nil);
238 ntExamine : LibraryCallbackProc(ecgelibNavigationTypeChanged, ecgenavExamine , 0, nil);
239 ntTurntable: LibraryCallbackProc(ecgelibNavigationTypeChanged, ecgenavTurntable, 0, nil);
240 ntNone : LibraryCallbackProc(ecgelibNavigationTypeChanged, ecgenavNone , 0, nil);
241 // nt2D: TODO
242 else WritelnWarning('Window', 'Current NavigationType cannot be expressed as enum for ecgelibNavigationTypeChanged');
243 end;
244 end;
245 end;
246
247 { Set Cursor = mcHand when we're over or keeping active
248 some pointing-device sensors. The engine doesn't do it automatically
249 (after https://github.com/castle-engine/castle-engine/commit/5b2810d9ef2fd0f851bc50b0a6aa7b414381dd2c )
250 but it makes total sense for X3D viewers with single viewport and single
251 TCastleScene. }
252 if (MainScene <> nil) and
253 ( ( (MainScene.PointingDeviceSensors <> nil) and
254 (MainScene.PointingDeviceSensors.EnabledCount <> 0)
255 ) or
256 (MainScene.PointingDeviceActiveSensors.Count <> 0)
257 ) then
258 Viewport.Cursor := mcHand
259 else
260 Viewport.Cursor := mcDefault;
261
262 CGEApp_Update;
263 except
264 on E: TObject do WritelnWarning('Window', 'CGE_Update: ' + ExceptMessage(E));
265 end;
266 end;
267
268 procedure CGE_MouseDown(X, Y: CInt32; bLeftBtn: cBool; FingerIndex: CInt32); cdecl;
269 begin
270 try
271 if not CGE_VerifyWindow('CGE_MouseDown') then exit;
272 CGEApp_MouseDown(X, Y, bLeftBtn, FingerIndex);
273 except
274 on E: TObject do WritelnWarning('Window', 'CGE_MouseDown: ' + ExceptMessage(E));
275 end;
276 end;
277
278 procedure CGE_Motion(X, Y: CInt32; FingerIndex: CInt32); cdecl;
279 begin
280 try
281 if not CGE_VerifyWindow('CGE_Motion') then exit;
282 CGEApp_Motion(X, Y, FingerIndex);
283 except
284 on E: TObject do WritelnWarning('Window', 'CGE_Motion: ' + ExceptMessage(E));
285 end;
286 end;
287
288 procedure CGE_MouseUp(X, Y: cInt32; bLeftBtn: cBool;
289 FingerIndex: CInt32; TrackReleased: cBool); cdecl;
290 begin
291 try
292 if not CGE_VerifyWindow('CGE_MouseUp') then exit;
293 CGEApp_MouseUp(X, Y, bLeftBtn, FingerIndex, TrackReleased);
294 except
295 on E: TObject do WritelnWarning('Window', 'CGE_MouseUp: ' + ExceptMessage(E));
296 end;
297 end;
298
299 procedure CGE_MouseWheel(zDelta: cFloat; bVertical: cBool); cdecl;
300 begin
301 try
302 if not CGE_VerifyWindow('CGE_MouseWheel') then exit;
303 // TODO: no corresponding CGEApp callback, as not implemented in iOS code
304 // (in ios_tested not used anyway, because USE_GESTURE_RECOGNIZERS
305 // undefined, and also --- pinch is not really a mouse wheel)
306 Window.LibraryMouseWheel(zDelta/120, bVertical);
307 except
308 on E: TObject do WritelnWarning('Window', 'CGE_MouseWheel: ' + ExceptMessage(E));
309 end;
310 end;
311
312 procedure CGE_KeyDown(eKey: CInt32); cdecl;
313 begin
314 try
315 if not CGE_VerifyWindow('CGE_KeyDown') then exit;
316 CGEApp_KeyDown(eKey);
317 except
318 on E: TObject do WritelnWarning('Window', 'CGE_KeyDown: ' + ExceptMessage(E));
319 end;
320 end;
321
322 procedure CGE_KeyUp(eKey: CInt32); cdecl;
323 begin
324 try
325 if not CGE_VerifyWindow('CGE_KeyUp') then exit;
326 CGEApp_KeyUp(eKey);
327 except
328 on E: TObject do WritelnWarning('Window', 'CGE_KeyUp: ' + ExceptMessage(E));
329 end;
330 end;
331
332 procedure CGE_LoadSceneFromFile(szFile: pcchar); cdecl;
333 begin
334 if Window = nil then exit;
335 try
336 FreeAndNil(MainScene); // if any previous scene exists, remove it
337
338 MainScene := TCastleScene.Create(Window);
339 MainScene.Load(StrPas(PChar(szFile)));
340 MainScene.PreciseCollisions := true;
341 MainScene.ProcessEvents := true;
342 MainScene.ListenPressRelease := true; // necessary to pass keys to X3D sensors
343 Viewport.Items.Add(MainScene);
344 { While CGE deprecated Items.MainScene, it is justified and recommended
345 solution in this case, to make MainScene affect various things
346 (skybox, fog, camera, navigation etc.). }
347 {$warnings off}
348 Viewport.Items.MainScene := MainScene;
349 {$warnings on}
350
351 Viewport.AssignDefaultCamera;
352 Viewport.AssignDefaultNavigation;
353 except
354 on E: TObject do WritelnWarning('Window', 'CGE_LoadSceneFromFile: ' + ExceptMessage(E));
355 end;
356 end;
357
358 procedure CGE_SaveSceneToFile(szFile: pcchar); cdecl;
359 begin
360 if not CGE_VerifyScene('CGE_SaveSceneToFile') then
361 exit;
362 try
363 MainScene.Save(StrPas(PChar(szFile)));
364 except
365 on E: TObject do WritelnWarning('Window', 'CGE_SaveSceneToFile: ' + ExceptMessage(E));
366 end;
367 end;
368
369 function CGE_GetViewpointsCount(): cInt32; cdecl;
370 begin
371 try
372 if not CGE_VerifyScene('CGE_GetViewpointsCount') then
373 begin
374 Result := 0;
375 exit;
376 end;
377
378 Result := MainScene.ViewpointsCount;
379 except
380 on E: TObject do
381 begin
382 WritelnLog('Window', 'CGE_GetViewpointsCount: ' + ExceptMessage(E));
383 Result := 0;
384 end;
385 end;
386 end;
387
388 procedure CGE_GetViewpointName(iViewpointIdx: cInt32; szName: pchar; nBufSize: cInt32); cdecl;
389 var
390 sName: string;
391 begin
392 try
393 if not CGE_VerifyScene('CGE_GetViewpointName') then exit;
394
395 sName := MainScene.GetViewpointName(iViewpointIdx);
396 StrPLCopy(szName, sName, nBufSize-1);
397 except
398 on E: TObject do WritelnWarning('Window', 'CGE_GetViewpointName: ' + ExceptMessage(E));
399 end;
400 end;
401
402 procedure CGE_MoveToViewpoint(iViewpointIdx: cInt32; bAnimated: cBool); cdecl;
403 begin
404 try
405 if not CGE_VerifyScene('CGE_MoveToViewpoint') then exit;
406
407 MainScene.MoveToViewpoint(iViewpointIdx, bAnimated);
408 except
409 on E: TObject do WritelnWarning('Window', 'CGE_MoveToViewpoint: ' + ExceptMessage(E));
410 end;
411 end;
412
413 procedure CGE_AddViewpointFromCurrentView(szName: pcchar); cdecl;
414 begin
415 try
416 if not CGE_VerifyScene('CGE_AddViewpointFromCurrentView') then exit;
417
418 if Viewport.Navigation <> nil then
419 MainScene.AddViewpointFromNavigation(
420 Viewport.Navigation, StrPas(PChar(szName)));
421 except
422 on E: TObject do WritelnWarning('Window', 'CGE_AddViewpointFromCurrentView: ' + ExceptMessage(E));
423 end;
424 end;
425
426 procedure CGE_GetBoundingBox(pfXMin, pfXMax, pfYMin, pfYMax, pfZMin, pfZMax: pcfloat); cdecl;
427 var
428 BBox: TBox3D;
429 begin
430 try
431 if not CGE_VerifyScene('CGE_GetBoundingBox') then exit;
432
433 BBox := MainScene.BoundingBox;
434 pfXMin^ := BBox.Data[0].X; pfXMax^ := BBox.Data[1].X;
435 pfYMin^ := BBox.Data[0].Y; pfYMax^ := BBox.Data[1].Y;
436 pfZMin^ := BBox.Data[0].Z; pfZMax^ := BBox.Data[1].Z;
437 except
438 on E: TObject do WritelnWarning('Window', 'CGE_GetBoundingBox: ' + ExceptMessage(E));
439 end;
440 end;
441
442 procedure CGE_GetViewCoords(pfPosX, pfPosY, pfPosZ, pfDirX, pfDirY, pfDirZ,
443 pfUpX, pfUpY, pfUpZ, pfGravX, pfGravY, pfGravZ: pcfloat); cdecl;
444 var
445 Pos, Dir, Up, GravityUp: TVector3;
446 begin
447 try
448 if not CGE_VerifyWindow('CGE_GetViewCoords') then exit;
449
450 Viewport.Camera.GetWorldView(Pos, Dir, Up);
451 GravityUp := Viewport.Camera.GravityUp;
452 pfPosX^ := Pos.X; pfPosY^ := Pos.Y; pfPosZ^ := Pos.Z;
453 pfDirX^ := Dir.X; pfDirY^ := Dir.Y; pfDirZ^ := Dir.Z;
454 pfUpX^ := Up.X; pfUpY^ := Up.Y; pfUpZ^ := Up.Z;
455 pfGravX^ := GravityUp.X; pfGravY^ := GravityUp.Y; pfGravZ^ := GravityUp.Z;
456 except
457 on E: TObject do WritelnWarning('Window', 'CGE_GetViewCoords: ' + ExceptMessage(E));
458 end;
459 end;
460
461 procedure CGE_MoveViewToCoords(fPosX, fPosY, fPosZ, fDirX, fDirY, fDirZ,
462 fUpX, fUpY, fUpZ, fGravX, fGravY, fGravZ: cFloat;
463 bAnimated: cBool); cdecl;
464 var
465 Pos, Dir, Up, GravityUp: TVector3;
466 begin
467 try
468 if not CGE_VerifyWindow('CGE_MoveViewToCoords') then exit;
469
470 Pos.X := fPosX; Pos.Y := fPosY; Pos.Z := fPosZ;
471 Dir.X := fDirX; Dir.Y := fDirY; Dir.Z := fDirZ;
472 Up.X := fUpX; Up.Y := fUpY; Up.Z := fUpZ;
473 GravityUp.X := fGravX; GravityUp.Y := fGravY; GravityUp.Z := fGravZ;
474 if bAnimated then
475 Viewport.Camera.AnimateTo(Pos, Dir, Up, 0.5)
476 else
477 Viewport.Camera.SetWorldView(Pos, Dir, Up);
478 Viewport.Camera.GravityUp := GravityUp;
479 except
480 on E: TObject do WritelnWarning('Window', 'CGE_MoveViewToCoords: ' + ExceptMessage(E));
481 end;
482 end;
483
484 procedure CGE_SetNavigationInputShortcut(eInput, eKey1, eKey2,
485 eMouseButton, eMouseWheel: cInt32); cdecl;
486 var
487 Nav: TCastleNavigation;
488 WalkNavigation: TCastleWalkNavigation;
489 ExamineNavigation: TCastleExamineNavigation;
490 InputShortcut: TInputShortcut;
491 InKey1: TKey;
492 InKey2: TKey = keyNone;
493 InKeyString: String = '';
494 InMouseButtonUse: boolean;
495 InMouseButton: TCastleMouseButton;
496 InMouseWheel: TMouseWheelDirection;
497 begin
498 try
499 if not CGE_VerifyWindow('CGE_SetCameraInputShortcut') then exit;
500
501 InKey1 := TKey(eKey1);
502 InKey2 := TKey(eKey2);
503 InMouseButtonUse := (eMouseButton <> 0);
504 case eMouseButton of
505 0: InMouseButton := buttonLeft;
506 1: InMouseButton := buttonLeft;
507 2: InMouseButton := buttonMiddle;
508 3: InMouseButton := buttonRight;
509 4: InMouseButton := buttonExtra1;
510 5: InMouseButton := buttonExtra2;
511 end;
512 case eMouseWheel of
513 0: InMouseWheel := mwNone;
514 1: InMouseWheel := mwUp;
515 2: InMouseWheel := mwDown;
516 3: InMouseWheel := mwLeft;
517 4: InMouseWheel := mwRight;
518 end;
519
520 InputShortcut := nil;
521 Nav := Viewport.Navigation;
522 if Nav is TCastleWalkNavigation then
523 begin
524 WalkNavigation := TCastleWalkNavigation(Nav);
525 case eInput of
526 1: InputShortcut := WalkNavigation.Input_ZoomIn;
527 2: InputShortcut := WalkNavigation.Input_ZoomOut;
528 11: InputShortcut := WalkNavigation.Input_Forward;
529 12: InputShortcut := WalkNavigation.Input_Backward;
530 13: InputShortcut := WalkNavigation.Input_LeftRotate;
531 14: InputShortcut := WalkNavigation.Input_RightRotate;
532 15: InputShortcut := WalkNavigation.Input_LeftStrafe;
533 16: InputShortcut := WalkNavigation.Input_RightStrafe;
534 17: InputShortcut := WalkNavigation.Input_UpRotate;
535 18: InputShortcut := WalkNavigation.Input_DownRotate;
536 19: InputShortcut := WalkNavigation.Input_IncreasePreferredHeight;
537 20: InputShortcut := WalkNavigation.Input_DecreasePreferredHeight;
538 21: InputShortcut := WalkNavigation.Input_GravityUp;
539 22: InputShortcut := WalkNavigation.Input_Run;
540 23: InputShortcut := WalkNavigation.Input_MoveSpeedInc;
541 24: InputShortcut := WalkNavigation.Input_MoveSpeedDec;
542 25: InputShortcut := WalkNavigation.Input_Jump;
543 26: InputShortcut := WalkNavigation.Input_Crouch;
544 else raise EInternalError.CreateFmt('CGE_SetCameraInputShortcut: Invalid input type %d for walk navigation', [eInput]);
545 end;
546 if InputShortcut <> nil then
547 InputShortcut.Assign(InKey1, InKey2, InKeyString, InMouseButtonUse, InMouseButton, InMouseWheel);
548 end
549 else if Nav is TCastleExamineNavigation then
550 begin
551 ExamineNavigation := TCastleExamineNavigation(Nav);
552 case eInput of
553 1: InputShortcut := ExamineNavigation.Input_ZoomIn;
554 2: InputShortcut := ExamineNavigation.Input_ZoomOut;
555 31: InputShortcut := ExamineNavigation.Input_Rotate;
556 32: InputShortcut := ExamineNavigation.Input_Move;
557 33: InputShortcut := ExamineNavigation.Input_Zoom;
558 else raise EInternalError.CreateFmt('CGE_SetCameraInputShortcut: Invalid input type %d for examine navigation', [eInput]);
559 end;
560 if InputShortcut <> nil then
561 InputShortcut.Assign(InKey1, InKey2, InKeyString, InMouseButtonUse, InMouseButton, InMouseWheel);
562 end;
563 except
564 on E: TObject do WritelnLog('Window', 'CGE_SetCameraInputShortcut: ' + ExceptMessage(E));
565 end;
566 end;
567
568 function CGE_GetNavigationType(): cInt32; cdecl;
569 begin
570 try
571 if not CGE_VerifyWindow('CGE_GetNavigationType') then Exit(-1);
572
573 case Viewport.NavigationType of
574 ntWalk : Result := 0;
575 ntFly : Result := 1;
576 ntExamine : Result := 2;
577 ntTurntable: Result := 3;
578 ntNone : Result := 4;
579 // nt2D: TODO
580 else raise EInternalError.Create('CGE_GetNavigationType: Unrecognized Viewport.NavigationType');
581 end;
582 except
583 on E: TObject do
584 begin
585 WritelnLog('Window', 'CGE_GetNavigationType: ' + ExceptMessage(E));
586 Result := -1;
587 end;
588 end;
589 end;
590
591 procedure CGE_SetNavigationType(NewType: cInt32); cdecl;
592 var
593 aNavType: TNavigationType;
594 begin
595 try
596 if not CGE_VerifyWindow('CGE_SetNavigationType') then exit;
597
598 case NewType of
599 0: aNavType := ntWalk;
600 1: aNavType := ntFly;
601 2: aNavType := ntExamine;
602 3: aNavType := ntTurntable;
603 4: aNavType := ntNone;
604 // TODO: aNavType := nt2D;
605 else raise EInternalError.CreateFmt('CGE_SetNavigationType: Invalid navigation type %d', [NewType]);
606 end;
607 Viewport.NavigationType := aNavType;
608 except
609 on E: TObject do WritelnWarning('Window', 'CGE_SetNavigationType: ' + ExceptMessage(E));
610 end;
611 end;
612
613 function cgehelper_TouchInterfaceFromConst(eMode: cInt32): TTouchInterface;
614 begin
615 case eMode of
616 0: Result := tiNone;
617 1: Result := tiWalk;
618 2: Result := tiWalkRotate;
619 3: Result := tiFlyWalk;
620 4: Result := tiPan;
621 else raise EInternalError.CreateFmt('cgehelper_TouchInterfaceFromConst: Invalid touch interface mode %d', [eMode]);
622 end;
623 end;
624
625 function cgehelper_ConstFromTouchInterface(eMode: TTouchInterface): cInt32;
626 begin
627 Result := 0;
628 case eMode of
629 tiWalk: Result := 1;
630 tiWalkRotate: Result := 2;
631 tiFlyWalk: Result := 3;
632 tiPan: Result := 4;
633 end;
634 end;
635
636 procedure CGE_SetTouchInterface(eMode: cInt32); cdecl;
637 begin
638 try
639 TouchNavigation.TouchInterface := cgehelper_TouchInterfaceFromConst(eMode);
640 except
641 on E: TObject do WritelnWarning('Window', 'CGE_SetTouchInterface: ' + ExceptMessage(E));
642 end;
643 end;
644
645 procedure CGE_SetUserInterface(bAutomaticTouchInterface: cBool); cdecl;
646 begin
647 try
648 TouchNavigation.AutoTouchInterface := bAutomaticTouchInterface;
649 except
650 on E: TObject do WritelnWarning('Window', 'CGE_SetUserInterface: ' + ExceptMessage(E));
651 end;
652 end;
653
654 procedure CGE_IncreaseSceneTime(fTimeS: cFloat); cdecl;
655 var
656 DummyRemoveType: TRemoveType;
657 begin
658 try
659 MainScene.IncreaseTime(fTimeS);
660 DummyRemoveType := rtNone;
661 Viewport.Camera.Update(fTimeS, DummyRemoveType);
662 except
663 on E: TObject do WritelnWarning('Window', 'CGE_IncreaseSceneTime: ' + ExceptMessage(E));
664 end;
665 end;
666
667 function GetWalkNavigation: TCastleWalkNavigation;
668 var
669 Nav: TCastleNavigation;
670 begin
671 Nav := Viewport.Navigation;
672 if Nav is TCastleWalkNavigation then
673 Result := TCastleWalkNavigation(Nav)
674 else
675 Result := nil;
676 end;
677
678 procedure CGE_SetVariableInt(eVar: cInt32; nValue: cInt32); cdecl;
679 var
680 WalkNavigation: TCastleWalkNavigation;
681 begin
682 if Window = nil then exit;
683 try
684 case eVar of
685 0: begin // ecgevarWalkHeadBobbing
686 WalkNavigation := GetWalkNavigation;
687 if WalkNavigation <> nil then
688 begin
689 if nValue > 0 then
690 WalkNavigation.HeadBobbing := TCastleWalkNavigation.DefaultHeadBobbing
691 else
692 WalkNavigation.HeadBobbing := 0.0;
693 end;
694 end;
695
696 1: begin // ecgevarEffectSSAO
697 if Viewport.ScreenSpaceAmbientOcclusionAvailable then
698 Viewport.ScreenSpaceAmbientOcclusion := (nValue > 0);
699 end;
700
701 2: begin // ecgevarMouseLook
702 WalkNavigation := GetWalkNavigation;
703 if WalkNavigation <> nil then
704 WalkNavigation.MouseLook := (nValue > 0);
705 end;
706
707 3: begin // ecgevarCrossHair
708 Crosshair.CrosshairCtl.Exists := (nValue > 0);
709 if nValue > 0 then
710 begin
711 if nValue = 2 then
712 Crosshair.CrosshairCtl.Shape := csCrossRect
713 else
714 Crosshair.CrosshairCtl.Shape := csCross;
715 Crosshair.UpdateCrosshairImage;
716 MainScene.OnPointingDeviceSensorsChange := @Crosshair.OnPointingDeviceSensorsChange;
717 end;
718 end;
719
720 5: begin // ecgevarWalkTouchCtl
721 TouchNavigation.AutoWalkTouchInterface := cgehelper_TouchInterfaceFromConst(nValue);
722 end;
723
724 6: begin // ecgevarScenePaused
725 Viewport.Items.Paused := (nValue > 0);
726 end;
727
728 7: begin // ecgevarAutoRedisplay
729 Window.AutoRedisplay := (nValue > 0);
730 end;
731
732 8: begin // ecgevarHeadlight
733 if MainScene <> nil then
734 MainScene.HeadlightOn := (nValue > 0);
735 end;
736
737 9: begin // ecgevarOcclusionCulling
738 if Viewport <> nil then
739 Viewport.OcclusionCulling := (nValue > 0);
740 end;
741
742 10: begin // ecgevarPhongShading
743 if MainScene <> nil then
744 MainScene.RenderOptions.PhongShading := (nValue > 0);
745 end;
746 end;
747 except
748 on E: TObject do WritelnWarning('Window', 'CGE_SetVariableInt: ' + ExceptMessage(E));
749 end;
750 end;
751
752 function CGE_GetVariableInt(eVar: cInt32): cInt32; cdecl;
753 var
754 WalkNavigation: TCastleWalkNavigation;
755 begin
756 Result := -1;
757 if Window = nil then exit;
758 try
759 case eVar of
760 0: begin // ecgevarWalkHeadBobbing
761 WalkNavigation := GetWalkNavigation;
762 if (WalkNavigation <> nil) and (WalkNavigation.HeadBobbing > 0) then
763 Result := 1
764 else
765 Result := 0;
766 end;
767
768 1: begin // ecgevarEffectSSAO
769 if Viewport.ScreenSpaceAmbientOcclusionAvailable and
770 Viewport.ScreenSpaceAmbientOcclusion then
771 Result := 1
772 else
773 Result := 0;
774 end;
775
776 2: begin // ecgevarMouseLook
777 WalkNavigation := GetWalkNavigation;
778 if (WalkNavigation <> nil) and WalkNavigation.MouseLook then
779 Result := 1
780 else
781 Result := 0;
782 end;
783
784 3: begin // ecgevarCrossHair
785 if not Crosshair.CrosshairCtl.Exists then
786 Result := 0
787 else
788 if Crosshair.CrosshairCtl.Shape = csCross then
789 Result := 1
790 else
791 if Crosshair.CrosshairCtl.Shape = csCrossRect then
792 Result := 2
793 else
794 Result := 1;
795 end;
796
797 4: begin // ecgevarAnimationRunning
798 if Viewport.Camera.Animation then
799 Result := 1
800 else
801 Result := 0;
802 end;
803
804 5: begin // ecgevarWalkTouchCtl
805 Result := cgehelper_ConstFromTouchInterface(TouchNavigation.AutoWalkTouchInterface);
806 end;
807
808 6: begin // ecgevarScenePaused
809 if Viewport.Items.Paused then
810 Result := 1
811 else
812 Result := 0;
813 end;
814
815 7: begin // ecgevarAutoRedisplay
816 if Window.AutoRedisplay then
817 Result := 1
818 else
819 Result := 0;
820 end;
821
822 8: begin // ecgevarHeadlight
823 if (MainScene <> nil) and MainScene.HeadlightOn then
824 Result := 1
825 else
826 Result := 0;
827 end;
828
829 9: begin // ecgevarOcclusionCulling
830 if (Viewport <> nil) and Viewport.OcclusionCulling then
831 Result := 1
832 else
833 Result := 0;
834 end;
835
836 10: begin // ecgevarPhongShading
837 if (MainScene <> nil) and MainScene.RenderOptions.PhongShading then
838 Result := 1 else
839 Result := 0;
840 end;
841
842 else Result := -1; // unsupported variable
843 end;
844 except
845 on E: TObject do WritelnWarning('Window', 'CGE_GetVariableInt: ' + ExceptMessage(E));
846 end;
847 end;
848
849 procedure CGE_SetNodeFieldValue(szNodeName, szFieldName: pcchar;
850 fVal1, fVal2, fVal3, fVal4: cFloat); cdecl;
851 var
852 aField: TX3DField;
853 begin
854 try
855 if not CGE_VerifyScene('CGE_SetNodeFieldValue') then exit;
856
857 // find node and field
858 aField := MainScene.Field(PChar(szNodeName), PChar(szFieldName));
859 if aField = nil then Exit;
860
861 if aField is TSFVec2f then
862 TSFVec2f(aField).Send(Vector2(fVal1, fVal2))
863 else
864 if aField is TSFVec3f then
865 TSFVec3f(aField).Send(Vector3(fVal1, fVal2, fVal3))
866 else
867 if aField is TSFVec4f then
868 TSFVec4f(aField).Send(Vector4(fVal1, fVal2, fVal3, fVal4))
869 else
870 if aField is TSFVec2d then
871 TSFVec2d(aField).Send(Vector2Double(fVal1, fVal2))
872 else
873 if aField is TSFVec3d then
874 TSFVec3d(aField).Send(Vector3Double(fVal1, fVal2, fVal3))
875 else
876 if aField is TSFVec4d then
877 TSFVec4d(aField).Send(Vector4Double(fVal1, fVal2, fVal3, fVal4))
878 else
879 if aField is TSFFloat then
880 TSFFloat(aField).Send(fVal1)
881 else
882 if aField is TSFDouble then
883 TSFDouble(aField).Send(fVal1)
884 else
885 if aField is TSFLong then
886 TSFLong(aField).Send(Round(fVal1))
887 else
888 if aField is TSFInt32 then
889 TSFInt32(aField).Send(Round(fVal1))
890 else
891 if aField is TSFBool then
892 TSFBool(aField).Send(fVal1 <> 0.0);
893
894 except
895 on E: TObject do WritelnWarning('Window', 'CGE_SetNodeFieldValue: ' + ExceptMessage(E));
896 end;
897 end;
898
899 constructor TCrosshairManager.Create;
900 begin
901 inherited;
902 CrosshairCtl := TCastleCrosshair.Create(Window);
903 CrosshairCtl.Exists := false; // start as invisible
904 Window.Controls.InsertFront(CrosshairCtl);
905 end;
906
907 destructor TCrosshairManager.Destroy;
908 begin
909 Window.Controls.Remove(CrosshairCtl);
910 FreeAndNil(CrosshairCtl);
911 inherited;
912 end;
913
914 procedure TCrosshairManager.UpdateCrosshairImage;
915 begin
916 begin
917 if not CrosshairCtl.Exists then Exit;
918
919 if CrosshairActive then
920 CrosshairCtl.Shape := csCrossRect else
921 CrosshairCtl.Shape := csCross;
922 end;
923 end;
924
925 procedure TCrosshairManager.OnPointingDeviceSensorsChange(Sender: TObject);
926 var
927 OverSensor: Boolean;
928 SensorList: TPointingDeviceSensorList;
929 begin
930 { check if the crosshair (mouse) is over any sensor }
931 OverSensor := false;
932 SensorList := MainScene.PointingDeviceSensors;
933 if (SensorList <> nil) then
934 OverSensor := (SensorList.EnabledCount>0);
935
936 if CrosshairActive <> OverSensor then
937 begin
938 CrosshairActive := OverSensor;
939 UpdateCrosshairImage;
940 end;
941 end;
942
943 exports
944 CGE_Initialize,
945 CGE_Finalize,
946 CGE_Open,
947 CGE_Close,
948 CGE_GetOpenGLInformation,
949 CGE_Render,
950 CGE_Resize,
951 CGE_SetLibraryCallbackProc,
952 CGE_Update,
953 CGE_MouseDown,
954 CGE_Motion,
955 CGE_MouseUp,
956 CGE_MouseWheel,
957 CGE_KeyDown,
958 CGE_KeyUp,
959 CGE_LoadSceneFromFile,
960 CGE_SaveSceneToFile,
961 CGE_SetNavigationInputShortcut,
962 CGE_GetNavigationType,
963 CGE_SetNavigationType,
964 CGE_GetViewpointsCount,
965 CGE_GetViewpointName,
966 CGE_MoveToViewpoint,
967 CGE_AddViewpointFromCurrentView,
968 CGE_GetBoundingBox,
969 CGE_GetViewCoords,
970 CGE_MoveViewToCoords,
971 CGE_SaveScreenshotToFile,
972 CGE_SetTouchInterface,
973 CGE_SetUserInterface,
974 CGE_IncreaseSceneTime,
975 CGE_SetVariableInt,
976 CGE_GetVariableInt,
977 CGE_SetNodeFieldValue;
978
979 begin
980 SetExceptionMask([exInvalidOp, exDenormalized, exZeroDivide,
981 exOverflow, exUnderflow, exPrecision]);
982 end.