basis for random subsystem

This commit is contained in:
a dinosaur 2024-08-22 03:09:53 +10:00
parent b7dba429cf
commit 394e340f09
5 changed files with 45 additions and 15 deletions

View File

@ -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/")

View File

@ -1,5 +1,4 @@
import simd
import Foundation
struct Instance {
let position: SIMD3<Float>
@ -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..<colors.count)])
} else {
.air
}
@ -86,7 +86,9 @@ class Game: GameDelegate {
color: Color<Float16>(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<Int>) {

View File

@ -1,8 +0,0 @@
//
// Random.swift
// voxelotl
//
// Created by a dinosaur on 20/8/2024.
//
import Foundation

View File

@ -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>) -> Int {
assert(bound.upperBound <= Self.max)
return bound.lowerBound + Int(arc4random_uniform(UInt32(bound.upperBound)))
}
}

View File

@ -0,0 +1,8 @@
public protocol RandomProvider {
associatedtype Output: FixedWidthInteger
static var min: Output { get }
static var max: Output { get }
mutating func next() -> Output
}