Zig bindings for orca (still WIP) #140
Loading…
Reference in New Issue
No description provided.
Delete Branch "zig_ffi"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
This is an incomplete set of zig bindings for orca. The included zig sample shows how to build and link the orca runtime with the zig app. It also shows how to use the orca bindings and provides a decent point of comparison to some of the C samples. It also proves that it works. ;)
Some of the API bindings are only partially complete, such as the oc_str8/16/32 functions, and others I haven't started at all, such as UI and GLES.
I tried to keep to zig paradigms and naming conventions, so in some cases functions are partially or entirely reimplemented in zig (such as a lot of the string code). Definitely welcome any feedback! And if we feel this PR isn't ready yet, we can wait for the bindings to be done before merging.
@rdunnington Before I merge this, can you put a Readme in the samples folder stating that it is a WIP and describing the current state of the bindings and the potential caveats for people wanting to test them at this early stage?
5d5f484744
to1190966b66
1190966b66
to6c4ddd298f
README added, let me know if you'd like me to change anything
Last few questions:
const
added tooc_font_create_from_memory
unicode range arg? Is this something Zig complains about?const
instead ofvar
. But if the function prototype takes a non-const that won't compile, which goes against the convention. The internals don't look like they change the array at all, so it seems like it was safe. Tbh I thought I had changed the C function as well to match but looks like I forgot. Is there a strong reason the C function doesn't take a const pointer?I don't find const really useful in C, and it has an annoying virality. Also sometimes clang emits warnings when passing non-const pointers to const parameters (eg for nested pointer types). I find the rationale for this very contrived (https://c-faq.com/ansi/constmismatch.html). So I rarely use const, except eg for taking string literals.
In the particular instance of the utf8 ranges stuff it's fine I guess, because it's a leaf function so the virality of const is limited, but I'd rather not have that creep up if we can avoid it.
I think your change makes things clearer, so I'll apply it directly and we'll just rebase on that.
@ -0,0 +1389,4 @@
// [GRAPHICS]: resources
//------------------------------------------------------------------------------------------
pub const Surface = extern struct {
Is there a reason for the function structs to be extern?
No, structs that only have functions don't need to be extern, but I think in this case
Surface
does since it's a handle and has some data inside@ -0,0 +1633,4 @@
// [FILE IO] basic API
//------------------------------------------------------------------------------------------
const File = extern struct {
Should non-core data structs be put into the namespace as well? Arena/Vec2 makes sense at the top level, File/IO/UI stuff seems like it can be contained
You mean like putting File/FileType/IoReq/etc into a struct named
io
, kind of like how the log stuff is namespaced intolog
? If so, I think that makes sense.What looked confusing to me was how functions are namespaced but the structs local to that module are not. Guess something like File is used for fonts and images too so it's a judgement call but something like FileDialog is pretty clear. I'm coming at this from UI angle where pretty much all of the structs are local.
Ohh I think I get it. File and Surface are legit structs here, just with a ton of methods. I thought they're more like function namespaces. Apologies for confusion.
I am still confused,
oc_file_size
andoc_file_open_with_request
are declared in the exact same way but former has File as the self argument and the latter doesn't. Does that translate into a method call and a namespaced function respectively?In Zig, calling funtions on structs is just syntax sugar for passing it as the first argument to the function. So these are equivalent:
And you can have other functions that don't pass the struct type as the first argument, you just have to call them explicitly on the struct type and not an instance, otherwise you'll get an error:
Hopefully that clears things up?
So I guess to answer your question, directly, yes. :P
And the latter one goes under File because it's a sort of a factory method. All makes sense, thank you for the explanation!
625301de56
tob14567753a
b14567753a
toe6feba1ccd
@MartinFouilleul There is one other case I ran into tonight where const is important to zig - all function inputs are always const. So if I want to pass one of them to a function, I'll need to make a non-const copy if the function doesn't handle it. This is another pretty compelling reason to have "const correctness" on the zig side.
I think it's probably fine if the zig function interfaces decorate types as const and the C versions don't...
oc_font_create_from_path
works just fine without it.I also made one more change you may want to look at in
memory.h
. Check outoc_arena_push_aligned()
. This was necessary because zig aborts in debug mode if you do an unaligned pointer type cast, which I ran into when implementingArena.pushType()
andArena.pushArray()
.Ha, I wanted to get to that eventually, but you beat me to it, nice! I'll just add this to main and rebase, so that this is separate from the zig-specific commits
e6feba1ccd
to9cab6031d0
9cab6031d0
toe6feba1ccd
e6feba1ccd
to9c4e2125c1