Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Debug simulator non embedded #16

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 11 additions & 10 deletions Examples/SwiftBreak/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,12 @@ guard let home = Context.environment["HOME"] else {
}

let swiftSettingsSimulator: [SwiftSetting] = [
.enableExperimentalFeature("Embedded"),
.enableExperimentalFeature("NoncopyableGenerics"),
.unsafeFlags([
"-Xfrontend", "-disable-objc-interop",
"-Xfrontend", "-disable-stack-protector",
"-Xfrontend", "-function-sections",
"-Xfrontend", "-gline-tables-only",
// "-Xfrontend", "-disable-stack-protector",
// "-Xfrontend", "-function-sections",
// "-Xfrontend", "-gline-tables-only",
"-Xcc", "-DTARGET_EXTENSION",
"-Xcc", "-I", "-Xcc", "\(gccIncludePrefix)/include",
"-Xcc", "-I", "-Xcc", "\(gccIncludePrefix)/include-fixed",
Expand All @@ -27,17 +26,19 @@ let swiftSettingsSimulator: [SwiftSetting] = [
let package = Package(
name: "SwiftBreak",
products: [
.library(name: "SwiftBreak", targets: ["SwiftBreak"])
.library(name: "SwiftBreak", type: .dynamic, targets: ["SwiftBreak"]),
],
dependencies: [
.package(path: "../..")
],
targets: [
.target(
name: "SwiftBreak",
dependencies: [
.product(name: "Playdate", package: "swift-playdate-examples")
],
swiftSettings: swiftSettingsSimulator)
name: "SwiftBreak",
dependencies: [
.product(name: "Playdate", package: "swift-playdate-examples"),
],
path: "Sources",
swiftSettings: swiftSettingsSimulator
)
],
swiftLanguageVersions: [.version("6"), .v5])
4 changes: 3 additions & 1 deletion Examples/SwiftBreak/Sources/Entry.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ nonisolated(unsafe) var game = Game()

@_cdecl("update")
func update(pointer: UnsafeMutableRawPointer?) -> Int32 {
game.updateGame()
if game.updateGame() {
Sprite.updateAndDrawSprites()
}
return 1
}

Expand Down
9 changes: 5 additions & 4 deletions Examples/SwiftBreak/Sources/Game.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,10 @@ struct Game: ~Copyable {
}

extension Game {
mutating func updateGame() {
mutating func updateGame() -> Bool {
let pushed = System.buttonState.pushed
switch self.state {
let state = self.state
switch state {
case .loading:
if pushed == .a {
self.startNewGame()
Expand Down Expand Up @@ -89,8 +90,8 @@ extension Game {
self.startNewGame()
}
}

Sprite.updateAndDrawSprites()
return true
}

mutating func startNewGame() {
Expand Down
4 changes: 4 additions & 0 deletions Examples/SwiftBreak/simbuild
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
swift build
cp .build/arm64-apple-macosx/debug/libSwiftBreak.dylib Source/pdex.dylib
${HOME}/Developer/PlaydateSDK/bin/pdc Source SwiftBreak.pdx
11 changes: 7 additions & 4 deletions Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ guard let home = Context.environment["HOME"] else {
}

let swiftSettingsSimulator: [SwiftSetting] = [
.enableExperimentalFeature("Embedded"),
// .enableExperimentalFeature("Embedded"),
.unsafeFlags([
"-g", "-Onone",
"-Xfrontend", "-Onone",
"-Xfrontend", "-disable-objc-interop",
"-Xfrontend", "-disable-stack-protector",
"-Xfrontend", "-function-sections",
"-Xfrontend", "-gline-tables-only",
// "-Xfrontend", "-gline-tables-only",
"-Xcc", "-DTARGET_EXTENSION",
"-Xcc", "-I", "-Xcc", "\(gccIncludePrefix)/include",
"-Xcc", "-I", "-Xcc", "\(gccIncludePrefix)/include-fixed",
Expand All @@ -26,6 +28,7 @@ let swiftSettingsSimulator: [SwiftSetting] = [
let cSettingsSimulator: [CSetting] = [
.unsafeFlags([
"-DTARGET_EXTENSION",
"-nostdlib",
"-I", "\(gccIncludePrefix)/include",
"-I", "\(gccIncludePrefix)/include-fixed",
"-I", "\(gccIncludePrefix)/../../../../arm-none-eabi/include",
Expand All @@ -36,8 +39,8 @@ let cSettingsSimulator: [CSetting] = [
let package = Package(
name: "swift-playdate-examples",
products: [
.library(name: "Playdate", targets: ["Playdate"]),
.library(name: "CPlaydate", targets: ["CPlaydate"]),
.library(name: "Playdate", type: .static, targets: ["Playdate"]),
.library(name: "CPlaydate", type: .static, targets: ["CPlaydate"]),
],
targets: [
.target(
Expand Down
18 changes: 11 additions & 7 deletions Sources/Playdate/Sprite.swift
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,11 @@ extension Sprite {

let rawCollisions = spriteAPI.checkCollisions.unsafelyUnwrapped(self.pointer, goalX, goalY, &actualX, &actualY, &count)
let collisions = UnsafeBufferPointer(start: rawCollisions, count: Int(count))
defer { rawCollisions?.deallocate() }
defer {
if let rawCollisions {
System.free(pointer: rawCollisions)
}
}
return body(actualX, actualY, collisions)
}

Expand Down Expand Up @@ -397,7 +401,7 @@ extension Sprite {

let rawCollisions = spriteAPI.moveWithCollisions.unsafelyUnwrapped(self.pointer, goalX, goalY, &actualX, &actualY, &count)
let collisions = UnsafeBufferPointer(start: rawCollisions, count: Int(count))
defer { rawCollisions?.deallocate() }
defer { rawCollisions.flatMap(System.free) }
return body(actualX, actualY, collisions)
}

Expand All @@ -407,7 +411,7 @@ extension Sprite {
var count: Int32 = 0
let rawSprites = spriteAPI.querySpritesAtPoint.unsafelyUnwrapped(x, y, &count)
let sprites = UnsafeBufferPointer(start: rawSprites, count: Int(count))
defer { rawSprites?.deallocate() }
defer { rawSprites.flatMap(System.free) }
return body(sprites)
}

Expand All @@ -417,7 +421,7 @@ extension Sprite {
var count: Int32 = 0
let rawSprites = spriteAPI.querySpritesInRect.unsafelyUnwrapped(x, y, width, height, &count)
let sprites = UnsafeBufferPointer(start: rawSprites, count: Int(count))
defer { rawSprites?.deallocate() }
defer { rawSprites.flatMap(System.free) }
return body(sprites)
}

Expand All @@ -427,7 +431,7 @@ extension Sprite {
var count: Int32 = 0
let rawSprites = spriteAPI.querySpritesAlongLine.unsafelyUnwrapped(x1, y1, x2, y2, &count)
let sprites = UnsafeBufferPointer(start: rawSprites, count: Int(count))
defer { rawSprites?.deallocate() }
defer { rawSprites.flatMap(System.free) }
return body(sprites)
}

Expand All @@ -439,7 +443,7 @@ extension Sprite {
var count: Int32 = 0
let rawSprites = spriteAPI.querySpriteInfoAlongLine.unsafelyUnwrapped(x1, y1, x2, y2, &count)
let sprites = UnsafeBufferPointer(start: rawSprites, count: Int(count))
defer { rawSprites?.deallocate() }
defer { rawSprites.flatMap(System.free) }
return body(sprites)
}

Expand All @@ -452,7 +456,7 @@ extension Sprite {
var count: Int32 = 0
let rawSprites = spriteAPI.allOverlappingSprites.unsafelyUnwrapped(&count)
let sprites = UnsafeBufferPointer(start: rawSprites, count: Int(count))
defer { rawSprites?.deallocate() }
defer { rawSprites.flatMap(System.free) }
return body(sprites)
}
}
Expand Down
12 changes: 12 additions & 0 deletions Sources/Playdate/System.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,4 +151,16 @@ extension System {
public static func clearICache() {
systemAPI.clearICache.unsafelyUnwrapped()
}

/// Reallocates a pointer.
/// If `pointer == nil`, and `size > 0`, allocates a new pointer.
/// If `pointer != nil` and `size == 0` deallocates the pointer.
public static func realloc(pointer: UnsafeMutableRawPointer?, size: size_t) -> UnsafeMutableRawPointer? {
systemAPI.realloc.unsafelyUnwrapped(pointer, size)
}

/// Frees a pointer previously allocated with `realloc()`.
public static func free<T>(pointer: UnsafeMutablePointer<T>) {
let _ = systemAPI.realloc.unsafelyUnwrapped(pointer, 0)
}
}