91 lines
4.0 KiB
Swift
91 lines
4.0 KiB
Swift
import CSDL3
|
|
import CSDL3_Image
|
|
|
|
public struct Engine {
|
|
|
|
/// Initalizes SDL. This must be called before any other SDL function.
|
|
///
|
|
/// > Warning: Errors encountered in this function are considered unrecoverable, and will crash the entire program.
|
|
///
|
|
/// The file I/O and threading subsystems are initalized by default.
|
|
/// You must specifically initialize other subsystems if you use them in your application.
|
|
///
|
|
/// - Parameters:
|
|
/// - subsystems: The SDL subsystem(s) to start.
|
|
/// - imageFormats: The SDL_image image formats to use.
|
|
public static func start(subsystems: InitSubSystems = [], imageFormats: InitImageFormats = []) {
|
|
guard SDL_Init(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!")")
|
|
}
|
|
}
|
|
|
|
/// Wait a number of milliseconds before continuing execution.
|
|
///
|
|
/// - Parameters:
|
|
/// - ms: The number of milliseconds to wait.
|
|
public static func delay(_ ms: UInt32) {
|
|
SDL_Delay(ms)
|
|
}
|
|
|
|
/// Clean up all initalized subsystems.
|
|
///
|
|
/// It is safe to call this method even if ``Engine/start(subsystems:imageFormats:)`` fails,
|
|
/// so you can use it in a `defer` to ensure that it is run when your app is shutdown.
|
|
public static func quit() {
|
|
SDL_Quit()
|
|
}
|
|
|
|
/// SDL Subsystem Initalizer values.
|
|
public struct InitSubSystems: OptionSet, Sendable {
|
|
public let rawValue: UInt32
|
|
|
|
public init(rawValue: UInt32) {
|
|
self.rawValue = rawValue
|
|
}
|
|
|
|
/// Audio Subsystem. Automatically initalizes the ``events`` subsystem.
|
|
public static let audio = InitSubSystems(rawValue: SDL_INIT_AUDIO)
|
|
/// Video subsystem. Automatically initalizes the ``events`` subsystem.
|
|
public static let video = InitSubSystems(rawValue: SDL_INIT_VIDEO)
|
|
/// Joystick subsystem. Automatically initalizes the ``events`` subsystem.
|
|
public static let joystick = InitSubSystems(rawValue: SDL_INIT_JOYSTICK)
|
|
/// Haptic (force feedback) subsystem.
|
|
public static let haptic = InitSubSystems(rawValue: SDL_INIT_HAPTIC)
|
|
/// Gamepad subsystem. Automatically initalizes the ``joystick`` subsystem.
|
|
public static let gamepad = InitSubSystems(rawValue: SDL_INIT_GAMEPAD)
|
|
/// Events subsystem.
|
|
public static let events = InitSubSystems(rawValue: SDL_INIT_EVENTS)
|
|
/// Sensor subsystem. Automatically initalizes the ``events`` subsystem.
|
|
public static let sensor = InitSubSystems(rawValue: SDL_INIT_SENSOR)
|
|
/// Camera subsystem. Automatically initializes the ``events`` subsystem.
|
|
public static let camera = InitSubSystems(rawValue: SDL_INIT_CAMERA)
|
|
}
|
|
|
|
/// Image format flags that can be passed SDL_Image enable their support.
|
|
public struct InitImageFormats: OptionSet, Sendable {
|
|
public let rawValue: UInt32
|
|
|
|
public init(rawValue: UInt32) {
|
|
self.rawValue = rawValue
|
|
}
|
|
|
|
/// Jpeg image format init flag.
|
|
public static let jpg = InitImageFormats(rawValue: UInt32(IMG_INIT_JPG))
|
|
/// Png image format init flag.
|
|
public static let png = InitImageFormats(rawValue: UInt32(IMG_INIT_PNG))
|
|
/// Tif image format init flag.
|
|
public static let tif = InitImageFormats(rawValue: UInt32(IMG_INIT_TIF))
|
|
/// Webp image format init flag.
|
|
public static let webp = InitImageFormats(rawValue: UInt32(IMG_INIT_WEBP))
|
|
/// Jxl image format init flag.
|
|
public static let jxl = InitImageFormats(rawValue: UInt32(IMG_INIT_JXL))
|
|
/// Avif image format init flag.
|
|
public static let avif = InitImageFormats(rawValue: UInt32(IMG_INIT_AVIF))
|
|
|
|
}
|
|
}
|