From 523a18776384fd5183628c26a38c355339c30ef2 Mon Sep 17 00:00:00 2001 From: Andrew Glaze Date: Thu, 24 Oct 2024 19:28:23 -0400 Subject: [PATCH] Engine.swift: add documentation --- Sources/Picross/main.swift | 6 ++-- Sources/SDL/Engine.swift | 59 +++++++++++++++++++++++++++----------- 2 files changed, 47 insertions(+), 18 deletions(-) diff --git a/Sources/Picross/main.swift b/Sources/Picross/main.swift index a6d8750..380288e 100644 --- a/Sources/Picross/main.swift +++ b/Sources/Picross/main.swift @@ -16,9 +16,11 @@ let puzzle = [ var board = [[Int]](repeating: [Int](repeating: 0, count: 10), count: 10) -Engine.start(subsystems: [.video, .audio]) +Engine.start() -let window = try! Window("Picross", width: 600, height: 600, shown: true) +defer { Engine.quit() } + +let window = try! Window("Picross", width: 800, height: 600, shown: true) let renderer = try! window.renderer let reimuTex = try! Texture(from: "texture.png", renderer: renderer) diff --git a/Sources/SDL/Engine.swift b/Sources/SDL/Engine.swift index 1920ef7..7cef66b 100644 --- a/Sources/SDL/Engine.swift +++ b/Sources/SDL/Engine.swift @@ -5,12 +5,16 @@ 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. + /// > 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. Default value only initalizes the video subsystem. - public static func start(subsystems: InitSubSystems = .video, imageFormats: InitImageFormats = .png) { - guard SDL_InitSubSystem(subsystems.rawValue) else { + /// - 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!")") } @@ -18,32 +22,50 @@ public struct Engine { 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) - public static let gameController = InitSubSystems(rawValue: SDL_INIT_GAMEPAD) + /// 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 @@ -51,13 +73,18 @@ public struct Engine { 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) + /// 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)) } }