Skip to content

Commit 6376de2

Browse files
authored
Merge pull request #5 from pcbeard/MorePortable
Make the demo more portable
2 parents bf1971b + ba163c5 commit 6376de2

File tree

10 files changed

+224
-90
lines changed

10 files changed

+224
-90
lines changed

Package.resolved

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Package.swift

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
// swift-tools-version:5.3
2-
// The swift-tools-version declares the minimum version of Swift required to build this package.
3-
42
import PackageDescription
53

64
let package = Package(
@@ -11,20 +9,20 @@ let package = Package(
119
.tvOS(.v11)
1210
],
1311
dependencies: [
14-
// Dependencies declare other packages that this package depends on.
15-
.package(name: "SDL2", url: "https://github.com/ctreffs/SwiftSDL2.git", from: "1.1.0"),
16-
.package(name: "FirebladeECS", url: "https://github.com/fireblade-engine/ecs.git", from: "0.17.4"),
17-
.package(name: "FirebladeMath", url: "https://github.com/fireblade-engine/math.git", from: "0.9.2")
12+
.package(name: "SDL2", url: "https://github.com/ctreffs/SwiftSDL2.git", from: "1.3.0"),
13+
.package(name: "FirebladeECS", url: "https://github.com/fireblade-engine/ecs.git", from: "0.17.5"),
14+
.package(name: "FirebladeMath", url: "https://github.com/fireblade-engine/math.git", from: "0.11.0")
1815
],
1916
targets: [
20-
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
21-
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
17+
.target(
18+
name: "SDLKit",
19+
dependencies: ["SDL2"]),
2220
.target(
2321
name: "Particles",
24-
dependencies: ["FirebladeECS", "SDL2"]),
22+
dependencies: ["FirebladeECS", "SDL2", "SDLKit"]),
2523
.target(
2624
name: "Asteroids",
27-
dependencies: ["FirebladeECS", "SDL2", "FirebladeMath", "AsteroidsGameLibrary"],
25+
dependencies: ["FirebladeECS", "SDL2", "SDLKit", "FirebladeMath", "AsteroidsGameLibrary"],
2826
exclude: ["Resources/source.txt"],
2927
resources: [.copy("Resources/asteroid.wav"), .copy("Resources/ship.wav"), .copy("Resources/shoot.wav")]),
3028
.target(name: "AsteroidsGameLibrary",

Sources/Asteroids/Systems/RenderSystem.swift

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

88
import FirebladeECS
99
import AsteroidsGameLibrary
10-
import SDL2
10+
import SDLKit
1111

1212
final class RenderSystem {
1313
typealias Parallel = (@escaping (Component) -> Void) -> Void
@@ -23,7 +23,7 @@ final class RenderSystem {
2323
handleComponentRemoved: @escaping Parallel,
2424
nexus: Nexus) {
2525
// use hardware accelerated renderer
26-
let flags: UInt32 = SDL_RENDERER_ACCELERATED.rawValue
26+
let flags: SDL_RendererFlags = [.accelerated]
2727
renderer = SDL_CreateRenderer(window,
2828
-1, // -1 to initialize the first driver supporting the requested flags
2929
flags)

Sources/Asteroids/Timer.swift

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,43 @@
55
// Created by Christian Treffs on 28.10.17.
66
//
77

8-
import Darwin.Mach.mach_time
8+
import SDL2
99

1010
public struct Timer {
11-
private let numerator: UInt64
12-
private let denominator: UInt64
11+
private let countPerSecond: UInt64
1312
private var startTime: UInt64 = 0
1413
private var stopTime: UInt64 = 0
1514

1615
public init() {
17-
var timeBaseInfo = mach_timebase_info.init(numer: 0, denom: 0 )
18-
let success: kern_return_t = mach_timebase_info(&timeBaseInfo)
19-
assert(KERN_SUCCESS == success)
20-
numerator = UInt64(timeBaseInfo.numer)
21-
denominator = UInt64(timeBaseInfo.denom)
16+
countPerSecond = SDL_GetPerformanceFrequency()
2217
}
2318

2419
public mutating func start() {
25-
startTime = mach_absolute_time()
20+
startTime = SDL_GetPerformanceCounter()
2621
}
22+
2723
public mutating func stop() {
28-
stopTime = mach_absolute_time()
24+
stopTime = SDL_GetPerformanceCounter()
2925
}
26+
3027
public mutating func reset() {
3128
startTime = 0
3229
stopTime = 0
3330
}
3431

3532
public var nanoSeconds: UInt64 {
36-
((stopTime - startTime) * numerator) / denominator
33+
return ((stopTime - startTime) * 1_000_000_000) / countPerSecond
3734
}
3835

3936
public var microSeconds: Double {
40-
Double(nanoSeconds) / 1.0e3
37+
return Double((stopTime - startTime) * 1_000_000) / Double(countPerSecond)
4138
}
4239

4340
public var milliSeconds: Double {
44-
Double(nanoSeconds) / 1.0e6
41+
return Double((stopTime - startTime) * 1_000) / Double(countPerSecond)
4542
}
4643

4744
public var seconds: Double {
48-
Double(nanoSeconds) / 1.0e9
45+
return Double(stopTime - startTime) / Double(countPerSecond)
4946
}
5047
}

Sources/Asteroids/main.swift

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import FirebladeECS
22
import AsteroidsGameLibrary
3-
import SDL2
3+
import SDLKit
44

55
// MARK: SDL and Nexus Engines Setup
66

77
// swiftlint:disable prefixed_toplevel_constant
88

99
// initialize the SDL library with video and audio
10-
if SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO) != 0 {
10+
if !SDL_Init(subsystems: [.video, .audio]) {
1111
fatalError("could not init video/audio - reason: \(String(cString: SDL_GetError()))")
1212
}
1313

@@ -38,8 +38,10 @@ var width: Int32 = max(displayMode.w / 2, 800)
3838
var height: Int32 = max(displayMode.h / 2, 600)
3939

4040
// flags for window to be created with
41-
let winFlags: UInt32 = SDL_WINDOW_SHOWN.rawValue // make window visible
42-
| SDL_WINDOW_RESIZABLE.rawValue // and resizable
41+
let winFlags: SDL_WindowFlags = [
42+
.shown, // make window visible
43+
.resizable // and resizable
44+
]
4345
// create window
4446
let hWin = SDL_CreateWindow(
4547
windowTitle,
@@ -222,17 +224,17 @@ while quit == false {
222224
// while event is received process its type
223225
while SDL_PollEvent(&event) == 1 {
224226
// determine what kind of event is received in order to react to it
225-
switch SDL_EventType(rawValue: event.type) {
227+
switch event.eventType {
226228
case SDL_QUIT:
227229
quit = true
228230

229-
case SDL_WINDOWEVENT where event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED.rawValue:
231+
case SDL_WINDOWEVENT where event.windowEvent == SDL_WINDOWEVENT_SIZE_CHANGED:
230232
width = Int32(event.window.data1)
231233
height = Int32(event.window.data2)
232234

233235
case SDL_KEYDOWN:
234236
keysDown.insert(event.key.keysym.sym)
235-
switch SDL_KeyCode(UInt32(event.key.keysym.sym)) {
237+
switch event.keyCode {
236238
case SDLK_ESCAPE:
237239
quit = true
238240

Sources/AsteroidsGameLibrary/Math.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ extension Matrix {
4040
/// multiply two matrices together
4141
public static func * (lhs: Self, rhs: Self) -> Self {
4242
Self(
43-
//first row
43+
// first row
4444
m11: (lhs.m11 * rhs.m11) + (lhs.m12 * rhs.m21) + (lhs.m13 * rhs.m31),
4545
m12: (lhs.m11 * rhs.m12) + (lhs.m12 * rhs.m22) + (lhs.m13 * rhs.m32),
4646
m13: (lhs.m11 * rhs.m13) + (lhs.m12 * rhs.m23) + (lhs.m13 * rhs.m33),
47-
//second
47+
// second
4848
m21: (lhs.m21 * rhs.m11) + (lhs.m22 * rhs.m21) + (lhs.m23 * rhs.m31),
4949
m22: (lhs.m21 * rhs.m12) + (lhs.m22 * rhs.m22) + (lhs.m23 * rhs.m32),
5050
m23: (lhs.m21 * rhs.m13) + (lhs.m22 * rhs.m23) + (lhs.m23 * rhs.m33),
51-
//third
51+
// third
5252
m31: (lhs.m31 * rhs.m11) + (lhs.m32 * rhs.m21) + (lhs.m33 * rhs.m31),
5353
m32: (lhs.m31 * rhs.m12) + (lhs.m32 * rhs.m22) + (lhs.m33 * rhs.m32),
5454
m33: (lhs.m31 * rhs.m13) + (lhs.m32 * rhs.m23) + (lhs.m33 * rhs.m33)

Sources/Particles/Timer.swift

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,46 +5,43 @@
55
// Created by Christian Treffs on 28.10.17.
66
//
77

8-
import Darwin.Mach.mach_time
8+
import SDL2
99

1010
public struct Timer {
11-
private let numerator: UInt64
12-
private let denominator: UInt64
13-
private var startTime: UInt64 = 0
14-
private var stopTime: UInt64 = 0
15-
16-
public init() {
17-
var timeBaseInfo = mach_timebase_info.init(numer: 0, denom: 0 )
18-
let success: kern_return_t = mach_timebase_info(&timeBaseInfo)
19-
assert(KERN_SUCCESS == success)
20-
numerator = UInt64(timeBaseInfo.numer)
21-
denominator = UInt64(timeBaseInfo.denom)
22-
}
23-
24-
public mutating func start() {
25-
startTime = mach_absolute_time()
26-
}
27-
public mutating func stop() {
28-
stopTime = mach_absolute_time()
29-
}
30-
public mutating func reset() {
31-
startTime = 0
32-
stopTime = 0
33-
}
34-
35-
public var nanoSeconds: UInt64 {
36-
return ((stopTime - startTime) * numerator) / denominator
37-
}
38-
39-
public var microSeconds: Double {
40-
return Double(nanoSeconds) / 1.0e3
41-
}
42-
43-
public var milliSeconds: Double {
44-
return Double(nanoSeconds) / 1.0e6
45-
}
46-
47-
public var seconds: Double {
48-
return Double(nanoSeconds) / 1.0e9
49-
}
11+
private let countPerSecond: UInt64
12+
private var startTime: UInt64 = 0
13+
private var stopTime: UInt64 = 0
14+
15+
public init() {
16+
countPerSecond = SDL_GetPerformanceFrequency()
17+
}
18+
19+
public mutating func start() {
20+
startTime = SDL_GetPerformanceCounter()
21+
}
22+
23+
public mutating func stop() {
24+
stopTime = SDL_GetPerformanceCounter()
25+
}
26+
27+
public mutating func reset() {
28+
startTime = 0
29+
stopTime = 0
30+
}
31+
32+
public var nanoSeconds: UInt64 {
33+
return ((stopTime - startTime) * 1_000_000_000) / countPerSecond
34+
}
35+
36+
public var microSeconds: Double {
37+
return Double((stopTime - startTime) * 1_000_000) / Double(countPerSecond)
38+
}
39+
40+
public var milliSeconds: Double {
41+
return Double((stopTime - startTime) * 1_000) / Double(countPerSecond)
42+
}
43+
44+
public var seconds: Double {
45+
return Double(stopTime - startTime) / Double(countPerSecond)
46+
}
5047
}

Sources/Particles/main.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import SDL2
1+
import SDLKit
22
import FirebladeECS
33

4-
if SDL_Init(SDL_INIT_VIDEO) != 0 {
4+
if !SDL_Init(subsystems: [.video]) {
55
fatalError("Could not initialize video \(String(cString: SDL_GetError()))")
66
}
77

@@ -26,7 +26,7 @@ var windowTitle: String {
2626
var width: Int32 = max(displayMode.w / 2, 800)
2727
var height: Int32 = max(displayMode.h / 2, 600)
2828

29-
let winFlags: UInt32 = SDL_WINDOW_SHOWN.rawValue | SDL_WINDOW_RESIZABLE.rawValue //| SDL_WINDOW_ALLOW_HIGHDPI.rawValue
29+
let winFlags: SDL_WindowFlags = [.shown, .resizable] // .allowHighDPI
3030
let hWin = SDL_CreateWindow(windowTitle,
3131
Int32(SDL_WINDOWPOS_CENTERED_MASK),
3232
Int32(SDL_WINDOWPOS_CENTERED_MASK),
@@ -39,8 +39,10 @@ if hWin == nil {
3939
fatalError("could not crate window")
4040
}
4141

42+
var rng = SystemRandomNumberGenerator()
43+
4244
func randNorm() -> Double {
43-
return Double(arc4random()) / Double(UInt32.max)
45+
return Double.random(in: 0...1, using: &rng)
4446
}
4547

4648
class Name: Component {
@@ -139,7 +141,7 @@ class RenderSystem {
139141

140142
init(hWin: OpaquePointer?) {
141143

142-
let flags: UInt32 = SDL_RENDERER_ACCELERATED.rawValue // | SDL_RENDERER_PRESENTVSYNC.rawValue
144+
let flags: SDL_RendererFlags = [.accelerated] // .presentVSync
143145
hRenderer = SDL_CreateRenderer(hWin, -1, flags)
144146
if hRenderer == nil {
145147
SDL_DestroyWindow(hWin)
@@ -209,17 +211,17 @@ SDL_SetWindowPosition(hWin, Int32(SDL_WINDOWPOS_CENTERED_MASK), Int32(SDL_WINDOW
209211
while quit == false {
210212
tFrame.start()
211213
while SDL_PollEvent(&event) == 1 {
212-
switch SDL_EventType(rawValue: event.type) {
214+
switch event.eventType {
213215
case SDL_QUIT:
214216
quit = true
215217
break
216218
case SDL_WINDOWEVENT:
217-
if event.window.event == SDL_WINDOWEVENT_SIZE_CHANGED.rawValue {
219+
if event.windowEvent == SDL_WINDOWEVENT_SIZE_CHANGED {
218220
width = Int32(event.window.data1)
219221
height = Int32(event.window.data2)
220222
}
221223
case SDL_KEYDOWN:
222-
switch SDL_KeyCode(UInt32(event.key.keysym.sym)) {
224+
switch event.keyCode {
223225
case SDLK_ESCAPE:
224226
quit = true
225227
break

0 commit comments

Comments
 (0)