{ Copyright 2019-2023 Michalis Kamburelis. This file is part of "Castle Game Engine". "Castle Game Engine" is free software; see the file COPYING.txt, included in this distribution, for details about the copyright. "Castle Game Engine" is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ---------------------------------------------------------------------------- } { Game initialization and logic. } unit GameInitialize; interface implementation uses SysUtils, Classes, URIParser, CastleWindow, CastleScene, CastleControls, CastleLog, CastleUtils, CastleFilesUtils, CastleSceneCore, CastleKeysMouse, CastleColors, CastleUIControls, CastleApplicationProperties, CastleDownload, CastleStringUtils, CastleUriUtils, CastleViewport, CastleCameras, GameUnzip; var Window: TCastleWindow; Viewport: TCastleViewport; Status: TCastleLabel; ExampleImage: TCastleImageControl; ExampleScene: TCastleScene; { TPackedDataReader ---------------------------------------------------------- } type TPackedDataReader = class public SourceZipFileName: String; function ReadUrl(const Url: String; out MimeType: string): TStream; destructor Destroy; override; end; var PackedDataReader: TPackedDataReader; function TPackedDataReader.ReadUrl(const Url: String; out MimeType: string): TStream; var U: TURI; FileInZip: String; begin U := ParseURI(Url); FileInZip := PrefixRemove('/', U.Path + U.Document, false); Result := UnzipFile(SourceZipFileName, FileInZip); { Determine mime type from Url, which practically means: determine content type from filename extension. } MimeType := UriMimeType(Url); end; destructor TPackedDataReader.Destroy; begin inherited; end; { routines ------------------------------------------------------------------- } procedure WindowUpdate(Container: TCastleContainer); begin // ... do something every frame Status.Caption := 'FPS: ' + Container.Fps.ToString; end; procedure WindowPress(Container: TCastleContainer; const Event: TInputPressRelease); begin // ... react to press of key, mouse, touch end; { One-time initialization of resources. } procedure ApplicationInitialize; begin { initialize PackedDataReader to read ZIP when we access my-packed-data:/ } PackedDataReader := TPackedDataReader.Create; PackedDataReader.SourceZipFileName := UriToFilenameSafe('castle-data:/packed_game_data.zip'); RegisterUrlProtocol('my-packed-data', {$ifdef FPC}@{$endif} PackedDataReader.ReadUrl, nil); { make following calls to castle-data:/ also load data from ZIP } ApplicationDataOverride := 'my-packed-data:/'; { Assign Window callbacks } Window.OnUpdate := @WindowUpdate; Window.OnPress := @WindowPress; { For a scalable UI (adjusts to any window size in a smart way), use UIScaling } Window.Container.UIReferenceWidth := 1024; Window.Container.UIReferenceHeight := 768; Window.Container.UIScaling := usEncloseReferenceSize; Viewport := TCastleViewport.Create(Application); Viewport.FullSize := true; Viewport.InsertBack(TCastleExamineNavigation.Create(Application)); Window.Controls.InsertFront(Viewport); { Show a label with frames per second information } Status := TCastleLabel.Create(Application); Status.Anchor(vpTop, -10); Status.Anchor(hpRight, -10); Status.Color := Yellow; // you could also use "Vector4(1, 1, 0, 1)" instead of Yellow Window.Controls.InsertFront(Status); { Show 2D image } ExampleImage := TCastleImageControl.Create(Application); ExampleImage.URL := 'castle-data:/example_image.png'; // This also works: // 'my-packed-data:/example_image.png'; ExampleImage.Anchor(vpBottom, 100); ExampleImage.Anchor(hpLeft, 100); Window.Controls.InsertFront(ExampleImage); { Show a 3D object (TCastleScene) inside a Viewport (which acts as a full-screen viewport by default). } ExampleScene := TCastleScene.Create(Application); ExampleScene.Load('castle-data:/example_scene.x3dv'); ExampleScene.PreciseCollisions := true; Viewport.Items.Add(ExampleScene); // headlight Viewport.Camera.Add(TCastleDirectionalLight.Create(Application)); // nice initial camera position Viewport.AssignDefaultCamera; end; initialization { This initialization section configures: - Application.OnInitialize - Application.MainWindow - determines initial window size You should not need to do anything more in this initialization section. Most of your actual application initialization (in particular, any file reading) should happen inside ApplicationInitialize. } Application.OnInitialize := @ApplicationInitialize; Window := TCastleWindow.Create(Application); Application.MainWindow := Window; { Optionally, adjust window fullscreen state and size at this point. Examples: Run fullscreen: Window.FullScreen := true; Run in a 600x400 window: Window.FullScreen := false; // default Window.Width := 600; Window.Height := 400; Run in a window taking 2/3 of screen (width and height): Window.FullScreen := false; // default Window.Width := Application.ScreenWidth * 2 div 3; Window.Height := Application.ScreenHeight * 2 div 3; Note that some platforms (like mobile) ignore these window sizes. } { Handle command-line parameters like --fullscreen and --window. By doing this last, you let user to override your fullscreen / mode setup. } Window.ParseParameters; finalization FreeAndNil(PackedDataReader); end.