| 1 |
{ |
| 2 |
Copyright 2018-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 |
{ Game initialization and logic. } |
| 17 |
unit GameInitialize; |
| 18 |
|
| 19 |
{$ifdef FPC} {$modeswitch advancedrecords} {$endif} |
| 20 |
|
| 21 |
interface |
| 22 |
|
| 23 |
implementation |
| 24 |
|
| 25 |
uses SysUtils, |
| 26 |
CastleWindow, CastleScene, CastleControls, CastleLog, |
| 27 |
CastleFilesUtils, CastleSceneCore, CastleKeysMouse, CastleColors, |
| 28 |
CastleUIControls, CastleApplicationProperties, CastleScreenEffects, |
| 29 |
CastleViewport, X3DNodes, X3DLoad, CastleUtils, CastleCameras, |
| 30 |
CastleRenderOptions, CastleVectors; |
| 31 |
|
| 32 |
var |
| 33 |
Window: TCastleWindow; |
| 34 |
LabelFPS: TCastleLabel; |
| 35 |
|
| 36 |
{ 3 controls below have methods AddScreenEffect / RemoveScreenEffect |
| 37 |
that we will use. |
| 38 |
|
| 39 |
Note that we *could* insert each TCastleViewport |
| 40 |
as child of a new TCastleScreenEffects instance, |
| 41 |
and thus have 3 TCastleScreenEffects instances like this: |
| 42 |
|
| 43 |
- ScreenEffectsOnButtonImage, |
| 44 |
- ScreenEffectsOnViewport3D |
| 45 |
- ScreenEffectsOnViewport2D |
| 46 |
|
| 47 |
However, for demo, we demonstrate below another approach: |
| 48 |
Since TCastleViewport already descends from TCastleScreenEffects, |
| 49 |
we can call AddScreenEffect method directly on TCastleViewport. |
| 50 |
So we don't need something like ScreenEffectsOnViewport3D, ScreenEffectsOnViewport2D. } |
| 51 |
ScreenEffectsOnButtonImage: TCastleScreenEffects; |
| 52 |
Viewport2D: TCastleViewport; |
| 53 |
Viewport3D: TCastleViewport; |
| 54 |
|
| 55 |
{ TEffect -------------------------------------------------------------------- } |
| 56 |
|
| 57 |
type |
| 58 |
{ Container for all the information related to a particular screen effect. } |
| 59 |
TEffect = record |
| 60 |
{ Button that controls whether this effect is enabled. |
| 61 |
Button.Pressed is always synchronized with Enabled value of the effect |
| 62 |
in all controls. } |
| 63 |
Button: TCastleButton; |
| 64 |
|
| 65 |
{ 3 instances of TScreenEffectNode, for 3 controls |
| 66 |
(for ScreenEffectsOnButtonImage, |
| 67 |
for Viewport2D, |
| 68 |
for Viewport3D). } |
| 69 |
EffectNodes: array [0..2] of TScreenEffectNode; |
| 70 |
|
| 71 |
{ 3 instances of X3D node graph that contains TScreenEffectNode, |
| 72 |
for 3 controls. |
| 73 |
RootNodes[I] is a graph that always contains EffectNodes[I]. |
| 74 |
It may be equal to EffectNodes[I], but may have something more, |
| 75 |
e.g. a "support" TimeSensor node that provides value to the shader. } |
| 76 |
RootNodes: array [0..2] of TAbstractChildNode; |
| 77 |
|
| 78 |
{ Change Button.Pressed, synchronize Enabled state of all effect nodes. } |
| 79 |
procedure ToggleButton; |
| 80 |
end; |
| 81 |
|
| 82 |
procedure TEffect.ToggleButton; |
| 83 |
var |
| 84 |
E: TScreenEffectNode; |
| 85 |
begin |
| 86 |
Button.Pressed := not Button.Pressed; |
| 87 |
for E in EffectNodes do |
| 88 |
E.Enabled := Button.Pressed; |
| 89 |
end; |
| 90 |
|
| 91 |
var |
| 92 |
FilmGrain, Pixelate, EdgeDetect: TEffect; |
| 93 |
|
| 94 |
{ handle button clicks ------------------------------------------------------- } |
| 95 |
|
| 96 |
type |
| 97 |
TEventsHandler = class |
| 98 |
class procedure ToggleFilmGrain(Sender: TObject); |
| 99 |
class procedure TogglePixelate(Sender: TObject); |
| 100 |
class procedure ToggleEdgeDetect(Sender: TObject); |
| 101 |
end; |
| 102 |
|
| 103 |
class procedure TEventsHandler.ToggleFilmGrain(Sender: TObject); |
| 104 |
begin |
| 105 |
FilmGrain.ToggleButton; |
| 106 |
end; |
| 107 |
|
| 108 |
class procedure TEventsHandler.TogglePixelate(Sender: TObject); |
| 109 |
begin |
| 110 |
Pixelate.ToggleButton; |
| 111 |
end; |
| 112 |
|
| 113 |
class procedure TEventsHandler.ToggleEdgeDetect(Sender: TObject); |
| 114 |
begin |
| 115 |
EdgeDetect.ToggleButton; |
| 116 |
end; |
| 117 |
|
| 118 |
{ routines ------------------------------------------------------------------- } |
| 119 |
|
| 120 |
procedure WindowUpdate(Container: TCastleContainer); |
| 121 |
begin |
| 122 |
LabelFPS.Caption := 'FPS: ' + Container.Fps.ToString; |
| 123 |
end; |
| 124 |
|
| 125 |
{ One-time initialization of resources. } |
| 126 |
procedure ApplicationInitialize; |
| 127 |
|
| 128 |
{ Just to show that it is possible, build TScreenEffectNode performing |
| 129 |
"edge detect" effect completely using Pascal, without any external |
| 130 |
X3D or GLSL files. } |
| 131 |
function CreateEdgeDetectNode: TScreenEffectNode; |
| 132 |
var |
| 133 |
ComposedShader: TComposedShaderNode; |
| 134 |
FragmentShader: TShaderPartNode; |
| 135 |
begin |
| 136 |
FragmentShader := TShaderPartNode.Create; |
| 137 |
FragmentShader.ShaderType := stFragment; |
| 138 |
FragmentShader.Contents := |
| 139 |
'void main (void)' + NL + |
| 140 |
'{' + NL + |
| 141 |
' float x = screenf_x();' + NL + |
| 142 |
' float y = screenf_y();' + NL + |
| 143 |
' #define SCAN_DISTANCE 1.0' + NL + |
| 144 |
' vec4 left = screenf_get_color(vec2(x - SCAN_DISTANCE, y));' + NL + |
| 145 |
' vec4 right = screenf_get_color(vec2(x + SCAN_DISTANCE, y));' + NL + |
| 146 |
' vec4 top = screenf_get_color(vec2(x, y - SCAN_DISTANCE));' + NL + |
| 147 |
' vec4 bottom = screenf_get_color(vec2(x, y + SCAN_DISTANCE));' + NL + |
| 148 |
' gl_FragColor = (abs(left - right) + abs(top - bottom)) / 2.0;' + NL + |
| 149 |
'}'; |
| 150 |
|
| 151 |
ComposedShader := TComposedShaderNode.Create; |
| 152 |
ComposedShader.SetParts([FragmentShader]); |
| 153 |
|
| 154 |
Result := TScreenEffectNode.Create; |
| 155 |
Result.SetShaders([ComposedShader]); |
| 156 |
end; |
| 157 |
|
| 158 |
procedure InitializeScreenEffects; |
| 159 |
var |
| 160 |
I: Integer; |
| 161 |
begin |
| 162 |
{ The implementation of this procedure got complicated due to the amount |
| 163 |
of things I wanted to demonstrate. But the basic API to add screen effects |
| 164 |
is very simple: you want to load the X3D graph containing one or more |
| 165 |
TScreenEffectNode, and call AddScreenEffect with it. |
| 166 |
For example, this is how to load film grain effect in Viewport3D: |
| 167 |
|
| 168 |
Viewport3D.AddScreenEffect( |
| 169 |
LoadNode('castle-data:/screen_effects/film_grain.x3dv')); |
| 170 |
|
| 171 |
Alternatively, you may want to save the LoadNode result, |
| 172 |
and find there an instance of TScreenEffectNode, to be able to |
| 173 |
toggle it's Enabled property: |
| 174 |
|
| 175 |
var |
| 176 |
RootNode: TX3DRootNode; |
| 177 |
ScreenEffectNode: TScreenEffectNode; |
| 178 |
begin |
| 179 |
RootNode := LoadNode('castle-data:/screen_effects/film_grain.x3dv'); |
| 180 |
ScreenEffectNode := RootNode.FindNodeByName(TScreenEffectNode, |
| 181 |
'FilmGrainScreenEffect', true) as TScreenEffectNode; |
| 182 |
Viewport3D.AddScreenEffect(RootNode); |
| 183 |
end; |
| 184 |
|
| 185 |
Note that you should call "AddScreenEffect(RootNode)", |
| 186 |
not "AddScreenEffect(ScreenEffectNode)". |
| 187 |
That is because RootNode may contain some additional "support" |
| 188 |
nodes, e.g. TimeSensor nodes that feed the time to your shader. |
| 189 |
|
| 190 |
What we do below is somewhat more complicated code using this API, |
| 191 |
as we want to repeat above idea 9 times, and store results for later. |
| 192 |
} |
| 193 |
|
| 194 |
for I := 0 to 2 do |
| 195 |
begin |
| 196 |
FilmGrain.RootNodes[I] := LoadNode('castle-data:/screen_effects/film_grain.x3dv'); |
| 197 |
FilmGrain.EffectNodes[I] := FilmGrain.RootNodes[I].FindNode(TScreenEffectNode, |
| 198 |
'FilmGrainScreenEffect') as TScreenEffectNode; |
| 199 |
|
| 200 |
Pixelate.RootNodes[I] := LoadNode('castle-data:/screen_effects/pixelate.x3dv'); |
| 201 |
Pixelate.EffectNodes[I] := Pixelate.RootNodes[I].FindNode(TScreenEffectNode, |
| 202 |
'PixelateScreenEffect') as TScreenEffectNode; |
| 203 |
|
| 204 |
{ In case of "edge detect" node, |
| 205 |
the EdgeDetect.EffectNodes[I] equals EdgeDetect.RootNodes[I]. |
| 206 |
The X3D graph to define this effect is only a simple TScreenEffectNode, |
| 207 |
without any additions. } |
| 208 |
EdgeDetect.EffectNodes[I] := CreateEdgeDetectNode; |
| 209 |
EdgeDetect.RootNodes[I] := EdgeDetect.EffectNodes[I]; |
| 210 |
|
| 211 |
{ Synchronize Enabled state with buttons pressed } |
| 212 |
FilmGrain.EffectNodes[I].Enabled := FilmGrain.Button.Pressed; |
| 213 |
Pixelate.EffectNodes[I].Enabled := Pixelate.Button.Pressed; |
| 214 |
EdgeDetect.EffectNodes[I].Enabled := EdgeDetect.Button.Pressed; |
| 215 |
end; |
| 216 |
|
| 217 |
ScreenEffectsOnButtonImage.AddScreenEffect(FilmGrain.RootNodes[0]); |
| 218 |
ScreenEffectsOnButtonImage.AddScreenEffect(Pixelate.RootNodes[0]); |
| 219 |
ScreenEffectsOnButtonImage.AddScreenEffect(EdgeDetect.RootNodes[0]); |
| 220 |
|
| 221 |
Viewport3D.AddScreenEffect(FilmGrain.RootNodes[1]); |
| 222 |
Viewport3D.AddScreenEffect(Pixelate.RootNodes[1]); |
| 223 |
Viewport3D.AddScreenEffect(EdgeDetect.RootNodes[1]); |
| 224 |
|
| 225 |
Viewport2D.AddScreenEffect(FilmGrain.RootNodes[2]); |
| 226 |
Viewport2D.AddScreenEffect(Pixelate.RootNodes[2]); |
| 227 |
Viewport2D.AddScreenEffect(EdgeDetect.RootNodes[2]); |
| 228 |
end; |
| 229 |
|
| 230 |
var |
| 231 |
Background: TCastleRectangleControl; |
| 232 |
ExampleImage: TCastleImageControl; |
| 233 |
ExampleButton: TCastleButton; |
| 234 |
Example3DScene: TCastleScene; |
| 235 |
Example2DScene: TCastleScene; |
| 236 |
Label1, Label2, Label3, LabelEffects: TCastleLabel; |
| 237 |
BottomControls: TCastleHorizontalGroup; |
| 238 |
begin |
| 239 |
{ Scalable UI (adjusts to any window size in a smart way). } |
| 240 |
Window.Container.UIReferenceWidth := 1024; |
| 241 |
Window.Container.UIReferenceHeight := 768; |
| 242 |
Window.Container.UIScaling := usEncloseReferenceSize; |
| 243 |
|
| 244 |
{ Insert almost-black background, otherwise (as Window is only TCastleWindow) |
| 245 |
the background would be undefined. } |
| 246 |
Background := TCastleRectangleControl.Create(Application); |
| 247 |
Background.Color := Vector4(0.1, 0.1, 0.1, 1); |
| 248 |
Background.FullSize := true; |
| 249 |
Window.Controls.InsertBack(Background); |
| 250 |
|
| 251 |
{ Show a label with frames per second information } |
| 252 |
LabelFPS := TCastleLabel.Create(Application); |
| 253 |
LabelFPS.Anchor(vpTop, -10); |
| 254 |
LabelFPS.Anchor(hpRight, -10); |
| 255 |
LabelFPS.Color := Yellow; |
| 256 |
Window.Controls.InsertFront(LabelFPS); |
| 257 |
|
| 258 |
ScreenEffectsOnButtonImage := TCastleScreenEffects.Create(Application); |
| 259 |
ScreenEffectsOnButtonImage.Anchor(hpMiddle, - 300 - 50); |
| 260 |
ScreenEffectsOnButtonImage.Anchor(vpMiddle); |
| 261 |
ScreenEffectsOnButtonImage.Width := 300; |
| 262 |
ScreenEffectsOnButtonImage.Height := 300; |
| 263 |
Window.Controls.InsertFront(ScreenEffectsOnButtonImage); |
| 264 |
|
| 265 |
ExampleImage := TCastleImageControl.Create(Application); |
| 266 |
ExampleImage.URL := 'castle-data:/example_image.png'; |
| 267 |
ExampleImage.Anchor(hpMiddle); |
| 268 |
ExampleImage.Anchor(vpTop); |
| 269 |
ExampleImage.Width := 300; |
| 270 |
ExampleImage.Height := 150; |
| 271 |
ExampleImage.Stretch := true; |
| 272 |
ScreenEffectsOnButtonImage.InsertFront(ExampleImage); |
| 273 |
|
| 274 |
ExampleButton := TCastleButton.Create(Application); |
| 275 |
ExampleButton.Caption := 'Example Button'; |
| 276 |
ExampleButton.AutoSize := false; |
| 277 |
ExampleButton.Anchor(hpMiddle); |
| 278 |
ExampleButton.Anchor(vpBottom); |
| 279 |
ExampleButton.Width := 300; |
| 280 |
ExampleButton.Height := 150; |
| 281 |
ScreenEffectsOnButtonImage.InsertFront(ExampleButton); |
| 282 |
|
| 283 |
Viewport2D := TCastleViewport.Create(Application); |
| 284 |
Viewport2D.Setup2D; |
| 285 |
Viewport2D.Anchor(hpMiddle); |
| 286 |
Viewport2D.Anchor(vpMiddle); |
| 287 |
Viewport2D.FullSize := false; |
| 288 |
Viewport2D.Width := 300; |
| 289 |
Viewport2D.Height := 300; |
| 290 |
Viewport2D.BackgroundColor := Gray; |
| 291 |
Viewport2D.Camera.Orthographic.Width := 2000; |
| 292 |
Viewport2D.Camera.Orthographic.Origin := Vector2(0.5, 0.5); |
| 293 |
Window.Controls.InsertFront(Viewport2D); |
| 294 |
|
| 295 |
Example2DScene := TCastleScene.Create(Application); |
| 296 |
Example2DScene.Load('castle-data:/example_2d_scene/dragon/dragon.json'); |
| 297 |
Example2DScene.PreciseCollisions := true; |
| 298 |
Example2DScene.ProcessEvents := true; |
| 299 |
Example2DScene.PlayAnimation('flying', true); |
| 300 |
Viewport2D.Items.Add(Example2DScene); |
| 301 |
Viewport2D.AssignDefaultCamera; |
| 302 |
|
| 303 |
Viewport3D := TCastleViewport.Create(Application); |
| 304 |
Viewport3D.InsertBack(TCastleExamineNavigation.Create(Application)); |
| 305 |
Viewport3D.Anchor(hpMiddle, 300 + 50); |
| 306 |
Viewport3D.Anchor(vpMiddle); |
| 307 |
Viewport3D.FullSize := false; |
| 308 |
Viewport3D.Width := 300; |
| 309 |
Viewport3D.Height := 300; |
| 310 |
Viewport3D.BackgroundColor := Gray; |
| 311 |
Window.Controls.InsertFront(Viewport3D); |
| 312 |
|
| 313 |
Example3DScene := TCastleScene.Create(Application); |
| 314 |
Example3DScene.Load('castle-data:/example_3d_scene.x3dv'); |
| 315 |
Example3DScene.PreciseCollisions := true; |
| 316 |
Example3DScene.ProcessEvents := true; |
| 317 |
Example3DScene.PlayAnimation('AnimateRotation', true); |
| 318 |
Viewport3D.Items.Add(Example3DScene); |
| 319 |
Viewport3D.Camera.Add(TCastleDirectionalLight.Create(Application)); // headlight |
| 320 |
Viewport3D.AssignDefaultCamera; |
| 321 |
|
| 322 |
Label1 := TCastleLabel.Create(Application); |
| 323 |
Label1.Caption := 'Example button and image'; |
| 324 |
Label1.Anchor(vpBottom, vpMiddle, 150 + 20); |
| 325 |
Label1.Anchor(hpMiddle, -300 - 50); |
| 326 |
Label1.Color := White; |
| 327 |
Window.Controls.InsertFront(Label1); |
| 328 |
|
| 329 |
Label2 := TCastleLabel.Create(Application); |
| 330 |
Label2.Caption := '2D Viewport'; |
| 331 |
Label2.Anchor(vpBottom, vpMiddle, 150 + 20); |
| 332 |
Label2.Anchor(hpMiddle); |
| 333 |
Label2.Color := White; |
| 334 |
Window.Controls.InsertFront(Label2); |
| 335 |
|
| 336 |
Label3 := TCastleLabel.Create(Application); |
| 337 |
Label3.Caption := '3D Viewport'; |
| 338 |
Label3.Anchor(vpBottom, vpMiddle, 150 + 20); |
| 339 |
Label3.Anchor(hpMiddle, 300 + 50); |
| 340 |
Label3.Color := White; |
| 341 |
Window.Controls.InsertFront(Label3); |
| 342 |
|
| 343 |
BottomControls := TCastleHorizontalGroup.Create(Application); |
| 344 |
BottomControls.Anchor(hpMiddle); |
| 345 |
BottomControls.Anchor(vpBottom, 10); |
| 346 |
BottomControls.Padding := 10; |
| 347 |
BottomControls.Spacing := 10; |
| 348 |
Window.Controls.InsertFront(BottomControls); |
| 349 |
|
| 350 |
LabelEffects := TCastleLabel.Create(Application); |
| 351 |
LabelEffects.Caption := 'Toggle Screen Effects:'; |
| 352 |
LabelEffects.Color := White; |
| 353 |
BottomControls.InsertFront(LabelEffects); |
| 354 |
|
| 355 |
FilmGrain.Button := TCastleButton.Create(Application); |
| 356 |
FilmGrain.Button.Caption := 'Film Grain'; |
| 357 |
FilmGrain.Button.Toggle := true; |
| 358 |
FilmGrain.Button.OnClick := {$ifdef FPC}@{$endif} TEventsHandler.ToggleFilmGrain; |
| 359 |
BottomControls.InsertFront(FilmGrain.Button); |
| 360 |
|
| 361 |
Pixelate.Button := TCastleButton.Create(Application); |
| 362 |
Pixelate.Button.Caption := 'Pixelate'; |
| 363 |
Pixelate.Button.Toggle := true; |
| 364 |
Pixelate.Button.OnClick := {$ifdef FPC}@{$endif} TEventsHandler.TogglePixelate; |
| 365 |
BottomControls.InsertFront(Pixelate.Button); |
| 366 |
|
| 367 |
EdgeDetect.Button := TCastleButton.Create(Application); |
| 368 |
EdgeDetect.Button.Caption := 'Edge Detect'; |
| 369 |
EdgeDetect.Button.Toggle := true; |
| 370 |
EdgeDetect.Button.OnClick := {$ifdef FPC}@{$endif} TEventsHandler.ToggleEdgeDetect; |
| 371 |
BottomControls.InsertFront(EdgeDetect.Button); |
| 372 |
|
| 373 |
InitializeScreenEffects; |
| 374 |
end; |
| 375 |
|
| 376 |
initialization |
| 377 |
{ Initialize Application.OnInitialize. } |
| 378 |
Application.OnInitialize := @ApplicationInitialize; |
| 379 |
|
| 380 |
{ Create and assign Application.MainWindow. } |
| 381 |
Window := TCastleWindow.Create(Application); |
| 382 |
Application.MainWindow := Window; |
| 383 |
end. |