Update docs

This commit is contained in:
candy committed 2024-10-24 14:38:04 -04:00
1 parent a18f0c7eff
commit 25415ab357
9 files changed
+161 -38

No files matched your search

+24
View File
@@ -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
}
+3 -2
View File
@@ -5,9 +5,10 @@ import PackageDescription
let package = Package( let package = Package(
name: "Picross", name: "Picross",
dependencies: [
.package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"),
],
targets: [ 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( .executableTarget(
name: "Picross", name: "Picross",
dependencies: [ dependencies: [
+3 -2
View File
@@ -16,10 +16,11 @@ let puzzle = [
var board = [[Int]](repeating: [Int](repeating: 0, count: 10), count: 10) 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 renderer = try! window.renderer
let reimuTex = try! Texture(from: "texture.png", renderer: renderer) let reimuTex = try! Texture(from: "texture.png", renderer: renderer)
let tileTex = try! Texture(from: "Assets/Tile.png", renderer: renderer) let tileTex = try! Texture(from: "Assets/Tile.png", renderer: renderer)
let crossTex = try! Texture(from: "Assets/Tile-Cross.png", renderer: renderer) let crossTex = try! Texture(from: "Assets/Tile-Cross.png", renderer: renderer)
+3 -3
View File
@@ -1,7 +1,7 @@
public struct Color { public struct Color {
public let r: UInt8 public var r: UInt8
public let g: UInt8 public var g: UInt8
public let b: UInt8 public var b: UInt8
public init(r: UInt8, g: UInt8, b: UInt8) { public init(r: UInt8, g: UInt8, b: UInt8) {
self.r = r self.r = r
+41 -12
View File
@@ -2,10 +2,17 @@ import CSDL2
import CSDL2_Image import CSDL2_Image
public struct Engine { 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) { public static func delay(_ ms: UInt32) {
@@ -16,14 +23,36 @@ public struct Engine {
SDL_Quit() SDL_Quit()
} }
/// Specific SDL subsystems. ///
public enum SubSystem: UInt32 { public struct InitSubSystems: OptionSet, Sendable {
case timer = 0x00000001 public let rawValue: UInt32
case audio = 0x00000010
case video = 0x00000020 public init(rawValue: UInt32) {
case joystick = 0x00000200 self.rawValue = rawValue
case haptic = 0x00001000 }
case gameController = 0x00002000
case events = 0x00004000 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)
} }
} }
+15 -5
View File
@@ -1,7 +1,20 @@
import CSDL2 import CSDL2
public class Renderer { 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 { public init(_ window: Window) throws {
handle = try SDL_CreateRenderer(window.handle, -1, SDL_RENDERER_ACCELERATED.rawValue).sdlThrow(type: type(of: self)) 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) 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 { public func clear() throws {
try SDL_RenderClear(handle).sdlThrow(type: type(of: self)) try SDL_RenderClear(handle).sdlThrow(type: type(of: self))
} }
+2 -2
View File
@@ -73,7 +73,7 @@ internal extension Int32 {
func sdlThrow(file: String = #file, func sdlThrow(file: String = #file,
type: Any, type: Any,
function: String = #function, function: String = #function,
line: UInt = #line) throws { line: UInt = #line) throws(SDLError) {
guard self >= 0 else { guard self >= 0 else {
let debugInformation = SDLError.DebugInformation(file: file, type: type, function: function, line: line) let debugInformation = SDLError.DebugInformation(file: file, type: type, function: function, line: line)
@@ -90,7 +90,7 @@ internal extension Optional {
func sdlThrow(file: String = #file, func sdlThrow(file: String = #file,
type: Any, type: Any,
function: String = #function, function: String = #function,
line: UInt = #line) throws -> Wrapped { line: UInt = #line) throws(SDLError) -> Wrapped {
guard let value = self else { guard let value = self else {
let debugInformation = SDLError.DebugInformation(file: file, type: type, function: function, line: line) let debugInformation = SDLError.DebugInformation(file: file, type: type, function: function, line: line)
+69 -11
View File
@@ -2,10 +2,48 @@ import Foundation
import CSDL2 import CSDL2
import CSDL2_Image 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( handle = try SDL_CreateTexture(
renderer.handle, renderer.handle,
SDL_PIXELFORMAT_RGB24.rawValue, SDL_PIXELFORMAT_RGB24.rawValue,
@@ -13,20 +51,40 @@ public class Texture {
Int32(width), Int32(height) Int32(width), Int32(height)
).sdlThrow(type: type(of: self)) ).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)) 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)) handle = try SDL_CreateTextureFromSurface(renderer.handle, surface.handle).sdlThrow(type: type(of: self))
} }
/// Calls SDL_DestroyTexture on the texture. Should be safe.
deinit { deinit {
SDL_DestroyTexture(handle) SDL_DestroyTexture(handle)
} }
public func setColorMod(color: Color) throws { // MARK - Methods
try SDL_SetTextureColorMod(handle, color.r, color.g, color.b).sdlThrow(type: type(of: self))
}
} }
+1 -1
View File
@@ -1,7 +1,7 @@
import CSDL2 import CSDL2
public class Window { public class Window {
var handle: OpaquePointer let handle: OpaquePointer
public init(_ title: String, width: Int, height: Int, shown: Bool) throws { public init(_ title: String, width: Int, height: Int, shown: Bool) throws {
let uint32_shown = shown ? SDL_WINDOW_SHOWN.rawValue : SDL_WINDOW_HIDDEN.rawValue let uint32_shown = shown ? SDL_WINDOW_SHOWN.rawValue : SDL_WINDOW_HIDDEN.rawValue