Skip to content

Commit 54c25f2

Browse files
committed
Change base type to struct
1 parent a38bc2d commit 54c25f2

File tree

6 files changed

+21
-25
lines changed

6 files changed

+21
-25
lines changed

Sources/GateEngine/System/Rendering/Drawables/Canvas.swift

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -416,15 +416,13 @@
416416
This function requires you to first call `setCamera(_:size:)`.
417417
- returns: A 2D position representing the location of a 3D object.
418418
*/
419-
public func convertFrom3DSpace(_ position: Position3) -> Position2 {
420-
guard let camera = camera else {
421-
preconditionFailure("Must set camera during `Canvas.init` to use \(#function).")
422-
}
419+
public mutating func convertFrom3DSpace(_ position: Position3) -> Position2 {
420+
precondition(camera != nil, "Must set camera during `Canvas.init` to use \(#function).")
423421
guard let size = size else {
424422
preconditionFailure("Must set size during `Canvas.init` to use \(#function).")
425423
}
426424

427-
let matricies = camera.matricies(withViewportSize: size)
425+
let matricies = camera!.matricies(withViewportSize: size)
428426
var position = position * matricies.viewProjection()
429427
position.x /= position.z
430428
position.y /= position.z
@@ -438,23 +436,21 @@
438436
return Position2(position.x, position.y)
439437
}
440438

441-
public func convertTo3DSpace(_ position: Position2) -> Ray3D {
442-
guard let camera = camera else {
443-
preconditionFailure("Must set camera during `Canvas.init` to use \(#function).")
444-
}
439+
public mutating func convertTo3DSpace(_ position: Position2) -> Ray3D {
440+
precondition(camera != nil, "Must set camera during `Canvas.init` to use \(#function).")
445441
guard let size = size else {
446442
preconditionFailure("Must set size during `Canvas.init` to use \(#function).")
447443
}
448444

449-
switch camera.fieldOfView {
445+
switch camera!.fieldOfView {
450446
case .perspective(let fov):
451447
let halfSize = size / 2
452448
let aspectRatio = size.aspectRatio
453449

454-
let inverseView = camera.matricies(withViewportSize: size).view.inverse
450+
let inverseView = camera!.matricies(withViewportSize: size).view.inverse
455451
let halfFOV = tan(fov.rawValueAsRadians * 0.5)
456-
let near = camera.clippingPlane.near
457-
let far = camera.clippingPlane.far
452+
let near = camera!.clippingPlane.near
453+
let far = camera!.clippingPlane.far
458454

459455
let dx = halfFOV * (position.x / halfSize.width - 1.0) * aspectRatio
460456
let dy = halfFOV * (1.0 - position.y / halfSize.height)
@@ -476,10 +472,10 @@
476472
let x = position.x
477473
let y = position.y
478474

479-
let inverseView = camera.matricies(withViewportSize: size).view.inverse
475+
let inverseView = camera!.matricies(withViewportSize: size).view.inverse
480476
let start = Position3(x, y, -1) * inverseView
481477

482-
return Ray3D(from: start, toward: camera.transform.rotation.forward)
478+
return Ray3D(from: start, toward: camera!.transform.rotation.forward)
483479
}
484480
}
485481

Sources/GateEngine/System/Rendering/Drawables/Drawable.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import Foundation
1010
@MainActor
1111
public protocol Drawable {
1212
var drawCommands: ContiguousArray<DrawCommand> { get }
13-
func matrices(withSize size: Size2) -> Matrices
13+
mutating func matrices(withSize size: Size2) -> Matrices
1414
}
1515

1616
internal extension Drawable {

Sources/GateEngine/System/Rendering/Drawables/Scene.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ import Shaders
362362
}
363363

364364
@inlinable
365-
public func matrices(withSize size: GameMath.Size2) -> Matrices {
365+
public mutating func matrices(withSize size: GameMath.Size2) -> Matrices {
366366
self.camera.matricies(withViewportSize: size)
367367
}
368368
}

Sources/GateEngine/System/Rendering/RenderTarget.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,8 @@ extension _RenderTargetProtocol {
230230
switch drawable {
231231
case let uiCanvas as UICanvas:
232232
drawUICanvas(uiCanvas, clipRect: nil, stencil: nil)
233-
case let scene as Scene:
234-
drawScene(scene, viewportSize: self.size.vector2, clipRect: nil, stencil: nil)
233+
case var scene as Scene:
234+
drawScene(&scene, viewportSize: self.size.vector2, clipRect: nil, stencil: nil)
235235
case let canvas as Canvas:
236236
drawCanvas(canvas, clipRect: nil, stencil: nil)
237237
case let container as RenderTargetFillContainer:
@@ -303,7 +303,7 @@ extension _RenderTargetProtocol {
303303
renderTargetBackend.didEndContent()
304304
}
305305

306-
private func drawScene(_ scene: Scene, viewportSize size: Size2, clipRect: Rect?, stencil: UInt8?) {
306+
private func drawScene(_ scene: inout Scene, viewportSize size: Size2, clipRect: Rect?, stencil: UInt8?) {
307307
let matrices = scene.camera.matricies(withViewportSize: size)
308308

309309
var scissorRect: Rect? = nil

Sources/GateEngine/Types/Camera.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
import GameMath
99

10-
public class Camera {
10+
public struct Camera {
1111
public var transform: Transform3 {
1212
didSet {
1313
needsUpdateTransform = true
@@ -36,7 +36,7 @@ public class Camera {
3636
self.clippingPlane = clippingPlane
3737
}
3838

39-
public convenience init?(_ entity: Entity?) {
39+
public init?(_ entity: Entity?) {
4040
guard let entity = entity else { return nil }
4141
guard let cameraComponent = entity.component(ofType: CameraComponent.self) else {
4242
return nil
@@ -59,7 +59,7 @@ public class Camera {
5959
private static let viewScale = Matrix4x4(scale: Size3(width: 1.0, height: 1.0, depth: -1.0))
6060
private var matrices: Matrices = Matrices(projection: .identity)
6161

62-
public func matricies(withViewportSize size: Size2) -> Matrices {
62+
public mutating func matricies(withViewportSize size: Size2) -> Matrices {
6363
guard self.needsUpdateTransform || self.needsUpdateProjection else { return matrices }
6464

6565
if needsUpdateProjection || self.size != size {

Sources/GateEngine/UI/GameViewController.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ extension GameView {
189189

190190
- returns: A 2D position representing the location of a 3D object in this view's bounds.
191191
*/
192-
public func convert(_ position: Position3, from camera: Camera) -> Position2 {
192+
public func convert(_ position: Position3, from camera: inout Camera) -> Position2 {
193193
let size = self.bounds.size
194194
let matricies = camera.matricies(withViewportSize: size * self.interfaceScale)
195195
var position = position * matricies.viewProjection()
@@ -210,7 +210,7 @@ extension GameView {
210210

211211
- returns: A Ray3D representing the location of a 2D point located on the view. The ray's direction is toward the 3D space accounting for perspective distortion.
212212
*/
213-
public func convert(_ position: Position2, to camera: Camera) -> Ray3D {
213+
public func convert(_ position: Position2, to camera: inout Camera) -> Ray3D {
214214
switch camera.fieldOfView {
215215
case .perspective(let fieldOfView):
216216
let size = self.bounds.size

0 commit comments

Comments
 (0)