# Byte-sized Swift: Building Tiny Games for the Playdate

**URL:** <https://forums.swift.org/t/byte-sized-swift-building-tiny-games-for-the-playdate/70615>\
**Category:** Community Showcase\
**Tags:** swiftorg-blog, embedded\
**Created:** [March 12, 2024, 5:06pm UTC](https://forums.swift.org/t/byte-sized-swift-building-tiny-games-for-the-playdate/70615 "2024-03-12T17:06:29Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![rauhul](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rauhul/32/35207_2.png) [@rauhul](https://forums.swift.org/u/rauhul)\
**Post date:** [March 12, 2024, 5:06pm UTC](https://forums.swift.org/t/byte-sized-swift-building-tiny-games-for-the-playdate/70615/1 "2024-03-12T17:06:29Z")

</div>

Hi all, I wrote a [blog post](https://www.swift.org/blog/byte-sized-swift-tiny-games-playdate/) about my experience bringing Swift to the [Playdate](https://play.date) using the embedded language mode.

Please feel free to discuss the blog post here.

---

<div class="post-metadata">

**Author:** ![machineko](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/machineko/32/27263_2.png) [@machineko](https://forums.swift.org/u/machineko)\
**Post date:** [March 12, 2024, 7:25pm UTC](https://forums.swift.org/t/byte-sized-swift-building-tiny-games-for-the-playdate/70615/2 "2024-03-12T19:25:16Z")

</div>

Superb project!

I love how Swift is evolving to be true general purpose language in the past few years.

---

<div class="post-metadata">

**Author:** ![Milkingcowboy](https://avatars.discourse-cdn.com/v4/letter/m/8491ac/32.png) [@Milkingcowboy](https://forums.swift.org/u/Milkingcowboy)\
**Post date:** [March 12, 2024, 7:55pm UTC](https://forums.swift.org/t/byte-sized-swift-building-tiny-games-for-the-playdate/70615/3 "2024-03-12T19:55:51Z")

</div>

This is great and all. Thank you.  
Now I will try to build and release a game on Playdate.  
I hope they will support Swift directly.

---

<div class="post-metadata">

**Author:** ![wadetregaskis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/wadetregaskis/32/8494_2.png) [@wadetregaskis](https://forums.swift.org/u/wadetregaskis)\
**Post date:** [March 12, 2024, 8:27pm UTC](https://forums.swift.org/t/byte-sized-swift-building-tiny-games-for-the-playdate/70615/4 "2024-03-12T20:27:43Z")

</div>

Very cool.

### Random numbers

I noticed [you do](https://github.com/apple/swift-playdate-examples/blob/c0c88f1da6c2b60d46e83e4dbccfa49c6730edb3/Examples/Life/Sources/Game.swift#L38) `UInt8(rand() & 0xff)` rather than `UInt8.random(in: .min ... .max)`. Is that just a stylistic choice?

### Why `UnsafeMutablePointer`?

Is there a performance concern with using `UnsafeMutableBufferPointer` instead of `UnsafeMutablePointer` for things like [accessing the frame buffer](https://github.com/apple/swift-playdate-examples/blob/c0c88f1da6c2b60d46e83e4dbccfa49c6730edb3/Examples/Life/Sources/Game.swift#L32C15-L32C35)? Or is there a different reason for not having the bounds checking?

### Bit vectors and matrices

It might be an interesting thought exercise to think about what it would take to be able to use [`BitArray` from `swift-collections`](https://swiftpackageindex.com/apple/swift-collections/1.1.0/documentation/bitcollections/bitarray) instead of manual pointers, for things like the frame buffer. Aside from potential questions about binary size and sufficient inlining / dead-code-stripping for a relatively large [for Embedded] package like `swift-collections`, one obvious limitation is that `BitArray` currently doesn't let you provide an existing memory allocation for its backing.

(there's also the aspect of `BitArray` being length-mutable, which is logically incompatible with use over things like frame buffers, but one can imagine either a fixed-length variant or pragmatically just not touching the length in practice)

I wonder if an `Array` of `BitArray`s could ultimately inline down to zero overhead, to completely eliminate all the manual index calculation and bit-shifting?

### Unsafe\*Ptr ergonomics / C bridging challenges

The [Swift.org](http://Swift.org) post notes some lingering challenges, like dealing with nullable-but-not-really pointers from the C API. But another annoyance in using pointers in Swift is that you end up with `.pointee.` everywhere. I wonder what your thoughts are on that, and on ideas like having an `->` operator instead? Or using [dynamic member lookup with keypaths](https://www.swiftbysundell.com/tips/combining-dynamic-member-lookup-with-key-paths/) for statically safe `.`?

---

<div class="post-metadata">

**Author:** ![ben-cohen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ben-cohen/32/296_2.png) [@ben-cohen](https://forums.swift.org/u/ben-cohen)\
**Post date:** [March 12, 2024, 9:43pm UTC](https://forums.swift.org/t/byte-sized-swift-building-tiny-games-for-the-playdate/70615/5 "2024-03-12T21:43:54Z")

</div>

> [@wadetregaskis](#):
>
> I noticed [you do](https://github.com/apple/swift-playdate-examples/blob/c0c88f1da6c2b60d46e83e4dbccfa49c6730edb3/Examples/Life/Sources/Game.swift#L38) `UInt8(rand() & 0xff)` rather than `UInt8.random(in: .min ... .max)`. Is that just a stylistic choice?

Embedded swift does not currently support `Swift.random` stuff. It would be nice to, but it's tricky because quality of it would vary a lot by platform, and when a good source of randomness is available, initializing it might have a significant cost. So there is work to be done to both come up with policies for it, and to implement it per platform.

---

<div class="post-metadata">

**Author:** ![scanon](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/scanon/32/2445_2.png) [@scanon](https://forums.swift.org/u/scanon)\
**Post date:** [March 12, 2024, 9:45pm UTC](https://forums.swift.org/t/byte-sized-swift-building-tiny-games-for-the-playdate/70615/6 "2024-03-12T21:45:07Z")

</div>

> [@ben-cohen](#):
>
> Embedded swift does not currently support `Swift.random` stuff. It would be nice to, but it's tricky because quality of it would vary a lot by platform, and when a good source of randomness is available, initializing it might have a significant cost. So there is work to be done to both come up with policies for it, and to implement it per platform.

We'll have a proposal coming soon-ish to address this.

---

<div class="post-metadata">

**Author:** ![Finagolfin](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/finagolfin/32/34388_2.png) [@Finagolfin](https://forums.swift.org/u/Finagolfin)\
**Post date:** [March 13, 2024, 2:35am UTC](https://forums.swift.org/t/byte-sized-swift-building-tiny-games-for-the-playdate/70615/7 "2024-03-13T02:35:35Z")

</div>

Nice, your submission made the front page of the orange site. Something like `-Xcc --sysroot -Xcc $GCC_LIB/gcc/arm-none-eabi/9.2.1/ -Xcc -I -Xcc include-fixed/ -Xcc -I -Xcc ../../../../arm-none-eabi/include/` might work better: try it and see. Gah, [I hate that Swift code generation is set through `-Xcc` options](https://forums.swift.org/t/targeting-specific-microarchitectures/69505/17) like `-Xcc -mcpu=cortex-m7 -Xcc -mfloat-abi=hard -Xcc -mfpu=fpv5-sp-d16 -Xcc -mthumb`.

---

<div class="post-metadata">

**Author:** ![rauhul](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rauhul/32/35207_2.png) [@rauhul](https://forums.swift.org/u/rauhul)\
**Post date:** [March 13, 2024, 2:55am UTC](https://forums.swift.org/t/byte-sized-swift-building-tiny-games-for-the-playdate/70615/8 "2024-03-13T02:55:07Z")

</div>

> [@wadetregaskis](#):
>
> ### Random numbers

As noted by Ben and Steve, this is a _current_ limitation of embedded swift, but won't be forever!

> [@wadetregaskis](#):
>
> ### Why `UnsafeMutablePointer`?

I think I could use a buffer pointer here, but as noted later in the post the cost of redundant null checks actually adds up on this platform and similarly the bounds checks might have an appreciable effect on Conways performance.

> [@wadetregaskis](#):
>
> ### Bit vectors and matrices

I didn't consider using other packages because I was building with makefiles. I also am not sure if that package uses features not available in embedded mode.

> [@wadetregaskis](#):
>
> ### Unsafe\*Ptr ergonomics / C bridging challenges

I actually tried to make this, but couldn't get it to work with function pointers! Have you had success with dynamic member look up and closures/function pointers?

---

<div class="post-metadata">

**Author:** ![rauhul](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rauhul/32/35207_2.png) [@rauhul](https://forums.swift.org/u/rauhul)\
**Post date:** [March 13, 2024, 3:20am UTC](https://forums.swift.org/t/byte-sized-swift-building-tiny-games-for-the-playdate/70615/9 "2024-03-13T03:20:01Z")

</div>

> [@Finagolfin](#):
>
> Nice, your submission made the front page of the orange site. Something like `-Xcc --sysroot -Xcc $GCC_LIB/gcc/arm-none-eabi/9.2.1/ -Xcc -I -Xcc include-fixed/ -Xcc -I -Xcc ../../../../arm-none-eabi/include/` might work better: try it and see.

I didn't consider setting the sysroot, good idea!

> [@Finagolfin](#):
>
> Gah, [I hate that Swift code generation is set through `-Xcc` options](https://forums.swift.org/t/targeting-specific-microarchitectures/69505/17) like `-Xcc -mcpu=cortex-m7 -Xcc -mfloat-abi=hard -Xcc -mfpu=fpv5-sp-d16 -Xcc -mthumb`.

I also dislike this, I'm not sure how much reinventing the wheel we should do on the Swift side tho.

---

<div class="post-metadata">

**Author:** ![Megus](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/megus/32/34446_2.png) [@Megus](https://forums.swift.org/u/Megus)\
**Post date:** [March 13, 2024, 3:51am UTC](https://forums.swift.org/t/byte-sized-swift-building-tiny-games-for-the-playdate/70615/10 "2024-03-13T03:51:33Z")

</div>

First of all, thanks a lot for this work!

I've tried building the examples and I'm getting this error:

`<unknown>:0: error: unable to load standard library for target 'armv7em-none-none-eabi'`

Any clues how I could fix it?

---

<div class="post-metadata">

**Author:** ![Douglas\_Gregor](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/douglas_gregor/32/1362_2.png) [@Douglas\_Gregor](https://forums.swift.org/u/Douglas_Gregor)\
**Post date:** [March 13, 2024, 4:11am UTC](https://forums.swift.org/t/byte-sized-swift-building-tiny-games-for-the-playdate/70615/11 "2024-03-13T04:11:37Z")

</div>

> [@Megus](#):
>
> I've tried building the examples and I'm getting this error:
> 
> `<unknown>:0: error: unable to load standard library for target 'armv7em-none-none-eabi'`
> 
> Any clues how I could fix it?

My guess is that `TOOLCHAINS` isn't getting set to the right toolchain identifier, so the `swiftc` you're getting isn't from the downloaded toolchain. You can check that you're getting the right `swiftc` by running this from the command line:

```swift
TOOLCHAINS=<the identifier you're using> xcrun -f swiftc

```

and it should point into `/Library/Developer/Toolchains/<the snapshot toolchain you downloaded>/usr/bin/swiftc`

Doug

---

<div class="post-metadata">

**Author:** ![Max\_Desiatov](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/max_desiatov/32/20654_2.png) [@Max\_Desiatov](https://forums.swift.org/u/Max_Desiatov)\
**Post date:** [March 13, 2024, 8:22am UTC](https://forums.swift.org/t/byte-sized-swift-building-tiny-games-for-the-playdate/70615/12 "2024-03-13T08:22:52Z")

</div>

> [@Douglas\_Gregor](#):
>
> My guess is that `TOOLCHAINS` isn't getting set to the right toolchain identifier, so the `swiftc` you're getting isn't from the downloaded toolchain.

I noticed that `TOOLCHAINS` has no effect when `xcode-select -p` point to CLI tools instead of an actual Xcode installation, which could be the case here.

---

<div class="post-metadata">

**Author:** ![grid](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/grid/32/31640_2.png) [@grid](https://forums.swift.org/u/grid)\
**Post date:** [March 13, 2024, 3:41pm UTC](https://forums.swift.org/t/byte-sized-swift-building-tiny-games-for-the-playdate/70615/13 "2024-03-13T15:41:23Z")

</div>

I was able to get the examples building and running in Panic's Nova (following all the excellent instructions/documentation – thanks for that! ❤), but even though both projects build and run, there are a fair amount of errors displayed in the code editor.

 ![Screenshot 2024-03-13 at 10.30.35 AM](https://global.discourse-cdn.com/swift/original/3X/9/6/96c5218713dcae1cb54fbb576f265fd7a6df0a82.jpeg)

Any idea what's going on there, or how to fix them? I haven't actually used Swift in Nova before (I've only used it for playdate development before), so I'm not even sure how to debug properly.

---

<div class="post-metadata">

**Author:** ![rauhul](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rauhul/32/35207_2.png) [@rauhul](https://forums.swift.org/u/rauhul)\
**Post date:** [March 13, 2024, 5:55pm UTC](https://forums.swift.org/t/byte-sized-swift-building-tiny-games-for-the-playdate/70615/14 "2024-03-13T17:55:42Z")

</div>

Thanks for bringing this up, it prompted me to figure out a solution with help from @ahoppen and @bnbarham!

The issue is that the build and the editor actually derive their build settings and complier selection from different places!

| | Build | IDE Support |
| --- | --- | --- |
| **Toolchain** | Makefile | Icarus Extension |
| **Build Settings** | Makefile | SourceKit + SwiftPM |

The toolchain selection is easier to fix, you need to configure Icarus to use the nightly toolchain:

 ![Screenshot 2024-03-13 at 10.35.12 AM](https://global.discourse-cdn.com/swift/original/3X/5/d/5d853cb8b1c254386078c6ea8fe968303c4542fd.jpeg)

The next issue to fix Package.swift to enable Swift 6 mode (if available) to match the Makefile:

```diff
diff --git a/Examples/SwiftBreak/Package.swift b/Examples/SwiftBreak/Package.swift
index 607f9cb..0319aaa 100644
--- a/Examples/SwiftBreak/Package.swift
+++ b/Examples/SwiftBreak/Package.swift
@@ -39,4 +39,5 @@ let package = Package(
         .product(name: "Playdate", package: "swift-playdate-examples")
       ],
       swiftSettings: swiftSettingsSimulator)
- ])
+ ],
+ swiftLanguageVersions: [.version("6"), .v5])

```

We then need to perform an initial release build with SwiftPM, so the Playdate module is available:

```console
$ cd swift-playdate-examples/Examples/SwiftBreak
$ swift build -c release

```

And lastly we need to configure sourcekit-lsp launched via Icarus to use the release folder. The issue here is that Icarus doesn't have any flags for this, so the solution is to symlink the `release` folder to `debug` to trick things into working.

```console
$ cd swift-playdate-examples/Examples/SwiftBreak/.build/arm64-apple-macosx
$ ln -s release debug

```

I will update the instructions in the repo with this information!

---

<div class="post-metadata">

**Author:** ![ahoppen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ahoppen/32/36046_2.png) [@ahoppen](https://forums.swift.org/u/ahoppen)\
**Post date:** [March 14, 2024, 4:01pm UTC](https://forums.swift.org/t/byte-sized-swift-building-tiny-games-for-the-playdate/70615/15 "2024-03-14T16:01:46Z")

</div>

I just want to add that if you are using Visual Studio Code, it is not necessary to add the `debug` -\> `release` symlink. Instead you can tell sourcekit-lsp to search the release build folder by adding `--configuration` and `release` to the `Sourcekit-lsp: Server Arguments` in your settings (Cmd-Shift-P -\> Open Settings (UI)) or by adding the following to your `settings.json`

```json
"swift.sourcekit-lsp.serverArguments": [
  "--configuration",
  "release"
],

```

---

<div class="post-metadata">

**Author:** ![grid](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/grid/32/31640_2.png) [@grid](https://forums.swift.org/u/grid)\
**Post date:** [March 14, 2024, 4:20pm UTC](https://forums.swift.org/t/byte-sized-swift-building-tiny-games-for-the-playdate/70615/16 "2024-03-14T16:20:37Z")

</div>

Thanks for continuing to work on this!

My red error indicators went away after the first step (just switching Icarus to use the Swift Trunk Toolchain), but when trying to build for release, I ran into a bunch of errors. (I could post them if it would be helpful.) Here's just the last one, (which is the only one listed as fatal):

```swift
/Users/grid/Developer/projects/externals/playdate/swift-playdate-examples/Sources/Playdate/System.swift:1:1: error: Access level on imports require '-enable-experimental-feature AccessLevelOnImport'
public import CPlaydate
^ ~~~~~~
error: fatalError

```

What does that step enable? Debugging? Auto-completion? I have noticed that I can't CMD-click on `System.buttonState.pushed`, for example, so it's not picking up the symbols correctly yet, I don't think.

---

<div class="post-metadata">

**Author:** ![rauhul](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/rauhul/32/35207_2.png) [@rauhul](https://forums.swift.org/u/rauhul)\
**Post date:** [March 17, 2024, 1:09am UTC](https://forums.swift.org/t/byte-sized-swift-building-tiny-games-for-the-playdate/70615/17 "2024-03-17T01:09:45Z")

</div>

Could you double check you have set the `TOOLCHAINS` environment variable prior to running `swift build -c release`

---

<div class="post-metadata">

**Author:** ![grid](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/grid/32/31640_2.png) [@grid](https://forums.swift.org/u/grid)\
**Post date:** [March 17, 2024, 4:12pm UTC](https://forums.swift.org/t/byte-sized-swift-building-tiny-games-for-the-playdate/70615/18 "2024-03-17T16:12:03Z")

</div>

Ahhh, I was following the instructions in your comment above, where it says to run

```swift
$ swift build -c release

```

...and not the ones in the (updated) documentation, where it says to run:

```swift
$ TOOLCHAINS="org.swift.59202312211a" swift build -c release

```

...which did work for me. (There was a warning, but it compiled fine.)

---

<div class="post-metadata">

**Author:** ![Datagram](https://avatars.discourse-cdn.com/v4/letter/d/7cd45c/32.png) [@Datagram](https://forums.swift.org/u/Datagram)\
**Post date:** [March 19, 2024, 5:35pm UTC](https://forums.swift.org/t/byte-sized-swift-building-tiny-games-for-the-playdate/70615/19 "2024-03-19T17:35:43Z")

</div>

It's very exciting to see Swift's good progress on multi-platform, cross-compiling, and supporting embedded environments with limited resources.

Definitely in the right direction!

---

<div class="post-metadata">

**Author:** ![ben-cohen](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/ben-cohen/32/296_2.png) [@ben-cohen](https://forums.swift.org/u/ben-cohen)\
**Post date:** [March 20, 2024, 2:46pm UTC](https://forums.swift.org/t/byte-sized-swift-building-tiny-games-for-the-playdate/70615/20 "2024-03-20T14:46:22Z")

</div>

Hey folks,

As of last night's (the 19th) nightly toolchain, the standard library now has support for noncopyable generics on its pointer types and on `Optional` (gated under the `-enable-experimental-feature NoncopyableGenerics`.

[Here is a PR](https://github.com/apple/swift-playdate-examples/pull/13) I put up on the Swift Playdate sample, applying this feature to the SwiftBreak game. This adds a very basic non-copyable array type, and then uses that to store the brick sprites.

This feature is very relevant to the embedded world, as it can dramatically reduce overhead otherwise needed from Swift's reference-counting approach to memory ownership.

Previously the `Sprite` had to be represented as an enum (indicating ownership) plus a class (to handle deallocation). The noncopyable version of that pattern is significantly simpler: it just holds the underlying opaque pointer (the playdate SDK's sprite handle) plus a bool to track ownership, then uses a `struct` with a `deinit` to free it if necessary.

This PR also adds a very basic fixed-size array type to hold the bricks. This was done for two reasons. The first is that `Swift.Array` does not yet support non-copyable types. But more importantly, the standard array type has more overhead than is necessary for all uses, including in this game. Tracking the buffer with reference counting, accounting for CoW uses, and automatically growing the array when needed, adds to binary size. For most uses cases, these costs are negligible so not a problem. But in an embedded context, they can make a significant difference.

The net effect of these changes is that binary size for the SwiftBreak game drops about 14%. Around half of this is from the simpler array type, and about half from the simpler sprite type.

If you're interested in the evolution of this feature, check out the ongoing [review thread](https://forums.swift.org/t/se-0427-noncopyable-generics/70525).

> <https://github.com/apple/swift-playdate-examples/pull/13>
>
> Now that support for \`UnsafeMutablePointer\<Pointee: ~Copyable\>\` has landed in th…e standard library (gated under the \`-enable-experimental-feature NoncopyableGenerics\`), we can use it to simplify the \`Sprite\` type.
> 
> This converts the \`Sprite\` wrapper for the Playdate SDK from an enum+class to track ownership to a non-copyable struct that can have a \`deinit\` and needs no reference counting. It also adds a very minimal array type to hold the bricks (since the regular \`Array\` type cannot yet hold non-copyable types).
> 
> This drops binary size by about 14%. Half of this is from simplifying the sprite type, and half is from the simpler array type.

[Next page](https://forums.swift.org/t/byte-sized-swift-building-tiny-games-for-the-playdate/70615.md?page=2)
