From 8830331774102248fccc845056caec1bce786e46 Mon Sep 17 00:00:00 2001 From: Jason Hilder Date: Thu, 25 Jun 2026 06:25:44 +0200 Subject: [PATCH] Restructure of code for wasm build. --- src/simulator/gui.odin | 132 ++++++++++++++++++++--------------- src/simulator/simulator.odin | 3 + 2 files changed, 79 insertions(+), 56 deletions(-) diff --git a/src/simulator/gui.odin b/src/simulator/gui.odin index cda4dd5..8b3b9f2 100644 --- a/src/simulator/gui.odin +++ b/src/simulator/gui.odin @@ -3,6 +3,9 @@ package simulator import emu "../machine" import rl "vendor:raylib" +// Globals +run: bool + // Window WINDOW_WIDTH :: 1920 WINDOW_HEIGHT :: 1080 @@ -38,13 +41,17 @@ Layout :: struct { info_box : rl.Rectangle, } -// Initialize main the gui 'window' -run_gui :: proc(sim: ^Simulator) { +init :: proc(sim: ^Simulator) { + run = true + rl.SetConfigFlags({.WINDOW_RESIZABLE}) rl.InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Octal Cookie - Chip 8 Simulator") rl.InitAudioDevice() rl.SetTargetFPS(60) + + // Load sound beep := rl.LoadSound("./assets/sounds/beep.wav") + sim.sound = beep // Load fonts font := rl.LoadFontEx("./assets/fonts/Inter_18pt-Regular.ttf", 18, nil, 0) @@ -56,71 +63,84 @@ run_gui :: proc(sim: ^Simulator) { rl.GuiSetFont(font) rl.GuiSetStyle(.DEFAULT, i32(rl.GuiDefaultProperty.TEXT_SIZE), 18) +} - // Draw each of the components in its own window within the main window - for !rl.WindowShouldClose() { - // Recalculate layout each frame based on current window size - // Pass these down to gui functions so they can setup their sizes? - screen_width := f32(rl.GetScreenWidth()) - screen_height := f32(rl.GetScreenHeight()) - sidebar_width := screen_width * 0.20 +update :: proc(sim: ^Simulator) { + // Recalculate layout each frame based on current window size + // Pass these down to gui functions so they can setup their sizes? + screen_width := f32(rl.GetScreenWidth()) + screen_height := f32(rl.GetScreenHeight()) + sidebar_width := screen_width * 0.20 - // set all the layout structs dynamically with the screen size - layout := calc_layout(screen_width, screen_height) + // set all the layout structs dynamically with the screen size + layout := calc_layout(screen_width, screen_height) - rl.BeginDrawing() - rl.ClearBackground(rl.Color{0x18, 0x18, 0x18, 0xFF}) + rl.BeginDrawing() + rl.ClearBackground(rl.Color{0x18, 0x18, 0x18, 0xFF}) - cycles := int(sim.cpu_hz / SIM_FPS) - if (!sim.paused) { - // Cycle the machine to update memory etc - emu.run_machine(sim.machine, cycles) - tick_timers(sim, beep) - } - - if(sim.paused && sim.step) { - // Cycle the machine to update memory etc - emu.run_machine(sim.machine, 1) - tick_timers(sim, beep) - sim.step = false - } - - // Top - // ------------------------------------------ - gui_control_bar(layout.control_bar, sim) - - // Left - // ------------------------------------------ - gui_file_loader(layout.file_loader, sim) - gui_key_pad(layout.keypad, sim.machine.keypad, sim.font) - - // Center - // ------------------------------------------ - gui_screen(layout.display, sim) - - // Right - // ------------------------------------------ - gui_cpu(layout.cpu, sim) - - // Bottom - // ------------------------------------------ - gui_bottom_panel(layout.bottom_panel, sim) - gui_status_bar(layout.status_bar, sim) - - // Info Box - // ------------------------------------------ - gui_info_box(layout.info_box, sim, screen_width, screen_height) - - rl.EndDrawing() + cycles := int(sim.cpu_hz / SIM_FPS) + if (!sim.paused) { + // Cycle the machine to update memory etc + emu.run_machine(sim.machine, cycles) + tick_timers(sim) } + if(sim.paused && sim.step) { + // Cycle the machine to update memory etc + emu.run_machine(sim.machine, 1) + tick_timers(sim) + sim.step = false + } + + // Top + // ------------------------------------------ + gui_control_bar(layout.control_bar, sim) + + // Left + // ------------------------------------------ + gui_file_loader(layout.file_loader, sim) + gui_key_pad(layout.keypad, sim.machine.keypad, sim.font) + + // Center + // ------------------------------------------ + gui_screen(layout.display, sim) + + // Right + // ------------------------------------------ + gui_cpu(layout.cpu, sim) + + // Bottom + // ------------------------------------------ + gui_bottom_panel(layout.bottom_panel, sim) + gui_status_bar(layout.status_bar, sim) + + // Info Box + // ------------------------------------------ + gui_info_box(layout.info_box, sim, screen_width, screen_height) + + rl.EndDrawing() +} + +shutdown :: proc(sim: ^Simulator) { rl.UnloadFont(sim.font) - rl.UnloadSound(beep) + rl.UnloadSound(sim.sound) rl.CloseAudioDevice() rl.CloseWindow() } -tick_timers :: proc(sim: ^Simulator, beep: rl.Sound) { +should_run :: proc() -> bool { + when ODIN_OS != .JS { + if rl.WindowShouldClose() { + run = false + } + } + + return run +} + +tick_timers :: proc(sim: ^Simulator) { + beep := sim.sound + if sim.machine.delay_timer > 0 do sim.machine.delay_timer -= 1 if sim.machine.sound_timer > 0 { sim.machine.sound_timer -= 1 diff --git a/src/simulator/simulator.odin b/src/simulator/simulator.odin index 7b2a483..3c3a69e 100644 --- a/src/simulator/simulator.odin +++ b/src/simulator/simulator.odin @@ -29,6 +29,7 @@ Simulator :: struct { cpu_hz: f32, info_box: bool, // GUI + sound: rl.Sound, font: rl.Font, active_tab: i32, mem_scroll: rl.Vector2, @@ -44,6 +45,7 @@ Simulator :: struct { } // Requires an initilized emulatore System Struct +/* run_simulator :: proc(s: ^emu.System) { sim := Simulator { machine = s, @@ -56,3 +58,4 @@ run_simulator :: proc(s: ^emu.System) { run_gui(&sim) } +*/