Added left panel rom loader.

Left panel has tinyfieldialogs to open a file picker, so you can swap
out roms, required a reset machine function aswell.
This commit is contained in:
2026-06-13 09:29:53 +02:00
parent 2e83baea8e
commit c3367cdf3e
4 changed files with 50 additions and 19 deletions
+15
View File
@@ -68,3 +68,18 @@ run_machine :: proc(s: ^System, cycles: int) {
cycle(s) cycle(s)
} }
} }
new_machine :: proc() -> System {
s: System
s.pc = 0x200
s.current_key = -1
// load fonts into the memory
for v, i in FONT_SET {
s.memory[i] = v
}
return s
}
reset_machine :: proc(s: ^System) {
s^ = new_machine()
}
-7
View File
@@ -23,13 +23,6 @@ main :: proc() {
// Init the emu 8 "cpu" // Init the emu 8 "cpu"
system := emu.init() system := emu.init()
// @TODO: move this into a gui component
// load rom, hardcoded for now, will eventually be cli or gui
err := emu.load_rom(&system, "./test_roms/7-beep.ch8")
if err != nil {
panic("failed to load rom!")
}
// Initilize sim, gui etc // Initilize sim, gui etc
sim.run_simulator(&system) sim.run_simulator(&system)
+34 -11
View File
@@ -4,6 +4,9 @@ import "core:log"
import "core:strings" import "core:strings"
import rl "vendor:raylib" import rl "vendor:raylib"
import emu "../machine"
import tfd "../../external/tinyfiledialogs"
gui_left_panel :: proc(rect: rl.Rectangle, sim: ^Simulator) { gui_left_panel :: proc(rect: rl.Rectangle, sim: ^Simulator) {
// ── Top panel and components ── // ── Top panel and components ──
top_panel := rl.Rectangle { top_panel := rl.Rectangle {
@@ -16,13 +19,12 @@ gui_left_panel :: proc(rect: rl.Rectangle, sim: ^Simulator) {
// Dropzone/file loader // Dropzone/file loader
file_loader_rect := rl.Rectangle { file_loader_rect := rl.Rectangle {
top_panel.x + PANEL_PADDING, top_panel.x,
top_panel.y + PANEL_HEADER, top_panel.y,
top_panel.width - PANEL_PADDING * 2, top_panel.width,
(top_panel.height / 3) - PANEL_HEADER - PANEL_PADDING * 2, (top_panel.height / 3) - PANEL_HEADER - PANEL_PADDING * 2,
} }
rl.GuiPanel(file_loader_rect, "Rom / File") gui_file_loader(file_loader_rect, sim)
gui_file_loader(file_loader_rect)
// ── Bottom panel and components ── // ── Bottom panel and components ──
bottom_panel := rl.Rectangle { bottom_panel := rl.Rectangle {
@@ -35,7 +37,9 @@ gui_left_panel :: proc(rect: rl.Rectangle, sim: ^Simulator) {
gui_key_pad(bottom_panel, sim.machine.keypad, sim.font) gui_key_pad(bottom_panel, sim.machine.keypad, sim.font)
} }
gui_file_loader :: proc(rect: rl.Rectangle) { gui_file_loader :: proc(rect: rl.Rectangle, sim: ^Simulator) {
rl.GuiPanel(rect, "Rom / File")
// drop-zone occupies the panel's content area, minus space for the button // drop-zone occupies the panel's content area, minus space for the button
drop_zone := rl.Rectangle { drop_zone := rl.Rectangle {
rect.x + PANEL_PADDING, rect.x + PANEL_PADDING,
@@ -47,10 +51,10 @@ gui_file_loader :: proc(rect: rl.Rectangle) {
// centered drop-zone text // centered drop-zone text
text: cstring = "Drop a CHIP-8 ROM here" text: cstring = "Drop a CHIP-8 ROM here"
text_width := rl.MeasureText(text, DROP_FONT_SIZE) text_width := rl.MeasureText(text, BIG_FONT_SIZE)
text_x := drop_zone.x + (drop_zone.width - f32(text_width)) / 2 text_x := drop_zone.x + (drop_zone.width - f32(text_width)) / 2
text_y := drop_zone.y + (drop_zone.height - f32(DROP_FONT_SIZE)) / 2 text_y := drop_zone.y + (drop_zone.height - f32(BIG_FONT_SIZE)) / 2
rl.DrawText(text, i32(text_x), i32(text_y), DROP_FONT_SIZE, rl.GRAY) rl.DrawTextEx(sim.font, text, {text_x, text_y}, BIG_FONT_SIZE, 1, rl.WHITE)
// open rom button below drop-zone // open rom button below drop-zone
btn_rect := rl.Rectangle { btn_rect := rl.Rectangle {
@@ -61,7 +65,23 @@ gui_file_loader :: proc(rect: rl.Rectangle) {
} }
if rl.GuiButton(btn_rect, "Open ROM") { if rl.GuiButton(btn_rect, "Open ROM") {
// @TODO: get tinyfiledialog working here ret := tfd.openFileDialog("Open File Dialog", nil, 0, nil, nil, 0,)
rom_path := string(ret)
if rom_path == "" do return
// reset machine state
emu.reset_machine(sim.machine)
// load new rom
err := emu.load_rom(sim.machine, rom_path)
if err != nil {
// @TODO: update status bar here
panic("failed to load rom!")
}
sim.rom_loaded = true
sim.running = true
sim.paused = false
} }
// Handle file drop // Handle file drop
@@ -96,6 +116,7 @@ gui_key_pad :: proc(rect: rl.Rectangle, display: [16]bool, font: rl.Font) {
label: string, label: string,
index: int, index: int,
} }
keys := [16]Key { keys := [16]Key {
{"1", 1}, {"2", 2}, {"3", 3}, {"C", 12}, {"1", 1}, {"2", 2}, {"3", 3}, {"C", 12},
{"4", 4}, {"5", 5}, {"6", 6}, {"D", 13}, {"4", 4}, {"5", 5}, {"6", 6}, {"D", 13},
@@ -122,7 +143,9 @@ gui_key_pad :: proc(rect: rl.Rectangle, display: [16]bool, font: rl.Font) {
if display[val.index] { rl.DrawRectangleRec(irect, rl.BLACK) } if display[val.index] { rl.DrawRectangleRec(irect, rl.BLACK) }
rl.DrawRectangleLinesEx(irect, 1, rl.GRAY) rl.DrawRectangleLinesEx(irect, 1, rl.GRAY)
rl.DrawTextEx( font, str, rl.DrawTextEx(
font,
str,
rl.Vector2{irect.x + btn_width / 2, irect.y + btn_height / 2}, rl.Vector2{irect.x + btn_width / 2, irect.y + btn_height / 2},
KEYPAD_FONT_SIZE, KEYPAD_FONT_SIZE,
1, 1,