Added globals and refactor for wasm.

This commit is contained in:
2026-06-28 06:59:46 +02:00
parent 5edae5d2d8
commit 717ef479fa
2 changed files with 30 additions and 35 deletions
+24 -16
View File
@@ -3,9 +3,6 @@ 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
@@ -42,21 +39,30 @@ Layout :: struct {
} }
init :: proc(sim: ^Simulator) { init :: proc(sim: ^Simulator) {
run = true _sim = sim
// desktop
when ODIN_OS != .JS {
rl.SetConfigFlags({.WINDOW_RESIZABLE}) rl.SetConfigFlags({.WINDOW_RESIZABLE})
rl.SetTargetFPS(60)
}
// web
when ODIN_OS == .JS {
rl.SetConfigFlags({.VSYNC_HINT})
}
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)
// Load sound // Load sound
beep := rl.LoadSound("./assets/sounds/beep.wav") beep := rl.LoadSound("./assets/sounds/beep.wav")
sim.sound = beep _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)
rl.SetTextureFilter(font.texture, .BILINEAR) rl.SetTextureFilter(font.texture, .BILINEAR)
sim.font = font _sim.font = font
rl.GuiLoadStyleDefault() rl.GuiLoadStyleDefault()
rl.GuiLoadStyle("./assets/raygui_styles/genesis.rgs") rl.GuiLoadStyle("./assets/raygui_styles/genesis.rgs")
@@ -65,7 +71,9 @@ init :: proc(sim: ^Simulator) {
rl.GuiSetStyle(.DEFAULT, i32(rl.GuiDefaultProperty.TEXT_SIZE), 18) rl.GuiSetStyle(.DEFAULT, i32(rl.GuiDefaultProperty.TEXT_SIZE), 18)
} }
update :: proc(sim: ^Simulator) { update :: proc() {
sim := _sim
// 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())
@@ -98,7 +106,7 @@ update :: proc(sim: ^Simulator) {
// Left // Left
// ------------------------------------------ // ------------------------------------------
gui_file_loader(layout.file_loader, sim) when ODIN_OS != .JS { gui_file_loader(layout.file_loader, sim)}
gui_key_pad(layout.keypad, sim.machine.keypad, sim.font) gui_key_pad(layout.keypad, sim.machine.keypad, sim.font)
// Center // Center
@@ -119,9 +127,11 @@ update :: 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()
free_all(context.temp_allocator)
} }
shutdown :: proc(sim: ^Simulator) { shutdown :: proc() {
sim := _sim
rl.UnloadFont(sim.font) rl.UnloadFont(sim.font)
rl.UnloadSound(sim.sound) rl.UnloadSound(sim.sound)
rl.CloseAudioDevice() rl.CloseAudioDevice()
@@ -130,12 +140,9 @@ shutdown :: proc(sim: ^Simulator) {
should_run :: proc() -> bool { should_run :: proc() -> bool {
when ODIN_OS != .JS { when ODIN_OS != .JS {
if rl.WindowShouldClose() { return !rl.WindowShouldClose()
run = false
} }
} return true // web loop is controlled by JS requestAnimationFrame
return run
} }
tick_timers :: proc(sim: ^Simulator) { tick_timers :: proc(sim: ^Simulator) {
@@ -146,7 +153,8 @@ tick_timers :: proc(sim: ^Simulator) {
sim.machine.sound_timer -= 1 sim.machine.sound_timer -= 1
if !rl.IsSoundPlaying(beep) do rl.PlaySound(beep) if !rl.IsSoundPlaying(beep) do rl.PlaySound(beep)
} else { } else {
rl.StopSound(beep) // only stop if actually playing
if rl.IsSoundPlaying(beep) do rl.StopSound(beep)
} }
} }
+3 -16
View File
@@ -3,6 +3,9 @@ package simulator
import emu "../machine" import emu "../machine"
import rl "vendor:raylib" import rl "vendor:raylib"
@(private="package")
_sim: ^Simulator
// Embed roms // Embed roms
roms := #load_directory("../roms") roms := #load_directory("../roms")
@@ -43,19 +46,3 @@ Simulator :: struct {
disasm_count: int, disasm_count: int,
disasm_scroll: rl.Vector2, disasm_scroll: rl.Vector2,
} }
// Requires an initilized emulatore System Struct
/*
run_simulator :: proc(s: ^emu.System) {
sim := Simulator {
machine = s,
rom_loaded = false,
paused = true,
step = false,
cpu_hz = 700,
disasm_follow = true,
}
run_gui(&sim)
}
*/