Files
2024-11-19 00:10:45 -05:00

58 lines
1.5 KiB
Swift

import CSDL3
import Foundation
public class Renderer {
var handle: OpaquePointer
var scale: (x: Float, y: Float) {
get {
var x: Float = 0.0
var y: Float = 0.0
SDL_GetRenderScale(handle, &x, &y)
return (x, y)
}
}
public func setScale(newScale: (x: Float, y: Float)) throws {
try SDL_SetRenderScale(handle, newScale.x, newScale.y).sdlThrow(type: type(of: self))
}
public init(_ window: Window, name: String? = nil) throws {
handle = try SDL_CreateRenderer(window.handle, name).sdlThrow(type: type(of: self))
}
deinit {
SDL_DestroyRenderer(handle)
}
public func clear() throws {
try SDL_RenderClear(handle).sdlThrow(type: type(of: self))
}
public func renderTexture(
_ texture: Texture,
srcRect: Rect? = nil,
dstRect: Rect? = nil
) throws {
var srcRectPtr: UnsafePointer<SDL_FRect>? = nil
var dstRectPtr: UnsafePointer<SDL_FRect>? = nil
if let srcRect = srcRect {
srcRectPtr = withUnsafePointer(to: srcRect.handle) { UnsafePointer<SDL_FRect>($0) }
}
if let dstRect = dstRect {
dstRectPtr = withUnsafePointer(to: dstRect.handle) { UnsafePointer<SDL_FRect>($0) }
}
try SDL_RenderTexture(
handle,
texture.handle,
srcRectPtr,
dstRectPtr
).sdlThrow(type: type(of: self))
}
public func present() {
SDL_RenderPresent(handle)
}
}