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
+28 -8
View File
@@ -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,9 +63,9 @@ 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() {
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())
@@ -75,13 +82,13 @@ run_gui :: proc(sim: ^Simulator) {
if (!sim.paused) {
// Cycle the machine to update memory etc
emu.run_machine(sim.machine, cycles)
tick_timers(sim, beep)
tick_timers(sim)
}
if(sim.paused && sim.step) {
// Cycle the machine to update memory etc
emu.run_machine(sim.machine, 1)
tick_timers(sim, beep)
tick_timers(sim)
sim.step = false
}
@@ -114,13 +121,26 @@ run_gui :: proc(sim: ^Simulator) {
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
+3
View File
@@ -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)
}
*/