From 7665786c611e10691c711796407a47130a6583ad Mon Sep 17 00:00:00 2001 From: Andrew Glaze Date: Thu, 24 Oct 2024 18:29:07 -0400 Subject: [PATCH] migrate to SDL3_image --- Package.swift | 17 +++------------- Sources/CSDL2_Image/module.modulemap | 5 ----- Sources/CSDL3/module.modulemap | 2 +- Sources/CSDL3_Image/module.modulemap | 5 +++++ Sources/{CSDL2_Image => CSDL3_Image}/shim.h | 2 +- Sources/Picross/main.swift | 2 +- Sources/SDL/Engine.swift | 19 ++++++++++-------- Sources/SDL/Event.swift | 2 +- Sources/SDL/Renderer.swift | 2 +- Sources/SDL/SDLError.swift | 5 +++-- Sources/SDL/Surface.swift | 22 ++++++++++----------- Sources/SDL/Texture.swift | 14 ++++++------- Sources/SDL/Window.swift | 2 +- 13 files changed, 45 insertions(+), 54 deletions(-) delete mode 100644 Sources/CSDL2_Image/module.modulemap create mode 100644 Sources/CSDL3_Image/module.modulemap rename Sources/{CSDL2_Image => CSDL3_Image}/shim.h (70%) diff --git a/Package.swift b/Package.swift index fa839d8..34f07f5 100644 --- a/Package.swift +++ b/Package.swift @@ -14,20 +14,9 @@ let package = Package( dependencies: [ "SDL", "CSDL3", - //"CSDL2_Image", ]), - .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"]) - // ] - //) + .target(name: "SDL", dependencies: ["CSDL3", "CSDL3_Image"]), + .systemLibrary(name: "CSDL3", pkgConfig: "sdl3"), + .systemLibrary(name: "CSDL3_Image", pkgConfig: "sdl3-image") ] ) diff --git a/Sources/CSDL2_Image/module.modulemap b/Sources/CSDL2_Image/module.modulemap deleted file mode 100644 index 962e3a8..0000000 --- a/Sources/CSDL2_Image/module.modulemap +++ /dev/null @@ -1,5 +0,0 @@ -module CSDL2_Image [system] { - header "shim.h" - link "SDL2" - export * -} diff --git a/Sources/CSDL3/module.modulemap b/Sources/CSDL3/module.modulemap index 2999668..059ec34 100644 --- a/Sources/CSDL3/module.modulemap +++ b/Sources/CSDL3/module.modulemap @@ -1,4 +1,4 @@ -module CSDL2 [system] { +module CSDL3 [system] { header "shim.h" link "SDL3" export * diff --git a/Sources/CSDL3_Image/module.modulemap b/Sources/CSDL3_Image/module.modulemap new file mode 100644 index 0000000..64d64df --- /dev/null +++ b/Sources/CSDL3_Image/module.modulemap @@ -0,0 +1,5 @@ +module CSDL3_Image [system] { + header "shim.h" + link "SDL3" + export * +} diff --git a/Sources/CSDL2_Image/shim.h b/Sources/CSDL3_Image/shim.h similarity index 70% rename from Sources/CSDL2_Image/shim.h rename to Sources/CSDL3_Image/shim.h index 163c5e0..b933c96 100644 --- a/Sources/CSDL2_Image/shim.h +++ b/Sources/CSDL3_Image/shim.h @@ -1,5 +1,5 @@ #ifdef __APPLE__ #include "/opt/homebrew/include/SDL2/SDL_image.h" #else -#include +#include #endif diff --git a/Sources/Picross/main.swift b/Sources/Picross/main.swift index 168dedc..a6d8750 100644 --- a/Sources/Picross/main.swift +++ b/Sources/Picross/main.swift @@ -1,5 +1,5 @@ import SDL -import CSDL2 +import CSDL3 let puzzle = [ [0,0,0,0,0,1,0,0,0,0], diff --git a/Sources/SDL/Engine.swift b/Sources/SDL/Engine.swift index 87600ae..1920ef7 100644 --- a/Sources/SDL/Engine.swift +++ b/Sources/SDL/Engine.swift @@ -1,5 +1,5 @@ -import CSDL2 -//import CSDL2_Image +import CSDL3 +import CSDL3_Image public struct Engine { @@ -9,10 +9,14 @@ public struct Engine { /// /// - Parameters: /// - subsystems: The SDL subsystem(s) to start. Default value only initalizes the video subsystem. - public static func start(subsystems: InitSubSystems = .video) { - try! SDL_InitSubSystem(subsystems.rawValue).sdlThrow(type: type(of: self)) + public static func start(subsystems: InitSubSystems = .video, imageFormats: InitImageFormats = .png) { + guard SDL_InitSubSystem(subsystems.rawValue) else { + fatalError("SDL failed to start: \(SDLError.current()?.errorMessage ?? "Failed to obtain error message!")") + } - //try! IMG_Init(Int32(IMG_INIT_PNG.rawValue)).sdlThrow(type: type(of: self)) + guard IMG_Init(imageFormats.rawValue) == imageFormats.rawValue else { + fatalError("SDL_image failed to start: \(SDLError.current()?.errorMessage ?? "Failed to obtain error message!")") + } } public static func delay(_ ms: UInt32) { @@ -23,7 +27,6 @@ public struct Engine { SDL_Quit() } - /// public struct InitSubSystems: OptionSet, Sendable { public let rawValue: UInt32 @@ -42,9 +45,9 @@ public struct Engine { } public struct InitImageFormats: OptionSet, Sendable { - public let rawValue: Int32 + public let rawValue: UInt32 - public init(rawValue: Int32) { + public init(rawValue: UInt32) { self.rawValue = rawValue } diff --git a/Sources/SDL/Event.swift b/Sources/SDL/Event.swift index a65d9ee..60139f0 100644 --- a/Sources/SDL/Event.swift +++ b/Sources/SDL/Event.swift @@ -1,4 +1,4 @@ -import CSDL2 +import CSDL3 public class EventHandler { var handle: SDL_Event diff --git a/Sources/SDL/Renderer.swift b/Sources/SDL/Renderer.swift index 7bd3a53..4dabb86 100644 --- a/Sources/SDL/Renderer.swift +++ b/Sources/SDL/Renderer.swift @@ -1,4 +1,4 @@ -import CSDL2 +import CSDL3 public class Renderer { var handle: OpaquePointer diff --git a/Sources/SDL/SDLError.swift b/Sources/SDL/SDLError.swift index e334223..991be8a 100644 --- a/Sources/SDL/SDLError.swift +++ b/Sources/SDL/SDLError.swift @@ -1,4 +1,4 @@ -import CSDL2 +import CSDL3 public struct SDLError: Error { @@ -64,6 +64,7 @@ internal extension SDLError { SDL_ClearError() let errorDescription = String(cString: cString) + print(errorDescription) return SDLError(errorMessage: errorDescription, debugInformation: debugInformation) } @@ -92,7 +93,7 @@ internal extension Bool { function: String = #function, line: UInt = #line) throws(SDLError) { - guard self == false else { + guard self 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)") diff --git a/Sources/SDL/Surface.swift b/Sources/SDL/Surface.swift index 57d2b55..43ba6d0 100644 --- a/Sources/SDL/Surface.swift +++ b/Sources/SDL/Surface.swift @@ -1,14 +1,14 @@ -//import CSDL2 -//import CSDL2_Image +import CSDL3 +import CSDL3_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_DestroySurface(handle) + } +} diff --git a/Sources/SDL/Texture.swift b/Sources/SDL/Texture.swift index 6c2d9d9..d4511bb 100644 --- a/Sources/SDL/Texture.swift +++ b/Sources/SDL/Texture.swift @@ -1,6 +1,5 @@ -import Foundation -import CSDL2 -//import CSDL2_Image +import CSDL3 +import CSDL3_Image /// An efficent, driver-specific representation of pixel data. @@ -64,8 +63,7 @@ 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) { - fatalError("init(from imagePath:renderer:) TODO") - //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. @@ -76,9 +74,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 d6e403c..a3e62e5 100644 --- a/Sources/SDL/Window.swift +++ b/Sources/SDL/Window.swift @@ -1,4 +1,4 @@ -import CSDL2 +import CSDL3 public class Window { let handle: OpaquePointer