From 19a188d9c2a05515ddbdf21b3e4d63916e535630 Mon Sep 17 00:00:00 2001 From: Anthony Pesch Date: Sun, 17 Jan 2016 23:58:51 -0800 Subject: [PATCH] graceful shutdown --- src/emu/emulator.cc | 8 +++++++- src/emu/emulator.h | 1 + src/sys/window.cc | 8 +++++++- src/sys/window.h | 5 +---- 4 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/emu/emulator.cc b/src/emu/emulator.cc index 47dee2e5..be6f251f 100644 --- a/src/emu/emulator.cc +++ b/src/emu/emulator.cc @@ -76,7 +76,9 @@ void Emulator::Run(const char *path) { auto scheduler_remaining = std::chrono::nanoseconds(0); auto frame_remaining = std::chrono::nanoseconds(0); - while (true) { + running_ = true; + + while (running_) { current_time = std::chrono::high_resolution_clock::now(); delta_time = current_time - last_time; last_time = current_time; @@ -231,6 +233,10 @@ void Emulator::PumpEvents() { case WE_RESIZE: { rb_->ResizeVideo(ev.resize.width, ev.resize.height); } break; + + case WE_QUIT: { + running_ = false; + } break; } } } diff --git a/src/emu/emulator.h b/src/emu/emulator.h index e5ae7a64..5164b4a1 100644 --- a/src/emu/emulator.h +++ b/src/emu/emulator.h @@ -36,6 +36,7 @@ class Emulator { trace::TraceWriter *trace_writer_; std::chrono::nanoseconds deltas_[MAX_SCHEDULER_DELTAS]; unsigned delta_seq_; + bool running_; }; } } diff --git a/src/sys/window.cc b/src/sys/window.cc index 52fbd597..776fc8dd 100644 --- a/src/sys/window.cc +++ b/src/sys/window.cc @@ -32,6 +32,12 @@ static inline WindowEvent MakeResizeEvent(int width, int height) { return ev; } +static inline WindowEvent MakeQuitEvent() { + WindowEvent ev; + ev.type = WE_QUIT; + return ev; +} + Window::Window() : window_(nullptr), width_(DEFAULT_WIDTH), @@ -736,7 +742,7 @@ void Window::PumpSDLEvents() { break; case SDL_QUIT: - exit(EXIT_SUCCESS); + QueueEvent(MakeQuitEvent()); break; } } diff --git a/src/sys/window.h b/src/sys/window.h index eed04330..baf513e2 100644 --- a/src/sys/window.h +++ b/src/sys/window.h @@ -18,6 +18,7 @@ enum WindowEventType { WE_KEY, WE_MOUSEMOVE, WE_RESIZE, + WE_QUIT, }; struct WindowEvent { @@ -36,10 +37,6 @@ struct WindowEvent { int width; int height; } resize; - - struct { - const char *buffer; - } tty; }; };