diff --git a/Sources/Picross/GameNode.swift b/Sources/Picross/GameNode.swift new file mode 100644 index 0000000..8e16670 --- /dev/null +++ b/Sources/Picross/GameNode.swift @@ -0,0 +1,9 @@ +import SDL + +class GameNode { + var position: Rect + + init() { + position = Rect() + } +} diff --git a/Sources/Picross/main.swift b/Sources/Picross/main.swift index 43f5472..8af0c4a 100644 --- a/Sources/Picross/main.swift +++ b/Sources/Picross/main.swift @@ -1,5 +1,4 @@ import SDL -import CSDL3 let puzzle = [ [0,0,0,0,0,1,0,0,0,0], @@ -63,7 +62,7 @@ while !quit { try renderer.renderTexture(reimuTex) for i in 0..<10 { for j in 0..<10 { - var rect = SDL_FRect(x: 32.0 * Float(i), y: 32.0 * Float(j), w: 32.0, h: 32.0) + let rect = Rect(x: 32.0 * Float(i), y: 32.0 * Float(j), w: 32.0, h: 32.0) let tex = switch board[j][i] { case 0: tileTex case 1: filledTex @@ -71,10 +70,10 @@ while !quit { case 3: failTex default: tileTex } - try renderer.renderTexture(tex, dstRect: &rect) + try renderer.renderTexture(tex, dstRect: rect) } } - + renderer.present() Engine.delay(100) diff --git a/Sources/SDL/Rect.swift b/Sources/SDL/Rect.swift new file mode 100644 index 0000000..69c9c57 --- /dev/null +++ b/Sources/SDL/Rect.swift @@ -0,0 +1,9 @@ +import CSDL3 + +public struct Rect: Sendable { + var handle: SDL_FRect + + public init(x: Float = 0, y: Float = 0, w: Float = 0, h: Float = 0) { + handle = SDL_FRect(x: x, y: y, w: w, h: h) + } +} diff --git a/Sources/SDL/Renderer.swift b/Sources/SDL/Renderer.swift index 4dabb86..05569be 100644 --- a/Sources/SDL/Renderer.swift +++ b/Sources/SDL/Renderer.swift @@ -1,4 +1,5 @@ import CSDL3 +import Foundation public class Renderer { var handle: OpaquePointer @@ -11,7 +12,7 @@ public class Renderer { return (x, y) } } - + public func setScale(newScale: (x: Float, y: Float)) throws { try SDL_SetRenderScale(handle, newScale.x, newScale.y).sdlThrow(type: type(of: self)) } @@ -24,13 +25,30 @@ public class Renderer { SDL_DestroyRenderer(handle) } - public func clear() throws { try SDL_RenderClear(handle).sdlThrow(type: type(of: self)) } - public func renderTexture(_ texture: Texture, srcRect: UnsafePointer! = nil, dstRect: UnsafePointer! = nil) throws { - try SDL_RenderTexture(handle, texture.handle, srcRect, dstRect).sdlThrow(type: type(of: self)) + public func renderTexture( + _ texture: Texture, + srcRect: Rect? = nil, + dstRect: Rect? = nil + ) throws { + var srcRectPtr: UnsafePointer? = nil + var dstRectPtr: UnsafePointer? = nil + if let srcRect = srcRect { + srcRectPtr = withUnsafePointer(to: srcRect.handle) { UnsafePointer($0) } + } + if let dstRect = dstRect { + dstRectPtr = withUnsafePointer(to: dstRect.handle) { UnsafePointer($0) } + } + + try SDL_RenderTexture( + handle, + texture.handle, + srcRectPtr, + dstRectPtr + ).sdlThrow(type: type(of: self)) } public func present() { diff --git a/Sources/SDL/SDLError.swift b/Sources/SDL/SDLError.swift index 991be8a..25de7a1 100644 --- a/Sources/SDL/SDLError.swift +++ b/Sources/SDL/SDLError.swift @@ -1,23 +1,23 @@ import CSDL3 public struct SDLError: Error { - + public let errorMessage: String - + internal let debugInformation: DebugInformation? } extension SDLError: CustomStringConvertible { - + public var description: String { - + return errorMessage } } extension SDLError: CustomDebugStringConvertible { public var debugDescription: String { - + var description = errorMessage if let debugInformation = debugInformation { description += " " + "(\(debugInformation.description))" @@ -27,28 +27,28 @@ extension SDLError: CustomDebugStringConvertible { } internal extension SDLError { - + final class DebugInformation: CustomStringConvertible, Sendable { - + public let file: String - + public let type: String - + public let function: String - + public let line: UInt - + internal init(file: String, type: Any, function: String, line: UInt) { - + self.file = file self.function = function self.line = line self.type = String(reflecting: type) } - + public var description: String { let fileName = file.split(separator: "/").last.flatMap { String($0) } ?? file return "\(fileName):\(line.description) \(type).\(function)" @@ -58,14 +58,14 @@ internal extension SDLError { internal extension SDLError { static func current(debugInformation: DebugInformation? = nil) -> SDLError? { - + guard let cString = SDL_GetError() else { return nil } - + SDL_ClearError() let errorDescription = String(cString: cString) print(errorDescription) - + return SDLError(errorMessage: errorDescription, debugInformation: debugInformation) } } @@ -75,7 +75,7 @@ internal extension Int32 { type: Any, function: String = #function, line: UInt = #line) throws(SDLError) { - + guard self >= 0 else { let debugInformation = SDLError.DebugInformation(file: file, type: type, function: function, line: line) guard let error = SDLError.current(debugInformation: debugInformation) else { @@ -92,7 +92,7 @@ internal extension Bool { type: Any, function: String = #function, line: UInt = #line) throws(SDLError) { - + guard self else { let debugInformation = SDLError.DebugInformation(file: file, type: type, function: function, line: line) guard let error = SDLError.current(debugInformation: debugInformation) else { @@ -110,7 +110,7 @@ internal extension Optional { type: Any, function: String = #function, line: UInt = #line) throws(SDLError) -> Wrapped { - + guard let value = self else { let debugInformation = SDLError.DebugInformation(file: file, type: type, function: function, line: line) guard let error = SDLError.current(debugInformation: debugInformation) else { diff --git a/Sources/SDL/Texture.swift b/Sources/SDL/Texture.swift index d4511bb..5aba8d1 100644 --- a/Sources/SDL/Texture.swift +++ b/Sources/SDL/Texture.swift @@ -14,14 +14,19 @@ public class Texture { 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)) + try SDL_SetTextureColorMod( + handle, + color.r, + color.g, + color.b + ).sdlThrow(type: type(of: self)) } // MARK - Private properties @@ -39,7 +44,8 @@ public class Texture { /// - 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. + /// - 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, @@ -48,14 +54,15 @@ public class Texture { Int32(width), Int32(height) ).sdlThrow(type: type(of: self)) } - + /// Creates a ``Texture`` from an image file. /// - /// This can be signfintally more efficient than using a CPU-bound ``Surface``, + /// 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. + /// + /// 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: @@ -63,25 +70,32 @@ public class Texture { /// - 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)) } - + /// 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. + /// 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 { SDL_DestroyTexture(handle) } - // MARK - Methods + // MARK - Methods }