| 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, CastleScene, CastleCameras, |
| 23 |
CastleUIControls, CastleControls, CastleKeysMouse, CastleGLImages, CastleViewport, |
| 24 |
CastleTransform, CastleGLUtils; |
| 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, LabelGlInformation: TCastleLabel; |
| 33 |
ScenePhong: TCastleScene; |
| 34 |
MainViewport: TCastleViewport; |
| 35 |
WalkNavigation: TCastleWalkNavigation; |
| 36 |
UnlitMeshParent: TCastleTransform; |
| 37 |
private |
| 38 |
DrawableImage: TDrawableImage; |
| 39 |
public |
| 40 |
constructor Create(AOwner: TComponent); override; |
| 41 |
procedure Start; override; |
| 42 |
procedure Stop; override; |
| 43 |
procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override; |
| 44 |
procedure RenderOverChildren; override; |
| 45 |
end; |
| 46 |
|
| 47 |
var |
| 48 |
ViewMain: TViewMain; |
| 49 |
|
| 50 |
implementation |
| 51 |
|
| 52 |
uses SysUtils, |
| 53 |
CastleLoadGltf, CastleRectangles, CastleImages, |
| 54 |
CastleBoxes, CastleColors, CastleRenderContext, CastleUtils, X3DLoad, |
| 55 |
GameMyMesh; |
| 56 |
|
| 57 |
{ TViewMain ----------------------------------------------------------------- } |
| 58 |
|
| 59 |
constructor TViewMain.Create(AOwner: TComponent); |
| 60 |
begin |
| 61 |
inherited; |
| 62 |
DesignUrl := 'castle-data:/gameviewmain.castle-user-interface'; |
| 63 |
end; |
| 64 |
|
| 65 |
procedure TViewMain.Start; |
| 66 |
var |
| 67 |
MyMesh: TMyMesh; |
| 68 |
begin |
| 69 |
inherited; |
| 70 |
|
| 71 |
LabelGlInformation.Caption := |
| 72 |
'OpenGL capabilities requested: ' + CapabilitiesStr[TGLFeatures.RequestCapabilities] + NL + |
| 73 |
'(see log for the details about context)'; |
| 74 |
|
| 75 |
{ Load ScenePhong.Url with temporary GltfForcePhongMaterials set to true. |
| 76 |
We want to test Phong shading works in ForceFixedFunction too. } |
| 77 |
GltfForcePhongMaterials := true; |
| 78 |
ScenePhong.Url := 'castle-data:/sample_3d.gltf'; |
| 79 |
GltfForcePhongMaterials := false; |
| 80 |
|
| 81 |
DrawableImage := TDrawableImage.Create('castle-data:/texture_alpha.png'); |
| 82 |
|
| 83 |
MyMesh := TMyMesh.Create(FreeAtStop); |
| 84 |
UnlitMeshParent.Add(MyMesh); |
| 85 |
|
| 86 |
MainViewport.AddScreenEffect(LoadNode('castle-data:/screen_effect_frame.x3dv')); |
| 87 |
end; |
| 88 |
|
| 89 |
procedure TViewMain.Stop; |
| 90 |
begin |
| 91 |
FreeAndNil(DrawableImage); |
| 92 |
inherited; |
| 93 |
end; |
| 94 |
|
| 95 |
procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean); |
| 96 |
begin |
| 97 |
inherited; |
| 98 |
{ This virtual method is executed every frame (many times per second). } |
| 99 |
Assert(LabelFps <> nil, 'If you remove LabelFps from the design, remember to remove also the assignment "LabelFps.Caption := ..." from code'); |
| 100 |
LabelFps.Caption := 'FPS: ' + Container.Fps.ToString; |
| 101 |
|
| 102 |
WalkNavigation.MouseLook := buttonRight in Container.MousePressed; |
| 103 |
end; |
| 104 |
|
| 105 |
procedure TViewMain.RenderOverChildren; |
| 106 |
begin |
| 107 |
inherited; |
| 108 |
{ Do some direct drawing, to test this API works with ForceFixedFunction too. } |
| 109 |
DrawRectangle(FloatRectangle(10, 10, 50, 50), Green); |
| 110 |
DrawableImage.Draw(100, 10); |
| 111 |
|
| 112 |
{ Render DrawableImage again, forcing alpha testing } |
| 113 |
DrawableImage.Alpha := acTest; |
| 114 |
DrawableImage.Draw(400, 10); |
| 115 |
DrawableImage.Alpha := acAuto; |
| 116 |
|
| 117 |
FallbackFont.Print(700, 10, Red, 'Another sample text'); |
| 118 |
end; |
| 119 |
|
| 120 |
end. |