Debian Codegraph @

examples/network/custom_url_handler/code/gameinitialize.pas

raw
1 {
2 Copyright 2019-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 interface
20
21 implementation
22
23 uses SysUtils, Classes, URIParser,
24 CastleWindow, CastleScene, CastleControls, CastleLog, CastleUtils,
25 CastleFilesUtils, CastleSceneCore, CastleKeysMouse, CastleColors,
26 CastleUIControls, CastleApplicationProperties, CastleDownload, CastleStringUtils,
27 CastleUriUtils, CastleViewport, CastleCameras,
28 GameUnzip;
29
30 var
31 Window: TCastleWindow;
32 Viewport: TCastleViewport;
33 Status: TCastleLabel;
34 ExampleImage: TCastleImageControl;
35 ExampleScene: TCastleScene;
36
37 { TPackedDataReader ---------------------------------------------------------- }
38
39 type
40 TPackedDataReader = class
41 public
42 SourceZipFileName: String;
43 function ReadUrl(const Url: String; out MimeType: string): TStream;
44 destructor Destroy; override;
45 end;
46
47 var
48 PackedDataReader: TPackedDataReader;
49
50 function TPackedDataReader.ReadUrl(const Url: String; out MimeType: string): TStream;
51 var
52 U: TURI;
53 FileInZip: String;
54 begin
55 U := ParseURI(Url);
56 FileInZip := PrefixRemove('/', U.Path + U.Document, false);
57 Result := UnzipFile(SourceZipFileName, FileInZip);
58
59 { Determine mime type from Url, which practically means:
60 determine content type from filename extension. }
61 MimeType := UriMimeType(Url);
62 end;
63
64 destructor TPackedDataReader.Destroy;
65 begin
66 inherited;
67 end;
68
69 { routines ------------------------------------------------------------------- }
70
71 procedure WindowUpdate(Container: TCastleContainer);
72 begin
73 // ... do something every frame
74 Status.Caption := 'FPS: ' + Container.Fps.ToString;
75 end;
76
77 procedure WindowPress(Container: TCastleContainer; const Event: TInputPressRelease);
78 begin
79 // ... react to press of key, mouse, touch
80 end;
81
82 { One-time initialization of resources. }
83 procedure ApplicationInitialize;
84 begin
85 { initialize PackedDataReader to read ZIP when we access my-packed-data:/ }
86 PackedDataReader := TPackedDataReader.Create;
87 PackedDataReader.SourceZipFileName := UriToFilenameSafe('castle-data:/packed_game_data.zip');
88 RegisterUrlProtocol('my-packed-data',
89 {$ifdef FPC}@{$endif} PackedDataReader.ReadUrl, nil);
90
91 { make following calls to castle-data:/ also load data from ZIP }
92 ApplicationDataOverride := 'my-packed-data:/';
93
94 { Assign Window callbacks }
95 Window.OnUpdate := @WindowUpdate;
96 Window.OnPress := @WindowPress;
97
98 { For a scalable UI (adjusts to any window size in a smart way), use UIScaling }
99 Window.Container.UIReferenceWidth := 1024;
100 Window.Container.UIReferenceHeight := 768;
101 Window.Container.UIScaling := usEncloseReferenceSize;
102
103 Viewport := TCastleViewport.Create(Application);
104 Viewport.FullSize := true;
105 Viewport.InsertBack(TCastleExamineNavigation.Create(Application));
106 Window.Controls.InsertFront(Viewport);
107
108 { Show a label with frames per second information }
109 Status := TCastleLabel.Create(Application);
110 Status.Anchor(vpTop, -10);
111 Status.Anchor(hpRight, -10);
112 Status.Color := Yellow; // you could also use "Vector4(1, 1, 0, 1)" instead of Yellow
113 Window.Controls.InsertFront(Status);
114
115 { Show 2D image }
116 ExampleImage := TCastleImageControl.Create(Application);
117 ExampleImage.URL := 'castle-data:/example_image.png';
118 // This also works:
119 // 'my-packed-data:/example_image.png';
120 ExampleImage.Anchor(vpBottom, 100);
121 ExampleImage.Anchor(hpLeft, 100);
122 Window.Controls.InsertFront(ExampleImage);
123
124 { Show a 3D object (TCastleScene) inside a Viewport
125 (which acts as a full-screen viewport by default). }
126 ExampleScene := TCastleScene.Create(Application);
127 ExampleScene.Load('castle-data:/example_scene.x3dv');
128 ExampleScene.PreciseCollisions := true;
129
130 Viewport.Items.Add(ExampleScene);
131
132 // headlight
133 Viewport.Camera.Add(TCastleDirectionalLight.Create(Application));
134
135 // nice initial camera position
136 Viewport.AssignDefaultCamera;
137 end;
138
139 initialization
140 { This initialization section configures:
141 - Application.OnInitialize
142 - Application.MainWindow
143 - determines initial window size
144
145 You should not need to do anything more in this initialization section.
146 Most of your actual application initialization (in particular, any file reading)
147 should happen inside ApplicationInitialize. }
148
149 Application.OnInitialize := @ApplicationInitialize;
150
151 Window := TCastleWindow.Create(Application);
152 Application.MainWindow := Window;
153
154 { Optionally, adjust window fullscreen state and size at this point.
155 Examples:
156
157 Run fullscreen:
158
159 Window.FullScreen := true;
160
161 Run in a 600x400 window:
162
163 Window.FullScreen := false; // default
164 Window.Width := 600;
165 Window.Height := 400;
166
167 Run in a window taking 2/3 of screen (width and height):
168
169 Window.FullScreen := false; // default
170 Window.Width := Application.ScreenWidth * 2 div 3;
171 Window.Height := Application.ScreenHeight * 2 div 3;
172
173 Note that some platforms (like mobile) ignore these window sizes.
174 }
175
176 { Handle command-line parameters like --fullscreen and --window.
177 By doing this last, you let user to override your fullscreen / mode setup. }
178 Window.ParseParameters;
179 finalization
180 FreeAndNil(PackedDataReader);
181 end.