Debian Codegraph @

examples/viewport_and_scenes/fog_and_distance_culling/code/gameviewmain.pas

raw
1 {
2 Copyright 2003-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 { Main view, where most of the application logic takes place. }
16 unit GameViewMain;
17
18 interface
19
20 uses Classes,
21 CastleVectors, CastleComponentSerialize,
22 CastleUIControls, CastleControls, CastleKeysMouse, CastleScene,
23 CastleViewport;
24
25 type
26 { Main view, where most of the application logic takes place. }
27 TViewMain = class(TCastleView)
28 published
29 { Components designed using CGE editor.
30 These fields will be automatically initialized at Start. }
31 LabelFps: TCastleLabel;
32 LabelInfo: TCastleLabel;
33 SceneMain: TCastleScene;
34 MainViewport: TCastleViewport;
35 Fog1: TCastleFog;
36 private
37 FFogCulling: Boolean;
38 procedure SetFogCulling(const Value: Boolean);
39 property FogCulling: Boolean read FFogCulling write SetFogCulling;
40 public
41 constructor Create(AOwner: TComponent); override;
42 procedure Start; override;
43 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
44 function Press(const Event: TInputPressRelease): Boolean; override;
45 end;
46
47 var
48 ViewMain: TViewMain;
49
50 implementation
51
52 uses SysUtils,
53 X3DNodes, CastleUtils, CastleStringUtils;
54
55 { TViewMain ----------------------------------------------------------------- }
56
57 constructor TViewMain.Create(AOwner: TComponent);
58 begin
59 inherited;
60 DesignUrl := 'castle-data:/gameviewmain.castle-user-interface';
61 end;
62
63 procedure TViewMain.SetFogCulling(const Value: Boolean);
64 begin
65 if FFogCulling <> Value then
66 begin
67 FFogCulling := Value;
68
69 if FogCulling then
70 begin
71 MainViewport.Fog := Fog1;
72 SceneMain.DistanceCulling := Fog1.VisibilityRange;
73 end else
74 begin
75 MainViewport.Fog := nil;
76 SceneMain.DistanceCulling := 0;
77 end;
78 end;
79 end;
80
81 procedure TViewMain.Start;
82 begin
83 inherited;
84 FogCulling := true;
85 end;
86
87 procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
88 begin
89 inherited;
90 { This virtual method is executed every frame (many times per second). }
91 LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
92
93 LabelInfo.Caption := Format(
94 'Rendered Shapes: %d / %d' + NL +
95 'Fog culling: %s (toggle by Ctrl+F)' + NL +
96 'Frustum culling of each shape: %s (toggle by Ctrl+C)', [
97 MainViewport.Statistics.ShapesRendered,
98 MainViewport.Statistics.ShapesVisible,
99 BoolToStr(FogCulling, true),
100 BoolToStr(SceneMain.ShapeFrustumCulling, true)
101 ]);
102 end;
103
104 function TViewMain.Press(const Event: TInputPressRelease): Boolean;
105 begin
106 Result := inherited;
107 if Result then Exit; // allow the ancestor to handle keys
108
109 if Event.IsKey(CtrlF) then
110 FogCulling := not FogCulling;
111
112 if Event.IsKey(CtrlC) then
113 SceneMain.ShapeFrustumCulling := not SceneMain.ShapeFrustumCulling;
114 end;
115
116 end.