mirror of
				https://github.com/GayPizzaSpecifications/voxelotl-engine.git
				synced 2025-11-04 02:59:37 +00:00 
			
		
		
		
	use stderr wrapper for error prints
This commit is contained in:
		@ -11,7 +11,6 @@ public class Application {
 | 
				
			|||||||
  private var lastCounter: UInt64 = 0
 | 
					  private var lastCounter: UInt64 = 0
 | 
				
			||||||
  private var fpsCalculator = FPSCalculator()
 | 
					  private var fpsCalculator = FPSCalculator()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
  private var stderr = FileHandle.standardError
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
  public init(configuration: ApplicationConfiguration) {
 | 
					  public init(configuration: ApplicationConfiguration) {
 | 
				
			||||||
    self.cfg = configuration
 | 
					    self.cfg = configuration
 | 
				
			||||||
@ -19,7 +18,7 @@ public class Application {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  private func initialize() -> ApplicationExecutionState {
 | 
					  private func initialize() -> ApplicationExecutionState {
 | 
				
			||||||
    guard SDL_Init(SDL_INIT_VIDEO) >= 0 else {
 | 
					    guard SDL_Init(SDL_INIT_VIDEO) >= 0 else {
 | 
				
			||||||
      print("SDL_Init() error: \(String(cString: SDL_GetError()))", to: &stderr)
 | 
					      printErr("SDL_Init() error: \(String(cString: SDL_GetError()))")
 | 
				
			||||||
      return .exitFailure
 | 
					      return .exitFailure
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -33,7 +32,7 @@ public class Application {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
    window = SDL_CreateWindow(cfg.title, cfg.width, cfg.height, windowFlags)
 | 
					    window = SDL_CreateWindow(cfg.title, cfg.width, cfg.height, windowFlags)
 | 
				
			||||||
    guard window != nil else {
 | 
					    guard window != nil else {
 | 
				
			||||||
      print("SDL_CreateWindow() error: \(String(cString: SDL_GetError()))", to: &stderr)
 | 
					      printErr("SDL_CreateWindow() error: \(String(cString: SDL_GetError()))")
 | 
				
			||||||
      return .exitFailure
 | 
					      return .exitFailure
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -44,16 +43,16 @@ public class Application {
 | 
				
			|||||||
      layer.displaySyncEnabled = cfg.vsyncMode == .off ? false : true
 | 
					      layer.displaySyncEnabled = cfg.vsyncMode == .off ? false : true
 | 
				
			||||||
      self.renderer = try Renderer(layer: layer)
 | 
					      self.renderer = try Renderer(layer: layer)
 | 
				
			||||||
    } catch RendererError.initFailure(let message) {
 | 
					    } catch RendererError.initFailure(let message) {
 | 
				
			||||||
      print("Renderer init error: \(message)", to: &stderr)
 | 
					      printErr("Renderer init error: \(message)")
 | 
				
			||||||
      return .exitFailure
 | 
					      return .exitFailure
 | 
				
			||||||
    } catch {
 | 
					    } catch {
 | 
				
			||||||
      print("Renderer init error: unexpected error", to: &stderr)
 | 
					      printErr("Renderer init error: unexpected error")
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // Get window metrics
 | 
					    // Get window metrics
 | 
				
			||||||
    var backBufferWidth: Int32 = 0, backBufferHeight: Int32 = 0
 | 
					    var backBufferWidth: Int32 = 0, backBufferHeight: Int32 = 0
 | 
				
			||||||
    guard SDL_GetWindowSizeInPixels(window, &backBufferWidth, &backBufferHeight) >= 0 else {
 | 
					    guard SDL_GetWindowSizeInPixels(window, &backBufferWidth, &backBufferHeight) >= 0 else {
 | 
				
			||||||
      print("SDL_GetWindowSizeInPixels() error: \(String(cString: SDL_GetError()))", to: &stderr)
 | 
					      printErr("SDL_GetWindowSizeInPixels() error: \(String(cString: SDL_GetError()))")
 | 
				
			||||||
      return .exitFailure
 | 
					      return .exitFailure
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
    renderer!.resize(size: SIMD2<Int>(Int(backBufferWidth), Int(backBufferHeight)))
 | 
					    renderer!.resize(size: SIMD2<Int>(Int(backBufferWidth), Int(backBufferHeight)))
 | 
				
			||||||
@ -95,15 +94,17 @@ public class Application {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  private func update(_ deltaTime: Double) -> ApplicationExecutionState {
 | 
					  private func update(_ deltaTime: Double) -> ApplicationExecutionState {
 | 
				
			||||||
    fpsCalculator.frame(deltaTime: deltaTime) { fps in
 | 
					    fpsCalculator.frame(deltaTime: deltaTime) { fps in
 | 
				
			||||||
      print("FPS: \(fps)", to: &stderr)
 | 
					      print("FPS: \(fps)")
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    do {
 | 
					    do {
 | 
				
			||||||
      try renderer!.paint()
 | 
					      try renderer!.paint()
 | 
				
			||||||
    } catch RendererError.drawFailure(let message) {
 | 
					    } catch RendererError.drawFailure(let message) {
 | 
				
			||||||
      print("Renderer draw error: \(message)", to: &stderr)
 | 
					      printErr("Renderer draw error: \(message)")
 | 
				
			||||||
 | 
					      return .exitFailure
 | 
				
			||||||
    } catch {
 | 
					    } catch {
 | 
				
			||||||
      print("Renderer draw error: unexpected error", to: &stderr)
 | 
					      printErr("Renderer draw error: unexpected error")
 | 
				
			||||||
 | 
					      return .exitFailure
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    return .running
 | 
					    return .running
 | 
				
			||||||
@ -174,6 +175,11 @@ fileprivate enum ApplicationExecutionState {
 | 
				
			|||||||
  case running
 | 
					  case running
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					func printErr(_ items: Any..., separator: String = " ", terminator: String = "\n") {
 | 
				
			||||||
 | 
					  var stderr = FileHandle.standardError
 | 
				
			||||||
 | 
					  print(items, separator: separator, terminator: terminator, to: &stderr)
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
extension FileHandle: TextOutputStream {
 | 
					extension FileHandle: TextOutputStream {
 | 
				
			||||||
  public func write(_ string: String) {
 | 
					  public func write(_ string: String) {
 | 
				
			||||||
    self.write(Data(string.utf8))
 | 
					    self.write(Data(string.utf8))
 | 
				
			||||||
 | 
				
			|||||||
@ -121,6 +121,7 @@ class Renderer {
 | 
				
			|||||||
    }
 | 
					    }
 | 
				
			||||||
    self.idxBuffer = idxBuffer
 | 
					    self.idxBuffer = idxBuffer
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Create a default texture
 | 
				
			||||||
    do {
 | 
					    do {
 | 
				
			||||||
      self.defaultTexture = try Self.loadTexture(device, image2D: Image2D(Data([
 | 
					      self.defaultTexture = try Self.loadTexture(device, image2D: Image2D(Data([
 | 
				
			||||||
          0xFF, 0x00, 0xFF, 0xFF,  0x00, 0x00, 0x00, 0xFF,
 | 
					          0xFF, 0x00, 0xFF, 0xFF,  0x00, 0x00, 0x00, 0xFF,
 | 
				
			||||||
@ -130,12 +131,13 @@ class Renderer {
 | 
				
			|||||||
      throw RendererError.initFailure("Failed to create default texture")
 | 
					      throw RendererError.initFailure("Failed to create default texture")
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // Load texture from a file in the bundle
 | 
				
			||||||
    do {
 | 
					    do {
 | 
				
			||||||
      self.cubeTexture = try Self.loadTexture(device, resourcePath: "test.png")
 | 
					      self.cubeTexture = try Self.loadTexture(device, resourcePath: "test.png")
 | 
				
			||||||
    } catch RendererError.loadFailure(let message) {
 | 
					    } catch RendererError.loadFailure(let message) {
 | 
				
			||||||
      print("Failed to load texture image: \(message)")
 | 
					      printErr("Failed to load texture image: \(message)")
 | 
				
			||||||
    } catch {
 | 
					    } catch {
 | 
				
			||||||
      print("Failed to load texture image: unknown error")
 | 
					      printErr("Failed to load texture image: unknown error")
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
  }
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user