Files
Picross/Sources/SDL/Engine.swift
T
2024-10-24 18:29:07 -04:00

64 lines
2.5 KiB
Swift

import CSDL3
import CSDL3_Image
public struct Engine {
/// Initalizes SDL. This must be called before any other SDL function.
///
/// Errors encountered in this function are considered unrecoverable, and will crash the entire program.
///
/// - Parameters:
/// - subsystems: The SDL subsystem(s) to start. Default value only initalizes the video subsystem.
public static func start(subsystems: InitSubSystems = .video, imageFormats: InitImageFormats = .png) {
guard SDL_InitSubSystem(subsystems.rawValue) else {
fatalError("SDL failed to start: \(SDLError.current()?.errorMessage ?? "Failed to obtain error message!")")
}
guard IMG_Init(imageFormats.rawValue) == imageFormats.rawValue else {
fatalError("SDL_image failed to start: \(SDLError.current()?.errorMessage ?? "Failed to obtain error message!")")
}
}
public static func delay(_ ms: UInt32) {
SDL_Delay(ms)
}
public static func quit() {
SDL_Quit()
}
public struct InitSubSystems: OptionSet, Sendable {
public let rawValue: UInt32
public init(rawValue: UInt32) {
self.rawValue = rawValue
}
public static let audio = InitSubSystems(rawValue: SDL_INIT_AUDIO)
public static let video = InitSubSystems(rawValue: SDL_INIT_VIDEO)
public static let joystick = InitSubSystems(rawValue: SDL_INIT_JOYSTICK)
public static let haptic = InitSubSystems(rawValue: SDL_INIT_HAPTIC)
public static let gameController = InitSubSystems(rawValue: SDL_INIT_GAMEPAD)
public static let events = InitSubSystems(rawValue: SDL_INIT_EVENTS)
public static let sensor = InitSubSystems(rawValue: SDL_INIT_SENSOR)
public static let camera = InitSubSystems(rawValue: SDL_INIT_CAMERA)
}
public struct InitImageFormats: OptionSet, Sendable {
public let rawValue: UInt32
public init(rawValue: UInt32) {
self.rawValue = rawValue
}
// TODO - fix this!
public static let jpg = InitImageFormats(rawValue: 0x00000001)
public static let png = InitImageFormats(rawValue: 0x00000002)
public static let tif = InitImageFormats(rawValue: 0x00000004)
public static let webp = InitImageFormats(rawValue: 0x00000008)
public static let jxl = InitImageFormats(rawValue: 0x000000010)
public static let avif = InitImageFormats(rawValue: 0x00000020)
}
}