From e84c85fbac119831c04db5c1bb9cab155b442aaf Mon Sep 17 00:00:00 2001 From: Candygoblen123 Date: Thu, 24 Oct 2024 17:55:42 -0400 Subject: [PATCH] update to sdl3 --- Package.swift | 28 +++++++++++------------ Sources/CSDL2/shim.h | 5 ---- Sources/{CSDL2 => CSDL3}/module.modulemap | 2 +- Sources/CSDL3/shim.h | 5 ++++ Sources/Picross/main.swift | 10 ++++---- Sources/SDL/Engine.swift | 14 +++++++----- Sources/SDL/Event.swift | 2 +- Sources/SDL/Renderer.swift | 12 +++++----- Sources/SDL/SDLError.swift | 18 +++++++++++++++ Sources/SDL/Surface.swift | 22 +++++++++--------- Sources/SDL/Texture.swift | 19 ++++++++------- Sources/SDL/Window.swift | 7 ++---- 12 files changed, 80 insertions(+), 64 deletions(-) delete mode 100644 Sources/CSDL2/shim.h rename Sources/{CSDL2 => CSDL3}/module.modulemap (78%) create mode 100644 Sources/CSDL3/shim.h diff --git a/Package.swift b/Package.swift index ef8bbd1..fa839d8 100644 --- a/Package.swift +++ b/Package.swift @@ -13,21 +13,21 @@ let package = Package( name: "Picross", dependencies: [ "SDL", - "CSDL2", - "CSDL2_Image", + "CSDL3", + //"CSDL2_Image", ]), - .target(name: "SDL", dependencies: ["CSDL2", "CSDL2_Image"]), - .systemLibrary(name: "CSDL2", pkgConfig: "sdl2", - providers: [ - .brew(["sdl2"]), - .apt(["libsdl2-dev"]) - ] - ), - .systemLibrary(name: "CSDL2_Image", pkgConfig: "SDL2_image", - providers: [ - .brew(["sdl2_image"]), - .apt(["libsdl2_image-dev"]) - ] + .target(name: "SDL", dependencies: ["CSDL3"/*, "CSDL2_Image" */]), + .systemLibrary(name: "CSDL3", pkgConfig: "sdl3"//, + //providers: [ + // .brew(["sdl2"]), + // .apt(["libsdl2-dev"]) + //] ) + //.systemLibrary(name: "CSDL2_Image", pkgConfig: "SDL2_image", + // providers: [ + // .brew(["sdl2_image"]), + // .apt(["libsdl2_image-dev"]) + // ] + //) ] ) diff --git a/Sources/CSDL2/shim.h b/Sources/CSDL2/shim.h deleted file mode 100644 index 1f926ed..0000000 --- a/Sources/CSDL2/shim.h +++ /dev/null @@ -1,5 +0,0 @@ -#ifdef __APPLE__ -#include "/opt/homebrew/include/SDL2/SDL.h" -#else -#include -#endif diff --git a/Sources/CSDL2/module.modulemap b/Sources/CSDL3/module.modulemap similarity index 78% rename from Sources/CSDL2/module.modulemap rename to Sources/CSDL3/module.modulemap index 8a02f15..2999668 100644 --- a/Sources/CSDL2/module.modulemap +++ b/Sources/CSDL3/module.modulemap @@ -1,5 +1,5 @@ module CSDL2 [system] { header "shim.h" - link "SDL2" + link "SDL3" export * } diff --git a/Sources/CSDL3/shim.h b/Sources/CSDL3/shim.h new file mode 100644 index 0000000..965f81d --- /dev/null +++ b/Sources/CSDL3/shim.h @@ -0,0 +1,5 @@ +#ifdef __APPLE__ +#include "/usr/local/include/SDL3/SDL.h" +#else +#include +#endif diff --git a/Sources/Picross/main.swift b/Sources/Picross/main.swift index 7c9d32c..168dedc 100644 --- a/Sources/Picross/main.swift +++ b/Sources/Picross/main.swift @@ -33,10 +33,10 @@ var eventHandler = EventHandler() while !quit { eventHandler.pump() { e in - if e.type == SDL_QUIT.rawValue { + if e.type == SDL_EVENT_QUIT.rawValue { quit = true } - if e.type == SDL_MOUSEBUTTONDOWN.rawValue { + if e.type == SDL_EVENT_MOUSE_BUTTON_DOWN.rawValue { let x = Int(e.button.y) / 32 let y = Int(e.button.x) / 32 @@ -57,10 +57,10 @@ while !quit { } try renderer.clear() - try renderer.copy(reimuTex) + try renderer.renderTexture(reimuTex) for i in 0..<10 { for j in 0..<10 { - var rect = SDL_Rect(x: 32 * Int32(i), y: 32 * Int32(j), w: 32, h: 32) + var rect = SDL_FRect(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 @@ -68,7 +68,7 @@ while !quit { case 3: failTex default: tileTex } - try renderer.copy(tex, dstRect: &rect) + try renderer.renderTexture(tex, dstRect: &rect) } } diff --git a/Sources/SDL/Engine.swift b/Sources/SDL/Engine.swift index 725bbdf..87600ae 100644 --- a/Sources/SDL/Engine.swift +++ b/Sources/SDL/Engine.swift @@ -1,9 +1,9 @@ import CSDL2 -import CSDL2_Image +//import CSDL2_Image public struct Engine { - /// Initalizes SDL. This must be called before any other SDL function, and can only be called once per program. + /// Initalizes SDL. This must be called before any other SDL function. /// /// Errors encountered in this function are considered unrecoverable, and will crash the entire program. /// @@ -12,7 +12,7 @@ public struct Engine { 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)) + //try! IMG_Init(Int32(IMG_INIT_PNG.rawValue)).sdlThrow(type: type(of: self)) } public static func delay(_ ms: UInt32) { @@ -31,13 +31,14 @@ public struct Engine { self.rawValue = rawValue } - 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 gameController = InitSubSystems(rawValue: SDL_INIT_GAMEPAD) public static let events = InitSubSystems(rawValue: SDL_INIT_EVENTS) + public static let sensor = InitSubSystems(rawValue: SDL_INIT_SENSOR) + public static let camera = InitSubSystems(rawValue: SDL_INIT_CAMERA) } public struct InitImageFormats: OptionSet, Sendable { @@ -46,7 +47,8 @@ public struct Engine { public init(rawValue: Int32) { self.rawValue = rawValue } - + + // TODO - fix this! public static let jpg = InitImageFormats(rawValue: 0x00000001) public static let png = InitImageFormats(rawValue: 0x00000002) public static let tif = InitImageFormats(rawValue: 0x00000004) diff --git a/Sources/SDL/Event.swift b/Sources/SDL/Event.swift index 97b5d2b..a65d9ee 100644 --- a/Sources/SDL/Event.swift +++ b/Sources/SDL/Event.swift @@ -8,7 +8,7 @@ public class EventHandler { } public func pump(handler: (SDL_Event) -> ()) { - while SDL_PollEvent(&handle) > 0 { + while SDL_PollEvent(&handle) == true { handler(handle) } } diff --git a/Sources/SDL/Renderer.swift b/Sources/SDL/Renderer.swift index c8f8f28..7bd3a53 100644 --- a/Sources/SDL/Renderer.swift +++ b/Sources/SDL/Renderer.swift @@ -7,17 +7,17 @@ public class Renderer { get { var x: Float = 0.0 var y: Float = 0.0 - SDL_RenderGetScale(handle, &x, &y) + SDL_GetRenderScale(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)) + try SDL_SetRenderScale(handle, newScale.x, newScale.y).sdlThrow(type: type(of: self)) } - public init(_ window: Window) throws { - handle = try SDL_CreateRenderer(window.handle, -1, SDL_RENDERER_ACCELERATED.rawValue).sdlThrow(type: type(of: self)) + public init(_ window: Window, name: String? = nil) throws { + handle = try SDL_CreateRenderer(window.handle, name).sdlThrow(type: type(of: self)) } deinit { @@ -29,8 +29,8 @@ public class Renderer { try SDL_RenderClear(handle).sdlThrow(type: type(of: self)) } - public func copy(_ texture: Texture, srcRect: UnsafePointer! = nil, dstRect: UnsafePointer! = nil) throws { - try SDL_RenderCopy(handle, texture.handle, srcRect, dstRect).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 present() { diff --git a/Sources/SDL/SDLError.swift b/Sources/SDL/SDLError.swift index 611ba15..e334223 100644 --- a/Sources/SDL/SDLError.swift +++ b/Sources/SDL/SDLError.swift @@ -86,6 +86,24 @@ internal extension Int32 { } } +internal extension Bool { + func sdlThrow(file: String = #file, + type: Any, + function: String = #function, + line: UInt = #line) throws(SDLError) { + + guard self == false else { + let debugInformation = SDLError.DebugInformation(file: file, type: type, function: function, line: line) + guard let error = SDLError.current(debugInformation: debugInformation) else { + assertionFailure("No error for error code \(self)") + throw SDLError(errorMessage: "Error code \(self)", debugInformation: debugInformation) + } + throw error + } + } + +} + internal extension Optional { func sdlThrow(file: String = #file, type: Any, diff --git a/Sources/SDL/Surface.swift b/Sources/SDL/Surface.swift index ee6d0e3..57d2b55 100644 --- a/Sources/SDL/Surface.swift +++ b/Sources/SDL/Surface.swift @@ -1,14 +1,14 @@ -import CSDL2 -import CSDL2_Image +//import CSDL2 +//import CSDL2_Image -public class Surface { - var handle: UnsafeMutablePointer +//public class Surface { +// var handle: UnsafeMutablePointer - public init(from imagePath: String) throws { - handle = try IMG_Load(imagePath).sdlThrow(type: type(of: self)) - } +// public init(from imagePath: String) throws { +// handle = try IMG_Load(imagePath).sdlThrow(type: type(of: self)) +// } - deinit { - SDL_FreeSurface(handle) - } -} +// deinit { +// SDL_FreeSurface(handle) +// } +//} diff --git a/Sources/SDL/Texture.swift b/Sources/SDL/Texture.swift index 6d0cf7b..6c2d9d9 100644 --- a/Sources/SDL/Texture.swift +++ b/Sources/SDL/Texture.swift @@ -1,11 +1,9 @@ import Foundation import CSDL2 -import CSDL2_Image +//import CSDL2_Image /// An efficent, driver-specific representation of pixel data. -/// -/// public class Texture { // MARK - Public properties @@ -30,7 +28,7 @@ public class Texture { // MARK - Private properties /// The pointer refrence to the underlying `SDL_Texture` - var handle: OpaquePointer + var handle: UnsafeMutablePointer // MARK - Initalizers @@ -46,8 +44,8 @@ public class Texture { public init(_ renderer: Renderer, width: Int, height: Int) throws(SDLError) { handle = try SDL_CreateTexture( renderer.handle, - SDL_PIXELFORMAT_RGB24.rawValue, - Int32(SDL_TEXTUREACCESS_TARGET.rawValue), + SDL_PIXELFORMAT_RGB24, + SDL_TEXTUREACCESS_TARGET, Int32(width), Int32(height) ).sdlThrow(type: type(of: self)) } @@ -66,7 +64,8 @@ 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)) + fatalError("init(from imagePath:renderer:) TODO") + //handle = try IMG_LoadTexture(renderer.handle, imagePath).sdlThrow(type: type(of: self)) } /// Creates a ``Texture`` from an existing surface. @@ -77,9 +76,9 @@ public class Texture { /// - 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)) - } + //public init(from surface: Surface, renderer: Renderer) throws(SDLError) { + // handle = try SDL_CreateTextureFromSurface(renderer.handle, surface.handle).sdlThrow(type: type(of: self)) + //} /// Calls SDL_DestroyTexture on the texture. Should be safe. deinit { diff --git a/Sources/SDL/Window.swift b/Sources/SDL/Window.swift index d06a14a..d6e403c 100644 --- a/Sources/SDL/Window.swift +++ b/Sources/SDL/Window.swift @@ -4,13 +4,10 @@ public class Window { let handle: OpaquePointer public init(_ title: String, width: Int, height: Int, shown: Bool) throws { - let uint32_shown = shown ? SDL_WINDOW_SHOWN.rawValue : SDL_WINDOW_HIDDEN.rawValue handle = try SDL_CreateWindow( - title, - Int32(SDL_WINDOWPOS_CENTERED_MASK), - Int32(SDL_WINDOWPOS_CENTERED_MASK), + title, Int32(width), Int32(height), - uint32_shown + 0 // TODO - impl window flags ).sdlThrow(type: type(of: self)) }