Skip to content

Commit e306531

Browse files
committed
Refactor to use RawTexture and new GameMath types
1 parent e14e0dc commit e306531

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+276
-282
lines changed

Sources/GateEngine/ECS/2D Specific/Sprite/SpriteComponent.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,14 @@ public final class SpriteComponent: Component {
105105
let texture = spriteSheet?.texture,
106106
texture.state == .ready
107107
{
108-
let columns = texture.size.width / spriteSize.width
109-
let rows = texture.size.height / spriteSize.height
108+
let columns = Float(texture.size.width / Int32(spriteSize.width))
109+
let rows = Float(texture.size.height / Int32(spriteSize.height))
110110
let startFrame = (animation.spriteSheetStart.y * columns) + animation.spriteSheetStart.x
111111
let endFrame = {
112112
if let frameCount = animation.frameCount {
113113
return startFrame + (frameCount - 1)
114114
}
115-
let framesInAnimation = (columns * rows) - startFrame
115+
let framesInAnimation = Float(columns * rows) - startFrame
116116
return framesInAnimation
117117
}()
118118
let currentFrame = floor(

Sources/GateEngine/ECS/2D Specific/TileMap/TileMapSystem.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,18 @@ public final class TileMapSystem: System {
5252

5353
let tileSize = tileSet.tileSize
5454

55-
let wM: Float = 1 / tileSet.texture.size.width
56-
let hM: Float = 1 / tileSet.texture.size.height
55+
let wM: Float = 1 / Float(tileSet.texture.size.width)
56+
let hM: Float = 1 / Float(tileSet.texture.size.height)
5757
for hIndex in 0 ..< Int(tileMap.size.height) {
5858
for wIndex in 0 ..< Int(tileMap.size.width) {
5959
let tile = layer.tileAtCoordinate(TileMap.Layer.Coordinate(column: wIndex, row: hIndex))
6060
guard tile.id > -1 else {continue}
6161
let tileRect = tileSet.rectForTile(tile)
6262
let position = Position2(
63-
x: Float(wIndex) * tileSize.width,
64-
y: Float(hIndex) * tileSize.height
63+
x: Float(wIndex) * Float(tileSize.width),
64+
y: Float(hIndex) * Float(tileSize.height)
6565
)
66-
let rect = Rect(position: position, size: tileSize)
66+
let rect = Rect(position: position, size: tileSize.vector2)
6767
var v1 = Vertex(
6868
px: rect.x,
6969
py: rect.y,

Sources/GateEngine/ECS/StandardRenderingSystem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public final class StandardRenderingSystem: RenderingSystem {
2020
if let verticalResolution = verticalResolution {
2121
var width = verticalResolution * view.frame.size.aspectRatio
2222
width -= width.truncatingRemainder(dividingBy: 2)
23-
renderTarget.size = Size2(width: width, height: verticalResolution)
23+
renderTarget.size = Size2i(width: Int32(width), height: Int32(verticalResolution))
2424
}
2525

2626
do { // 3D

Sources/GateEngine/Helpers/TextureAtlas.swift

Lines changed: 33 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,19 @@
77
#if GATEENGINE_PLATFORM_HAS_SynchronousFileSystem
88
import Foundation
99

10-
public struct TextureAtlas {
11-
public let imageSize: Size2
12-
public let imageData: Data
10+
public struct TextureAtlas: Sendable {
11+
public let rawTexture: RawTexture
1312
private let blockSize: Int
1413
private let textures: [Texture]
1514

1615
internal struct Texture {
1716
let path: String
18-
let size: Size2
17+
let size: Size2i
1918
let coordinate: (x: Int, y: Int)
2019
}
2120

22-
internal init(size: Size2, data: Data, blockSize: Int, textures: [Texture]) {
23-
self.imageSize = size
24-
self.imageData = data
21+
internal init(rawTexture: RawTexture, blockSize: Int, textures: [Texture]) {
22+
self.rawTexture = rawTexture
2523
self.blockSize = blockSize
2624
self.textures = textures
2725
}
@@ -44,12 +42,12 @@ public struct TextureAtlas {
4442
var uv = inUV
4543

4644
//Scale
47-
uv.x *= texture.size.width / imageSize.width
48-
uv.y *= texture.size.height / imageSize.height
45+
uv.x *= Float(texture.size.width) / Float(rawTexture.imageSize.width)
46+
uv.y *= Float(texture.size.height) / Float(rawTexture.imageSize.height)
4947

5048
//Offset
51-
uv.x += Float(texture.coordinate.x * blockSize) * (1 / imageSize.width)
52-
uv.y += Float(texture.coordinate.y * blockSize) * (1 / imageSize.height)
49+
uv.x += Float(texture.coordinate.x * blockSize) * (1 / Float(rawTexture.imageSize.width))
50+
uv.y += Float(texture.coordinate.y * blockSize) * (1 / Float(rawTexture.imageSize.height))
5351

5452
return uv
5553
}
@@ -60,14 +58,17 @@ public struct TextureAtlas {
6058
return Rect(
6159
x: Float(texture.coordinate.x * blockSize),
6260
y: Float(texture.coordinate.y * blockSize),
63-
width: texture.size.width,
64-
height: texture.size.height
61+
width: Float(texture.size.width),
62+
height: Float(texture.size.height)
6563
)
6664
}
6765

6866
@MainActor
6967
public func createTexture(withMipMapping mipMapping: MipMapping = .auto(levels: .max)) -> GateEngine.Texture {
70-
return GateEngine.Texture(data: imageData, size: imageSize, mipMapping: mipMapping)
68+
return GateEngine.Texture(
69+
rawTexture: rawTexture,
70+
mipMapping: mipMapping
71+
)
7172
}
7273
}
7374

@@ -84,28 +85,26 @@ extension TextureAtlasBuilder {
8485
}
8586
}
8687
struct TextureData: Equatable, Hashable, Sendable {
87-
let width: Int
88-
let height: Int
88+
let size: Size2i
8989
let imageData: Data
9090
var coordinate: (x: Int, y: Int)
9191

9292
nonisolated static func == (lhs: TextureData, rhs: TextureData) -> Bool {
93-
guard lhs.width == rhs.width && lhs.height == rhs.height else {return false}
93+
guard lhs.size == rhs.size else {return false}
9494
return lhs.imageData.elementsEqual(rhs.imageData)
9595
}
9696
nonisolated func hash(into hasher: inout Hasher) {
97-
hasher.combine(width)
98-
hasher.combine(height)
97+
hasher.combine(size)
9998
hasher.combine(imageData)
10099
}
101100
}
102101

103-
func textureBlocksWide(texturePixelWidth: Float) -> Int {
104-
return Int(ceil(texturePixelWidth / Float(blockSize)))
102+
func textureBlocksWide(texturePixelWidth: Int) -> Int {
103+
return texturePixelWidth / blockSize
105104
}
106105

107-
func textureBlocksTall(texturePixelHeight: Float) -> Int {
108-
return Int(ceil(texturePixelHeight / Float(blockSize)))
106+
func textureBlocksTall(texturePixelHeight: Int) -> Int {
107+
return texturePixelHeight / blockSize
109108
}
110109
}
111110

@@ -149,18 +148,16 @@ public final class TextureAtlasBuilder {
149148

150149
let importer = PNGImporter()
151150
try importer.synchronousPrepareToImportResourceFrom(path: unresolvedPath)
152-
let png = try importer.loadTexture(options: .none)
153-
let width = Int(png.size.width)
154-
let height = Int(png.size.height)
151+
let rawTexture = try importer.loadTexture(options: .none)
155152

156-
var textureData = TextureData(width: width, height: height, imageData: png.data, coordinate: (0,0))
153+
var textureData = TextureData(size: rawTexture.imageSize, imageData: rawTexture.imageData, coordinate: (0,0))
157154
var dataIndex = self.textureDatas.endIndex
158155
if sacrificePerformanceForSize, let existingIndex = self.textureDatas.firstIndex(where: {$0 == textureData}) {
159156
dataIndex = existingIndex
160157
}else{
161158
let coord = searchGrid.firstUnoccupiedFor(
162-
width: textureBlocksWide(texturePixelWidth: png.size.width),
163-
height: textureBlocksTall(texturePixelHeight: png.size.height),
159+
width: textureBlocksWide(texturePixelWidth: Int(rawTexture.imageSize.width)),
160+
height: textureBlocksTall(texturePixelHeight: Int(rawTexture.imageSize.height)),
164161
markOccupied: true
165162
)
166163
textureData.coordinate = coord
@@ -190,8 +187,8 @@ public final class TextureAtlasBuilder {
190187
false,
191188
x: textureData.coordinate.x,
192189
y: textureData.coordinate.y,
193-
width: textureBlocksWide(texturePixelWidth: Float(textureData.width)),
194-
height: textureBlocksTall(texturePixelHeight: Float(textureData.height))
190+
width: textureBlocksWide(texturePixelWidth: Int(textureData.size.width)),
191+
height: textureBlocksTall(texturePixelHeight: Int(textureData.size.height))
195192
)
196193

197194
// Reindex the Texture dataIndex values
@@ -215,7 +212,7 @@ public final class TextureAtlasBuilder {
215212
}
216213

217214
public func generateAtlas() -> TextureAtlas {
218-
let textureSize: Size2 = Size2(Float(self.searchGrid.width * blockSize), Float(self.searchGrid.height * blockSize))
215+
let textureSize: Size2i = .castInit(width: self.searchGrid.width * blockSize, height: self.searchGrid.height * blockSize)
219216

220217
let dstWidth = Int(textureSize.width * 4)
221218

@@ -227,9 +224,9 @@ public final class TextureAtlasBuilder {
227224
var coord = textureData.coordinate
228225
coord.x *= blockSize
229226
coord.y *= blockSize
230-
let srcWidth = textureData.width * 4
227+
let srcWidth = Int(textureData.size.width) * 4
231228
let x = (coord.x * 4)
232-
for row in 0 ..< textureData.height {
229+
for row in 0 ..< Int(textureData.size.height) {
233230
let srcStart = srcWidth * row
234231
let srcRange = srcStart ..< (srcStart + srcWidth)
235232

@@ -248,15 +245,14 @@ public final class TextureAtlasBuilder {
248245

249246

250247
let atlas = TextureAtlas(
251-
size: textureSize,
252-
data: imageData,
248+
rawTexture: RawTexture(imageSize: textureSize, imageData: imageData),
253249
blockSize: blockSize,
254250
textures: textures.indices.map({
255251
let texture = textures[$0]
256252
let textureData = textureDatas[texture.dataIndex]
257253
return TextureAtlas.Texture(
258254
path: texture.resourcePath,
259-
size: Size2(Float(textureData.width), Float(textureData.height)),
255+
size: textureData.size,
260256
coordinate: textureData.coordinate
261257
)
262258
})

Sources/GateEngine/Resources/Animation/ObjectAnimation3D.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,10 +619,10 @@ extension ResourceManager {
619619

620620
do {
621621
guard let fileExtension = path.components(separatedBy: ".").last else {
622-
throw GateEngineError.failedToLoad("Unknown file type.")
622+
throw GateEngineError.failedToLoad(resource: path, "Unknown file type.")
623623
}
624624
guard let importer: any ObjectAnimation3DImporter = Game.unsafeShared.resourceManager.importerForFileType(fileExtension) else {
625-
throw GateEngineError.failedToLoad("No importer for \(fileExtension).")
625+
throw GateEngineError.failedToLoad(resource: path, "No importer for \(fileExtension).")
626626
}
627627

628628
let data = try await Platform.current.loadResource(from: path)

Sources/GateEngine/Resources/Geometry/Geometry.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ extension Geometry: Equatable, Hashable {
132132
// MARK: - Resource Manager
133133

134134
public protocol GeometryImporter: ResourceImporter {
135-
func loadGeometry(options: GeometryImporterOptions) async throws -> RawGeometry
135+
func loadGeometry(options: GeometryImporterOptions) async throws(GateEngineError) -> RawGeometry
136136
}
137137

138138
public struct GeometryImporterOptions: Equatable, Hashable, Sendable {
@@ -197,7 +197,7 @@ extension RawGeometry {
197197
guard
198198
let importer: any GeometryImporter = try await Game.unsafeShared.resourceManager.geometryImporterForPath(path)
199199
else {
200-
throw GateEngineError.failedToLoad("No importer for \(URL(fileURLWithPath: path).pathExtension).")
200+
throw GateEngineError.failedToLoad(resource: path, "No importer for \(URL(fileURLWithPath: path).pathExtension).")
201201
}
202202

203203
do {

Sources/GateEngine/Resources/Geometry/Lines.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ extension RawLines {
6969
}
7070
public init(path: String, options: GeometryImporterOptions = .none) async throws {
7171
guard let importer: any GeometryImporter = try await Game.unsafeShared.resourceManager.geometryImporterForPath(path) else {
72-
throw GateEngineError.failedToLoad("No importer for \(URL(fileURLWithPath: path).pathExtension).")
72+
throw GateEngineError.failedToLoad(resource: path, "No importer for \(URL(fileURLWithPath: path).pathExtension).")
7373
}
7474

7575
do {

Sources/GateEngine/Resources/Geometry/Points.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ extension RawPoints {
6969
}
7070
public init(path: String, options: GeometryImporterOptions = .none) async throws {
7171
guard let importer: any GeometryImporter = try await Game.unsafeShared.resourceManager.geometryImporterForPath(path) else {
72-
throw GateEngineError.failedToLoad("No importer for \(URL(fileURLWithPath: path).pathExtension).")
72+
throw GateEngineError.failedToLoad(resource: path, "No importer for \(URL(fileURLWithPath: path).pathExtension).")
7373
}
7474

7575
do {

Sources/GateEngine/Resources/Import & Export/Coding/PNGCoder.swift

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,15 @@
55
* http://stregasgate.com
66
*/
77

8-
public final class PNGDecoder {
9-
public func decode(_ data: Data) throws(GateEngineError) -> Image {
8+
public struct PNGDecoder {
9+
public func decode(_ data: Data) throws(GateEngineError) -> RawTexture {
1010
#if canImport(LibSPNG)
1111
try LibSPNG.decode(data: data)
1212
#else
1313
fatalError("PNGDecoder is not supported on this platform.")
1414
#endif
1515
}
16-
17-
public struct Image {
18-
let width: Int
19-
let height: Int
20-
let data: Data
21-
}
22-
16+
2317
public init() {
2418

2519
}
@@ -36,19 +30,19 @@ public final class PNGDecoder {
3630
/**
3731
Encodes raw pixel data as PNG formatted data.
3832
*/
39-
public final class PNGEncoder {
33+
public struct PNGEncoder {
4034
/**
4135
- parameter data: RGBA8 formatted image data
4236
- parameter width: The count of pixel columns for `data`
4337
- parameter height: The count of pixel rows for `data`
4438
- parameter sacrificePerformanceToShrinkData: `true` if extra processingshould be done to produce smaller PNG data.
4539
*/
46-
public func encode(_ data: Data, width: Int, height: Int, sacrificePerformanceToShrinkData: Bool = false) throws(GateEngineError) -> Data {
40+
public func encode(_ rawTexture: RawTexture, sacrificePerformanceToShrinkData: Bool = false) throws(GateEngineError) -> Data {
4741
#if canImport(LibSPNG)
4842
if sacrificePerformanceToShrinkData {
49-
try LibSPNG.encodeSmallest(data: data, width: width, height: height)
43+
try LibSPNG.encodeSmallest(data: rawTexture.imageData, width: Int(rawTexture.imageSize.width), height: Int(rawTexture.imageSize.height))
5044
}else{
51-
try LibSPNG.encodeRGBA(data: data, width: width, height: height, optimizeAlpha: false)
45+
try LibSPNG.encodeRGBA(data: rawTexture.imageData, width: Int(rawTexture.imageSize.width), height: Int(rawTexture.imageSize.height), optimizeAlpha: false)
5246
}
5347
#else
5448
fatalError("PNGEncoder is not supported on this platform.")
@@ -235,7 +229,7 @@ enum LibSPNG {
235229
}
236230

237231
@inlinable
238-
static func decode(data: Data) throws(GateEngineError) -> PNGDecoder.Image {
232+
static func decode(data: Data) throws(GateEngineError) -> RawTexture {
239233
do {
240234
return try data.withUnsafeBytes { data in
241235
/* Create a context */
@@ -283,7 +277,7 @@ enum LibSPNG {
283277
throw GateEngineError.failedToDecode(String(cString: spng_strerror(header_err)))
284278
}
285279

286-
return PNGDecoder.Image(width: Int(header.width), height: Int(header.height), data: out)
280+
return RawTexture(imageSize: .castInit(width: header.width, height: header.height), imageData: out)
287281
}
288282
}catch let error as GateEngineError {
289283
throw error // Typed throws not supported by closures as of Swift 6.2

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import UniformTypeIdentifiers
1414

1515
public final class ApplePlatformImageImporter: TextureImporter {
1616
var data: Data! = nil
17-
var size: Size2! = nil
17+
var size: Size2i! = nil
1818
public required init() {}
1919

2020
public func synchronousPrepareToImportResourceFrom(path: String) throws(GateEngineError) {
@@ -38,23 +38,23 @@ public final class ApplePlatformImageImporter: TextureImporter {
3838
func populateFromData(_ data: Data) throws(GateEngineError) {
3939
do {
4040
guard let imageSource = CGImageSourceCreateWithData(data as CFData, nil) else {
41-
throw GateEngineError.generic("Failed to decode image source.")
41+
throw GateEngineError.failedToDecode("Failed to decode image source.")
4242
}
4343
guard let image = CGImageSourceCreateImageAtIndex(imageSource, 0, nil) else {
44-
throw GateEngineError.generic("Failed to decode subimage zero.")
44+
throw GateEngineError.failedToDecode("Failed to decode subimage zero.")
4545
}
46-
self.size = Size2(Float(image.width), Float(image.height))
46+
self.size = .castInit(width: image.width, height: image.height)
4747
guard let data = image.dataProvider?.data as? Data else {
48-
throw GateEngineError.generic("Failed to decode data.")
48+
throw GateEngineError.failedToDecode("Failed to decode data.")
4949
}
5050
self.data = data
5151
}catch{
5252
throw GateEngineError(error)
5353
}
5454
}
5555

56-
public func loadTexture(options: TextureImporterOptions) throws(GateEngineError) -> (data: Data, size: Size2) {
57-
return (data, size)
56+
public func loadTexture(options: TextureImporterOptions) throws(GateEngineError) -> RawTexture {
57+
return RawTexture(imageSize: size, imageData: data)
5858
}
5959

6060
public static func canProcessFile(_ path: String) -> Bool {

0 commit comments

Comments
 (0)