use stderr wrapper for error prints

This commit is contained in:
a dinosaur 2024-08-05 20:56:06 +10:00
parent c0393e2687
commit a26d80ea5b
2 changed files with 19 additions and 11 deletions

View File

@ -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>(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))

View File

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