1
0

add raygui

This commit is contained in:
Rokas Puzonas 2024-01-21 12:36:42 +02:00
parent d4bc5f7f10
commit 193316f9ee
5 changed files with 53 additions and 7 deletions

3
.gitmodules vendored
View File

@ -1,3 +1,6 @@
[submodule "libs/raylib/raylib"]
path = libs/raylib/raylib
url = git@github.com:raysan5/raylib.git
[submodule "libs/raygui"]
path = libs/raygui
url = git@github.com:ryupold/raygui.zig.git

View File

@ -1,5 +1,6 @@
const std = @import("std");
const raylib = @import("libs/raylib/build.zig");
const raygui = @import("libs/raygui/build.zig");
pub fn build(b: *std.Build) !void {
const target = b.standardTargetOptions(.{});
@ -33,7 +34,8 @@ pub fn build(b: *std.Build) !void {
exe.addOptions("options", options);
}
raylib.addTo(b, exe, target, optimize, .{});
raylib.addTo(b, exe, target, optimize, .{ });
raygui.addTo(b, exe, target, optimize);
const run_cmd = b.addRunArtifact(exe);

1
libs/raygui Submodule

@ -0,0 +1 @@
Subproject commit 1f5aac4949ca485f548e623652ffda0080470179

View File

@ -1,7 +1,9 @@
const Self = @This();
const rl = @import("raylib");
const gui = @import("raygui");
const std = @import("std");
const ROM = @import("./roms.zig").ROM;
const roms = @import("./roms.zig");
const ROM = roms.ROM;
const ChipContext = @import("chip.zig");
const RaylibChip = @import("raylib-chip.zig");
@ -10,6 +12,8 @@ const assert = std.debug.assert;
const Allocator = std.mem.Allocator;
const StringList = std.ArrayList([]const u8);
const example_roms = roms.listROMs();
allocator: Allocator,
chip: *ChipContext,
@ -17,6 +21,8 @@ raylib_chip: *RaylibChip,
chip_sound: rl.Sound,
rom: ?ROM = null,
is_gui_open: bool = true,
pub fn genSinWave(wave: *rl.Wave, frequency: f32) void {
assert(wave.sampleSize == 16); // Only 16 bits are supported
@ -54,13 +60,17 @@ pub fn init(allocator: Allocator) !Self {
var raylib_chip = try allocator.create(RaylibChip);
raylib_chip.* = RaylibChip.init(chip, chip_sound);
return Self {
var self = Self {
.allocator = allocator,
.chip = chip,
.raylib_chip = raylib_chip,
.chip_sound = chip_sound,
};
self.set_rom(example_roms[3]);
return self;
}
pub fn deinit(self: *Self) void {
@ -78,7 +88,9 @@ pub fn set_rom(self: *Self, rom: ROM) void {
}
pub fn update(self: *Self, dt: f32) void {
self.raylib_chip.update(dt);
if (!self.is_gui_open) {
self.raylib_chip.update(dt);
}
}
pub fn draw(self: *Self) void {
@ -102,4 +114,35 @@ pub fn draw(self: *Self) void {
render_width,
render_height
);
self.draw_gui();
}
pub fn draw_gui(self: *Self) void {
if (rl.IsKeyPressed(.KEY_TAB)) {
self.is_gui_open = !self.is_gui_open;
}
if (!self.is_gui_open) return;
const screen_width: f32 = @floatFromInt(rl.GetScreenWidth());
const screen_height: f32 = @floatFromInt(rl.GetScreenHeight());
rl.DrawRectangle(0, 0, rl.GetScreenWidth(), rl.GetScreenHeight(), .{
.r = 22,
.g = 22,
.b = 22,
.a = 128,
});
const window_width: f32 = 300;
const window_height: f32 = 100;
const window = rl.Rectangle{
.x = (screen_width-window_width)/2,
.y = (screen_height-window_height)/2,
.width = window_width,
.height = window_height
};
if (gui.GuiWindowBox(window, "CHIP-8 Settings") == 1) {
self.is_gui_open = false;
}
}

View File

@ -31,9 +31,6 @@ pub fn main() anyerror!void {
var scene = try MainScene.init(allocator);
defer scene.deinit();
const roms = comptime listROMs();
scene.set_rom(roms[3]);
const font_size = 24;
const font_ttf_default_numchars = 95; // TTF font generation default charset: 95 glyphs (ASCII 32..126)
const font = rl.LoadFontEx("src/assets/fonts/generic-mono.otf", font_size, null, font_ttf_default_numchars);