Add basic raylib functionality: open window

This commit is contained in:
Dusk 2023-09-21 16:40:53 +02:00
parent 821162015a
commit 914e1127db
4 changed files with 49 additions and 43 deletions

View File

@ -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);

View File

@ -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 {

View File

@ -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));
//}
}
}

36
src/raylib.zig Normal file
View File

@ -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});
}