From 394e340f09df2d419d82e43f039c0f3f79385385 Mon Sep 17 00:00:00 2001 From: a dinosaur Date: Thu, 22 Aug 2024 03:09:53 +1000 Subject: [PATCH] basis for random subsystem --- Sources/Voxelotl/CMakeLists.txt | 10 +++++--- Sources/Voxelotl/Game.swift | 10 ++++---- Sources/Voxelotl/Random.swift | 8 ------- Sources/Voxelotl/Random/Arc4Random.swift | 24 ++++++++++++++++++++ Sources/Voxelotl/Random/RandomProvider.swift | 8 +++++++ 5 files changed, 45 insertions(+), 15 deletions(-) delete mode 100644 Sources/Voxelotl/Random.swift create mode 100644 Sources/Voxelotl/Random/Arc4Random.swift create mode 100644 Sources/Voxelotl/Random/RandomProvider.swift diff --git a/Sources/Voxelotl/CMakeLists.txt b/Sources/Voxelotl/CMakeLists.txt index 4a99689..3a129e7 100644 --- a/Sources/Voxelotl/CMakeLists.txt +++ b/Sources/Voxelotl/CMakeLists.txt @@ -3,8 +3,6 @@ add_executable(Voxelotl MACOSX_BUNDLE test.png - Chunk.swift - shadertypes.h shader.metal @@ -14,12 +12,17 @@ add_executable(Voxelotl MACOSX_BUNDLE AABB.swift Color.swift + Random/RandomProvider.swift + Random/Arc4Random.swift + NSImageLoader.swift Renderer.swift GameController.swift + FPSCalculator.swift + + Chunk.swift Camera.swift Player.swift - FPSCalculator.swift GameDelegate.swift Application.swift @@ -61,3 +64,4 @@ set_source_files_properties(test.png PROPERTIES MACOSX_PACKAGE_LOCATION Resource source_group("Resources" FILES Assets.xcassets test.png) source_group("Source Files" REGULAR_EXPRESSION "\\.(swift|metal)$") +source_group("Source Files\\Random" REGULAR_EXPRESSION "Random/") diff --git a/Sources/Voxelotl/Game.swift b/Sources/Voxelotl/Game.swift index 0614193..174c132 100644 --- a/Sources/Voxelotl/Game.swift +++ b/Sources/Voxelotl/Game.swift @@ -1,5 +1,4 @@ import simd -import Foundation struct Instance { let position: SIMD3 @@ -26,6 +25,7 @@ class Game: GameDelegate { var player = Player() var projection: matrix_float4x4 = .identity var chunk = Chunk(position: .zero) + var random = Arc4Random.instance init() { player.position = SIMD3(0.5, Float(Chunk.chunkSize) + 0.5, 0.5) @@ -37,8 +37,8 @@ class Game: GameDelegate { .magenta, .yellow, .cyan ] chunk.fill(allBy: { - if (arc4random() & 0x1) == 0x1 { - .solid(colors[Int(arc4random_uniform(UInt32(colors.count)))]) + if (random.next() & 0x1) == 0x1 { + .solid(colors[random.next(in: 0..(color).linear) } else { nil } } - renderer.batch(instances: instances, camera: self.camera) + if !instances.isEmpty { + renderer.batch(instances: instances, camera: self.camera) + } } func resize(_ size: Size) { diff --git a/Sources/Voxelotl/Random.swift b/Sources/Voxelotl/Random.swift deleted file mode 100644 index 45ede1a..0000000 --- a/Sources/Voxelotl/Random.swift +++ /dev/null @@ -1,8 +0,0 @@ -// -// Random.swift -// voxelotl -// -// Created by a dinosaur on 20/8/2024. -// - -import Foundation diff --git a/Sources/Voxelotl/Random/Arc4Random.swift b/Sources/Voxelotl/Random/Arc4Random.swift new file mode 100644 index 0000000..d0a17bd --- /dev/null +++ b/Sources/Voxelotl/Random/Arc4Random.swift @@ -0,0 +1,24 @@ +import Foundation + +public class Arc4Random: RandomProvider { + public typealias Output = UInt32 + + public static var min: UInt32 { 0x00000000 } + public static var max: UInt32 { 0xFFFFFFFF } + + private init() {} + public static let instance = Arc4Random() + + public func stir() { + arc4random_stir() + } + + public func next() -> UInt32 { + arc4random() + } + + public func next(in bound: Range) -> Int { + assert(bound.upperBound <= Self.max) + return bound.lowerBound + Int(arc4random_uniform(UInt32(bound.upperBound))) + } +} diff --git a/Sources/Voxelotl/Random/RandomProvider.swift b/Sources/Voxelotl/Random/RandomProvider.swift new file mode 100644 index 0000000..110a301 --- /dev/null +++ b/Sources/Voxelotl/Random/RandomProvider.swift @@ -0,0 +1,8 @@ +public protocol RandomProvider { + associatedtype Output: FixedWidthInteger + + static var min: Output { get } + static var max: Output { get } + + mutating func next() -> Output +}