Zig bindings for orca (still WIP) #140

Open
rdunnington wants to merge 24 commits from zig_ffi into main
1 changed files with 67 additions and 10 deletions
Showing only changes of commit 47cba86c03 - Show all commits

View File

@ -1550,6 +1550,62 @@ pub const clip = struct {
pub const top = oc_clip_top;
};
//------------------------------------------------------------------------------------------
// [GRAPHICS]: path primitives
//------------------------------------------------------------------------------------------
const PathEltType = enum(c_uint) {
Move,
Line,
Quadratic,
Cubic,
};
const PathElt = extern struct {
type: PathEltType,
p: [3]Vec2,
};
const PathDescriptor = extern struct {
start_index: u32,
count: u32,
start_point: Vec2,
};
const Attributes = extern struct {
width: f32,
tolerance: f32,
color: Color,
joint: JointType,
max_joint_excursion: f32,
cap: CapType,
font: Font,
font_size: f32,
image: Image,
src_region: Rect,
transform: Mat2x3,
clip: Rect,
};
const PrimitiveCmd = enum(c_uint) {
Fill,
Stroke,
Jump,
};
const Primitive = extern struct {
cmd: PrimitiveCmd,
attributes: Attributes,
data: extern union {
path: PathDescriptor,
rect: Rect,
jump: u32,
},
};
//------------------------------------------------------------------------------------------
// [GRAPHICS]: resources
//------------------------------------------------------------------------------------------
@ -1567,15 +1623,14 @@ pub const Surface = extern struct {
extern fn oc_surface_contents_scaling(surface: Surface) Vec2;
extern fn oc_surface_bring_to_front(surface: Surface) void;
extern fn oc_surface_send_to_back(surface: Surface) void;
// TODO
// extern fn oc_surface_render_commands(
// surface: Surface,
// color: Color,
// primitive_count: u32,
// primitives: [*]Primitive,
// elt_count: u32,
// elements: [*]PathElt,
// ) void;
extern fn oc_surface_render_commands(
surface: Surface,
color: Color,
primitive_count: u32,
primitives: [*]Primitive,
elt_count: u32,
elements: [*]PathElt,
) void;
pub const nil = oc_surface_nil;
pub const isNil = oc_surface_is_nil;
ilidemi marked this conversation as resolved Outdated

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

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 into log? If so, I think that makes sense.

You mean like putting File/FileType/IoReq/etc into a struct named `io`, kind of like how the log stuff is namespaced into `log`? 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.

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.

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 and oc_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?

I am still confused, `oc_file_size` and `oc_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:

_ = oc.File.size(file);
_ = file.size();

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:

var file1: File = File.openWithRequest(path, rights, flags); // OK
var file2: File = file.openWithRequest(path, rights, flags); // Compile error saying that File is not a Str8

Hopefully that clears things up?

In Zig, calling funtions on structs is just syntax sugar for passing it as the first argument to the function. So these are equivalent: ``` _ = oc.File.size(file); _ = file.size(); ``` 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: ``` var file1: File = File.openWithRequest(path, rights, flags); // OK var file2: File = file.openWithRequest(path, rights, flags); // Compile error saying that File is not a Str8 ``` Hopefully that clears things up?

So I guess to answer your question, directly, yes. :P

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!

And the latter one goes under File because it's a sort of a factory method. All makes sense, thank you for the explanation!
@ -1587,7 +1642,9 @@ pub const Surface = extern struct {
pub const contentsScaling = oc_surface_contents_scaling;
pub const bringToFront = oc_surface_bring_to_front;
pub const sendToBack = oc_surface_send_to_back;
// pub const renderCommands = oc_surface_render_commands;
pub fn renderCommands(surface: Surface, color: Color, primitives: []Primitive, elements: []PathElt) void {
oc_surface_render_commands(surface, color, @intCast(primitives.len), primitives.ptr, @intCast(elements.len), elements.ptr);
}
};
pub const Image = extern struct {