SDL: add Rect struct
This commit is contained in:
1 parent
461d0c96de
commit
7a7018fd2d
6 files changed
+91
-42
No files matched your search
@@ -0,0 +1,9 @@
|
|||||||
|
import SDL
|
||||||
|
|
||||||
|
class GameNode {
|
||||||
|
var position: Rect
|
||||||
|
|
||||||
|
init() {
|
||||||
|
position = Rect()
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
import SDL
|
import SDL
|
||||||
import CSDL3
|
|
||||||
|
|
||||||
let puzzle = [
|
let puzzle = [
|
||||||
[0,0,0,0,0,1,0,0,0,0],
|
[0,0,0,0,0,1,0,0,0,0],
|
||||||
@@ -63,7 +62,7 @@ while !quit {
|
|||||||
try renderer.renderTexture(reimuTex)
|
try renderer.renderTexture(reimuTex)
|
||||||
for i in 0..<10 {
|
for i in 0..<10 {
|
||||||
for j 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] {
|
let tex = switch board[j][i] {
|
||||||
case 0: tileTex
|
case 0: tileTex
|
||||||
case 1: filledTex
|
case 1: filledTex
|
||||||
@@ -71,10 +70,10 @@ while !quit {
|
|||||||
case 3: failTex
|
case 3: failTex
|
||||||
default: tileTex
|
default: tileTex
|
||||||
}
|
}
|
||||||
try renderer.renderTexture(tex, dstRect: &rect)
|
try renderer.renderTexture(tex, dstRect: rect)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
renderer.present()
|
renderer.present()
|
||||||
|
|
||||||
Engine.delay(100)
|
Engine.delay(100)
|
||||||
|
|||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import CSDL3
|
import CSDL3
|
||||||
|
import Foundation
|
||||||
|
|
||||||
public class Renderer {
|
public class Renderer {
|
||||||
var handle: OpaquePointer
|
var handle: OpaquePointer
|
||||||
@@ -11,7 +12,7 @@ public class Renderer {
|
|||||||
return (x, y)
|
return (x, y)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public func setScale(newScale: (x: Float, y: Float)) throws {
|
public func setScale(newScale: (x: Float, y: Float)) throws {
|
||||||
try SDL_SetRenderScale(handle, newScale.x, newScale.y).sdlThrow(type: type(of: self))
|
try SDL_SetRenderScale(handle, newScale.x, newScale.y).sdlThrow(type: type(of: self))
|
||||||
}
|
}
|
||||||
@@ -24,13 +25,30 @@ public class Renderer {
|
|||||||
SDL_DestroyRenderer(handle)
|
SDL_DestroyRenderer(handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
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))
|
||||||
}
|
}
|
||||||
|
|
||||||
public func renderTexture(_ texture: Texture, srcRect: UnsafePointer<SDL_FRect>! = nil, dstRect: UnsafePointer<SDL_FRect>! = nil) throws {
|
public func renderTexture(
|
||||||
try SDL_RenderTexture(handle, texture.handle, srcRect, dstRect).sdlThrow(type: type(of: self))
|
_ 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() {
|
public func present() {
|
||||||
|
|||||||
+19
-19
@@ -1,23 +1,23 @@
|
|||||||
import CSDL3
|
import CSDL3
|
||||||
|
|
||||||
public struct SDLError: Error {
|
public struct SDLError: Error {
|
||||||
|
|
||||||
public let errorMessage: String
|
public let errorMessage: String
|
||||||
|
|
||||||
internal let debugInformation: DebugInformation?
|
internal let debugInformation: DebugInformation?
|
||||||
}
|
}
|
||||||
|
|
||||||
extension SDLError: CustomStringConvertible {
|
extension SDLError: CustomStringConvertible {
|
||||||
|
|
||||||
public var description: String {
|
public var description: String {
|
||||||
|
|
||||||
return errorMessage
|
return errorMessage
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extension SDLError: CustomDebugStringConvertible {
|
extension SDLError: CustomDebugStringConvertible {
|
||||||
public var debugDescription: String {
|
public var debugDescription: String {
|
||||||
|
|
||||||
var description = errorMessage
|
var description = errorMessage
|
||||||
if let debugInformation = debugInformation {
|
if let debugInformation = debugInformation {
|
||||||
description += " " + "(\(debugInformation.description))"
|
description += " " + "(\(debugInformation.description))"
|
||||||
@@ -27,28 +27,28 @@ extension SDLError: CustomDebugStringConvertible {
|
|||||||
}
|
}
|
||||||
|
|
||||||
internal extension SDLError {
|
internal extension SDLError {
|
||||||
|
|
||||||
final class DebugInformation: CustomStringConvertible, Sendable {
|
final class DebugInformation: CustomStringConvertible, Sendable {
|
||||||
|
|
||||||
public let file: String
|
public let file: String
|
||||||
|
|
||||||
public let type: String
|
public let type: String
|
||||||
|
|
||||||
public let function: String
|
public let function: String
|
||||||
|
|
||||||
public let line: UInt
|
public let line: UInt
|
||||||
|
|
||||||
internal init(file: String,
|
internal init(file: String,
|
||||||
type: Any,
|
type: Any,
|
||||||
function: String,
|
function: String,
|
||||||
line: UInt) {
|
line: UInt) {
|
||||||
|
|
||||||
self.file = file
|
self.file = file
|
||||||
self.function = function
|
self.function = function
|
||||||
self.line = line
|
self.line = line
|
||||||
self.type = String(reflecting: type)
|
self.type = String(reflecting: type)
|
||||||
}
|
}
|
||||||
|
|
||||||
public var description: String {
|
public var description: String {
|
||||||
let fileName = file.split(separator: "/").last.flatMap { String($0) } ?? file
|
let fileName = file.split(separator: "/").last.flatMap { String($0) } ?? file
|
||||||
return "\(fileName):\(line.description) \(type).\(function)"
|
return "\(fileName):\(line.description) \(type).\(function)"
|
||||||
@@ -58,14 +58,14 @@ internal extension SDLError {
|
|||||||
|
|
||||||
internal extension SDLError {
|
internal extension SDLError {
|
||||||
static func current(debugInformation: DebugInformation? = nil) -> SDLError? {
|
static func current(debugInformation: DebugInformation? = nil) -> SDLError? {
|
||||||
|
|
||||||
guard let cString = SDL_GetError()
|
guard let cString = SDL_GetError()
|
||||||
else { return nil }
|
else { return nil }
|
||||||
|
|
||||||
SDL_ClearError()
|
SDL_ClearError()
|
||||||
let errorDescription = String(cString: cString)
|
let errorDescription = String(cString: cString)
|
||||||
print(errorDescription)
|
print(errorDescription)
|
||||||
|
|
||||||
return SDLError(errorMessage: errorDescription, debugInformation: debugInformation)
|
return SDLError(errorMessage: errorDescription, debugInformation: debugInformation)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -75,7 +75,7 @@ internal extension Int32 {
|
|||||||
type: Any,
|
type: Any,
|
||||||
function: String = #function,
|
function: String = #function,
|
||||||
line: UInt = #line) throws(SDLError) {
|
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)
|
||||||
guard let error = SDLError.current(debugInformation: debugInformation) else {
|
guard let error = SDLError.current(debugInformation: debugInformation) else {
|
||||||
@@ -92,7 +92,7 @@ internal extension Bool {
|
|||||||
type: Any,
|
type: Any,
|
||||||
function: String = #function,
|
function: String = #function,
|
||||||
line: UInt = #line) throws(SDLError) {
|
line: UInt = #line) throws(SDLError) {
|
||||||
|
|
||||||
guard self else {
|
guard 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)
|
||||||
guard let error = SDLError.current(debugInformation: debugInformation) else {
|
guard let error = SDLError.current(debugInformation: debugInformation) else {
|
||||||
@@ -110,7 +110,7 @@ internal extension Optional {
|
|||||||
type: Any,
|
type: Any,
|
||||||
function: String = #function,
|
function: String = #function,
|
||||||
line: UInt = #line) throws(SDLError) -> 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)
|
||||||
guard let error = SDLError.current(debugInformation: debugInformation) else {
|
guard let error = SDLError.current(debugInformation: debugInformation) else {
|
||||||
|
|||||||
+29
-15
@@ -14,14 +14,19 @@ public class Texture {
|
|||||||
return color
|
return color
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Sets an additional color value multiplied into render copy operations.
|
/// Sets an additional color value multiplied into render copy operations.
|
||||||
///
|
///
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - color: The color value to be multiplied into copy operations.
|
/// - color: The color value to be multiplied into copy operations.
|
||||||
/// - Throws: ``SDLError`` on failure.
|
/// - Throws: ``SDLError`` on failure.
|
||||||
public func setColorMod(color: Color) throws(SDLError) {
|
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
|
// MARK - Private properties
|
||||||
@@ -39,7 +44,8 @@ public class Texture {
|
|||||||
/// - renderer: The rendering context.
|
/// - renderer: The rendering context.
|
||||||
/// - width: The width of the texture in pixels.
|
/// - width: The width of the texture in pixels.
|
||||||
/// - height: The height 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) {
|
public init(_ renderer: Renderer, width: Int, height: Int) throws(SDLError) {
|
||||||
handle = try SDL_CreateTexture(
|
handle = try SDL_CreateTexture(
|
||||||
renderer.handle,
|
renderer.handle,
|
||||||
@@ -48,14 +54,15 @@ public class Texture {
|
|||||||
Int32(width), Int32(height)
|
Int32(width), Int32(height)
|
||||||
).sdlThrow(type: type(of: self))
|
).sdlThrow(type: type(of: self))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a ``Texture`` from an image file.
|
/// 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 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.
|
/// If the loaded image has transparency or a colorkey,
|
||||||
/// Otherwise, a ``Texture`` will be created in the most reasonable format that represents the image data.
|
/// 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.
|
/// In most cases, this will end up being 32-bit RGB or 32-bit RGBA.
|
||||||
///
|
///
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
@@ -63,25 +70,32 @@ public class Texture {
|
|||||||
/// - renderer: The `Renderer` to attach the created `Texture` to.
|
/// - renderer: The `Renderer` to attach the created `Texture` to.
|
||||||
/// - Throws: ``SDLError`` on failure.
|
/// - Throws: ``SDLError`` on failure.
|
||||||
public init(from imagePath: String, renderer: Renderer) throws(SDLError) {
|
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.
|
/// 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
|
/// The surface is not modified or freed by this function.
|
||||||
/// different from the pixel format of the surface.
|
/// The pixel format of the created ``Texture`` may be different
|
||||||
|
/// from the pixel format of the surface.
|
||||||
/// - Parameters:
|
/// - Parameters:
|
||||||
/// - surface: The ``Surface`` structure containing pixel data used to fill the ``Texture``.
|
/// - surface: The ``Surface`` structure containing pixel data used to fill the ``Texture``.
|
||||||
/// - renderer: The ``Renderer`` to attach the newly created ``Texture`` to.
|
/// - renderer: The ``Renderer`` to attach the newly created ``Texture`` to.
|
||||||
/// - Throws: ``SDLError`` on failure.
|
/// - Throws: ``SDLError`` on failure.
|
||||||
public init(from surface: Surface, renderer: Renderer) throws(SDLError) {
|
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.
|
/// Calls SDL_DestroyTexture on the texture. Should be safe.
|
||||||
deinit {
|
deinit {
|
||||||
SDL_DestroyTexture(handle)
|
SDL_DestroyTexture(handle)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MARK - Methods
|
// MARK - Methods
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user