From a26d80ea5b723ded8197e4cb31dec20704659ea9 Mon Sep 17 00:00:00 2001 From: a dinosaur Date: Mon, 5 Aug 2024 20:56:06 +1000 Subject: [PATCH] use stderr wrapper for error prints --- Sources/Voxelotl/Application.swift | 24 +++++++++++++++--------- Sources/Voxelotl/Renderer.swift | 6 ++++-- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/Sources/Voxelotl/Application.swift b/Sources/Voxelotl/Application.swift index 5fd7341..641636c 100644 --- a/Sources/Voxelotl/Application.swift +++ b/Sources/Voxelotl/Application.swift @@ -11,7 +11,6 @@ public class Application { private var lastCounter: UInt64 = 0 private var fpsCalculator = FPSCalculator() - private var stderr = FileHandle.standardError public init(configuration: ApplicationConfiguration) { self.cfg = configuration @@ -19,7 +18,7 @@ public class Application { private func initialize() -> ApplicationExecutionState { 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 } @@ -33,7 +32,7 @@ public class Application { } window = SDL_CreateWindow(cfg.title, cfg.width, cfg.height, windowFlags) guard window != nil else { - print("SDL_CreateWindow() error: \(String(cString: SDL_GetError()))", to: &stderr) + printErr("SDL_CreateWindow() error: \(String(cString: SDL_GetError()))") return .exitFailure } @@ -44,16 +43,16 @@ public class Application { layer.displaySyncEnabled = cfg.vsyncMode == .off ? false : true self.renderer = try Renderer(layer: layer) } catch RendererError.initFailure(let message) { - print("Renderer init error: \(message)", to: &stderr) + printErr("Renderer init error: \(message)") return .exitFailure } catch { - print("Renderer init error: unexpected error", to: &stderr) + printErr("Renderer init error: unexpected error") } // Get window metrics var backBufferWidth: Int32 = 0, backBufferHeight: Int32 = 0 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 } renderer!.resize(size: SIMD2(Int(backBufferWidth), Int(backBufferHeight))) @@ -95,15 +94,17 @@ public class Application { private func update(_ deltaTime: Double) -> ApplicationExecutionState { fpsCalculator.frame(deltaTime: deltaTime) { fps in - print("FPS: \(fps)", to: &stderr) + print("FPS: \(fps)") } do { try renderer!.paint() } catch RendererError.drawFailure(let message) { - print("Renderer draw error: \(message)", to: &stderr) + printErr("Renderer draw error: \(message)") + return .exitFailure } catch { - print("Renderer draw error: unexpected error", to: &stderr) + printErr("Renderer draw error: unexpected error") + return .exitFailure } return .running @@ -174,6 +175,11 @@ fileprivate enum ApplicationExecutionState { 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 { public func write(_ string: String) { self.write(Data(string.utf8)) diff --git a/Sources/Voxelotl/Renderer.swift b/Sources/Voxelotl/Renderer.swift index 342e491..45f9422 100644 --- a/Sources/Voxelotl/Renderer.swift +++ b/Sources/Voxelotl/Renderer.swift @@ -121,6 +121,7 @@ class Renderer { } self.idxBuffer = idxBuffer + // Create a default texture do { self.defaultTexture = try Self.loadTexture(device, image2D: Image2D(Data([ 0xFF, 0x00, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0xFF, @@ -130,12 +131,13 @@ class Renderer { throw RendererError.initFailure("Failed to create default texture") } + // Load texture from a file in the bundle do { self.cubeTexture = try Self.loadTexture(device, resourcePath: "test.png") } catch RendererError.loadFailure(let message) { - print("Failed to load texture image: \(message)") + printErr("Failed to load texture image: \(message)") } catch { - print("Failed to load texture image: unknown error") + printErr("Failed to load texture image: unknown error") } }