Skip to content

Commit 3b73a1b

Browse files
committed
Allow async and sync texture loading
1 parent 650852e commit 3b73a1b

File tree

6 files changed

+21
-9
lines changed

6 files changed

+21
-9
lines changed

Sources/GateEngine/Helpers/TextureAtlas.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ public final class TextureAtlasBuilder {
148148

149149
let importer = PNGImporter()
150150
try importer.synchronousPrepareToImportResourceFrom(path: unresolvedPath)
151-
let rawTexture = try importer.loadTexture(options: .none)
151+
let rawTexture = try importer.synchronousLoadTexture(options: .none)
152152

153153
var textureData = TextureData(size: rawTexture.imageSize, imageData: rawTexture.imageData, coordinate: (0,0))
154154
var dataIndex = self.textureDatas.endIndex

Sources/GateEngine/Resources/Import & Export/Importers/ApplePlatformImageImporter.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,15 @@ public final class ApplePlatformImageImporter: TextureImporter {
5252
throw GateEngineError(error)
5353
}
5454
}
55-
56-
public func loadTexture(options: TextureImporterOptions) throws(GateEngineError) -> RawTexture {
55+
56+
public func synchronousLoadTexture(options: TextureImporterOptions) throws(GateEngineError) -> RawTexture {
5757
return RawTexture(imageSize: size, imageData: data)
5858
}
5959

60+
public func loadTexture(options: TextureImporterOptions) async throws(GateEngineError) -> RawTexture {
61+
return try synchronousLoadTexture(options: options)
62+
}
63+
6064
public static func canProcessFile(_ path: String) -> Bool {
6165
let pathExtension = URL(fileURLWithPath: path).pathExtension
6266
guard pathExtension.isEmpty == false else {return false}

Sources/GateEngine/Resources/Import & Export/Importers/GLTransmissionFormat.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1118,7 +1118,7 @@ extension GLTransmissionFormat: ObjectAnimation3DImporter {
11181118

11191119
extension GLTransmissionFormat: TextureImporter {
11201120
// TODO: Supports only PNG. Add other formats (JPEG, WebP, ...)
1121-
public func loadTexture(options: TextureImporterOptions) throws(GateEngineError) -> RawTexture {
1121+
public func synchronousLoadTexture(options: TextureImporterOptions) throws(GateEngineError) -> RawTexture {
11221122
let imageData: Data
11231123
func loadImageData(image: GLTF.Image) throws(GateEngineError) -> Data {
11241124
if let uri = image.uri {
@@ -1153,6 +1153,10 @@ extension GLTransmissionFormat: TextureImporter {
11531153

11541154
return try PNGDecoder().decode(imageData)
11551155
}
1156+
1157+
public func loadTexture(options: TextureImporterOptions) async throws(GateEngineError) -> RawTexture {
1158+
return try synchronousLoadTexture(options: options)
1159+
}
11561160
}
11571161

11581162
extension GLTransmissionFormat: CollisionMeshImporter {

Sources/GateEngine/Resources/Import & Export/Importers/PNGImporter.swift

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,13 @@ public final class PNGImporter: TextureImporter {
2222
public func prepareToImportResourceFrom(path: String) async throws(GateEngineError) {
2323
self.data = try await Platform.current.loadResource(from: path)
2424
}
25-
26-
public func loadTexture(options: TextureImporterOptions) throws(GateEngineError) -> RawTexture {
25+
26+
public func synchronousLoadTexture(options: TextureImporterOptions) throws(GateEngineError) -> RawTexture {
2727
return try PNGDecoder().decode(data)
28+
}
2829

30+
public func loadTexture(options: TextureImporterOptions) async throws(GateEngineError) -> RawTexture {
31+
return try synchronousLoadTexture(options: options)
2932
}
3033

3134
public static func supportedFileExtensions() -> [String] {

Sources/GateEngine/Resources/Text/Backends/ImageFont.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ struct ImageFont: FontBackend {
1616
init(regular: String) async throws {
1717
let importer = try await Game.unsafeShared.resourceManager.textureImporterForPath(regular)
1818

19-
let rawTexture = try importer.loadTexture(options: .none)
19+
let rawTexture = try await importer.loadTexture(options: .none)
2020

2121
let fontData: [Font.Style: (rawTexture: RawTexture, importer: any TextureImporter.Type)] =
2222
[.regular: (rawTexture, type(of: importer))]

Sources/GateEngine/Resources/Texture/Texture.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ extension Texture: Equatable, Hashable {
164164
// MARK: - Resource Manager
165165

166166
public protocol TextureImporter: ResourceImporter {
167-
func loadTexture(options: TextureImporterOptions) throws(GateEngineError) -> RawTexture
167+
func synchronousLoadTexture(options: TextureImporterOptions) throws(GateEngineError) -> RawTexture
168+
func loadTexture(options: TextureImporterOptions) async throws(GateEngineError) -> RawTexture
168169
}
169170

170171
public struct TextureImporterOptions: Equatable, Hashable, Sendable {
@@ -366,7 +367,7 @@ extension ResourceManager {
366367

367368
let importer = try await Game.unsafeShared.resourceManager.textureImporterForPath(path)
368369

369-
let rawTexture = try importer.loadTexture(options: key.textureOptions)
370+
let rawTexture = try await importer.loadTexture(options: key.textureOptions)
370371
guard rawTexture.imageData.isEmpty == false else {
371372
throw GateEngineError.failedToLoad(resource: path, "File is empty.")
372373
}

0 commit comments

Comments
 (0)