From 3d524039fe5562065e6d4ad11411deea1706caa2 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 19 Aug 2023 14:25:13 +0200 Subject: Initial commit --- src/main.zig | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/main.zig (limited to 'src/main.zig') diff --git a/src/main.zig b/src/main.zig new file mode 100644 index 0000000..cc3ae26 --- /dev/null +++ b/src/main.zig @@ -0,0 +1,48 @@ +const std = @import("std"); +const c = @import("c.zig"); + +pub fn main() !void { + if (c.mgl_init() != 0) + return error.MglInitFailed; + defer c.mgl_deinit(); + + var window: c.mgl_window = undefined; + var create_params = std.mem.zeroes(c.mgl_window_create_params); + create_params.size = .{ .x = 1280, .y = 720 }; + + if (c.mgl_window_create(&window, "mgl", &create_params) != 0) + return error.MglFailedToCreateWindow; + defer c.mgl_window_deinit(&window); + + var font_file: c.mgl_memory_mapped_file = undefined; + var file_load_options = c.mgl_memory_mapped_file_load_options{ .readable = true, .writable = false }; + if (c.mgl_mapped_file_load("/usr/share/fonts/noto/NotoSans-Regular.ttf", &font_file, &file_load_options) != 0) + return error.MglFailedToLoadFontFile; + defer c.mgl_mapped_file_unload(&font_file); + + var font: c.mgl_font = undefined; + if (c.mgl_font_load_from_file(&font, &font_file, 26) != 0) + return error.MglFailedToLoadFont; + defer c.mgl_font_unload(&font); + + var text: c.mgl_text = undefined; + c.mgl_text_init(&text, &font, "hello world", 11); + defer c.mgl_text_deinit(&text); + + var event: c.mgl_event = undefined; + while (c.mgl_window_is_open(&window)) { + while (c.mgl_window_poll_event(&window, &event)) { + switch (event.type) { + c.MGL_EVENT_TEXT_ENTERED => { + const str = event.unnamed_0.text.str[0..@intCast(event.unnamed_0.text.size)]; + std.debug.print("text event, codepoint: {any}, str: {s}\n", .{ event.unnamed_0.text.codepoint, str }); + }, + else => std.debug.print("unhandled event: {any}\n", .{ event.type }), + } + } + + c.mgl_window_clear(&window, c.mgl_color{ .r = 0, .g = 0, .b = 0, .a = 255}); + c.mgl_text_draw(c.mgl_get_context(), &text); + c.mgl_window_display(&window); + } +} \ No newline at end of file -- cgit v1.2.3