From 3d524039fe5562065e6d4ad11411deea1706caa2 Mon Sep 17 00:00:00 2001 From: dec05eba Date: Sat, 19 Aug 2023 14:25:13 +0200 Subject: Initial commit --- .gitignore | 2 ++ .gitmodules | 3 +++ LICENSE | 19 +++++++++++++++++++ README.md | 1 + build.zig | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ depends/mgl | 1 + src/c.zig | 8 ++++++++ src/main.zig | 48 ++++++++++++++++++++++++++++++++++++++++++++++++ 8 files changed, 138 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 LICENSE create mode 100644 README.md create mode 100644 build.zig create mode 160000 depends/mgl create mode 100644 src/c.zig create mode 100644 src/main.zig diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e73c965 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +zig-cache/ +zig-out/ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..b6b69cf --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "depends/mgl"] + path = depends/mgl + url = https://repo.dec05eba.com/mgl diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..bc8b574 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) dec05eba + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..7d2be6f --- /dev/null +++ b/README.md @@ -0,0 +1 @@ +Minimal example of [mgl](https://git.dec05eba.com/mgl/about/) in zig. \ No newline at end of file diff --git a/build.zig b/build.zig new file mode 100644 index 0000000..ad01829 --- /dev/null +++ b/build.zig @@ -0,0 +1,56 @@ +const std = @import("std"); + +pub fn build(b: *std.Build) void { + const target = b.standardTargetOptions(.{}); + const optimize = b.standardOptimizeOption(.{}); + + const exe = b.addExecutable(.{ + .name = "mgl-zig-example", + .root_source_file = .{ .path = "src/main.zig" }, + .target = target, + .optimize = optimize, + }); + + exe.addCSourceFiles(&[_][]const u8{ + "depends/mgl/src/graphics/texture.c", + "depends/mgl/src/graphics/sprite.c", + "depends/mgl/src/graphics/image.c", + "depends/mgl/src/graphics/font_char_map.c", + "depends/mgl/src/graphics/font.c", + "depends/mgl/src/graphics/vertex.c", + "depends/mgl/src/graphics/primitive_type.c", + "depends/mgl/src/graphics/vertex_buffer.c", + "depends/mgl/src/graphics/text.c", + "depends/mgl/src/graphics/shader.c", + "depends/mgl/src/graphics/rectangle.c", + "depends/mgl/src/system/fileutils.c", + "depends/mgl/src/system/utf8.c", + "depends/mgl/src/system/clock.c", + "depends/mgl/src/mgl.c", + "depends/mgl/src/window/window.c", + "depends/mgl/src/gl.c", + }, &[_][]const u8{ + "-Wall", + "-Wextra", + }); + exe.addIncludePath(.{ .path = "depends/mgl/include" }); + exe.linkSystemLibrary("x11"); + exe.linkSystemLibrary("xrender"); + exe.linkLibC(); + + exe.strip = optimize != std.builtin.OptimizeMode.Debug; + exe.single_threaded = true; + exe.want_lto = optimize != std.builtin.OptimizeMode.Debug; + + b.installArtifact(exe); + + const run_cmd = b.addRunArtifact(exe); + run_cmd.step.dependOn(b.getInstallStep()); + + if (b.args) |args| { + run_cmd.addArgs(args); + } + + const run_step = b.step("run", "Run the app"); + run_step.dependOn(&run_cmd.step); +} \ No newline at end of file diff --git a/depends/mgl b/depends/mgl new file mode 160000 index 0000000..15cb00d --- /dev/null +++ b/depends/mgl @@ -0,0 +1 @@ +Subproject commit 15cb00dfffd777e97abf8732f84a3c3c3bd659e9 diff --git a/src/c.zig b/src/c.zig new file mode 100644 index 0000000..4d3f62b --- /dev/null +++ b/src/c.zig @@ -0,0 +1,8 @@ +pub usingnamespace @cImport({ + @cInclude("mgl/mgl.h"); + @cInclude("mgl/window/window.h"); + @cInclude("mgl/window/event.h"); + @cInclude("mgl/system/fileutils.h"); + @cInclude("mgl/graphics/font.h"); + @cInclude("mgl/graphics/text.h"); +}); \ No newline at end of file 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