diff --git a/src/chip8.hpp b/src/chip8.hpp index 32cbbb1..a1e07f2 100644 --- a/src/chip8.hpp +++ b/src/chip8.hpp @@ -41,6 +41,8 @@ class Chip8 { 0xF0, 0x80, 0xF0, 0x80, 0x80 // F }; bool needs_render = true; + bool needs_restart = false; + bool paused = false; std::default_random_engine randGen; std::uniform_int_distribution randByte; diff --git a/src/control_panel.cpp b/src/control_panel.cpp index 4ee6851..090c231 100644 --- a/src/control_panel.cpp +++ b/src/control_panel.cpp @@ -8,23 +8,38 @@ ControlPanel::ControlPanel() { void ControlPanel::init(sf::Vector2f size) { $gui.position(0, size.y, size.x, WINDOW_HEIGHT - size.y); $gui.layout( - "[pause |*%(200, 400)code|_]" - "[step |_|_]" - "[play |_|_]" - "[restart|_|_]" + "[Pause |*%(200, 400)code|_]" + "[Step |_|_]" + "[Play |_|_]" + "[Restart|_|_]" ); for(auto& [name, cell] : $gui.cells()) { auto id = $gui.entity(name); $gui.set(id, {}); $gui.set(id, {guecs::to_wstring(name)}); - if(name != "code") { - $gui.set(id, {[name](auto) { - fmt::println("CLICKED {}", name); - }}); - } } + auto id = $gui.entity("Pause"); + $gui.set(id, {[&](auto) { + $paused = true; + }}); + + id = $gui.entity("Step"); + $gui.set(id, {[&](auto) { + $step = true; + }}); + + id = $gui.entity("Play"); + $gui.set(id, {[&](auto) { + $paused = false; + }}); + + id = $gui.entity("Restart"); + $gui.set(id, {[&](auto) { + $restart = true; + }}); + $gui.init(); } @@ -33,6 +48,20 @@ void ControlPanel::render(sf::RenderTarget& window) { } void ControlPanel::update(Chip8& vm) { + if($restart) { + vm.needs_restart = true; + $restart = false; + return; + } + + vm.paused = $paused; + + if($step && $paused) { + vm.Cycle(); + vm.needs_render = true; + } + + $step = false; } bool ControlPanel::mouse(float x, float y, guecs::Modifiers mods) { diff --git a/src/control_panel.hpp b/src/control_panel.hpp index 9cbeb97..0261dcb 100644 --- a/src/control_panel.hpp +++ b/src/control_panel.hpp @@ -6,6 +6,9 @@ struct ControlPanel { guecs::UI $gui; + bool $paused = false; + bool $step = false; + bool $restart = false; ControlPanel(); diff --git a/src/display.cpp b/src/display.cpp index 440b3d2..f4f2c30 100644 --- a/src/display.cpp +++ b/src/display.cpp @@ -53,6 +53,7 @@ void Display::update(Chip8& vm) { handle_inputs(vm); $texture.update((uint8_t *)(vm.video), {64, 32}, {0,0}); $status.update(vm); + $control.update(vm); } void Display::render() { diff --git a/src/main.cpp b/src/main.cpp index 71f972a..95e2179 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -36,9 +36,11 @@ int main(int argc, char* argv[]) { Stats update_stats; while (display.active()) { - auto t = cycle_stats.time_start(); - chip8.Cycle(); - cycle_stats.sample_time(t); + if(!chip8.paused) { + auto t = cycle_stats.time_start(); + chip8.Cycle(); + cycle_stats.sample_time(t); + } if(chip8.needs_render) { auto ut = update_stats.time_start(); @@ -49,6 +51,12 @@ int main(int argc, char* argv[]) { display.render(); render_stats.sample_time(rt); } + + if(chip8.needs_restart) { + Chip8 new_chip8; + new_chip8.LoadROM(romFilename); + chip8 = new_chip8; + } } cycle_stats.dump("CYCLE TIMES");