SDL: add Rect struct

This commit is contained in:
candy committed 2024-11-19 00:10:45 -05:00
1 parent 461d0c96de
commit 7a7018fd2d
6 files changed
+91 -42

No files matched your search

+9
View File
@@ -0,0 +1,9 @@
import SDL
class GameNode {
var position: Rect
init() {
position = Rect()
}
}
+3 -4
View File
@@ -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)
+9
View File
@@ -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)
}
}
+22 -4
View File
@@ -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<SDL_FRect>! = nil, dstRect: UnsafePointer<SDL_FRect>! = 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<SDL_FRect>? = nil
var dstRectPtr: UnsafePointer<SDL_FRect>? = nil
if let srcRect = srcRect {
srcRectPtr = withUnsafePointer(to: srcRect.handle) { UnsafePointer<SDL_FRect>($0) }
}
if let dstRect = dstRect {
dstRectPtr = withUnsafePointer(to: dstRect.handle) { UnsafePointer<SDL_FRect>($0) }
}
try SDL_RenderTexture(
handle,
texture.handle,
srcRectPtr,
dstRectPtr
).sdlThrow(type: type(of: self))
}
public func present() {
+19 -19
View File
@@ -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 {
+29 -15
View File
@@ -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
}