SDL: add Rect struct

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

No files matched your search

+9
View File
@@ -0,0 +1,9 @@
import SDL
class GameNode {
var position: Rect
init() {
position = Rect()
}
}
+2 -3
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,7 +70,7 @@ while !quit {
case 3: failTex
default: tileTex
}
try renderer.renderTexture(tex, dstRect: &rect)
try renderer.renderTexture(tex, dstRect: rect)
}
}
+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)
}
}
+21 -3
View File
@@ -1,4 +1,5 @@
import CSDL3
import Foundation
public class Renderer {
var handle: OpaquePointer
@@ -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() {
+22 -8
View File
@@ -21,7 +21,12 @@ public class Texture {
/// - 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,
@@ -54,8 +60,9 @@ public class Texture {
/// 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,19 +70,26 @@ 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.