Debian Codegraph @

src/scene/castletiledmap_scene.inc

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 {%MainUnit castletiledmap.pas}
2 {
3 Copyright 2023-2024 Michalis Kamburelis.
4
5 This file is part of "Castle Game Engine".
6
7 "Castle Game Engine" is free software; see the file COPYING.txt,
8 included in this distribution, for details about the copyright.
9
10 "Castle Game Engine" is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13
14 ----------------------------------------------------------------------------
15 }
16
17 { Display a map created in @url(https://www.mapeditor.org/ Tiled) in a viewport (TCastleTiledMap class). }
18
19 {$ifdef read_interface}
20
21 type
22 { Display a map created in @url(https://www.mapeditor.org/ Tiled) in a viewport.
23 See https://castle-engine.io/tiled_maps for a documentation. }
24 TCastleTiledMap = class(TCastleTransform)
25 public
26 { These types and consts are synchronized with X3DLoadInternalTiledMap }
27 type
28 { Layer index that can be used with @link(Layers). }
29 TLayerIndex = 0..30;
30 { Type for @link(Layers), set of visible layers. }
31 TLayers = set of TLayerIndex;
32 const
33 { Indicates that all @link(Layers) should be visible. }
34 AllLayers = [Low(TLayerIndex)..High(TLayerIndex)];
35 strict private
36 FForceTilesetSpacing: Boolean;
37 FData: TCastleTiledMapData;
38 FUrl: String;
39 FUrlMonitoring: TUrlMonitoring;
40 FSmoothScaling, FSmoothScalingSafeBorder: Boolean;
41 FScene: TCastleScene;
42 FLayers: TLayers;
43 procedure SetForceTilesetSpacing(const AValue: Boolean);
44 procedure SetUrl(const Value: String);
45 procedure SetSmoothScaling(const Value: Boolean);
46 procedure SetSmoothScalingSafeBorder(const Value: Boolean);
47 procedure ReloadMap;
48 procedure UpdateSmoothScaling;
49 procedure SetLayers(const Value: TLayers);
50 public
51 constructor Create(AOwner: TComponent); override;
52 destructor Destroy; override;
53 procedure BeforeDestruction; override;
54 function PropertySections(const PropertyName: String): TPropertySections; override;
55
56 procedure PlayAnimations;
57 procedure StopAnimations(const ResetAnimations: Boolean);
58 function HasAnimations: Boolean;
59
60 { Currently loaded map (from @link(Url)), if any.
61 @nil if no map is loaded (when @link(Url) is empty). Read-only. }
62 property Data: TCastleTiledMapData read FData;
63
64 property Map: TCastleTiledMapData read FData; {$ifdef FPC} deprecated 'use Data'; {$endif}
65
66 { Rectangle coordinates where a tile at given TilePosition would be rendered on the map.
67 This doesn't check whether TilePosition is within map range -- any coordinates
68 (negative, or larger than map size) are allowed. }
69 function TileRectangle(const TilePosition: TVector2Integer;
70 const TilesetIndex: Cardinal = 0): TFloatRectangle;
71 published
72 { URL (or just filename) of the Tiled map file (TMX).
73
74 Note that loading new map from new URL automatically starts animations.
75 You can stop them with @link(StopAnimations). }
76 property Url: String read FUrl write SetUrl;
77
78 { How are images scaled.
79 @true means we use nice "bilinear" filter.
80 @false means we use pixelated "nearest" filter.
81
82 Beware that using @true, and scaling the map, requires preparing
83 the tileset (atlas of tile images) in a proper way. The space
84 around each tile must have at least 1-pixel margin
85 filled with the same color and alpha,
86 otherwise bad colors will creep in because of bilinear filtering.
87 See @url(https://castle-engine.io/manual_alpha_bleeding.php alpha bleeding).
88 Alternatively, you can use @link(SmoothScalingSafeBorder). }
89 property SmoothScaling: Boolean read FSmoothScaling write SetSmoothScaling default false;
90
91 { In case of @link(SmoothScaling),
92 prevent the color outside of rendered frame from creeping in,
93 by stretching the rendered frame a bit.
94 See @link(TSprite.SmoothScalingSafeBorder).
95
96 It is better to prepare the sprite (tileset) images
97 with some spacing between tiles, and @url(https://castle-engine.io/manual_alpha_bleeding.php
98 alpha bleeding) instead of using this property.
99 This property workarounds the fact that some sprite (tileset) images
100 lack proper spacing and color bleeding, but it distorts the image
101 a bit, so it's no longer pixel-perfect.
102
103 Note that changing this when map is loaded reloads it,
104 and automatically starts animations.
105 You can stop them with @link(StopAnimations).
106
107 See also @link(ForceTilesetSpacing) that workarounds artifacts with
108 @link(SmoothScaling) in a different way, by adding spacing in the tileset texture,
109 which makes this trick (@name) not necessary.
110 Do not use both @link(ForceTilesetSpacing) and @name --
111 these 2 properties are different workarounds for the same problem,
112 it doesn't make sense to use them both.
113
114 @seealso ForceTilesetSpacing }
115 property SmoothScalingSafeBorder: Boolean read FSmoothScalingSafeBorder write SetSmoothScalingSafeBorder
116 default false;
117
118 { Use a modified version of the tileset image,
119 with a proper space between each tile in the texture (1-pixel size border around each tile,
120 effectively 2-pixel wide spacing between each tile).
121
122 Makes sense to be used only with @link(SmoothScaling) = @true,
123 it is pointless (but harmless) when @link(SmoothScaling) = @false.
124
125 See also @link(SmoothScalingSafeBorder) that workarounds artifacts with
126 @link(SmoothScaling) in a different way, by changing texture coordinates
127 such that this spacing is not necessary.
128 Do not use both @link(SmoothScalingSafeBorder) and @name --
129 these 2 properties are different workarounds for the same problem,
130 it doesn't make sense to use them both.
131
132 Note that changing this when map is loaded reloads it,
133 and automatically starts animations.
134 You can stop them with @link(StopAnimations).
135
136 @seealso SmoothScalingSafeBorder }
137 property ForceTilesetSpacing: Boolean read FForceTilesetSpacing write SetForceTilesetSpacing default false;
138
139 { Layers to include when loading this map.
140
141 Note that changing this when map is loaded reloads it,
142 and automatically starts animations.
143 You can stop them with @link(StopAnimations).
144
145 TODO:
146 There is an alternative way to hide/show layers: you can read/write
147 @code(MyMap.Data.Layers[<layer-index>].Exists) property.
148 However, we do not encourage using the @code(Data) property now,
149 and we plan to add a better layer API,
150 see https://castle-engine.io/roadmap#tiled_layers .
151 So eventually, we want to allow just using @code(MyMap.Layers[<layer-index>].Exists)
152 to hide/show layers. }
153 property Layers: TLayers read FLayers write SetLayers default AllLayers;
154 end;
155
156 {$endif read_interface}
157
158 {$ifdef read_implementation}
159
160 { TCastleTiledMap ------------------------------------------------------------ }
161
162 constructor TCastleTiledMap.Create(AOwner: TComponent);
163 begin
164 inherited Create(AOwner);
165
166 FScene := TCastleScene.Create(nil);
167 FScene.SetTransient;
168 Add(FScene);
169
170 FSmoothScaling := false;
171 FSmoothScalingSafeBorder := false;
172 FForceTilesetSpacing := false;
173
174 FLayers := AllLayers;
175
176 UpdateSmoothScaling;
177
178 FUrlMonitoring.Init({$ifdef FPC}@{$endif} ReloadMap);
179 end;
180
181 destructor TCastleTiledMap.Destroy;
182 begin
183 FreeAndNil(FScene);
184 FreeAndNil(FData);
185 inherited;
186 end;
187
188 procedure TCastleTiledMap.BeforeDestruction;
189 begin
190 FUrlMonitoring.Finish(FUrl);
191 inherited;
192 end;
193
194 function TCastleTiledMap.PropertySections(
195 const PropertyName: String): TPropertySections;
196 begin
197 if ArrayContainsString(PropertyName, [
198 'Url', 'SmoothScaling', 'SmoothScalingSafeBorder', 'Layers', 'ForceTilesetSpacing'
199 ]) then
200 Result := [psBasic]
201 else
202 Result := inherited PropertySections(PropertyName);
203 end;
204
205 procedure TCastleTiledMap.SetUrl(const Value: String);
206 begin
207 if FUrl <> Value then
208 begin
209 FUrlMonitoring.ChangeUrl(FUrl, Value);
210 try
211 ReloadMap;
212 except
213 { If loading file failed, and we're inside CGE editor,
214 merely report a warning. This allows deserializing in CGE editor
215 designs with broken URLs. }
216 on E: Exception do
217 begin
218 if CastleDesignMode then
219 begin
220 WritelnWarning('TCastleTiledMap', 'Failed to load tiledmap "%s": %s',
221 [UriDisplay(Value), ExceptMessage(E)]);
222 Exit;
223 end else
224 raise;
225 end;
226 end;
227 end;
228 end;
229
230 procedure TCastleTiledMap.SetForceTilesetSpacing(const AValue: Boolean);
231 begin
232 if FForceTilesetSpacing <> AValue then
233 begin
234 FForceTilesetSpacing := AValue;
235 ReloadMap;
236 end;
237 end;
238
239 procedure TCastleTiledMap.ReloadMap;
240 var
241 Converter: TCastleTiledMapConverter;
242 begin
243 FreeAndNil(FData);
244 FScene.Load(nil, true);
245
246 if FUrl <> '' then
247 begin
248 FData := TCastleTiledMapData.Create(Url);
249 Converter := TCastleTiledMapConverter.Create(FData);
250 try
251 Converter.SmoothScalingSafeBorder := SmoothScalingSafeBorder;
252 Converter.ForceTilesetSpacing := ForceTilesetSpacing;
253 Converter.Layers := Layers;
254 Converter.ConvertMap;
255 FScene.Load(Converter.RootNode, true);
256 PlayAnimations;
257
258 finally FreeAndNil(Converter) end;
259 end;
260 end;
261
262 procedure TCastleTiledMap.SetSmoothScaling(const Value: Boolean);
263 begin
264 if FSmoothScaling <> Value then
265 begin
266 FSmoothScaling := Value;
267 UpdateSmoothScaling;
268 end;
269 end;
270
271 procedure TCastleTiledMap.UpdateSmoothScaling;
272 begin
273 if SmoothScaling then
274 begin
275 FScene.RenderOptions.MinificationFilter := minLinear;
276 FScene.RenderOptions.MagnificationFilter := magLinear;
277 end else
278 begin
279 FScene.RenderOptions.MinificationFilter := minNearest;
280 FScene.RenderOptions.MagnificationFilter := magNearest;
281 end;
282 end;
283
284 procedure TCastleTiledMap.SetSmoothScalingSafeBorder(const Value: Boolean);
285 begin
286 if FSmoothScalingSafeBorder <> Value then
287 begin
288 FSmoothScalingSafeBorder := Value;
289 ReloadMap;
290 end;
291 end;
292
293 procedure TCastleTiledMap.SetLayers(const Value: TLayers);
294 begin
295 if FLayers <> Value then
296 begin
297 FLayers := Value;
298 ReloadMap;
299 end;
300 end;
301
302 procedure TCastleTiledMap.PlayAnimations;
303 var
304 I: Integer;
305 TimeSensor: TTimeSensorNode;
306 begin
307 FScene.ProcessEvents := true;
308 for I := 0 to FScene.AnimationsList.Count - 1 do
309 begin
310 TimeSensor := FScene.AnimationsList.Objects[I] as TTimeSensorNode;
311 TimeSensor.Start(true);
312 end;
313 end;
314
315 procedure TCastleTiledMap.StopAnimations(const ResetAnimations: Boolean);
316 var
317 I: Integer;
318 TimeSensor: TTimeSensorNode;
319 begin
320 for I := 0 to FScene.AnimationsList.Count - 1 do
321 begin
322 TimeSensor := FScene.AnimationsList.Objects[I] as TTimeSensorNode;
323 TimeSensor.Stop;
324 if ResetAnimations then
325 TimeSensor.FakeTime(0, false, true);
326 end;
327 end;
328
329 function TCastleTiledMap.HasAnimations: Boolean;
330 begin
331 Result := FScene.AnimationsList.Count > 0;
332 end;
333
334 function TCastleTiledMap.TileRectangle(const TilePosition: TVector2Integer;
335 const TilesetIndex: Cardinal): TFloatRectangle;
336 var
337 Tileset: TCastleTiledMapData.TTileset;
338 begin
339 if (Data = nil) or (TilesetIndex >= Data.Tilesets.Count) then
340 raise Exception.CreateFmt('Invalid tileset index %d', [TilesetIndex]);
341
342 Tileset := Data.Tilesets[TilesetIndex];
343 Result := FloatRectangle(
344 Data.TileRenderPosition(TilePosition) + Vector2(Tileset.TileOffset.X, - Tileset.TileOffset.Y),
345 Tileset.TileWidth,
346 Tileset.TileHeight
347 );
348 end;
349
350 {$endif read_implementation}