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
TilesheetFactory.cs
3using SkiaSharp;
4
6
10internal static class TilesheetFactory
11{
12 #region factory methods
13
14 internal static Tilesheet FromBitmap(string name, SKBitmap bitmap) => new(name, bitmap);
15
16 internal static Tilesheet FromStream(string name, Stream stream) => new(name, stream);
17
18 internal static Tilesheet FromImageFile(string name, string imageFilePath) => new(name, imageFilePath);
19
20 internal static Tilesheet FromAssetsFile(AssetsFile assetsFile, string entryName) => new(assetsFile, entryName);
21
26 internal static Tilesheet FromDefinitionFile(string gtsPath, string? name = null)
27 {
28 if (string.IsNullOrWhiteSpace(gtsPath))
29 throw new ArgumentException("GTS path must be a non-empty string.", nameof(gtsPath));
30
31 var fullPath = Path.GetFullPath(gtsPath);
32
33 if (!File.Exists(fullPath))
34 throw new FileNotFoundException($"GTS file not found: {fullPath}", fullPath);
35
36 var definition = TilesheetDefinitionSerializer.Load(fullPath, name);
37 return FromDefinition(definition, Path.GetDirectoryName(fullPath));
38 }
39
45 internal static Tilesheet FromDefinition(
46 TilesheetDefinition definition,
47 string? baseDirectory = null,
48 AssetsFile? defaultAssetsFile = null)
49 {
50 ArgumentNullException.ThrowIfNull(definition);
51 var origin = definition.Source.GtsFilePath ?? definition.Source.AssetsFilePath;
52
53 baseDirectory ??= definition.LoadStamp?.ReferenceBaseDirectory ??
54 (string.IsNullOrWhiteSpace(origin) || !Path.IsPathFullyQualified(origin)
55 ? null
56 : Path.GetDirectoryName(origin));
57
58 var archivePath = definition.LoadStamp?.AssetsFilePath ?? definition.Source.AssetsFilePath;
59
60 if (defaultAssetsFile is null && !string.IsNullOrWhiteSpace(archivePath) &&
61 string.IsNullOrWhiteSpace(definition.Image?.FilePath) &&
62 string.IsNullOrWhiteSpace(definition.Image?.AssetsFilePath) &&
63 !string.IsNullOrWhiteSpace(definition.Image?.AssetEntryName))
64 defaultAssetsFile = LoadExistingAssetsFile(ResolvePath(archivePath, baseDirectory));
65
66 var tilesheet = CreateTilesheet(
67 definition,
68 addDefaultRegion: false,
69 baseDirectory,
70 defaultAssetsFile);
71
72 try
73 {
74 foreach (var region in definition.Regions)
75 {
76 var runtimeRegion = tilesheet.AddRegion(
77 region.Name,
78 region.Area,
79 region.TileSize,
80 region.TilePadding,
81 region.RegionMargin,
82 region.Overhang,
83 region.CollisionAdjust,
84 region.CollisionType);
85
86 // A missing frame value inherits the region default. A present value is
87 // an explicit override, even when it currently equals that default.
88 foreach (var frame in region.Frames ?? [])
89 {
90 if (frame.CollisionAdjust is { } adjust)
91 {
92 runtimeRegion.SetFrameCollisionAdjust(
93 frame.XTile,
94 frame.YTile,
95 adjust);
96 }
97
98 if (frame.CollisionType is { } collisionType)
99 {
100 runtimeRegion.SetFrameCollisionType(
101 frame.XTile,
102 frame.YTile,
103 collisionType);
104 }
105 }
106 }
107
108 if (definition.Mask is not null)
109 {
110 tilesheet.ApplyMask(
111 new SKColor(
112 definition.Mask.Red,
113 definition.Mask.Green,
114 definition.Mask.Blue,
115 definition.Mask.Alpha),
116 definition.Mask.Tolerance);
117 }
118 else if (definition.PremultiplyAlpha)
119 {
120 tilesheet.ApplyPremultiplyAlpha();
121 }
122
123 tilesheet.DefinitionProvenance = definition.LoadStamp?.Materialized(
124 definition,
125 () => TilesheetDefinitionSerializer.FromTilesheet(tilesheet));
126
127 return tilesheet;
128 }
129 catch
130 {
131 tilesheet.Dispose();
132 throw;
133 }
134 }
135
141 internal static Tilesheet FromDefinitionAsset(AssetsFile assetsFile, string gtsEntryName, string? name = null)
142 {
143 ArgumentNullException.ThrowIfNull(assetsFile);
144
145 if (string.IsNullOrWhiteSpace(gtsEntryName))
146 {
147 throw new ArgumentException(
148 "GTS asset entry name must be a non-empty string.",
149 nameof(gtsEntryName));
150 }
151
152 var definition = TilesheetDefinitionSerializer.Load(assetsFile, gtsEntryName, name);
153
154 var baseDirectory = string.IsNullOrWhiteSpace(assetsFile.SourcePath)
155 ? null
156 : Path.GetDirectoryName(Path.GetFullPath(assetsFile.SourcePath));
157
158 return FromDefinition(
159 definition,
160 baseDirectory,
161 defaultAssetsFile: assetsFile);
162 }
163
164 #endregion factory methods
165
166 #region private methods
167
168 private static Tilesheet CreateTilesheet(
169 TilesheetDefinition definition,
170 bool addDefaultRegion,
171 string? baseDirectory = null,
172 AssetsFile? defaultAssetsFile = null)
173 {
174 var image = definition.Image
175 ?? throw new InvalidOperationException(
176 "TilesheetDefinition must specify an image source.");
177
178 ValidateImageDefinition(image, defaultAssetsFile);
179
180 if (!string.IsNullOrWhiteSpace(image.FilePath))
181 {
182 return new Tilesheet(
183 definition.Name,
184 ResolvePath(image.FilePath, baseDirectory),
185 addDefaultRegion);
186 }
187
188 AssetsFile assetsFile;
189
190 if (!string.IsNullOrWhiteSpace(image.AssetsFilePath))
191 {
192 assetsFile = LoadExistingAssetsFile(
193 ResolvePath(image.AssetsFilePath, baseDirectory));
194 }
195 else
196 {
197 assetsFile = defaultAssetsFile!;
198 }
199
200 var tilesheet = new Tilesheet(
201 assetsFile,
202 image.AssetEntryName!,
203 addDefaultRegion);
204
205 // The asset entry identifies the image; it is not necessarily the logical
206 // name assigned to the tilesheet by the GTS definition.
207 tilesheet.Name = definition.Name;
208
209 return tilesheet;
210 }
211
212 private static void ValidateImageDefinition(TilesheetImageDefinition image, AssetsFile? defaultAssetsFile)
213 {
214 var hasFilePath = !string.IsNullOrWhiteSpace(image.FilePath);
215 var hasAssetsFilePath = !string.IsNullOrWhiteSpace(image.AssetsFilePath);
216 var hasAssetEntryName = !string.IsNullOrWhiteSpace(image.AssetEntryName);
217
218 if (hasFilePath && (hasAssetsFilePath || hasAssetEntryName))
219 {
220 throw new InvalidOperationException(
221 "TilesheetDefinition image source is ambiguous. Image.FilePath cannot be combined with Image.AssetsFilePath or Image.AssetEntryName.");
222 }
223
224 if (hasAssetsFilePath && !hasAssetEntryName)
225 {
226 throw new InvalidOperationException(
227 "TilesheetDefinition image specifies an AssetsFilePath but does not specify an AssetEntryName.");
228 }
229
230 if (hasFilePath)
231 return;
232
233 if (!hasAssetEntryName)
234 {
235 throw new InvalidOperationException(
236 "TilesheetDefinition must specify either Image.FilePath or Image.AssetEntryName.");
237 }
238
239 if (!hasAssetsFilePath && defaultAssetsFile is null)
240 {
241 throw new InvalidOperationException(
242 "TilesheetDefinition image specifies an AssetEntryName but does not specify an AssetsFilePath, and no default AssetsFile was provided.");
243 }
244 }
245
246 private static AssetsFile LoadExistingAssetsFile(string path)
247 {
248 if (string.IsNullOrWhiteSpace(path))
249 throw new ArgumentException("Assets file path must be a non-empty string.", nameof(path));
250
251 var fullPath = Path.GetFullPath(path);
252
253 var loaded = AssetsFile.AllAssetsFiles.FirstOrDefault(file =>
254 !string.IsNullOrWhiteSpace(file.SourcePath) && string.Equals(
255 Path.GetFullPath(file.SourcePath),
256 fullPath,
257 OperatingSystem.IsWindows() ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal));
258
259 if (loaded is not null)
260 return loaded;
261
262 if (!File.Exists(fullPath))
263 throw new FileNotFoundException($"Assets file not found: {fullPath}", fullPath);
264
265 return AssetsFile.LoadOrCreate(fullPath);
266 }
267
268 private static string ResolvePath(string path, string? baseDirectory)
269 {
270 if (Path.IsPathRooted(path) || string.IsNullOrWhiteSpace(baseDirectory))
271 return path;
272
273 return Path.GetFullPath(Path.Combine(baseDirectory, path));
274 }
275
276 #endregion private methods
277}
string? SourcePath
Gets the archive's reconstructible origin, including an optional stream origin.
string? AssetsFilePath
Gets the .gaf path when loaded from a packed definition file.
List< TilesheetRegionDefinition > Regions
Gets or sets the list of region definitions that define subdivisions within the tilesheet.
TilesheetImageDefinition Image
Gets or sets the image definition that specifies the source image for the tilesheet.
TilesheetMaskDefinition? Mask
Gets or sets the mask definition for color transparency, or null if no mask is applied.
string Name
Gets or sets the name of the tilesheet.
bool PremultiplyAlpha
Gets or sets a value indicating whether alpha should be premultiplied when loading the tilesheet imag...
TilesheetDefinitionSource Source
Gets or sets the provenance metadata for where this definition came from.
string? AssetsFilePath
Gets or sets the path to the assets file containing the tilesheet image.
string? AssetEntryName
Gets or sets the name of the specific entry within an assets file that contains the tilesheet image.
string? FilePath
Gets or sets the direct file path to the tilesheet image.
byte Tolerance
Gets or sets the tolerance value for color matching, allowing colors within a range to be considered ...
byte Alpha
Gets or sets the alpha (opacity) component of the mask color (0-255).
byte Blue
Gets or sets the blue component of the mask color (0-255).
byte Red
Gets or sets the red component of the mask color (0-255).
byte Green
Gets or sets the green component of the mask color (0-255).
@ TilesheetDefinition
Represents a tilesheet definition file (.gts) for Gondwana.Drawing.Tilesheets.GTS....
Definition AssetTypes.cs:47