| 1 |
{ |
| 2 |
Copyright 2023-2023 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 |
|
| 16 |
{ Main view, where most of the application logic takes place. } |
| 17 |
unit GameViewMain; |
| 18 |
|
| 19 |
interface |
| 20 |
|
| 21 |
uses Classes, |
| 22 |
CastleVectors, CastleComponentSerialize, CastleCameras, |
| 23 |
CastleUIControls, CastleControls, CastleKeysMouse, CastleViewport, |
| 24 |
CastleTransform, CastleScene; |
| 25 |
|
| 26 |
type |
| 27 |
{ Main view, where most of the application logic takes place. } |
| 28 |
TViewMain = class(TCastleView) |
| 29 |
published |
| 30 |
{ Components designed using CGE editor. |
| 31 |
These fields will be automatically initialized at Start. } |
| 32 |
LabelFps: TCastleLabel; |
| 33 |
MainWalkNavigation: TCastleWalkNavigation; |
| 34 |
MainViewport: TCastleViewport; |
| 35 |
CheckboxOcclusionCulling: TCastleCheckbox; |
| 36 |
private |
| 37 |
procedure ChangeOcclusionCulling(Sender: TObject); |
| 38 |
public |
| 39 |
constructor Create(AOwner: TComponent); override; |
| 40 |
procedure Start; override; |
| 41 |
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override; |
| 42 |
function Press(const Event: TInputPressRelease): Boolean; override; |
| 43 |
end; |
| 44 |
|
| 45 |
var |
| 46 |
ViewMain: TViewMain; |
| 47 |
|
| 48 |
implementation |
| 49 |
|
| 50 |
uses SysUtils, |
| 51 |
CastleUtils, CastleStringUtils; |
| 52 |
|
| 53 |
{ TViewMain ----------------------------------------------------------------- } |
| 54 |
|
| 55 |
constructor TViewMain.Create(AOwner: TComponent); |
| 56 |
begin |
| 57 |
inherited; |
| 58 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 59 |
end; |
| 60 |
|
| 61 |
procedure TViewMain.Start; |
| 62 |
begin |
| 63 |
inherited; |
| 64 |
|
| 65 |
CheckboxOcclusionCulling.OnChange := {$ifdef FPC}@{$endif} ChangeOcclusionCulling; |
| 66 |
|
| 67 |
{ initialize UI } |
| 68 |
CheckboxOcclusionCulling.Checked := MainViewport.OcclusionCulling; |
| 69 |
end; |
| 70 |
|
| 71 |
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 72 |
begin |
| 73 |
inherited; |
| 74 |
{ This virtual method is executed every frame (many times per second). } |
| 75 |
LabelFps.Caption := |
| 76 |
'FPS: ' + Container.Fps.ToString + NL + |
| 77 |
'Rendered: ' + MainViewport.Statistics.ToString; |
| 78 |
end; |
| 79 |
|
| 80 |
procedure TViewMain.ChangeOcclusionCulling(Sender: TObject); |
| 81 |
begin |
| 82 |
MainViewport.OcclusionCulling := CheckboxOcclusionCulling.Checked; |
| 83 |
end; |
| 84 |
|
| 85 |
function TViewMain.Press(const Event: TInputPressRelease): Boolean; |
| 86 |
begin |
| 87 |
Result := inherited; |
| 88 |
if Result then Exit; // allow the ancestor to handle keys |
| 89 |
|
| 90 |
if Event.IsKey(CtrlO) then |
| 91 |
begin |
| 92 |
MainViewport.OcclusionCulling := not MainViewport.OcclusionCulling; |
| 93 |
CheckboxOcclusionCulling.Checked := MainViewport.OcclusionCulling; |
| 94 |
Exit(true); |
| 95 |
end; |
| 96 |
|
| 97 |
if Event.IsMouseButton(buttonRight) then |
| 98 |
begin |
| 99 |
MainWalkNavigation.MouseLook := not MainWalkNavigation.MouseLook; |
| 100 |
Exit(true); |
| 101 |
end; |
| 102 |
end; |
| 103 |
|
| 104 |
end. |