Skip to content

Commit

Permalink
Add initial changes for UI
Browse files Browse the repository at this point in the history
  • Loading branch information
STREGA committed Mar 10, 2024
1 parent 7abfe75 commit 11724c8
Show file tree
Hide file tree
Showing 51 changed files with 1,741 additions and 362 deletions.
4 changes: 3 additions & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ let package = Package(

settings.append(contentsOf: [
/// Prints the output of generated shaders
.define("GATEENGINE_LOG_SHADERS"),
.define("GATEENGINE_DEBUG_LAYOUT"),
/// Prints the output of generated shaders
//.define("GATEENGINE_LOG_SHADERS"),
/// Enables various additional checks and output for rendering
.define("GATEENGINE_DEBUG_RENDERING"),
/// Enables various additional checks and output for input
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

public final class Collision2DSystem: System {
public override func update(game: Game, input: HID, withTimePassed deltaTime: Float) async {
public override func update(context: ECSContext, input: HID, withTimePassed deltaTime: Float) async {
for entity in game.entities {
guard entity.hasComponent(Collision2DComponent.self) else { continue }
guard entity.hasComponent(Transform2Component.self) else { continue }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import GameMath

public final class Physics2DSystem: System {
public override func update(game: Game, input: HID, withTimePassed deltaTime: Float) async {
public override func update(context: ECSContext, input: HID, withTimePassed deltaTime: Float) async {
// Skip Physics if we don't have at least 20 fps
guard deltaTime < 1 / 20 else { return }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

public final class SpriteSystem: System {
public override func update(game: Game, input: HID, withTimePassed deltaTime: Float) async {
public override func update(context: ECSContext, input: HID, withTimePassed deltaTime: Float) async {
for entity in game.entities {
if let spriteComponent = entity.component(ofType: SpriteComponent.self) {
if spriteComponent.moveToNextAnimationIfNeeded {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

public final class TileMapSystem: System {
public override func update(game: Game, input: HID, withTimePassed deltaTime: Float) async {
public override func update(context: ECSContext, input: HID, withTimePassed deltaTime: Float) async {
for entity in game.entities {
if let component = entity.component(ofType: TileMapComponent.self) {
if component.needsSetup {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

public final class BillboardSystem: System {
public override func update(game: Game, input: HID, withTimePassed deltaTime: Float) async {
public override func update(context: ECSContext, input: HID, withTimePassed deltaTime: Float) async {
guard let camera = game.cameraEntity else {return}
let cameraTransform = camera.transform3

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public final class ObjectAnimation3DSystem: System {
return nil
}

public override func update(game: Game, input: HID, withTimePassed deltaTime: Float) async {
public override func update(context: ECSContext, input: HID, withTimePassed deltaTime: Float) async {
func shouldAccumulate(entity: Entity) -> Bool {
guard
let cameraTransform = game.cameraEntity?.component(ofType: Transform3Component.self)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*/

public final class Collision3DSystem: System {
public override func update(game: Game, input: HID, withTimePassed deltaTime: Float) async {
public override func update(context: ECSContext, input: HID, withTimePassed deltaTime: Float) async {
let staticEntities = game.entities.filter({
guard let collisionComponenet = $0.component(ofType: Collision3DComponent.self) else {return false}
if case .static = collisionComponenet.kind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import GameMath

public final class Physics3DSystem: System {
public override func update(game: Game, input: HID, withTimePassed deltaTime: Float) async {
public override func update(context: ECSContext, input: HID, withTimePassed deltaTime: Float) async {
// Skip Physics if we don't have at least 20 fps
guard deltaTime < 1 / 20 else { return }

Expand Down
2 changes: 1 addition & 1 deletion Sources/GateEngine/ECS/3D Specific/Rig/Rig3DSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public final class Rig3DSystem: System {
return nil
}

public override func update(game: Game, input: HID, withTimePassed deltaTime: Float) async {
public override func update(context: ECSContext, input: HID, withTimePassed deltaTime: Float) async {
func shouldAccumulate(entity: Entity) -> Bool {
guard
let cameraTransform = game.cameraEntity?.component(ofType: Transform3Component.self)
Expand Down
43 changes: 20 additions & 23 deletions Sources/GateEngine/ECS/Base/ECSContext.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@
}
}

@usableFromInline
@MainActor final class ECSContext {
@MainActor public final class ECSContext {
private var previousFrameWasDropped: Bool = false
private var platformSystemsNeedSorting = true
private var _platformSystems: [PlatformSystem] = []
Expand Down Expand Up @@ -225,15 +224,14 @@
_sortedEntities.sort(by: { $0.priority.rawValue > $1.priority.rawValue })
}

public private(set) var performance: Performance? = nil
internal private(set) var performance: Performance? = nil
func recordPerformance() {
precondition(performance == nil, "Performance recording was already started!")
self.performance = Performance()
}

public let game: Game
public init(game: Game) {
self.game = game
public init() {

}
}

Expand All @@ -250,8 +248,7 @@ public struct WindowLayout {
//MARK: Update
extension ECSContext {
func shouldRenderAfterUpdate(withTimePassed deltaTime: Float) async -> Bool {
let game = game
let input = game.hid
let input = Game.shared.hid

if let performance = performance {
performance.prepareForReuse()
Expand All @@ -266,31 +263,31 @@ extension ECSContext {
// }
for system in self.systems {
self.performance?.beginStatForSystem(system)
await system.willUpdate(game: game, input: input, withTimePassed: deltaTime)
await system.willUpdate(context: self, input: input, withTimePassed: deltaTime)
self.performance?.endCurrentStatistic()
}
for system in self.platformSystems {
// guard type(of: system).phase == .postDeferred else { continue }
self.performance?.beginStatForSystem(system)
await system.willUpdate(game: game, input: input, withTimePassed: deltaTime)
await system.willUpdate(context: self, input: input, withTimePassed: deltaTime)
self.performance?.endCurrentStatistic()
}

let removed = self._removedEntities
_removedEntities.removeAll(keepingCapacity: true)
for entity in removed {
for system in self.systems {
await system.gameDidRemove(entity: entity, game: game, input: input)
await system.didRemove(entity: entity, from: self, input: input)
}
for system in self.platformSystems {
await system.gameDidRemove(entity: entity, game: game, input: input)
await system.didRemove(entity: entity, from: self, input: input)
}
}

// Drop frame if less then 12fps
let dropFrame: Bool = deltaTime > /* 1/12 */ 0.08333333333
// Only drop 1 frame before requiring a frame
let shouldRender: Bool = game.isHeadless ? false : (previousFrameWasDropped ? true : !dropFrame)
let shouldRender: Bool = Game.shared.isHeadless ? false : (previousFrameWasDropped ? true : !dropFrame)

if previousFrameWasDropped {
previousFrameWasDropped = false
Expand All @@ -309,14 +306,14 @@ extension ECSContext {
return shouldRender
}

func updateRendering(withTimePassed deltaTime: Float, window: Window) {
func updateRendering(into view: GameView, deltaTime: Float) {
if let performance = performance {
performance.startRenderingSystems()
}

for system in self.renderingSystems {
self.performance?.beginStatForSystem(system)
system.willRender(game: game, window: window, withTimePassed: deltaTime)
system.willRender(context: self, into: view, withTimePassed: deltaTime)
self.performance?.endCurrentStatistic()
}

Expand All @@ -336,8 +333,8 @@ extension ECSContext {
for componentID in entity.componentIDs {
if let component = entity.componentBank[componentID] {
if let system = type(of: component).systemThatProcessesThisComponent() {
if game.hasSystem(ofType: system) == false {
game.insertSystem(system)
if self.hasSystem(ofType: system) == false {
self.insertSystem(system)
}
}
}
Expand Down Expand Up @@ -468,25 +465,25 @@ extension ECSContext {
@usableFromInline
func removeSystem(_ system: System) {
if let index = self._systems.firstIndex(where: { $0 === system }) {
self._systems.remove(at: index).teardown(game: game)
self._systems.remove(at: index).teardown(context: self)
}
}
@usableFromInline
func removeSystem(_ system: RenderingSystem) {
if let index = self._renderingSystems.firstIndex(where: { $0 === system }) {
self._renderingSystems.remove(at: index).teardown(game: game)
self._renderingSystems.remove(at: index).teardown(context: self)
}
}
func removeSystem(_ system: PlatformSystem) {
if let index = self._platformSystems.firstIndex(where: { $0 === system }) {
self._platformSystems.remove(at: index).teardown(game: game)
self._platformSystems.remove(at: index).teardown(context: self)
}
}
@usableFromInline @discardableResult
func removeSystem<T: System>(_ system: T.Type) -> System? {
if let index = self._systems.firstIndex(where: { type(of: $0) == system }) {
let system = self._systems.remove(at: index)
system.teardown(game: game)
system.teardown(context: self)
return system
}
return nil
Expand All @@ -495,7 +492,7 @@ extension ECSContext {
func removeSystem<T: RenderingSystem>(_ system: T.Type) -> RenderingSystem? {
if let index = self._renderingSystems.firstIndex(where: { type(of: $0) == system }) {
let system = self._renderingSystems.remove(at: index)
system.teardown(game: game)
system.teardown(context: self)
return system
}
return nil
Expand All @@ -504,7 +501,7 @@ extension ECSContext {
func removeSystem<T: PlatformSystem>(_ system: T.Type) -> PlatformSystem? {
if let index = self._platformSystems.firstIndex(where: { type(of: $0) == system }) {
let system = self._platformSystems.remove(at: index)
system.teardown(game: game)
system.teardown(context: self)
return system
}
return nil
Expand Down
18 changes: 9 additions & 9 deletions Sources/GateEngine/ECS/Base/PlatformSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ import GameMath

required init() {}

internal final func willUpdate(game: Game, input: HID, withTimePassed deltaTime: Float) async {
internal final func willUpdate(context: ECSContext, input: HID, withTimePassed deltaTime: Float) async {
if didSetup == false {
didSetup = true
await setup(game: game, input: input)
await setup(context: context, input: input)
}
if await shouldUpdate(game: game, input: input, withTimePassed: deltaTime) {
await update(game: game, input: input, withTimePassed: deltaTime)
if await shouldUpdate(context: context, input: input, withTimePassed: deltaTime) {
await update(context: context, input: input, withTimePassed: deltaTime)
}
}

Expand All @@ -29,7 +29,7 @@ import GameMath
Use `gameDidRemove()` to cleanup an Entity that will be destoryed.
*/
open func gameDidRemove(entity: Entity, game: Game, input: HID) async {
open func didRemove(entity: Entity, from context: ECSContext, input: HID) async {

}

Expand All @@ -39,23 +39,23 @@ import GameMath
Use `setup()` to create any system specific data and add it to the game.
- note: The call to `setup()` is deferred until the next update frame after the system has been inserted and will be called immediatled before `update(withTimePassed:)`.
*/
open func setup(game: Game, input: HID) async {
open func setup(context: ECSContext, input: HID) async {

}

/**
Called before `update(withTimePassed:)`. Return `true` if you would like `update(withTimePassed:)` to be called, otherwise return `false`.
- parameter deltaTime: The duration of time since the last update frame.
*/
open func shouldUpdate(game: Game, input: HID, withTimePassed deltaTime: Float) async -> Bool {
open func shouldUpdate(context: ECSContext, input: HID, withTimePassed deltaTime: Float) async -> Bool {
return true
}

/**
Called every update frame.
- parameter deltaTime: The duration of time since the last update frame.
*/
open func update(game: Game, input: HID, withTimePassed deltaTime: Float) async {
open func update(context: ECSContext, input: HID, withTimePassed deltaTime: Float) async {
preconditionFailure("Must Override \"\(#function)\" in \(type(of: Self.self))")
}

Expand All @@ -65,7 +65,7 @@ import GameMath
Use teardown to cleanup any system specific data within the game.
- note: The call to `teardown()` happens immediately updon removal from the game.
*/
open func teardown(game: Game) {
open func teardown(context: ECSContext) {

}

Expand Down
18 changes: 9 additions & 9 deletions Sources/GateEngine/ECS/Base/RenderingSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@

}

internal final func willRender(game: Game, window: Window, withTimePassed deltaTime: Float) {
internal final func willRender(context: ECSContext, into view: GameView, withTimePassed deltaTime: Float) {
if didSetup == false {
didSetup = true
setup(game: game)
setup(context: context)
}
if shouldRender(game: game, window: window, withTimePassed: deltaTime) {
render(game: game, window: window, withTimePassed: deltaTime)
if shouldRender(context: context, into: view, withTimePassed: deltaTime) {
render(context: context, into: view, withTimePassed: deltaTime)
}
}

Expand All @@ -35,8 +35,8 @@
- parameter game: The game instance.
**/
open func setup(game: Game) {

open func setup(context: ECSContext) {
}

/**
Expand All @@ -46,7 +46,7 @@
- parameter deltaTime: The duration since the last time this window was rendered.
- returns: `true` if you want to render, otherwise `false`.
**/
open func shouldRender(game: Game, window: Window, withTimePassed deltaTime: Float) -> Bool {
open func shouldRender(context: ECSContext, into view: View, withTimePassed deltaTime: Float) -> Bool {
return true
}

Expand All @@ -57,7 +57,7 @@
- parameter deltaTime: The duration since the last time this window was rendered.
- returns: `true` if you want to render, otherwise `false`.
**/
open func render(game: Game, window: Window, withTimePassed deltaTime: Float) {
open func render(context: ECSContext, into view: GameView, withTimePassed deltaTime: Float) {
preconditionFailure("Must Override \"\(#function)\" in \(type(of: Self.self))")
}

Expand All @@ -68,7 +68,7 @@
- parameter game: The game instance.
**/
open func teardown(game: Game) {
open func teardown(context: ECSContext) {

}

Expand Down
Loading

0 comments on commit 11724c8

Please sign in to comment.