Debian Codegraph @

examples/user_interface/screen_resolution_change/code/gameviewmain.pas

raw
1 {
2 Copyright 2024-2024 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,
23 CastleUIControls, CastleControls, CastleKeysMouse;
24
25 type
26 { Main view, where most of the application logic takes place. }
27 TViewMain = class(TCastleView)
28 published
29 { Components designed using CGE editor.
30 These fields will be automatically initialized at Start. }
31 LabelFps: TCastleLabel;
32 LabelScreenStatus: TCastleLabel;
33 ButtonResolution1024x768: TCastleButton;
34 ButtonResolution1280x720: TCastleButton;
35 ButtonResolutionCustom: TCastleButton;
36 EditResolutionWidth: TCastleIntegerEdit;
37 EditResolutionHeight: TCastleIntegerEdit;
38 ButtonResolutionDefault: TCastleButton;
39 private
40 procedure ClickResolution1024x768(Sender: TObject);
41 procedure ClickResolution1280x720(Sender: TObject);
42 procedure ClickResolutionCustom(Sender: TObject);
43 procedure ClickResolutionDefault(Sender: TObject);
44 procedure ChangeResolution(const NewWidth, NewHeight: Integer);
45 public
46 constructor Create(AOwner: TComponent); override;
47 procedure Start; override;
48 procedure Update(const SecondsPassed: Single; var HandleInput: Boolean); override;
49 function Press(const Event: TInputPressRelease): Boolean; override;
50 end;
51
52 var
53 ViewMain: TViewMain;
54
55 implementation
56
57 uses SysUtils,
58 CastleWindow, CastleMessages;
59
60 { TViewMain ----------------------------------------------------------------- }
61
62 constructor TViewMain.Create(AOwner: TComponent);
63 begin
64 inherited;
65 DesignUrl := 'castle-data:/gameviewmain.castle-user-interface';
66 end;
67
68 procedure TViewMain.Start;
69 begin
70 inherited;
71 ButtonResolution1024x768.OnClick := {$ifdef FPC}@{$endif} ClickResolution1024x768;
72 ButtonResolution1280x720.OnClick := {$ifdef FPC}@{$endif} ClickResolution1280x720;
73 ButtonResolutionCustom.OnClick := {$ifdef FPC}@{$endif} ClickResolutionCustom;
74 ButtonResolutionDefault.OnClick := {$ifdef FPC}@{$endif} ClickResolutionDefault;
75 end;
76
77 procedure TViewMain.Update(const SecondsPassed: Single; var HandleInput: Boolean);
78 begin
79 inherited;
80
81 { This virtual method is executed every frame (many times per second). }
82 Assert(LabelFps <> nil, 'If you remove LabelFps from the design, remember to remove also the assignment "LabelFps.Caption := ..." from code');
83 LabelFps.Caption := 'FPS: ' + Container.Fps.ToString;
84
85 LabelScreenStatus.Caption := Format('Current Screen Resolution: %d x %d', [
86 Application.ScreenWidth,
87 Application.ScreenHeight
88 ]);
89 end;
90
91 procedure TViewMain.ChangeResolution(const NewWidth, NewHeight: Integer);
92 begin
93 Application.VideoResizeWidth := NewWidth;
94 Application.VideoResizeHeight := NewHeight;
95 Application.VideoResize := true;
96 if not Application.TryVideoChange then
97 MessageOK(Application.MainWindow, Format(
98 'Cannot change screen resolution to %d x %d', [
99 NewWidth,
100 NewHeight
101 ]));
102 end;
103
104 procedure TViewMain.ClickResolution1024x768(Sender: TObject);
105 begin
106 ChangeResolution(1024, 768);
107 end;
108
109 procedure TViewMain.ClickResolution1280x720(Sender: TObject);
110 begin
111 ChangeResolution(1280, 720);
112 end;
113
114 procedure TViewMain.ClickResolutionCustom(Sender: TObject);
115 begin
116 ChangeResolution(EditResolutionWidth.Value, EditResolutionHeight.Value);
117 end;
118
119 procedure TViewMain.ClickResolutionDefault(Sender: TObject);
120 begin
121 Application.VideoReset;
122 end;
123
124 function TViewMain.Press(const Event: TInputPressRelease): Boolean;
125 begin
126 Result := inherited;
127 if Result then Exit;
128
129 if Event.IsKey(keyF5) then
130 begin
131 Container.SaveScreenToDefaultFile;
132 Exit(true);
133 end;
134 end;
135
136 end.