Initial commit

This commit is contained in:
candy committed 2024-10-14 16:18:24 -04:00
commit 909736ac67
18 files changed
+312

No files matched your search

+8
View File
@@ -0,0 +1,8 @@
.DS_Store
/.build
/Packages
xcuserdata/
DerivedData/
.swiftpm/configuration/registries.json
.swiftpm/xcode/package.xcworkspace/contents.xcworkspacedata
.netrc
Binary file not shown.
Binary file not shown.

After

Width:  |  Height:  |  Size: 327 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 185 B

BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 161 B

+32
View File
@@ -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"])
]
)
]
)
+5
View File
@@ -0,0 +1,5 @@
module CSDL2 [system] {
header "shim.h"
link "SDL2"
export *
}
+5
View File
@@ -0,0 +1,5 @@
#ifdef __APPLE__
#include "/opt/homebrew/include/SDL2/SDL.h"
#else
#include <SDL2/SDL.h>
#endif
+5
View File
@@ -0,0 +1,5 @@
module CSDL2_Image [system] {
header "shim.h"
link "SDL2"
export *
}
+5
View File
@@ -0,0 +1,5 @@
#ifdef __APPLE__
#include "/opt/homebrew/include/SDL2/SDL_image.h"
#else
#include <SDL2/SDL_image.h>
#endif
+30
View File
@@ -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()
+30
View File
@@ -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
}
}
+29
View File
@@ -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<SDL_Rect>! = nil, dstRect: UnsafePointer<SDL_Rect>! = nil) throws {
try SDL_RenderCopy(handle, texture.handle, srcRect, dstRect).sdlThrow(type: type(of: self))
}
public func present() {
SDL_RenderPresent(handle)
}
}
+105
View File
@@ -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
}
}
+14
View File
@@ -0,0 +1,14 @@
import CSDL2
import CSDL2_Image
public class Surface {
var handle: UnsafeMutablePointer<SDL_Surface>
public init(from imagePath: String) throws {
handle = try IMG_Load(imagePath).sdlThrow(type: type(of: self))
}
deinit {
SDL_FreeSurface(handle)
}
}
+22
View File
@@ -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))
}
}
+22
View File
@@ -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) } }
}
BIN
View File
Binary file not shown.

After

Width:  |  Height:  |  Size: 346 KiB