diff --git a/Package.resolved b/Package.resolved new file mode 100644 index 0000000..44be26b --- /dev/null +++ b/Package.resolved @@ -0,0 +1,24 @@ +{ + "originHash" : "16fa104638e1eb907b366c8c98dc75f2b4fdc8e33872c3e4f69b9023d802cbfb", + "pins" : [ + { + "identity" : "swift-docc-plugin", + "kind" : "remoteSourceControl", + "location" : "https://github.com/apple/swift-docc-plugin", + "state" : { + "revision" : "85e4bb4e1cd62cec64a4b8e769dcefdf0c5b9d64", + "version" : "1.4.3" + } + }, + { + "identity" : "swift-docc-symbolkit", + "kind" : "remoteSourceControl", + "location" : "https://github.com/swiftlang/swift-docc-symbolkit", + "state" : { + "revision" : "b45d1f2ed151d057b54504d653e0da5552844e34", + "version" : "1.0.0" + } + } + ], + "version" : 3 +} diff --git a/Package.swift b/Package.swift index 95e9095..ef8bbd1 100644 --- a/Package.swift +++ b/Package.swift @@ -5,9 +5,10 @@ import PackageDescription let package = Package( name: "Picross", + dependencies: [ + .package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"), + ], targets: [ - // Targets are the basic building blocks of a package, defining a module or a test suite. - // Targets can depend on other targets in this package and products from dependencies. .executableTarget( name: "Picross", dependencies: [ diff --git a/Sources/Picross/main.swift b/Sources/Picross/main.swift index 91cacc7..7c9d32c 100644 --- a/Sources/Picross/main.swift +++ b/Sources/Picross/main.swift @@ -16,10 +16,11 @@ let puzzle = [ var board = [[Int]](repeating: [Int](repeating: 0, count: 10), count: 10) -try! Engine.initalize(subsystem: .video) +Engine.start(subsystems: [.video, .audio]) -let window = try! Window("Picross", width: 800, height: 600, shown: true) +let window = try! Window("Picross", width: 600, height: 600, shown: true) let renderer = try! window.renderer + let reimuTex = try! Texture(from: "texture.png", renderer: renderer) let tileTex = try! Texture(from: "Assets/Tile.png", renderer: renderer) let crossTex = try! Texture(from: "Assets/Tile-Cross.png", renderer: renderer) diff --git a/Sources/SDL/Color.swift b/Sources/SDL/Color.swift index 98dda7e..a479cea 100644 --- a/Sources/SDL/Color.swift +++ b/Sources/SDL/Color.swift @@ -1,7 +1,7 @@ public struct Color { - public let r: UInt8 - public let g: UInt8 - public let b: UInt8 + public var r: UInt8 + public var g: UInt8 + public var b: UInt8 public init(r: UInt8, g: UInt8, b: UInt8) { self.r = r diff --git a/Sources/SDL/Engine.swift b/Sources/SDL/Engine.swift index 4b76599..725bbdf 100644 --- a/Sources/SDL/Engine.swift +++ b/Sources/SDL/Engine.swift @@ -2,10 +2,17 @@ import CSDL2 import CSDL2_Image public struct Engine { - public static func initalize(subsystem: SubSystem) throws { - try SDL_Init(subsystem.rawValue).sdlThrow(type: type(of: self)) - try IMG_Init(Int32(IMG_INIT_PNG.rawValue)).sdlThrow(type: type(of: self)) + /// Initalizes SDL. This must be called before any other SDL function, and can only be called once per program. + /// + /// 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) { + try! SDL_InitSubSystem(subsystems.rawValue).sdlThrow(type: type(of: self)) + + try! IMG_Init(Int32(IMG_INIT_PNG.rawValue)).sdlThrow(type: type(of: self)) } public static func delay(_ ms: UInt32) { @@ -16,14 +23,36 @@ public struct Engine { SDL_Quit() } - /// Specific SDL subsystems. - public enum SubSystem: UInt32 { - case timer = 0x00000001 - case audio = 0x00000010 - case video = 0x00000020 - case joystick = 0x00000200 - case haptic = 0x00001000 - case gameController = 0x00002000 - case events = 0x00004000 + /// + public struct InitSubSystems: OptionSet, Sendable { + public let rawValue: UInt32 + + public init(rawValue: UInt32) { + self.rawValue = rawValue + } + + public static let timer = InitSubSystems(rawValue: SDL_INIT_TIMER) + 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_GAMECONTROLLER) + public static let events = InitSubSystems(rawValue: SDL_INIT_EVENTS) + } + + public struct InitImageFormats: OptionSet, Sendable { + public let rawValue: Int32 + + public init(rawValue: Int32) { + self.rawValue = rawValue + } + + 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) + } } diff --git a/Sources/SDL/Renderer.swift b/Sources/SDL/Renderer.swift index dc68d48..c8f8f28 100644 --- a/Sources/SDL/Renderer.swift +++ b/Sources/SDL/Renderer.swift @@ -1,7 +1,20 @@ import CSDL2 public class Renderer { - public var handle: OpaquePointer + var handle: OpaquePointer + + var scale: (x: Float, y: Float) { + get { + var x: Float = 0.0 + var y: Float = 0.0 + SDL_RenderGetScale(handle, &x, &y) + return (x, y) + } + } + + public func setScale(newScale: (x: Float, y: Float)) throws { + try SDL_RenderSetScale(handle, newScale.x, newScale.y).sdlThrow(type: type(of: self)) + } public init(_ window: Window) throws { handle = try SDL_CreateRenderer(window.handle, -1, SDL_RENDERER_ACCELERATED.rawValue).sdlThrow(type: type(of: self)) @@ -11,10 +24,7 @@ public class Renderer { SDL_DestroyRenderer(handle) } - public func setScale(x: Float, y: Float) throws { - try SDL_RenderSetScale(handle, x, y).sdlThrow(type: type(of: self)) - } - + public func clear() throws { try SDL_RenderClear(handle).sdlThrow(type: type(of: self)) } diff --git a/Sources/SDL/SDLError.swift b/Sources/SDL/SDLError.swift index 7cf797c..611ba15 100644 --- a/Sources/SDL/SDLError.swift +++ b/Sources/SDL/SDLError.swift @@ -73,7 +73,7 @@ internal extension Int32 { func sdlThrow(file: String = #file, type: Any, function: String = #function, - line: UInt = #line) throws { + line: UInt = #line) throws(SDLError) { guard self >= 0 else { let debugInformation = SDLError.DebugInformation(file: file, type: type, function: function, line: line) @@ -90,7 +90,7 @@ internal extension Optional { func sdlThrow(file: String = #file, type: Any, function: String = #function, - line: UInt = #line) throws -> Wrapped { + line: UInt = #line) throws(SDLError) -> Wrapped { guard let value = self else { let debugInformation = SDLError.DebugInformation(file: file, type: type, function: function, line: line) diff --git a/Sources/SDL/Texture.swift b/Sources/SDL/Texture.swift index c3cbda7..6d0cf7b 100644 --- a/Sources/SDL/Texture.swift +++ b/Sources/SDL/Texture.swift @@ -2,10 +2,48 @@ import Foundation import CSDL2 import CSDL2_Image -public class Texture { - public var handle: OpaquePointer - public init(_ renderer: Renderer, width: Int, height: Int) throws { +/// An efficent, driver-specific representation of pixel data. +/// +/// +public class Texture { + // MARK - Public properties + + /// An additional color value multiplied into render copy operations. + public var colorMod: Color { + get { + var color = Color(r: 0, g: 0, b: 0) + SDL_GetTextureColorMod(handle, &color.r, &color.g, &color.b) + return color + } + } + + /// Sets an additional color value multiplied into render copy operations. + /// + /// - Parameters: + /// - color: The color value to be multiplied into copy operations. + /// - Throws: ``SDLError`` on failure. + public func setColorMod(color: Color) throws(SDLError) { + try SDL_SetTextureColorMod(handle, color.r, color.g, color.b).sdlThrow(type: type(of: self)) + } + + // MARK - Private properties + + /// The pointer refrence to the underlying `SDL_Texture` + var handle: OpaquePointer + + // MARK - Initalizers + + /// Create an empty texture for a rendering context. + /// + /// Chances are you won't use this directly. + /// + /// - Parameters: + /// - renderer: The rendering context. + /// - width: The width of the texture in pixels. + /// - height: The height of the texture in pixels. + /// - Throws: ``SDLError`` if no rendering context was active, the format was unsupported, or the width or height were out of range. + public init(_ renderer: Renderer, width: Int, height: Int) throws(SDLError) { handle = try SDL_CreateTexture( renderer.handle, SDL_PIXELFORMAT_RGB24.rawValue, @@ -13,20 +51,40 @@ public class Texture { Int32(width), Int32(height) ).sdlThrow(type: type(of: self)) } - - public init(from imagePath: String, renderer: Renderer) throws { + + /// Creates a ``Texture`` from an image file. + /// + /// This can be signfintally more efficient than using a CPU-bound ``Surface``, + /// if you dont need to manipulate the image directly after loading it. + /// + /// If the loaded image has transparency or a colorkey, a texture with an alpha channel will be created. + /// Otherwise, a ``Texture`` will be created in the most reasonable format that represents the image data. + /// In most cases, this will end up being 32-bit RGB or 32-bit RGBA. + /// + /// - Parameters: + /// - imagePath: The filesystem path to the texture file. + /// - renderer: The `Renderer` to attach the created `Texture` to. + /// - Throws: ``SDLError`` on failure. + public init(from imagePath: String, renderer: Renderer) throws(SDLError) { handle = try IMG_LoadTexture(renderer.handle, imagePath).sdlThrow(type: type(of: self)) } - - public init(from surface: Surface, renderer: Renderer) throws { + + /// Creates a ``Texture`` from an existing surface. + /// + /// The surface is not modified or freed by this function. The pixel format of the created ``Texture`` may be + /// different from the pixel format of the surface. + /// - Parameters: + /// - surface: The ``Surface`` structure containing pixel data used to fill the ``Texture``. + /// - renderer: The ``Renderer`` to attach the newly created ``Texture`` to. + /// - Throws: ``SDLError`` on failure. + public init(from surface: Surface, renderer: Renderer) throws(SDLError) { handle = try SDL_CreateTextureFromSurface(renderer.handle, surface.handle).sdlThrow(type: type(of: self)) } - + + /// Calls SDL_DestroyTexture on the texture. Should be safe. deinit { SDL_DestroyTexture(handle) } - public func setColorMod(color: Color) throws { - try SDL_SetTextureColorMod(handle, color.r, color.g, color.b).sdlThrow(type: type(of: self)) - } + // MARK - Methods } diff --git a/Sources/SDL/Window.swift b/Sources/SDL/Window.swift index 9f70ac0..d06a14a 100644 --- a/Sources/SDL/Window.swift +++ b/Sources/SDL/Window.swift @@ -1,7 +1,7 @@ import CSDL2 public class Window { - var handle: OpaquePointer + let handle: OpaquePointer public init(_ title: String, width: Int, height: Int, shown: Bool) throws { let uint32_shown = shown ? SDL_WINDOW_SHOWN.rawValue : SDL_WINDOW_HIDDEN.rawValue