Restructure of code for wasm build.
This commit is contained in:
+76
-56
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user