commit 909736ac676b43fa654bd5cc73f193cdfa1370cf Author: Andrew Glaze Date: Mon Oct 14 16:18:24 2024 -0400 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0023a53 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +.DS_Store +/.build +/Packages +xcuserdata/ +DerivedData/ +.swiftpm/configuration/registries.json +.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata +.netrc diff --git a/Assets/Tile-001.aseprite b/Assets/Tile-001.aseprite new file mode 100644 index 0000000..3d351c1 Binary files /dev/null and b/Assets/Tile-001.aseprite differ diff --git a/Assets/Tile-Cross.png b/Assets/Tile-Cross.png new file mode 100644 index 0000000..1a2b23c Binary files /dev/null and b/Assets/Tile-Cross.png differ diff --git a/Assets/Tile-Filled.png b/Assets/Tile-Filled.png new file mode 100644 index 0000000..6a3a6a0 Binary files /dev/null and b/Assets/Tile-Filled.png differ diff --git a/Assets/Tile.png b/Assets/Tile.png new file mode 100644 index 0000000..7b6c674 Binary files /dev/null and b/Assets/Tile.png differ diff --git a/Package.swift b/Package.swift new file mode 100644 index 0000000..95e9095 --- /dev/null +++ b/Package.swift @@ -0,0 +1,32 @@ +// swift-tools-version: 6.0 +// The swift-tools-version declares the minimum version of Swift required to build this package. + +import PackageDescription + +let package = Package( + name: "Picross", + targets: [ + // Targets are the basic building blocks of a package, defining a module or a test suite. + // Targets can depend on other targets in this package and products from dependencies. + .executableTarget( + name: "Picross", + dependencies: [ + "SDL", + "CSDL2", + "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"]) + ] + ) + ] +) diff --git a/Sources/CSDL2/module.modulemap b/Sources/CSDL2/module.modulemap new file mode 100644 index 0000000..8a02f15 --- /dev/null +++ b/Sources/CSDL2/module.modulemap @@ -0,0 +1,5 @@ +module CSDL2 [system] { + header "shim.h" + link "SDL2" + export * +} diff --git a/Sources/CSDL2/shim.h b/Sources/CSDL2/shim.h new file mode 100644 index 0000000..1f926ed --- /dev/null +++ b/Sources/CSDL2/shim.h @@ -0,0 +1,5 @@ +#ifdef __APPLE__ +#include "/opt/homebrew/include/SDL2/SDL.h" +#else +#include +#endif diff --git a/Sources/CSDL2_Image/module.modulemap b/Sources/CSDL2_Image/module.modulemap new file mode 100644 index 0000000..962e3a8 --- /dev/null +++ b/Sources/CSDL2_Image/module.modulemap @@ -0,0 +1,5 @@ +module CSDL2_Image [system] { + header "shim.h" + link "SDL2" + export * +} diff --git a/Sources/CSDL2_Image/shim.h b/Sources/CSDL2_Image/shim.h new file mode 100644 index 0000000..163c5e0 --- /dev/null +++ b/Sources/CSDL2_Image/shim.h @@ -0,0 +1,5 @@ +#ifdef __APPLE__ +#include "/opt/homebrew/include/SDL2/SDL_image.h" +#else +#include +#endif diff --git a/Sources/Picross/main.swift b/Sources/Picross/main.swift new file mode 100644 index 0000000..6ba00bd --- /dev/null +++ b/Sources/Picross/main.swift @@ -0,0 +1,30 @@ +import SDL +import CSDL2 + +try! Engine.initalize(subsystem: .video) + +let window = try! Window("Picross", width: 800, height: 600, shown: true) +let renderer = try! window.renderer +let surface = try! Surface(from: "texture.png") +let texture = try! Texture(from: surface, renderer: renderer) + + +var quit = false +var event = SDL_Event() + +while !quit { + while SDL_PollEvent(&event) > 0 { + if event.type == SDL_QUIT.rawValue { + quit = true + } + } + + try renderer.clear() + try renderer.copy(texture) + renderer.present() + + + Engine.delay(100) +} + +Engine.quit() diff --git a/Sources/SDL/Engine.swift b/Sources/SDL/Engine.swift new file mode 100644 index 0000000..419577f --- /dev/null +++ b/Sources/SDL/Engine.swift @@ -0,0 +1,30 @@ +import CSDL2 +import CSDL2_Image + +public struct Engine { + public static func initalize(subsystem: SubSystem) throws { + try SDL_Init(subsystem.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) { + SDL_Delay(ms) + } + + public static func quit() { + SDL_Quit() + } + + /// Specific SDL subsystems. + public enum SubSystem: UInt32 { + case timer = 0x00000001 + case audio = 0x00000010 + case video = 0x00000020 + case joystick = 0x00000200 + case haptic = 0x00001000 + case gameController = 0x00002000 + case events = 0x00004000 + } +} diff --git a/Sources/SDL/Renderer.swift b/Sources/SDL/Renderer.swift new file mode 100644 index 0000000..dc68d48 --- /dev/null +++ b/Sources/SDL/Renderer.swift @@ -0,0 +1,29 @@ +import CSDL2 + +public class Renderer { + public var handle: OpaquePointer + + public init(_ window: Window) throws { + handle = try SDL_CreateRenderer(window.handle, -1, SDL_RENDERER_ACCELERATED.rawValue).sdlThrow(type: type(of: self)) + } + + deinit { + SDL_DestroyRenderer(handle) + } + + public func setScale(x: Float, y: Float) throws { + try SDL_RenderSetScale(handle, x, y).sdlThrow(type: type(of: self)) + } + + public func clear() throws { + 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 present() { + SDL_RenderPresent(handle) + } +} diff --git a/Sources/SDL/SDLError.swift b/Sources/SDL/SDLError.swift new file mode 100644 index 0000000..7cf797c --- /dev/null +++ b/Sources/SDL/SDLError.swift @@ -0,0 +1,105 @@ +import CSDL2 + +public struct SDLError: Error { + + public let errorMessage: String + + internal let debugInformation: DebugInformation? +} + +extension SDLError: CustomStringConvertible { + + public var description: String { + + return errorMessage + } +} + +extension SDLError: CustomDebugStringConvertible { + public var debugDescription: String { + + var description = errorMessage + if let debugInformation = debugInformation { + description += " " + "(\(debugInformation.description))" + } + return description + } +} + +internal extension SDLError { + + final class DebugInformation: CustomStringConvertible, Sendable { + + public let file: String + + public let type: String + + public let function: String + + public let line: UInt + + internal init(file: String, + type: Any, + function: String, + line: UInt) { + + self.file = file + self.function = function + self.line = line + self.type = String(reflecting: type) + } + + public var description: String { + let fileName = file.split(separator: "/").last.flatMap { String($0) } ?? file + return "\(fileName):\(line.description) \(type).\(function)" + } + } +} + +internal extension SDLError { + static func current(debugInformation: DebugInformation? = nil) -> SDLError? { + + guard let cString = SDL_GetError() + else { return nil } + + SDL_ClearError() + let errorDescription = String(cString: cString) + + return SDLError(errorMessage: errorDescription, debugInformation: debugInformation) + } +} + +internal extension Int32 { + func sdlThrow(file: String = #file, + type: Any, + function: String = #function, + line: UInt = #line) throws { + + guard self >= 0 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, + function: String = #function, + line: UInt = #line) throws -> Wrapped { + + guard let value = 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 nil value \(Wrapped.self)") + throw SDLError(errorMessage: "Nil value \(Wrapped.self)", debugInformation: debugInformation) + } + throw error + } + return value + } +} diff --git a/Sources/SDL/Surface.swift b/Sources/SDL/Surface.swift new file mode 100644 index 0000000..ee6d0e3 --- /dev/null +++ b/Sources/SDL/Surface.swift @@ -0,0 +1,14 @@ +import CSDL2 +import CSDL2_Image + +public class Surface { + var handle: UnsafeMutablePointer + + public init(from imagePath: String) throws { + handle = try IMG_Load(imagePath).sdlThrow(type: type(of: self)) + } + + deinit { + SDL_FreeSurface(handle) + } +} diff --git a/Sources/SDL/Texture.swift b/Sources/SDL/Texture.swift new file mode 100644 index 0000000..cc1a120 --- /dev/null +++ b/Sources/SDL/Texture.swift @@ -0,0 +1,22 @@ +import CSDL2 + +public class Texture { + var handle: OpaquePointer + + public init(_ renderer: Renderer, width: Int, height: Int) throws { + handle = try SDL_CreateTexture( + renderer.handle, + SDL_PIXELFORMAT_RGB24.rawValue, + Int32(SDL_TEXTUREACCESS_TARGET.rawValue), + Int32(width), Int32(height) + ).sdlThrow(type: type(of: self)) + } + + deinit { + SDL_DestroyTexture(handle) + } + + public init(from surface: Surface, renderer: Renderer) throws { + handle = try SDL_CreateTextureFromSurface(renderer.handle, surface.handle).sdlThrow(type: type(of: self)) + } +} diff --git a/Sources/SDL/Window.swift b/Sources/SDL/Window.swift new file mode 100644 index 0000000..9f70ac0 --- /dev/null +++ b/Sources/SDL/Window.swift @@ -0,0 +1,22 @@ +import CSDL2 + +public class Window { + var 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), + Int32(width), Int32(height), + uint32_shown + ).sdlThrow(type: type(of: self)) + } + + deinit { + SDL_DestroyWindow(handle) + } + + public var renderer: Renderer { get throws { try Renderer(self) } } +} diff --git a/texture.png b/texture.png new file mode 100644 index 0000000..3f7ecb8 Binary files /dev/null and b/texture.png differ