Gondwana Game Engine Development (master) - 2.7.0
Gondwana is a cross-platform 2.5D game and rendering engine written in C#/.NET 8. It provides fine-grained control over rendering, timing, and scene composition, with built-in support for parallax, z-ordering, pixel overhang, collision detection, and particle effects. Gondwana targets desktop and web platforms using SkiaSharp for graphics and pluggable audio backends for desktop and browser targets.
Loading...
Searching...
No Matches
RenderTileQuery.cs
1using System.Drawing;
3
5
13internal readonly record struct RenderTileCandidates(
14 List<SceneLayerTile> Tiles,
15 bool IsRenderOrdered);
16
21internal static class RenderTileQuery
22{
23 internal static RenderTileCandidates GetCandidates(
24 ISceneLayerCoordinates coordinates,
25 SceneLayer layer,
26 Rectangle worldPixelRange,
27 bool includeOverhang)
28 {
29 var tiles = new List<SceneLayerTile>();
30
31 VisitCandidates(
32 coordinates,
33 layer,
34 worldPixelRange,
35 includeOverhang,
36 tile =>
37 {
38 tiles.Add(tile);
39 return true;
40 });
41
42 return new(tiles, IsRenderOrdered(layer));
43 }
44
55 internal static bool VisitCandidates(
56 ISceneLayerCoordinates coordinates,
57 SceneLayer layer,
58 Rectangle worldPixelRange,
59 bool includeOverhang,
60 Func<SceneLayerTile, bool> visitor)
61 {
62 ArgumentNullException.ThrowIfNull(visitor);
63
64 return coordinates switch
65 {
66 OrthogonalCoordinates =>
67 VisitOrthogonal(
68 coordinates,
69 layer,
70 worldPixelRange,
71 includeOverhang,
72 visitor),
73 IsometricRhombicCoordinates =>
74 VisitIsometricRhombic(
75 coordinates,
76 layer,
77 worldPixelRange,
78 includeOverhang,
79 visitor),
80 IsometricAxialCoordinates =>
81 VisitIsometricAxial(
82 coordinates,
83 layer,
84 worldPixelRange,
85 includeOverhang,
86 visitor),
87 HexAxialFlatTopCoordinates =>
88 VisitHexFlat(
89 coordinates,
90 layer,
91 worldPixelRange,
92 includeOverhang,
93 visitor),
95 VisitHexPointed(
96 coordinates,
97 layer,
98 worldPixelRange,
99 includeOverhang,
100 visitor),
101 ObliqueRightCoordinates or ObliqueLeftCoordinates =>
102 VisitOblique(
103 coordinates,
104 layer,
105 worldPixelRange,
106 includeOverhang,
107 visitor),
108 _ => VisitExactFallback(
109 coordinates,
110 layer,
111 worldPixelRange,
112 includeOverhang,
113 visitor)
114 };
115 }
116
117 internal static bool IsRenderOrdered(SceneLayer layer) =>
118 layer.TransformedTiles.Count == 0 ||
119 layer.TileWidth == layer.TileHeight;
120
121 internal static Rectangle GetFixedTileCellBounds(
122 SceneLayer layer,
123 SceneLayerTile tile)
124 {
125 Point grid = tile.GridCoordinatesAbs;
126 int width = layer.TileWidth;
127 int height = layer.TileHeight;
128
129 if (layer.CoordinateSystem is OrthogonalCoordinates)
130 {
131 return new Rectangle(
132 grid.X * width - layer.OriginPx.X,
133 grid.Y * height - layer.OriginPx.Y,
134 width,
135 height);
136 }
137
138 Point anchor = layer.CoordinateSystem.GetAnchorPixelAtSceneLayerCoordinates(
139 layer,
140 grid);
141
142 if (layer.CoordinateSystem is IsometricRhombicCoordinates or IsometricAxialCoordinates)
143 return new Rectangle(
144 anchor.X - width / 2,
145 anchor.Y,
146 width,
147 height);
148
149 return new Rectangle(
150 anchor.X,
151 anchor.Y,
152 width,
153 height);
154 }
155
156 private static bool VisitOrthogonal(
157 ISceneLayerCoordinates coordinates,
158 SceneLayer layer,
159 Rectangle query,
160 bool includeOverhang,
161 Func<SceneLayerTile, bool> visitor)
162 {
163 var candidate = TileBounds.GetTransformedTileCandidateRange(layer, query, includeOverhang);
164
165 var ul = coordinates.GetSceneLayerCoordinatesAtPixel(
166 layer,
167 new PointF(candidate.Left, candidate.Top));
168
169 var br = coordinates.GetSceneLayerCoordinatesAtPixel(
170 layer,
171 new PointF(candidate.Right - 1, candidate.Bottom - 1));
172
173 int minX = Math.Max(0, (int)Math.Floor(ul.X) - 1);
174 int maxX = Math.Min(layer.GridColumnCount - 1, (int)Math.Ceiling(br.X) + 1);
175 int minY = Math.Max(0, (int)Math.Floor(ul.Y) - 1);
176 int maxY = Math.Min(layer.GridRowCount - 1, (int)Math.Ceiling(br.Y) + 1);
177
178 return VisitRowMajor(
179 layer,
180 minX,
181 maxX,
182 minY,
183 maxY,
184 visitor);
185 }
186
187 private static bool VisitIsometricRhombic(
188 ISceneLayerCoordinates coordinates,
189 SceneLayer layer,
190 Rectangle query,
191 bool includeOverhang,
192 Func<SceneLayerTile, bool> visitor)
193 {
194 GetCornerBounds(
195 coordinates,
196 layer,
197 query,
198 includeOverhang,
199 1,
200 out int minX,
201 out int maxX,
202 out int minY,
203 out int maxY);
204
205 if (!Clamp(
206 layer,
207 ref minX,
208 ref maxX,
209 ref minY,
210 ref maxY))
211 return true;
212
213 for (int depth = minX + minY; depth <= maxX + maxY; depth++)
214 {
215 int xStart = Math.Max(minX, depth - maxY);
216 int xEnd = Math.Min(maxX, depth - minY);
217
218 for (int x = xStart; x <= xEnd; x++)
219 {
220 int y = depth - x;
221
222 if (layer[x, y] is { } tile && !visitor(tile))
223 return false;
224 }
225 }
226
227 return true;
228 }
229
230 private static bool VisitIsometricAxial(
231 ISceneLayerCoordinates coordinates,
232 SceneLayer layer,
233 Rectangle query,
234 bool includeOverhang,
235 Func<SceneLayerTile, bool> visitor)
236 {
237 GetCornerBounds(
238 coordinates,
239 layer,
240 query,
241 includeOverhang,
242 2,
243 out int minX,
244 out int maxX,
245 out int minY,
246 out int maxY);
247
248 return VisitRowMajorClamped(
249 layer,
250 minX,
251 maxX,
252 minY,
253 maxY,
254 visitor);
255 }
256
257 private static bool VisitHexPointed(
258 ISceneLayerCoordinates coordinates,
259 SceneLayer layer,
260 Rectangle query,
261 bool includeOverhang,
262 Func<SceneLayerTile, bool> visitor)
263 {
264 var candidate = TileBounds.GetTransformedTileCandidateRange(layer, query, includeOverhang);
265 int width = layer.TileWidth;
266 int height = layer.TileHeight;
267
268 int minRow = Math.Max(
269 0,
270 (int)Math.Floor((candidate.Top + layer.OriginPx.Y) / (height * 0.75f)) - 2);
271
272 int maxRow = Math.Min(
273 layer.GridRowCount - 1,
274 (int)Math.Ceiling((candidate.Bottom + layer.OriginPx.Y) / (height * 0.75f)) + 2);
275
276 if (minRow > maxRow)
277 return true;
278
279 for (int row = minRow; row <= maxRow; row++)
280 {
281 int xOffset = (row & 1) == 0 ? 0 : width / 2;
282
283 int minCol = Math.Max(
284 0,
285 (int)Math.Floor(
286 (candidate.Left + layer.OriginPx.X - xOffset) / (float)width) - 2);
287
288 int maxCol = Math.Min(
289 layer.GridColumnCount - 1,
290 (int)Math.Ceiling(
291 (candidate.Right + layer.OriginPx.X - xOffset) / (float)width) + 2);
292
293 for (int col = minCol; col <= maxCol; col++)
294 {
295 if (layer[col, row] is { } tile && !visitor(tile))
296 return false;
297 }
298 }
299
300 return true;
301 }
302
303 private static bool VisitHexFlat(
304 ISceneLayerCoordinates coordinates,
305 SceneLayer layer,
306 Rectangle query,
307 bool includeOverhang,
308 Func<SceneLayerTile, bool> visitor)
309 {
310 var candidate = TileBounds.GetTransformedTileCandidateRange(layer, query, includeOverhang);
311 int width = layer.TileWidth;
312 int height = layer.TileHeight;
313 int halfHeight = height / 2;
314
315 int minCol = Math.Max(
316 0,
317 (int)Math.Floor((candidate.Left + layer.OriginPx.X) / (width * 0.75f)) - 2);
318
319 int maxCol = Math.Min(
320 layer.GridColumnCount - 1,
321 (int)Math.Ceiling((candidate.Right + layer.OriginPx.X) / (width * 0.75f)) + 2);
322
323 if (minCol > maxCol)
324 return true;
325
326 int minEven =
327 (int)Math.Floor((candidate.Top + layer.OriginPx.Y) / (float)height) - 2;
328
329 int maxEven =
330 (int)Math.Ceiling((candidate.Bottom + layer.OriginPx.Y) / (float)height) + 2;
331
332 int minOdd =
333 (int)Math.Floor(
334 (candidate.Top + layer.OriginPx.Y - halfHeight) / (float)height) - 2;
335
336 int maxOdd =
337 (int)Math.Ceiling(
338 (candidate.Bottom + layer.OriginPx.Y - halfHeight) / (float)height) + 2;
339
340 int minRow = Math.Max(0, Math.Min(minEven, minOdd));
341 int maxRow = Math.Min(layer.GridRowCount - 1, Math.Max(maxEven, maxOdd));
342
343 if (minRow > maxRow)
344 return true;
345
346 for (int depth = minRow * 2; depth <= maxRow * 2 + 1; depth++)
347 {
348 for (int col = minCol; col <= maxCol; col++)
349 {
350 int numerator = depth - (col & 1);
351
352 if ((numerator & 1) != 0)
353 continue;
354
355 int row = numerator / 2;
356
357 if (row < minRow || row > maxRow)
358 continue;
359
360 if (layer[col, row] is { } tile && !visitor(tile))
361 return false;
362 }
363 }
364
365 return true;
366 }
367
368 private static bool VisitOblique(
369 ISceneLayerCoordinates coordinates,
370 SceneLayer layer,
371 Rectangle query,
372 bool includeOverhang,
373 Func<SceneLayerTile, bool> visitor)
374 {
375 GetCornerBounds(
376 coordinates,
377 layer,
378 query,
379 includeOverhang,
380 1,
381 out int minX,
382 out int maxX,
383 out int minY,
384 out int maxY);
385
386 return VisitRowMajorClamped(
387 layer,
388 minX,
389 maxX,
390 minY,
391 maxY,
392 visitor);
393 }
394
395 private static bool VisitExactFallback(
396 ISceneLayerCoordinates coordinates,
397 SceneLayer layer,
398 Rectangle query,
399 bool includeOverhang,
400 Func<SceneLayerTile, bool> visitor)
401 {
402 var exact = coordinates.GetSceneLayerTilesInPixelRange(
403 layer,
404 query,
405 includeOverhang);
406
407 for (int i = 0; i < exact.Count; i++)
408 {
409 if (!visitor(exact[i]))
410 return false;
411 }
412
413 return true;
414 }
415
416 private static void GetCornerBounds(
417 ISceneLayerCoordinates coordinates,
418 SceneLayer layer,
419 Rectangle query,
420 bool includeOverhang,
421 int margin,
422 out int minX,
423 out int maxX,
424 out int minY,
425 out int maxY)
426 {
427 var candidate = TileBounds.GetTransformedTileCandidateRange(layer, query, includeOverhang);
428
429 var ul = coordinates.GetSceneLayerCoordinatesAtPixel(
430 layer,
431 new PointF(candidate.Left, candidate.Top));
432
433 var ur = coordinates.GetSceneLayerCoordinatesAtPixel(
434 layer,
435 new PointF(candidate.Right, candidate.Top));
436
437 var ll = coordinates.GetSceneLayerCoordinatesAtPixel(
438 layer,
439 new PointF(candidate.Left, candidate.Bottom));
440
441 var lr = coordinates.GetSceneLayerCoordinatesAtPixel(
442 layer,
443 new PointF(candidate.Right, candidate.Bottom));
444
445 minX =
446 (int)Math.Floor(Math.Min(Math.Min(ul.X, ur.X), Math.Min(ll.X, lr.X))) -
447 margin;
448
449 maxX =
450 (int)Math.Ceiling(Math.Max(Math.Max(ul.X, ur.X), Math.Max(ll.X, lr.X))) +
451 margin;
452
453 minY =
454 (int)Math.Floor(Math.Min(Math.Min(ul.Y, ur.Y), Math.Min(ll.Y, lr.Y))) -
455 margin;
456
457 maxY =
458 (int)Math.Ceiling(Math.Max(Math.Max(ul.Y, ur.Y), Math.Max(ll.Y, lr.Y))) +
459 margin;
460 }
461
462 private static bool VisitRowMajorClamped(
463 SceneLayer layer,
464 int minX,
465 int maxX,
466 int minY,
467 int maxY,
468 Func<SceneLayerTile, bool> visitor)
469 {
470 if (!Clamp(
471 layer,
472 ref minX,
473 ref maxX,
474 ref minY,
475 ref maxY))
476 return true;
477
478 return VisitRowMajor(
479 layer,
480 minX,
481 maxX,
482 minY,
483 maxY,
484 visitor);
485 }
486
487 private static bool VisitRowMajor(
488 SceneLayer layer,
489 int minX,
490 int maxX,
491 int minY,
492 int maxY,
493 Func<SceneLayerTile, bool> visitor)
494 {
495 if (minX > maxX || minY > maxY)
496 return true;
497
498 for (int y = minY; y <= maxY; y++)
499 {
500 for (int x = minX; x <= maxX; x++)
501 {
502 if (layer[x, y] is { } tile && !visitor(tile))
503 return false;
504 }
505 }
506
507 return true;
508 }
509
510 private static bool Clamp(
511 SceneLayer layer,
512 ref int minX,
513 ref int maxX,
514 ref int minY,
515 ref int maxY)
516 {
517 minX = Math.Max(0, minX);
518 maxX = Math.Min(layer.GridColumnCount - 1, maxX);
519 minY = Math.Max(0, minY);
520 maxY = Math.Min(layer.GridRowCount - 1, maxY);
521 return minX <= maxX && minY <= maxY;
522 }
523}
Point GridCoordinatesAbs
Gets the absolute grid coordinates of this tile within its parent scene layer as integer values.
Point OriginPx
Gets or sets the world-space pixel origin of this layer's (0,0) tile position.
int GridRowCount
Gets the number of rows (tiles high) in this layer's grid.
int TileHeight
Gets or sets the height of each tile in this layer, measured in pixels.
int GridColumnCount
Gets the number of columns (tiles wide) in this layer's grid.
int TileWidth
Gets or sets the width of each tile in this layer, measured in pixels.
@ HexAxialPointedTop
Hexagonal grid using axial coordinates with pointy-topped hexes.
@ Rectangle
Particles are spawned uniformly within a rectangular area. This is the default distribution.