zig bindings: namespace clock/canvas functions

This commit is contained in:
Reuben Dunnington 2023-09-29 23:42:10 -04:00
parent 3e6cd07b34
commit 49213353fc
Signed by: rdunnington
GPG Key ID: 3D57C8938EA08E90
2 changed files with 186 additions and 202 deletions

View File

@ -36,9 +36,11 @@ export fn oc_on_init() void {
.Specials, .Specials,
}); });
font = oc.Font.createFromPath("/zig.ttf", &ranges); font = oc.Font.createFromPath("/zig.ttf", &ranges);
oc.assert(oc.Font.nil().isNil() == true, "nil font should be nil", .{}, @src());
oc.assert(font.isNil() == false, "created font should not be nil", .{}, @src()); oc.assert(font.isNil() == false, "created font should not be nil", .{}, @src());
orca_image = oc.Image.createFromPath(surface, Str8.fromSlice("/orca_jumping.jpg"), false); orca_image = oc.Image.createFromPath(surface, Str8.fromSlice("/orca_jumping.jpg"), false);
oc.assert(oc.Image.nil().isNil() == true, "nil image should be nil", .{}, @src());
oc.assert(orca_image.isNil() == false, "created image should not be nil", .{}, @src()); oc.assert(orca_image.isNil() == false, "created image should not be nil", .{}, @src());
} }
@ -79,7 +81,7 @@ export fn oc_on_key_up(scan: oc.ScanCode, key: oc.KeyCode) void {
export fn oc_on_frame_refresh() void { export fn oc_on_frame_refresh() void {
counter += 1; counter += 1;
const secs: f64 = oc.clockTime(oc.ClockKind.Date); const secs: f64 = oc.clock.time(.Date);
if (last_seconds != @floor(secs)) { if (last_seconds != @floor(secs)) {
last_seconds = @floor(secs); last_seconds = @floor(secs);
@ -87,10 +89,10 @@ export fn oc_on_frame_refresh() void {
} }
_ = canvas.select(); _ = canvas.select();
oc.setColorRgba(0.05, 0.05, 0.05, 1.0); oc.Canvas.setColorRgba(0.05, 0.05, 0.05, 1.0);
oc.clear(); oc.Canvas.clear();
oc.setColorRgba(1.0, 0.05, 0.05, 1.0); oc.Canvas.setColorRgba(1.0, 0.05, 0.05, 1.0);
{ {
const translation: Mat2x3 = .{ .m = [_]f32{ 1, 0, 50, 0, 1, 50 } }; const translation: Mat2x3 = .{ .m = [_]f32{ 1, 0, 50, 0, 1, 50 } };
@ -98,24 +100,24 @@ export fn oc_on_frame_refresh() void {
defer Mat2x3.pop(); defer Mat2x3.pop();
oc.assert(std.meta.eql(Mat2x3.top(), translation), "top of matrix stack should be what we pushed", .{}, @src()); oc.assert(std.meta.eql(Mat2x3.top(), translation), "top of matrix stack should be what we pushed", .{}, @src());
oc.setWidth(1); oc.Canvas.setWidth(1);
oc.rectangleFill(50, 0, 10, 10); oc.Canvas.rectangleFill(50, 0, 10, 10);
oc.rectangleStroke(70, 0, 10, 10); oc.Canvas.rectangleStroke(70, 0, 10, 10);
oc.roundedRectangleFill(90, 0, 10, 10, 3); oc.Canvas.roundedRectangleFill(90, 0, 10, 10, 3);
oc.roundedRectangleStroke(110, 0, 10, 10, 3); oc.Canvas.roundedRectangleStroke(110, 0, 10, 10, 3);
const green = oc.Color{ .Flat = .{ .r = 0.05, .g = 1, .b = 0.05, .a = 1 } }; const green = oc.Color{ .Flat = .{ .r = 0.05, .g = 1, .b = 0.05, .a = 1 } };
oc.setColor(green); oc.Canvas.setColor(green);
oc.assert(std.meta.eql(oc.getColor().Flat, green.Flat), "color should be green", .{}, @src()); oc.assert(std.meta.eql(oc.Canvas.getColor().Flat, green.Flat), "color should be green", .{}, @src());
oc.setTolerance(1); oc.Canvas.setTolerance(1);
oc.setJoint(.Bevel); oc.Canvas.setJoint(.Bevel);
oc.ellipseFill(140, 5, 10, 5); oc.Canvas.ellipseFill(140, 5, 10, 5);
oc.ellipseStroke(170, 5, 10, 5); oc.Canvas.ellipseStroke(170, 5, 10, 5);
oc.circleFill(195, 5, 5); oc.Canvas.circleFill(195, 5, 5);
oc.circleStroke(215, 5, 5); oc.Canvas.circleStroke(215, 5, 5);
oc.arc(230, 5, 5, 0, std.math.pi); oc.Canvas.arc(230, 5, 5, 0, std.math.pi);
} }
{ {
@ -126,7 +128,7 @@ export fn oc_on_frame_refresh() void {
Mat2x3.push(Mat2x3.mul_m(trans, rot)); Mat2x3.push(Mat2x3.mul_m(trans, rot));
defer Mat2x3.pop(); defer Mat2x3.pop();
oc.rectangleFill(-5, -5, 10, 10); oc.Canvas.rectangleFill(-5, -5, 10, 10);
} }
{ {
@ -170,15 +172,14 @@ export fn oc_on_frame_refresh() void {
Mat2x3.push(Mat2x3.translate(text_begin_x, 100)); Mat2x3.push(Mat2x3.translate(text_begin_x, 100));
defer Mat2x3.pop(); defer Mat2x3.pop();
oc.setColorRgba(1.0, 0.05, 0.05, 1.0); oc.Canvas.setColorRgba(1.0, 0.05, 0.05, 1.0);
oc.Canvas.setFont(font);
oc.setFont(font); oc.Canvas.setFontSize(font_size);
oc.setFontSize(font_size); oc.Canvas.moveTo(0, 0);
oc.moveTo(0, 0); oc.Canvas.textOutlines(str1);
oc.textOutlines(str1); oc.Canvas.moveTo(0, 35);
oc.moveTo(0, 35); oc.Canvas.textOutlines(str2);
oc.textOutlines(str2); oc.Canvas.fill();
oc.fill();
} }
{ {
@ -196,10 +197,10 @@ export fn oc_on_frame_refresh() void {
var strings: oc.Str8List = big_string.split(scratch, separators) catch |e| fatal(e, @src()); var strings: oc.Str8List = big_string.split(scratch, separators) catch |e| fatal(e, @src());
var collated = strings.join(scratch) catch |e| fatal(e, @src()); var collated = strings.join(scratch) catch |e| fatal(e, @src());
oc.setFontSize(12); oc.Canvas.setFontSize(12);
oc.moveTo(0, 170); oc.Canvas.moveTo(0, 170);
oc.textOutlines(collated); oc.Canvas.textOutlines(collated);
oc.fill(); oc.Canvas.fill();
} }
{ {

View File

@ -1458,68 +1458,28 @@ pub const MouseButton = enum(c_uint) {
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
extern fn oc_window_set_title(title: Str8) void; extern fn oc_window_set_title(title: Str8) void;
extern fn oc_window_set_size(size: Vec2) void;
pub fn windowSetTitle(title: [:0]const u8) void { pub fn windowSetTitle(title: []const u8) void {
// var title_str8: Str8 = .{
// .ptr = @constCast(title.ptr),
// .len = title.len,
// };
oc_window_set_title(Str8.fromSlice(title)); oc_window_set_title(Str8.fromSlice(title));
} }
extern fn oc_window_set_size(size: Vec2) void;
// pub const windowSetTitle = oc_window_set_title;
pub const windowSetSize = oc_window_set_size; pub const windowSetSize = oc_window_set_size;
//------------------------------------------------------------------------------------------
// [APP] file dialogs
//------------------------------------------------------------------------------------------
const FileDialogKind = enum(c_uint) {
Save,
Open,
};
const FileDialogFlags = packed struct(u32) {
files: bool,
directories: bool,
multiple: bool,
create_directories: bool,
};
const FileDialogDesc = extern struct {
kind: FileDialogKind,
flags: FileDialogFlags,
title: Str8,
ok_label: Str8,
start_at: File,
start_path: Str8,
filters: Str8List,
};
const FileDialogButton = enum(c_uint) {
Cancel,
Ok,
};
const FileDialogResult = extern struct {
button: FileDialogButton,
path: Str8,
selection: Str8List,
};
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
// [CLOCK] // [CLOCK]
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
pub const ClockKind = enum(c_uint) { pub const clock = struct {
pub const Kind = enum(c_uint) {
Monotonic, Monotonic,
Uptime, Uptime,
Date, Date,
}; };
extern fn oc_clock_time(clock: ClockKind) f64; extern fn oc_clock_time(clock: Kind) f64;
pub const clockTime = oc_clock_time; pub const time = oc_clock_time;
};
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
// [MATH] Vec2 // [MATH] Vec2
@ -1545,7 +1505,7 @@ pub const Vec2 = extern struct {
}; };
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
// [MATH] Matrix // [MATH] Matrix stack
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
pub const Mat2x3 = extern struct { pub const Mat2x3 = extern struct {
@ -1632,28 +1592,10 @@ pub const Surface = extern struct {
// pub const renderCommands = oc_surface_render_commands; // pub const renderCommands = oc_surface_render_commands;
}; };
pub const Canvas = extern struct {
h: u64,
extern fn oc_canvas_nil() Canvas;
extern fn oc_canvas_is_nil(canvas: Canvas) bool;
extern fn oc_canvas_create() Canvas;
extern fn oc_canvas_destroy(canvas: Canvas) void;
extern fn oc_canvas_select(canvas: Canvas) Canvas;
extern fn oc_render(canvas: Canvas) void;
pub const nil = oc_canvas_nil;
pub const isNil = oc_canvas_is_nil;
pub const create = oc_canvas_create;
pub const destroy = oc_canvas_destroy;
pub const select = oc_canvas_select;
pub const render = oc_render;
};
pub const Image = extern struct { pub const Image = extern struct {
h: u64, h: u64,
extern fn oc_image_nil(void) Image; extern fn oc_image_nil() Image;
extern fn oc_image_is_nil(image: Image) bool; extern fn oc_image_is_nil(image: Image) bool;
extern fn oc_image_create(surface: Surface, width: u32, height: u32) Image; extern fn oc_image_create(surface: Surface, width: u32, height: u32) Image;
extern fn oc_image_create_from_rgba8(surface: Surface, width: u32, height: u32, pixels: [*]u8) Image; extern fn oc_image_create_from_rgba8(surface: Surface, width: u32, height: u32, pixels: [*]u8) Image;
@ -1709,9 +1651,38 @@ pub const Color = extern union {
}; };
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
// [GRAPHICS]: graphics attributes setting/getting // [GRAPHICS]: Canvas
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
pub const Canvas = extern struct {
h: u64,
extern fn oc_canvas_nil() Canvas;
extern fn oc_canvas_is_nil(canvas: Canvas) bool;
extern fn oc_canvas_create() Canvas;
extern fn oc_canvas_destroy(canvas: Canvas) void;
extern fn oc_canvas_select(canvas: Canvas) Canvas;
extern fn oc_render(canvas: Canvas) void;
pub const nil = oc_canvas_nil;
pub const isNil = oc_canvas_is_nil;
pub const create = oc_canvas_create;
pub const destroy = oc_canvas_destroy;
pub const select = oc_canvas_select;
pub const render = oc_render;
// vector graphics
extern fn oc_clear() void;
extern fn oc_fill() void;
extern fn oc_stroke() void;
pub const clear = oc_clear;
pub const fill = oc_fill;
pub const stroke = oc_stroke;
// attributes setting/getting
extern fn oc_set_color(color: Color) void; extern fn oc_set_color(color: Color) void;
extern fn oc_set_color_rgba(r: f32, g: f32, b: f32, a: f32) void; extern fn oc_set_color_rgba(r: f32, g: f32, b: f32, a: f32) void;
extern fn oc_set_width(width: f32) void; extern fn oc_set_width(width: f32) void;
@ -1762,9 +1733,7 @@ pub const getTextFlip = oc_get_text_flip;
pub const getImage = oc_get_image; pub const getImage = oc_get_image;
pub const getImageSourceRegion = oc_get_image_source_region; pub const getImageSourceRegion = oc_get_image_source_region;
//------------------------------------------------------------------------------------------ // path construction
// [GRAPHICS]: construction: path
//------------------------------------------------------------------------------------------
extern fn oc_get_position() Vec2; extern fn oc_get_position() Vec2;
extern fn oc_move_to(x: f32, y: f32) void; extern fn oc_move_to(x: f32, y: f32) void;
@ -1788,21 +1757,7 @@ pub const glyphOutlines = oc_glyph_outlines;
pub const codepointsOutlines = oc_codepoints_outlines; pub const codepointsOutlines = oc_codepoints_outlines;
pub const textOutlines = oc_text_outlines; pub const textOutlines = oc_text_outlines;
//------------------------------------------------------------------------------------------ // shape helpers
// [GRAPHICS]: vector graphics
//------------------------------------------------------------------------------------------
extern fn oc_clear() void;
extern fn oc_fill() void;
extern fn oc_stroke() void;
pub const clear = oc_clear;
pub const fill = oc_fill;
pub const stroke = oc_stroke;
//------------------------------------------------------------------------------------------
// [GRAPHICS]: shape helpers
//------------------------------------------------------------------------------------------
extern fn oc_rectangle_fill(x: f32, y: f32, w: f32, h: f32) void; extern fn oc_rectangle_fill(x: f32, y: f32, w: f32, h: f32) void;
extern fn oc_rectangle_stroke(x: f32, y: f32, w: f32, h: f32) void; extern fn oc_rectangle_stroke(x: f32, y: f32, w: f32, h: f32) void;
@ -1823,6 +1778,7 @@ pub const ellipseStroke = oc_ellipse_stroke;
pub const circleFill = oc_circle_fill; pub const circleFill = oc_circle_fill;
pub const circleStroke = oc_circle_stroke; pub const circleStroke = oc_circle_stroke;
pub const arc = oc_arc; pub const arc = oc_arc;
};
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
// [GRAPHICS]: GLES // [GRAPHICS]: GLES
@ -1831,7 +1787,7 @@ pub const arc = oc_arc;
// TODO // TODO
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
// [UI]: GLES // [UI]
//------------------------------------------------------------------------------------------ //------------------------------------------------------------------------------------------
// TODO // TODO
@ -2015,6 +1971,33 @@ const FileStatus = extern struct {
modification_date: DateStamp, modification_date: DateStamp,
}; };
const FileDialogKind = enum(c_uint) {
Save,
Open,
};
const FileDialogFlags = packed struct(u32) {
files: bool,
directories: bool,
multiple: bool,
create_directories: bool,
};
const FileDialogDesc = extern struct {
kind: FileDialogKind,
flags: FileDialogFlags,
title: Str8,
ok_label: Str8,
start_at: File,
start_path: Str8,
filters: Str8List,
};
const FileDialogButton = enum(c_uint) {
Cancel,
Ok,
};
const FileOpenWithDialogElt = extern struct { const FileOpenWithDialogElt = extern struct {
list_elt: ListElt, list_elt: ListElt,
file: File, file: File,