mirror of
				https://github.com/GayPizzaSpecifications/voxelotl-engine.git
				synced 2025-11-04 02:59:37 +00:00 
			
		
		
		
	basis for random subsystem
This commit is contained in:
		@ -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/")
 | 
			
		||||
 | 
			
		||||
@ -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,8 +86,10 @@ class Game: GameDelegate {
 | 
			
		||||
          color:    Color<Float16>(color).linear)
 | 
			
		||||
      } else { nil }
 | 
			
		||||
    }
 | 
			
		||||
    if !instances.isEmpty {
 | 
			
		||||
      renderer.batch(instances: instances, camera: self.camera)
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  func resize(_ size: Size<Int>) {
 | 
			
		||||
    self.camera.setSize(size)
 | 
			
		||||
 | 
			
		||||
@ -1,8 +0,0 @@
 | 
			
		||||
//
 | 
			
		||||
//  Random.swift
 | 
			
		||||
//  voxelotl
 | 
			
		||||
//
 | 
			
		||||
//  Created by a dinosaur on 20/8/2024.
 | 
			
		||||
//
 | 
			
		||||
 | 
			
		||||
import Foundation
 | 
			
		||||
							
								
								
									
										24
									
								
								Sources/Voxelotl/Random/Arc4Random.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								Sources/Voxelotl/Random/Arc4Random.swift
									
									
									
									
									
										Normal 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)))
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										8
									
								
								Sources/Voxelotl/Random/RandomProvider.swift
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										8
									
								
								Sources/Voxelotl/Random/RandomProvider.swift
									
									
									
									
									
										Normal 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
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user