| 1 |
{ |
| 2 |
Copyright 2012-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 |
{ Example of a fully-working 3D FPS game. |
| 17 |
This is the main game unit, which contains most of the code. |
| 18 |
This is a cross-platform game code that will work on any platform |
| 19 |
(desktop or mobile). } |
| 20 |
unit GameInitialize; |
| 21 |
|
| 22 |
interface |
| 23 |
|
| 24 |
implementation |
| 25 |
|
| 26 |
uses SysUtils, Classes, |
| 27 |
CastleWindow, CastleApplicationProperties, CastleConfig, |
| 28 |
CastleComponentSerialize, CastleSoundEngine, GameViewportUnderUi |
| 29 |
{$region 'Castle Initialization Uses'} |
| 30 |
// The content here may be automatically updated by CGE editor. |
| 31 |
, GameViewPlay |
| 32 |
, GameViewMenu |
| 33 |
, GameViewOptions |
| 34 |
, GameViewDeath |
| 35 |
, GameViewWin |
| 36 |
, GameViewCredits |
| 37 |
{$endregion 'Castle Initialization Uses'}; |
| 38 |
|
| 39 |
var |
| 40 |
Window: TCastleWindow; |
| 41 |
|
| 42 |
{ Initialize the game. |
| 43 |
This is assigned to Application.OnInitialize, and will be called only once. } |
| 44 |
procedure ApplicationInitialize; |
| 45 |
|
| 46 |
procedure InitializeMusicSound; |
| 47 |
var |
| 48 |
Sounds: TComponent; |
| 49 |
MusicSound: TCastleSound; |
| 50 |
begin |
| 51 |
Sounds := TComponent.Create(Application); |
| 52 |
ComponentLoad('castle-data:/sounds.castle-component', Sounds); |
| 53 |
MusicSound := Sounds.FindRequiredComponent('MusicSound') as TCastleSound; |
| 54 |
SoundEngine.LoopingChannel[0].Sound := MusicSound; |
| 55 |
end; |
| 56 |
|
| 57 |
begin |
| 58 |
{ Adjust container settings for a scalable UI (adjusts to any window size in a smart way). } |
| 59 |
Window.Container.LoadSettings('castle-data:/CastleSettings.xml'); |
| 60 |
|
| 61 |
{ Create views (see https://castle-engine.io/views ). } |
| 62 |
{$region 'Castle View Creation'} |
| 63 |
// The content here may be automatically updated by CGE editor. |
| 64 |
ViewPlay := TViewPlay.Create(Application); |
| 65 |
ViewMenu := TViewMenu.Create(Application); |
| 66 |
ViewOptions := TViewOptions.Create(Application); |
| 67 |
ViewDeath := TViewDeath.Create(Application); |
| 68 |
ViewWin := TViewWin.Create(Application); |
| 69 |
ViewCredits := TViewCredits.Create(Application); |
| 70 |
{$endregion 'Castle View Creation'} |
| 71 |
|
| 72 |
UserConfig.Load; |
| 73 |
SoundEngine.Volume := UserConfig.GetFloat('sound_volume', 1); |
| 74 |
|
| 75 |
InitializeMusicSound; |
| 76 |
|
| 77 |
LoadViewportUnderUi; |
| 78 |
|
| 79 |
Window.Container.View := ViewMenu; |
| 80 |
end; |
| 81 |
|
| 82 |
initialization |
| 83 |
{ This initialization section configures: |
| 84 |
- Application.OnInitialize |
| 85 |
- Application.MainWindow |
| 86 |
- determines initial window size |
| 87 |
|
| 88 |
You should not need to do anything more in this initialization section. |
| 89 |
Most of your actual application initialization (in particular, any file reading) |
| 90 |
should happen inside ApplicationInitialize. } |
| 91 |
|
| 92 |
Application.OnInitialize := @ApplicationInitialize; |
| 93 |
|
| 94 |
Window := TCastleWindow.Create(Application); |
| 95 |
Application.MainWindow := Window; |
| 96 |
|
| 97 |
{ Optionally, adjust window fullscreen state and size at this point. |
| 98 |
Examples: |
| 99 |
|
| 100 |
Run fullscreen: |
| 101 |
|
| 102 |
Window.FullScreen := true; |
| 103 |
|
| 104 |
Run in a 600x400 window: |
| 105 |
|
| 106 |
Window.FullScreen := false; // default |
| 107 |
Window.Width := 600; |
| 108 |
Window.Height := 400; |
| 109 |
|
| 110 |
Run in a window taking 2/3 of screen (width and height): |
| 111 |
|
| 112 |
Window.FullScreen := false; // default |
| 113 |
Window.Width := Application.ScreenWidth * 2 div 3; |
| 114 |
Window.Height := Application.ScreenHeight * 2 div 3; |
| 115 |
|
| 116 |
Note that some platforms (like mobile) ignore these window sizes. |
| 117 |
} |
| 118 |
|
| 119 |
{ Handle command-line parameters like --fullscreen and --window. |
| 120 |
By doing this last, you let user to override your fullscreen / mode setup. } |
| 121 |
Window.ParseParameters; |
| 122 |
end. |