From 914e1127db4bc88f2414c377c78ddce25ed893b5 Mon Sep 17 00:00:00 2001 From: dusk Date: Thu, 21 Sep 2023 16:40:53 +0200 Subject: [PATCH] Add basic raylib functionality: open window --- build.zig | 3 +++ src/Renderer.zig | 27 +++++++-------------------- src/main.zig | 26 +++----------------------- src/raylib.zig | 36 ++++++++++++++++++++++++++++++++++++ 4 files changed, 49 insertions(+), 43 deletions(-) create mode 100644 src/raylib.zig diff --git a/build.zig b/build.zig index ff4612e..a5d7f4a 100644 --- a/build.zig +++ b/build.zig @@ -18,6 +18,9 @@ pub fn build(b: *std.Build) void { .optimize = optimize, }); + exe.linkSystemLibrary("raylib"); + exe.linkSystemLibrary("c"); + b.installArtifact(exe); const run_cmd = b.addRunArtifact(exe); diff --git a/src/Renderer.zig b/src/Renderer.zig index a8439c2..36af8ce 100644 --- a/src/Renderer.zig +++ b/src/Renderer.zig @@ -3,10 +3,10 @@ const std = @import("std"); //const zlm = @import("zlm"); //const gl = @import("zgl"); //const m = zlm.SpecializeOn(f32); +const rl = @import("raylib.zig"); const Self = @This(); -//window: sdl.Window, color: [4]f32 = .{ 0, 0, 0, 0 }, // There's a vbo for each shader program @@ -20,9 +20,9 @@ const max_objects: usize = 16384; const quadSize: usize = 9 * 6; pub fn init() !Self { - var renderer = Self{ - //.window = window, - }; + rl.initWindow(640, 480, "USG", 60); + + var renderer = Self{}; return renderer; } @@ -30,23 +30,10 @@ pub fn init() !Self { pub fn render(self: *Self) void { _ = self; - //// TODO: Submit with SubData instead to not pass the whole buffer - //self.buffer.data(f32, &self.vbo, .static_draw); + rl.endDrawing(); + rl.beginDrawing(); - //var i: usize = 0; - //const amount = self.vbo_index / quadSize; - //while (i < amount) : (i += 1) { - // gl.bindTexture(self.textures[i].texture, .@"2d"); - // gl.drawArrays(.triangles, i * 6, 6); - //} - //self.vbo_index = 0; - - //// Clear the vbo, this really shouldn't be necessary as the index is set to zero, - //// but otherwise it leads to bugs - //self.vbo = .{0.0} ** max_objects; - - //sdl.gl.swapWindow(self.window); - //gl.clear(.{ .color = true }); + rl.clearBackground(232, 216, 166, 255); } pub fn deinit(self: *Self) void { diff --git a/src/main.zig b/src/main.zig index c171aa5..ae7520f 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,5 +1,6 @@ const std = @import("std"); -//const SDL = @import("sdl2"); +// TODO: Get this from a input class, not from rl directly +const shouldClose = @import("raylib.zig").windowShouldClose; const Renderer = @import("Renderer.zig"); const Game = @import("Game.zig"); @@ -16,20 +17,7 @@ pub fn main() !void { var current_state: State = main_menu.state; - //try SDL.image.init(.{ .jpg = true }); - - //mainLoop: while (true) { - while (true) { - //const start = SDL.getTicks64(); - - // Manage window close request - //while (SDL.pollEvent()) |ev| { - // switch (ev) { - // .quit => break :mainLoop, - // else => {}, - // } - //} - + while (!shouldClose()) { if (game.needs_reinit) { game = Game.init(&renderer); } @@ -44,13 +32,5 @@ pub fn main() !void { }; renderer.render(); - - // Limit to 60 fps - //const delay = SDL.getTicks64() - start; - - //std.debug.print("{} ms\n", .{delay}); - //if (delay < 17) { - // SDL.delay(17 - @intCast(u32, delay)); - //} } } diff --git a/src/raylib.zig b/src/raylib.zig new file mode 100644 index 0000000..a199175 --- /dev/null +++ b/src/raylib.zig @@ -0,0 +1,36 @@ +pub const c = @cImport({ + @cInclude("raylib.h"); +}); + +const std = @import("std"); + +//const Self = @This(); + +pub fn initWindow(width: i32, height: i32, title: [*c]const u8, fps: i32) void { + c.InitWindow(width, height, title); + c.SetTargetFPS(fps); +} + +pub fn windowShouldClose() bool { + return c.WindowShouldClose(); +} + +pub fn closeWindow() void { + c.CloseWindow(); +} + +pub fn beginDrawing() void { + c.BeginDrawing(); +} + +pub fn endDrawing() void { + c.EndDrawing(); +} + +pub fn drawText(text: [*c]const u8, x: i32, y: i32, fontSize: i32, r: u8, g: u8, b: u8, a: u8) void { + c.DrawText(text, x, y, fontSize, c.Color{ .r = r, .g = g, .b = b, .a = a}); +} + +pub fn clearBackground(r: u8, g: u8, b: u8, a: u8) void { + c.ClearBackground(c.Color{ .r = r, .g = g, .b = b, .a = a}); +}