Debian Codegraph @

examples/test_local_filename_chars/code/gameinitialize.pas

raw
1 {
2 Copyright 2020-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. }
17 unit GameInitialize;
18
19 interface
20
21 implementation
22
23 uses SysUtils, Classes,
24 CastleWindow, CastleLog, CastleApplicationProperties, CastleDownload, CastleClassUtils,
25 CastleControls, CastleUIControls, CastleColors, CastleUnicode, CastleUtils,
26 CastleFilesUtils, CastleFonts;
27
28 var
29 Window: TCastleWindow;
30
31 { One-time initialization of resources. }
32 procedure ApplicationInitialize;
33
34 procedure TestLocalChars;
35
36 procedure AssertEquals(const Expected, Actual: String);
37 begin
38 if Expected <> Actual then
39 raise Exception.CreateFmt('Expected "%s", actual "%s"', [Expected, Actual]);
40 end;
41
42 { Test reading filenames using FPC RTL, without any CGE functions in the middle. }
43 procedure TestReadingRtl(const FileName: String);
44 var
45 F: TextFile;
46 S: String;
47 begin
48 try
49 AssignFile(F, FileName);
50 Reset(F);
51 try
52 Readln(F, S);
53 AssertEquals('Testing.', Trim(S));
54 finally CloseFile(F) end;
55 except
56 // catch EInOutError and add FileName
57 on E: EInOutError do
58 begin
59 E.Message := Format('Reading file "%s": ', [FileName]) + E.Message;
60 raise;
61 end;
62 end;
63 end;
64
65 { Test reading file using URL (through CGE function). }
66 procedure TestReading(const Url: String);
67 var
68 Stream: TStream;
69 S: String;
70 begin
71 Stream := Download(Url);
72 try
73 S := StreamToString(Stream);
74 AssertEquals('Testing.', Trim(S));
75 finally FreeAndNil(Stream) end;
76 end;
77
78 { Test reading file, whose URL is written inside another file, in UTF-8 encoding. }
79 procedure TestReadingThroughReference(const Url: String);
80 var
81 Stream: TStream;
82 ReferredUrl: String;
83 begin
84 Stream := Download(Url);
85 try
86 ReferredUrl := Trim(StreamToString(Stream));
87 TestReading(ReferredUrl);
88 finally FreeAndNil(Stream) end;
89 end;
90
91 { Test reading font (as it goes through FreeType library). }
92 procedure TestReadingFont(const FontUrl: String);
93 var
94 MyNewFont: TCastleFont;
95 begin
96 MyNewFont := TCastleFont.Create(nil);
97 try
98 MyNewFont.Size := 20;
99 MyNewFont.AntiAliased := true;
100 MyNewFont.Url := FontUrl;
101 finally FreeAndNil(MyNewFont) end;
102 end;
103
104 var
105 DataPath: String;
106 begin
107 {$ifdef MSWINDOWS}
108 if not FileExists(ExeNameFromGetModule) then
109 raise Exception.CreateFmt('Cannot find own exe file "%s"', [ExeNameFromGetModule]);
110 {$endif}
111
112 { This is an *extremely* simplified (only correct in simplest cases) implementation
113 of determining application data directory
114 ( https://castle-engine.io/data ).
115 We do it only for TestReadingRtl test here (that uses FPC functions
116 *without* any CGE API, just for test).
117 In real CGE applications, you should always use 'castle-data:/'
118 URL instead of doing it like this! }
119 DataPath :=
120 {$ifdef MSWINDOWS} ExtractFilePath(ExeNameFromGetModule)
121 {$else} InclPathDelim(GetCurrentDir)
122 {$endif} + InclPathDelim('data');
123
124 if not DirectoryExists(DataPath) then
125 raise Exception.CreateFmt('Cannot find directory "%s"', [DataPath]);
126
127 TestReadingRtl(DataPath + 'ascii_name.txt');
128 (*TODO:
129 This doesn't work (anymore?) with FPC 3.2.2 or FPC 3.3.1 on Linux/x86_64.
130 Unknown how to make it work, {$codepage utf8} doesn't help.
131
132 TestReadingRtl(DataPath + 'name with Polish chars ćma źrebak żmija wąż królik.txt');
133 TestReadingRtl(DataPath + 'name with Chinese chars 样例中文文本.txt');
134 TestReadingRtl(DataPath + '样例中文文本/name with Chinese chars 样例中文文本.txt');
135 TestReadingRtl(DataPath + 'name with Russian chars образец русского текста.txt');
136 TestReadingRtl(DataPath + 'образец русского текста/name with Russian chars образец русского текста.txt');
137 *)
138
139 TestReading('castle-data:/ascii_name.txt');
140 TestReading('castle-data:/name with Polish chars ćma źrebak żmija wąż królik.txt');
141 TestReading('castle-data:/name with Chinese chars 样例中文文本.txt');
142 TestReading('castle-data:/样例中文文本/name with Chinese chars 样例中文文本.txt');
143 TestReading('castle-data:/name with Russian chars образец русского текста.txt');
144 TestReading('castle-data:/образец русского текста/name with Russian chars образец русского текста.txt');
145
146 TestReadingThroughReference('castle-data:/reference to file with Chinese chars.txt');
147 TestReadingThroughReference('castle-data:/reference to file with Russian chars.txt');
148 TestReadingThroughReference('castle-data:/reference to file with Polish chars.txt');
149
150 StringToFile(ApplicationConfig('config_ascii.txt'), 'Testing save.');
151 StringToFile(ApplicationConfig('config with Chinese chars 样例中文文本.txt'), 'Testing save.');
152 StringToFile(ApplicationConfig('config with Polish chars ćma źrebak żmija wąż królik.txt'), 'Testing save.');
153 StringToFile(ApplicationConfig('config with Russian chars образец русского текста.txt'), 'Testing save.');
154
155 TestReadingFont('castle-data:/DejaVuSans name with Russian chars образец русского текста.ttf');
156 end;
157
158 var
159 L: TCastleLabel;
160 begin
161 TestLocalChars;
162
163 L := TCastleLabel.Create(Application);
164 L.Color := Yellow;
165 L.Caption := 'All tests executed OK :)';
166 L.Anchor(hpMiddle);
167 L.Anchor(vpMiddle);
168 Window.Controls.InsertFront(L);
169 end;
170
171 initialization
172 { This initialization section configures:
173 - Application.OnInitialize
174 - Application.MainWindow
175 - determines initial window size
176
177 You should not need to do anything more in this initialization section.
178 Most of your actual application initialization (in particular, any file reading)
179 should happen inside ApplicationInitialize. }
180
181 Application.OnInitialize := @ApplicationInitialize;
182
183 Window := TCastleWindow.Create(Application);
184 Application.MainWindow := Window;
185
186 { Optionally, adjust window fullscreen state and size at this point.
187 Examples:
188
189 Run fullscreen:
190
191 Window.FullScreen := true;
192
193 Run in a 600x400 window:
194
195 Window.FullScreen := false; // default
196 Window.Width := 600;
197 Window.Height := 400;
198
199 Run in a window taking 2/3 of screen (width and height):
200
201 Window.FullScreen := false; // default
202 Window.Width := Application.ScreenWidth * 2 div 3;
203 Window.Height := Application.ScreenHeight * 2 div 3;
204
205 Note that some platforms (like mobile) ignore these window sizes.
206 }
207
208 { Handle command-line parameters like --fullscreen and --window.
209 By doing this last, you let user to override your fullscreen / mode setup. }
210 Window.ParseParameters;
211 end.