I’ve been working on Playdate SDK for Zig based off of GitHub - DanB91/Zig-Playdate-Template: Starter code for a Playdate program written in Zig
I’ll preface this by saying that DanB91 has done all the preliminary work to get Zig running on the Playdate. Cross compiling for novel targets isn’t my area of expertise.
I’ve been running into some memory allocation issues and weird panics when running in the Playdate Simulator. For example, a call to math.Vector2.toVector2i()
causes a Zig panic in @intFromFloat
.
Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0 libsystem_kernel.dylib 0x18b58311c __pthread_kill + 8
1 libsystem_pthread.dylib 0x18b5bacc0 pthread_kill + 288
2 libsystem_c.dylib 0x18b4caa50 abort + 180
3 pdex.dylib 0x118850c90 posix.abort + 12 (posix.zig:689)
4 pdex.dylib 0x118848964 debug.panicImpl + 148 (debug.zig:501)
5 pdex.dylib 0x118844914 builtin.default_panic + 104 (builtin.zig:845)
6 pdex.dylib 0x118853ea8 math.Vector2.toVector2i + 252 (math.zig:44)
7 pdex.dylib 0x11884e698 main.drawLines + 112 (main.zig:190)
8 pdex.dylib 0x11884e8b4 main.drawAsteroid + 412 (main.zig:302)
9 pdex.dylib 0x11884efa0 main.render + 1068 (main.zig:712)
10 pdex.dylib 0x1188468e0 main.update_and_render + 284 (main.zig:870)
11 Playdate Simulator 0x104523f4c pd_update + 504
12 Playdate Simulator 0x104577450 -[PCPlaydateSimulator update] + 508
13 Foundation 0x18c7bbfe8 __NSThreadPerformPerform + 264
14 CoreFoundation 0x18b697d28 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 28
15 CoreFoundation 0x18b697cbc __CFRunLoopDoSource0 + 176
16 CoreFoundation 0x18b697a2c __CFRunLoopDoSources0 + 244
17 CoreFoundation 0x18b69661c __CFRunLoopRun + 828
18 CoreFoundation 0x18b695c2c CFRunLoopRunSpecific + 608
19 HIToolbox 0x195bee448 RunCurrentEventLoopInMode + 292
20 HIToolbox 0x195bee284 ReceiveNextEventCommon + 648
21 HIToolbox 0x195bedfdc _BlockUntilNextEventMatchingListInModeWithFilter + 76
22 AppKit 0x18ee70f90 _DPSNextEvent + 660
23 AppKit 0x18f644b94 -[NSApplication(NSEventRouting) _nextEventMatchingEventMask:untilDate:inMode:dequeue:] + 716
24 AppKit 0x18ee6443c -[NSApplication run] + 476
25 AppKit 0x18ee3b708 NSApplicationMain + 880
26 dyld 0x18b241058 start + 2224
Here is the function in question:
pub fn toVector2i(self: @This()) Vector2i {
const x = @round(self.x);
const y = @round(self.y);
const xi: i32 = @intFromFloat(x);
const yi: i32 = @intFromFloat(y);
return Vector2i.init(xi, yi);
}
Secondly, I’ve been trying to implement a std.mem.Allocator
using the playdate.system.realloc
function. Allocating seems to work, but resizing and freeing just crashes the simulator.
My naive attempt at an allocator can be found here: Zig-Playdate-SDK/src/sdk/mem.zig at main · deevus/Zig-Playdate-SDK · GitHub
SDK: GitHub - deevus/Zig-Playdate-SDK
Game: GitHub - deevus/zigsteroids-playdate: asteroids in zig