From 95a039c8d1f926365c8bf3c20b09ec6250e60d8a Mon Sep 17 00:00:00 2001 From: Markus Gerber Date: Fri, 7 Feb 2025 12:51:29 +0100 Subject: added internal buffer to get rid of display::get --- lib/nes/app/application.cc | 15 +++++++++++++-- lib/nes/app/application.hh | 8 ++++++-- lib/nes/app/ui/screen-freeze.cc | 10 ++-------- lib/nes/app/ui/screen-freeze.hh | 2 +- lib/nes/common/display.hh | 2 -- 5 files changed, 22 insertions(+), 15 deletions(-) (limited to 'lib') diff --git a/lib/nes/app/application.cc b/lib/nes/app/application.cc index 4498f98..bb2f1e4 100644 --- a/lib/nes/app/application.cc +++ b/lib/nes/app/application.cc @@ -7,6 +7,16 @@ namespace nes::app { + auto application::display_proxy::get_front() const -> span + { + return buffer_front_; + } + + auto application::display_proxy::set(u32 const x, u32 const y, rgb const value) -> void { + buffer_back_[y * display::width + x] = value; + base.set(x, y, value); + } + auto application::display_proxy::switch_buffers() -> void { auto r = renderer{ base }; @@ -30,6 +40,7 @@ namespace nes::app r.render_text_format(32, 29, color::fixed_white, attrs, "{}", fps_counter.get_fps()); } + swap(buffer_front_, buffer_back_); base.switch_buffers(); } @@ -74,7 +85,7 @@ namespace nes::app { if (event == input_event::key_down(key::escape)) { - screen_freeze_.freeze(display_); + screen_freeze_.freeze(display_.get_front()); screen_confirm_quit_.set_confirm(false); display_.screen = &screen_freeze_; display_.popup = &screen_confirm_quit_; @@ -93,7 +104,7 @@ namespace nes::app if (console_->get_status() != status::success) { - screen_freeze_.freeze(display_); + screen_freeze_.freeze(display_.get_front()); display_.screen = &screen_freeze_; show_error("Runtime error", console_->get_status(), action::go_to_browser()); console_.clear(); diff --git a/lib/nes/app/application.hh b/lib/nes/app/application.hh index f0117b1..d0973e8 100644 --- a/lib/nes/app/application.hh +++ b/lib/nes/app/application.hh @@ -31,6 +31,9 @@ namespace nes::app class display_proxy final : public display { preferences& preferences_; + rgb buffers_[2][display::width * display::height]{}; + span buffer_front_ = buffers_[0]; + span buffer_back_ = buffers_[1]; public: fps_counter fps_counter; @@ -44,8 +47,9 @@ namespace nes::app { } - auto get(u32 const x, u32 const y) const -> rgb override { return base.get(x, y); } - auto set(u32 const x, u32 const y, rgb const value) -> void override { base.set(x, y, value); } + auto set(u32 const x, u32 const y, rgb const value) -> void override; + auto get_front() const -> span; + auto switch_buffers() -> void override; }; diff --git a/lib/nes/app/ui/screen-freeze.cc b/lib/nes/app/ui/screen-freeze.cc index 60410dd..68507db 100644 --- a/lib/nes/app/ui/screen-freeze.cc +++ b/lib/nes/app/ui/screen-freeze.cc @@ -3,15 +3,9 @@ namespace nes::app { - auto screen_freeze::freeze(display const& disp) -> void + auto screen_freeze::freeze(span front_buffer) -> void { - for (auto y = u32{ 0 }; y < display::height; ++y) - { - for (auto x = u32{ 0 }; x < display::width; ++x) - { - frozen_data_[y * display::width + x] = disp.get(x, y); - } - } + copy(front_buffer.get_data(), frozen_data_, front_buffer.get_length()); } auto screen_freeze::render(renderer& renderer) -> void diff --git a/lib/nes/app/ui/screen-freeze.hh b/lib/nes/app/ui/screen-freeze.hh index fe67aad..2ae1eb2 100644 --- a/lib/nes/app/ui/screen-freeze.hh +++ b/lib/nes/app/ui/screen-freeze.hh @@ -20,6 +20,6 @@ namespace nes::app auto process_events() -> action override; /// Update the image shown to the back buffer of the given display. - auto freeze(display const&) -> void; + auto freeze(span front_buffer) -> void; }; } // namespace nes::app \ No newline at end of file diff --git a/lib/nes/common/display.hh b/lib/nes/common/display.hh index f7d53c1..5e9f4d7 100644 --- a/lib/nes/common/display.hh +++ b/lib/nes/common/display.hh @@ -25,8 +25,6 @@ namespace nes virtual auto switch_buffers() -> void = 0; /// Update the pixel at the given position in the back buffer. virtual auto set(u32 x, u32 y, rgb value) -> void = 0; - /// Load the pixel value at the given position in the front buffer. - virtual auto get(u32 x, u32 y) const -> rgb = 0; protected: explicit display() = default; -- cgit v1.3.1