Restructure of code for wasm build.

This commit is contained in:
2026-06-25 06:25:44 +02:00
parent eded8b60b7
commit 8830331774
2 changed files with 79 additions and 56 deletions
+29 -9
View File
@@ -3,6 +3,9 @@ package simulator
import emu "../machine" import emu "../machine"
import rl "vendor:raylib" import rl "vendor:raylib"
// Globals
run: bool
// Window // Window
WINDOW_WIDTH :: 1920 WINDOW_WIDTH :: 1920
WINDOW_HEIGHT :: 1080 WINDOW_HEIGHT :: 1080
@@ -38,13 +41,17 @@ Layout :: struct {
info_box : rl.Rectangle, info_box : rl.Rectangle,
} }
// Initialize main the gui 'window' init :: proc(sim: ^Simulator) {
run_gui :: proc(sim: ^Simulator) { run = true
rl.SetConfigFlags({.WINDOW_RESIZABLE}) rl.SetConfigFlags({.WINDOW_RESIZABLE})
rl.InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Octal Cookie - Chip 8 Simulator") rl.InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Octal Cookie - Chip 8 Simulator")
rl.InitAudioDevice() rl.InitAudioDevice()
rl.SetTargetFPS(60) rl.SetTargetFPS(60)
// Load sound
beep := rl.LoadSound("./assets/sounds/beep.wav") beep := rl.LoadSound("./assets/sounds/beep.wav")
sim.sound = beep
// Load fonts // Load fonts
font := rl.LoadFontEx("./assets/fonts/Inter_18pt-Regular.ttf", 18, nil, 0) font := rl.LoadFontEx("./assets/fonts/Inter_18pt-Regular.ttf", 18, nil, 0)
@@ -56,9 +63,9 @@ run_gui :: proc(sim: ^Simulator) {
rl.GuiSetFont(font) rl.GuiSetFont(font)
rl.GuiSetStyle(.DEFAULT, i32(rl.GuiDefaultProperty.TEXT_SIZE), 18) rl.GuiSetStyle(.DEFAULT, i32(rl.GuiDefaultProperty.TEXT_SIZE), 18)
}
// Draw each of the components in its own window within the main window update :: proc(sim: ^Simulator) {
for !rl.WindowShouldClose() {
// Recalculate layout each frame based on current window size // Recalculate layout each frame based on current window size
// Pass these down to gui functions so they can setup their sizes? // Pass these down to gui functions so they can setup their sizes?
screen_width := f32(rl.GetScreenWidth()) screen_width := f32(rl.GetScreenWidth())
@@ -75,13 +82,13 @@ run_gui :: proc(sim: ^Simulator) {
if (!sim.paused) { if (!sim.paused) {
// Cycle the machine to update memory etc // Cycle the machine to update memory etc
emu.run_machine(sim.machine, cycles) emu.run_machine(sim.machine, cycles)
tick_timers(sim, beep) tick_timers(sim)
} }
if(sim.paused && sim.step) { if(sim.paused && sim.step) {
// Cycle the machine to update memory etc // Cycle the machine to update memory etc
emu.run_machine(sim.machine, 1) emu.run_machine(sim.machine, 1)
tick_timers(sim, beep) tick_timers(sim)
sim.step = false sim.step = false
} }
@@ -112,15 +119,28 @@ run_gui :: proc(sim: ^Simulator) {
gui_info_box(layout.info_box, sim, screen_width, screen_height) gui_info_box(layout.info_box, sim, screen_width, screen_height)
rl.EndDrawing() rl.EndDrawing()
} }
shutdown :: proc(sim: ^Simulator) {
rl.UnloadFont(sim.font) rl.UnloadFont(sim.font)
rl.UnloadSound(beep) rl.UnloadSound(sim.sound)
rl.CloseAudioDevice() rl.CloseAudioDevice()
rl.CloseWindow() 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.delay_timer > 0 do sim.machine.delay_timer -= 1
if sim.machine.sound_timer > 0 { if sim.machine.sound_timer > 0 {
sim.machine.sound_timer -= 1 sim.machine.sound_timer -= 1
+3
View File
@@ -29,6 +29,7 @@ Simulator :: struct {
cpu_hz: f32, cpu_hz: f32,
info_box: bool, info_box: bool,
// GUI // GUI
sound: rl.Sound,
font: rl.Font, font: rl.Font,
active_tab: i32, active_tab: i32,
mem_scroll: rl.Vector2, mem_scroll: rl.Vector2,
@@ -44,6 +45,7 @@ Simulator :: struct {
} }
// Requires an initilized emulatore System Struct // Requires an initilized emulatore System Struct
/*
run_simulator :: proc(s: ^emu.System) { run_simulator :: proc(s: ^emu.System) {
sim := Simulator { sim := Simulator {
machine = s, machine = s,
@@ -56,3 +58,4 @@ run_simulator :: proc(s: ^emu.System) {
run_gui(&sim) run_gui(&sim)
} }
*/