Add a GUI to ZBA #7

Merged
paoda merged 24 commits from imgui into main 2023-03-11 03:18:15 +00:00
3 changed files with 20 additions and 23 deletions
Showing only changes of commit ff609c85ba - Show all commits

View File

@ -188,7 +188,7 @@ pub fn init(allocator: Allocator, cpu: *Arm7tdmi, rom_path: []const u8, save_pat
const kind = Backup.guess(file_buf); const kind = Backup.guess(file_buf);
const device = if (config.config().guest.force_rtc) .Rtc else guessDevice(file_buf); const device = if (config.config().guest.force_rtc) .Rtc else guessDevice(file_buf);
logHeader(file_buf, &title); logHeader(file_buf, title);
return .{ return .{
.buf = file_buf, .buf = file_buf,
@ -220,19 +220,17 @@ fn guessDevice(buf: []const u8) Gpio.Device.Kind {
} }
// TODO: Detect other GPIO devices // TODO: Detect other GPIO devices
return .None; return .None;
} }
fn logHeader(buf: []const u8, title: *const [12]u8) void { fn logHeader(buf: []const u8, title: [12]u8) void {
const code = buf[0xAC..0xB0]; const ver = buf[0xBC];
const maker = buf[0xB0..0xB2];
const version = buf[0xBC];
log.info("Title: {s}", .{title}); log.info("Title: {s}", .{title});
if (version != 0) log.info("Version: {}", .{version}); if (ver != 0) log.info("Version: {}", .{ver});
log.info("Game Code: {s}", .{code});
log.info("Maker Code: {s}", .{maker}); log.info("Game Code: {s}", .{buf[0xAC..0xB0]});
log.info("Maker Code: {s}", .{buf[0xB0..0xB2]});
} }
test "OOB Access" { test "OOB Access" {

View File

@ -90,7 +90,7 @@ pub fn main() void {
} }
// TODO: Just copy the title instead of grabbing a pointer to it // TODO: Just copy the title instead of grabbing a pointer to it
var gui = Gui.init(allocator, &bus.pak.title, &bus.apu) catch |e| exitln("failed to init gui: {}", .{e}); var gui = Gui.init(allocator, bus.pak.title, &bus.apu) catch |e| exitln("failed to init gui: {}", .{e});
defer gui.deinit(); defer gui.deinit();
var quit = Atomic(bool).init(false); var quit = Atomic(bool).init(false);

View File

@ -34,19 +34,14 @@ pub const Gui = struct {
const State = struct { const State = struct {
fps_hist: RingBuffer(u32), fps_hist: RingBuffer(u32),
allocator: Allocator,
pub fn init(allocator: Allocator) !@This() { pub fn init(allocator: Allocator) !@This() {
const history = try allocator.alloc(u32, 0x400); const history = try allocator.alloc(u32, 0x400);
return .{ .fps_hist = RingBuffer(u32).init(history) };
return .{
.fps_hist = RingBuffer(u32).init(history),
.allocator = allocator,
};
} }
fn deinit(self: *@This()) void { fn deinit(self: *@This(), allocator: Allocator) void {
self.fps_hist.deinit(self.allocator); self.fps_hist.deinit(allocator);
self.* = undefined; self.* = undefined;
} }
}; };
@ -68,14 +63,15 @@ pub const Gui = struct {
window: *SDL.SDL_Window, window: *SDL.SDL_Window,
ctx: SDL_GLContext, ctx: SDL_GLContext,
title: []const u8, title: [:0]const u8,
audio: Audio, audio: Audio,
state: State, state: State,
allocator: Allocator,
program_id: gl.GLuint, program_id: gl.GLuint,
pub fn init(allocator: Allocator, title: *const [12]u8, apu: *Apu) !Self { pub fn init(allocator: Allocator, title: [12]u8, apu: *Apu) !Self {
if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_EVENTS | SDL.SDL_INIT_AUDIO) < 0) panic(); if (SDL.SDL_Init(SDL.SDL_INIT_VIDEO | SDL.SDL_INIT_EVENTS | SDL.SDL_INIT_AUDIO) < 0) panic();
if (SDL.SDL_GL_SetAttribute(SDL.SDL_GL_CONTEXT_PROFILE_MASK, SDL.SDL_GL_CONTEXT_PROFILE_CORE) < 0) panic(); if (SDL.SDL_GL_SetAttribute(SDL.SDL_GL_CONTEXT_PROFILE_MASK, SDL.SDL_GL_CONTEXT_PROFILE_CORE) < 0) panic();
if (SDL.SDL_GL_SetAttribute(SDL.SDL_GL_CONTEXT_MAJOR_VERSION, 3) < 0) panic(); if (SDL.SDL_GL_SetAttribute(SDL.SDL_GL_CONTEXT_MAJOR_VERSION, 3) < 0) panic();
@ -104,18 +100,19 @@ pub const Gui = struct {
return Self{ return Self{
.window = window, .window = window,
.title = std.mem.sliceTo(title, 0), .title = try allocator.dupeZ(u8, &title),
.ctx = ctx, .ctx = ctx,
.program_id = try compileShaders(), .program_id = try compileShaders(),
.audio = Audio.init(apu), .audio = Audio.init(apu),
.allocator = allocator,
.state = try State.init(allocator), .state = try State.init(allocator),
}; };
} }
pub fn deinit(self: *Self) void { pub fn deinit(self: *Self) void {
self.audio.deinit(); self.audio.deinit();
self.state.deinit(); self.state.deinit(self.allocator);
zgui.backend.deinit(); zgui.backend.deinit();
zgui.plot.deinit(); zgui.plot.deinit();
@ -125,6 +122,8 @@ pub const Gui = struct {
SDL.SDL_GL_DeleteContext(self.ctx); SDL.SDL_GL_DeleteContext(self.ctx);
SDL.SDL_DestroyWindow(self.window); SDL.SDL_DestroyWindow(self.window);
SDL.SDL_Quit(); SDL.SDL_Quit();
self.allocator.free(self.title);
self.* = undefined; self.* = undefined;
} }
@ -266,7 +265,7 @@ pub const Gui = struct {
const w = @intToFloat(f32, gba_width * win_scale); const w = @intToFloat(f32, gba_width * win_scale);
const h = @intToFloat(f32, gba_height * win_scale); const h = @intToFloat(f32, gba_height * win_scale);
_ = zgui.begin("Game Boy Advance Screen", .{ .flags = .{ .no_resize = true, .always_auto_resize = true } }); _ = zgui.begin(self.title, .{ .flags = .{ .no_resize = true, .always_auto_resize = true } });
defer zgui.end(); defer zgui.end();
zgui.image(@intToPtr(*anyopaque, tex_id), .{ .w = w, .h = h, .uv0 = .{ 0, 1 }, .uv1 = .{ 1, 0 } }); zgui.image(@intToPtr(*anyopaque, tex_id), .{ .w = w, .h = h, .uv0 = .{ 0, 1 }, .uv1 = .{ 1, 0 } });