Zig lang to code via the C API?

Can the Zig lang be used to code for the playdate?

I was taken in consideration to use C for coding on the playdate, but by C knowledge is from more then 10 years ago and was never pretty good. So while I was learning some of the basics in C, I've read about Zig lang and it seems the more modern approach to using C.

I couldn't really figure out how to use Zig to create a code which could be integrated and run with the playdate though. Is it possible?

If so how to set it up for vscode on Windows?

@DanB91 you mentioned using Zig, how could you set it up and run it on the playdate?
Also are you mixing it with Lua code or Zig only?

Blockquote
@DanB91 you mentioned using Zig, how could you set it up and run it on the playdate?
Also are you mixing it with Lua code or Zig only?

Nope! Got my software working with just Zig with a bit of assembly (though I rarely used std). I do not believe it is officially supported by Panic, but you can absolutely write your game in Zig. I released a Zig project template here: GitHub - DanB91/Zig-Playdate-Template: Starter code for a Playdate program written in Zig so feel free to use that! You shouldn't need any assembly if you use the template.

There are things to be aware of which is in the README including the fact that you only get 10KB of stack space, so it might be difficult to use some of the Zig std library.

Let me know if you run into any issues or have any questions!

3 Likes

Thanks Dan, I've made an inital setup on windows but building threw an error. I'll give it another try soon.

Do you know how I could combine lua and zig?
I'd assume one could build C compatible libs and call the functions from Lua code?

How do you manage only having 10kb stack memory?
How can I make I'm not going over that amount in a game loop?

I guess there is heap memory available?
I only coded with auto-CG languages... So I a memory management n00b :slight_smile:

Sorry for delay!

Ah, i think i know what the error might be. Zig removed a method I was using from the std, so I just put in a fix. Please pull the latest version of my template from Github!

I have not done any Lua programming on the Playdate unfortunately, but it looks like you would make C (or Zig functions, when using callconv(.C)) available via Inside Playdate with C

So with regard to memory management, yes, you'd be using the heap for large-sized (or even medium-sized) data structures. The sole API Playdate has for this is realloc(): Inside Playdate with C

So, when you need to allocate and free you'll call realloc(). On the face of it, the idea of manual memory management is that you allocate memory and use it, and then once you are done with it, you free it. It can be quite cumbersome to keep track of the objects you are done with and when to free them.

Luckily, that's not the end of the story. There are some nice techniques where you don't have to worry freeing every little allocation. Take a look at this series of articles by Ginger Bill (creator of the Odin programming language) about manual memory allocation: Memory Allocation Strategies - gingerBill Take note of arena allocators and free list allocators. There also videos on Youtube, especially about arena allocators.

In UPWARD, the workhorses are 3 arena allocators: one arena for global allocations that will last the entire game and is never freed, one arena that is reset every level, and one arena that is reset every frame. My arenas are preallocated by Playdate's realloc() API with a fixed size of memory.

https://github.com/DanB91/UPWARD-for-Playdate/blob/main/src/playdate_platform_main.zig#L63

I also use free lists for my doubly-linked list implementation.

I hope this is enough to get you started. Let me know if you have further questions!

Okay, I've pulled the latest changed and gave it another try.

Vscode shows me a error in the playdate_hardware_main.zig file, before I've pulled I got this error when running build.zig.

Now after pulling I'm getting: "error: no field or member function named 'standardOptimizeOption' in 'build.Builder'" on the first line. I guess because I'm only running zig build run.

Do I need to provide a builder via the command?
How to do that?

Errors:

Really sorry, I did not see your post until now for some reason. After SDK 2.0 dropped, it took me a while to get Zig working again on the Playdate, so the code was broken for a bit.

But now everything should be working with the latest version of Zig and the latest version of the Playdate SDK (I just tested it).

If you're still experimenting with Zig, please pull down the latest version of everything and give it a shot. Let me know how it goes

Is this 10 KB of stack size limit also valid for C or this is specific to Zig only?

Is this 10 KB of stack size limit also valid for C or this is specific to Zig only?

This is for C as well. @dave provided me this info some months ago when helping me out with debugging some issues I was having getting Zig working. Though not sure if anything changed with SDK 2.0