update to sdl3

This commit is contained in:
Candygoblen123 committed 2024-10-24 17:55:42 -04:00
1 parent 25415ab357
commit e84c85fbac
12 files changed
+80 -64

No files matched your search

+14 -14
View File
@@ -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"])
// ]
//)
]
)
-5
View File
@@ -1,5 +0,0 @@
#ifdef __APPLE__
#include "/opt/homebrew/include/SDL2/SDL.h"
#else
#include <SDL2/SDL.h>
#endif
@@ -1,5 +1,5 @@
module CSDL2 [system] {
header "shim.h"
link "SDL2"
link "SDL3"
export *
}
+5
View File
@@ -0,0 +1,5 @@
#ifdef __APPLE__
#include "/usr/local/include/SDL3/SDL.h"
#else
#include <SDL3/SDL.h>
#endif
+5 -5
View File
@@ -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)
}
}
+8 -6
View File
@@ -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)
+1 -1
View File
@@ -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)
}
}
+6 -6
View File
@@ -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<SDL_Rect>! = nil, dstRect: UnsafePointer<SDL_Rect>! = nil) throws {
try SDL_RenderCopy(handle, texture.handle, srcRect, dstRect).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 present() {
+18
View File
@@ -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,
+11 -11
View File
@@ -1,14 +1,14 @@
import CSDL2
import CSDL2_Image
//import CSDL2
//import CSDL2_Image
public class Surface {
var handle: UnsafeMutablePointer<SDL_Surface>
//public class Surface {
// var handle: UnsafeMutablePointer<SDL_Surface>
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)
// }
//}
+9 -10
View File
@@ -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<SDL_Texture>
// 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 {
+2 -5
View File
@@ -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))
}