Skip to content
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
71 changes: 44 additions & 27 deletions Examples/SwiftBreak/Sources/FixedArray.swift
Original file line number Diff line number Diff line change
@@ -1,35 +1,52 @@
struct FixedArray<Element: ~Copyable>: ~Copyable {
let count: Int
private let _buffer: UnsafeMutablePointer<Element>
import Builtin

init(count: Int, first: consuming Element, rest: (borrowing Element) -> Element) {
precondition(count > 0)
self.count = count
_buffer = .allocate(capacity: count)
for i in 1..<count {
(_buffer + i).initialize(to: rest(first))
}
_buffer.initialize(to: first)
}
@frozen
public struct Vector<let Count: Int, Element: ~Copyable>: ~Copyable {
private var storage: Builtin.FixedArray<Count, Element>
}

deinit {
_buffer.deinitialize(count: count)
_buffer.deallocate()
}
extension Vector where Element: ~Copyable {
init(first: @autoclosure ()->Element, rest valueForIndex: (borrowing Element) -> Element) {
storage = Builtin.emplace { rawPointer in
let first = first()
let base = UnsafeMutablePointer<Element>(rawPointer)
for i in 1..<Count {
(base + i).initialize(to: valueForIndex(first))
}
base.initialize(to: first)
}
}
}

extension FixedArray where Element: ~Copyable {
func forEach(_ body: (borrowing Element) -> Void) {
for i in 0..<self.count {
body((self._buffer + i).pointee)
extension Vector where Element: ~Copyable {
public subscript(i: Int) -> Element {
_read {
assert(i >= 0 && i < Count)
let rawPointer = Builtin.addressOfBorrow(self)
let base = UnsafePointer<Element>(rawPointer)
yield ((base + i).pointee)
}

_modify {
assert(i >= 0 && i < Count)
let rawPointer = Builtin.addressof(&self)
let base = UnsafeMutablePointer<Element>(rawPointer)
yield (&(base + i).pointee)
}
}

func forEach(_ body: (borrowing Element) -> Void) {
for i in 0..<Count {
body(self[i])
}
}
}

func enumerated(_ body: (Int, borrowing Element) -> Void) {
var i = 0
self.forEach {
body(i, $0)
i += 1
func enumerated(_ body: (Int, borrowing Element) -> Void) {
for i in 0..<Count {
body(i,self[i])
}
}
}

var count: Int { Count }
}

7 changes: 3 additions & 4 deletions Examples/SwiftBreak/Sources/Game.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ struct Sprites: ~Copyable {
var ball: Sprite
var paddle: Sprite
var gameOver: Sprite
var bricks: FixedArray<Sprite>
var bricks: Vector<40,Sprite>
}

struct ActiveGame {
var score: Int
var ballVelocity: Vector
var ballVelocity: Vec2
var bricksRemaining: Int
}

Expand All @@ -36,7 +36,6 @@ struct Game: ~Copyable {
Sprite.setupWalls()

// Start in loading state with 4 x 10 bricks.
let brick = Sprite.brick()
let splash = Sprite.splash()
splash.addSprite()
self.sprites =
Expand All @@ -46,7 +45,7 @@ struct Game: ~Copyable {
ball: .ball(),
paddle: .paddle(),
gameOver: .gameOver(),
bricks: .init(count: 40, first: brick) { $0.copy() })
bricks: .init(first: Sprite.brick()) { $0.copy() })
Sprite.drawSprites()
self.state = .loading
}
Expand Down
2 changes: 1 addition & 1 deletion Examples/SwiftBreak/Sources/Sprite+Game.swift
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ extension Sprite {
activeGame.bricksRemaining -= 1
}

var normal = Vector(collision.normal)
var normal = Vec2(collision.normal)

if otherSprite.tag == .paddle {
// compute placement of ball on paddle in domain -1 to 1.
Expand Down
10 changes: 5 additions & 5 deletions Examples/SwiftBreak/Sources/Vector.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import Playdate

typealias Vector = SIMD2<Float32>
typealias Vec2 = SIMD2<Float32>

extension Vector {
extension Vec2 {
init(_ collisionVector: CollisionVector) {
self = .init(Float32(collisionVector.x), Float32(collisionVector.y))
}
Expand All @@ -12,7 +12,7 @@ extension Vector {
}
}

extension Vector {
extension Vec2 {
func reflected(along normal: Self) -> Self {
self - (2 * (self • normal)) * normal
}
Expand All @@ -22,7 +22,7 @@ extension Vector {
}
}

extension Vector {
extension Vec2 {
func rotated(by theta: Float32) -> Self {
.init(
x: self.x * cosf(theta) + self.y * -sinf(theta),
Expand All @@ -35,7 +35,7 @@ extension Vector {
}

infix operator • : MultiplicationPrecedence
extension Vector {
extension Vec2 {
static func • (lhs: Self, rhs: Self) -> Float {
(lhs.x * rhs.x) + (lhs.y * rhs.y)
}
Expand Down
4 changes: 4 additions & 0 deletions Examples/swift.mk
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ SWIFT_FLAGS := \
-O \
-wmo -enable-experimental-feature Embedded \
-enable-experimental-feature NoncopyableGenerics \
-enable-experimental-feature ValueGenerics \
-enable-experimental-feature BuiltinModule \
-Xfrontend -disable-stack-protector \
-Xfrontend -function-sections \
-Xfrontend -disable-availability-checking \
-disable-experimental-parser-round-trip \
-swift-version 6 \
-Xcc -DTARGET_EXTENSION \
-module-cache-path build/module-cache \
Expand Down