Skip to content

Commit 1a3a247

Browse files
committed
Refactor to be consistent
1 parent 98c7f01 commit 1a3a247

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

Sources/GateEngine/Resources/Texture/Raw/RawTexture.swift

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ public struct RawTexture: Sendable {
1818
public extension RawTexture {
1919
@inlinable
2020
nonmutating func color(at pixelCoordinate: Position2i) -> Color {
21-
return self[index(for: pixelCoordinate)]
21+
return self[index(at: pixelCoordinate)]
2222
}
2323

2424
@inlinable
2525
mutating func setColor(_ color: Color, at pixelCoordinate: Position2i) {
26-
self[index(for: pixelCoordinate)] = color
26+
self[index(at: pixelCoordinate)] = color
2727
}
2828

2929
@inlinable
@@ -47,6 +47,11 @@ public extension RawTexture {
4747
)
4848
}
4949

50+
@inlinable
51+
func textureCoordinate(at index: Index) -> Position2f {
52+
return self.textureCoordinate(at: self.pixelCoordinate(at: index))
53+
}
54+
5055
@inlinable
5156
func pixelCoordinate(at textureCoordinate: Position2f) -> Position2i {
5257
return Position2i(
@@ -56,30 +61,39 @@ public extension RawTexture {
5661
}
5762

5863
@inlinable
59-
func index(for pixelCoordinate: Position2i) -> Int {
64+
func pixelCoordinate(at index: Index) -> Position2i {
65+
return Position2i(
66+
x: index % self.imageSize.width,
67+
y: index / self.imageSize.width
68+
)
69+
}
70+
71+
@inlinable
72+
func index(at pixelCoordinate: Position2i) -> Index {
6073
return ((self.imageSize.width * pixelCoordinate.y) + pixelCoordinate.x)
6174
}
6275
}
6376

6477
extension RawTexture: RandomAccessCollection, MutableCollection {
6578
public typealias Element = Color
79+
public typealias Index = Int
6680

6781
@inlinable
68-
public var startIndex: Int {
82+
public var startIndex: Index {
6983
nonmutating get {
7084
return 0
7185
}
7286
}
7387

7488
@inlinable
75-
public var endIndex: Int {
89+
public var endIndex: Index {
7690
nonmutating get {
7791
return self.imageSize.width * self.imageSize.height
7892
}
7993
}
8094

8195
@inlinable
82-
public subscript(index: Int) -> Color {
96+
public subscript(index: Index) -> Color {
8397
nonmutating get {
8498
return self.color(at: index)
8599
}
@@ -92,7 +106,7 @@ extension RawTexture: RandomAccessCollection, MutableCollection {
92106
internal extension RawTexture {
93107
@safe // <- Bounds is checked
94108
@inlinable
95-
nonmutating func color(at index: Int) -> Color {
109+
nonmutating func color(at index: Index) -> Color {
96110
let offset = index * 4
97111
precondition(self.imageData.indices.contains(offset), "Index out of range.")
98112
return self.imageData.withUnsafeBytes { data in
@@ -103,7 +117,7 @@ internal extension RawTexture {
103117

104118
@safe // <- Bounds is checked
105119
@inlinable
106-
mutating func setColor(_ color: Color, at index: Int) {
120+
mutating func setColor(_ color: Color, at index: Index) {
107121
let offset = index * 4
108122
precondition(self.imageData.indices.contains(offset), "Index out of range.")
109123
self.imageData.withUnsafeMutableBytes { data in

0 commit comments

Comments
 (0)