From c752c9a3d981b3a6f0c1de37516a2bf19e21e740 Mon Sep 17 00:00:00 2001 From: ALONSO Laurent Date: Wed, 6 Oct 2021 10:28:38 +0200 Subject: [PATCH] try to make the dialogs less CPU hungry... --- src/dialogxml/dialogs/dialog.cpp | 23 ++++++++++++++--------- src/dialogxml/dialogs/dialog.hpp | 4 ++-- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/src/dialogxml/dialogs/dialog.cpp b/src/dialogxml/dialogs/dialog.cpp index b69cf9d4..8a8cf6a5 100644 --- a/src/dialogxml/dialogs/dialog.cpp +++ b/src/dialogxml/dialogs/dialog.cpp @@ -567,10 +567,17 @@ void cDialog::handle_events() { cFramerateLimiter fps_limiter; while(dialogNotToast) { - while(win.pollEvent(currentEvent)) handle_one_event(currentEvent); + bool need_redraw=false; // OSNOLA + while(win.pollEvent(currentEvent)) handle_one_event(currentEvent, need_redraw); + if(doAnimations && animTimer.getElapsedTime().asMilliseconds() >= 500) { + need_redraw = true; + cPict::advanceAnim(); + animTimer.restart(); + } // Ideally, this should be the only draw call that is done in a cycle. - draw(); + if (need_redraw) + draw(); // Prevent the loop from executing too fast. fps_limiter.frame_finished(); @@ -578,7 +585,7 @@ void cDialog::handle_events() { } // This method handles one event received by the dialog. -void cDialog::handle_one_event(const sf::Event& currentEvent) { +void cDialog::handle_one_event(const sf::Event& currentEvent, bool &need_redraw) { using kb = sf::Keyboard; cKey key; @@ -586,7 +593,7 @@ void cDialog::handle_one_event(const sf::Event& currentEvent) { static cKey pendingKey = {true}; std::string itemHit = ""; location where; - + need_redraw=true; switch(currentEvent.type) { case sf::Event::KeyPressed: switch(currentEvent.key.code){ @@ -658,6 +665,7 @@ void cDialog::handle_one_event(const sf::Event& currentEvent) { case kb::RControl: case kb::LSystem: case kb::RSystem: + need_redraw=false; return; default: key.spec = false; @@ -711,7 +719,8 @@ void cDialog::handle_one_event(const sf::Event& currentEvent) { process_click(where, key.mod); break; default: // To silence warning of unhandled enum values - break; + need_redraw=false; + return; case sf::Event::GainedFocus: case sf::Event::MouseMoved: bool inField = false; @@ -1040,10 +1049,6 @@ bool cDialog::doAnimations = false; void cDialog::draw(){ win.setActive(false); tileImage(win,winRect,::bg[bg]); - if(doAnimations && animTimer.getElapsedTime().asMilliseconds() >= 500) { - cPict::advanceAnim(); - animTimer.restart(); - } ctrlIter iter = controls.begin(); while(iter != controls.end()){ diff --git a/src/dialogxml/dialogs/dialog.hpp b/src/dialogxml/dialogs/dialog.hpp index 85480c76..c35f8844 100644 --- a/src/dialogxml/dialogs/dialog.hpp +++ b/src/dialogxml/dialogs/dialog.hpp @@ -216,14 +216,14 @@ public: private: void draw(); void handle_events(); - void handle_one_event(const sf::Event&); + void handle_one_event(const sf::Event&, bool &need_redraw); void process_keystroke(cKey keyHit); void process_click(location where, eKeyMod mods); bool dialogNotToast, didAccept; rectangle winRect; boost::any result; std::string fname; - sf::Clock animTimer, paintTimer; + sf::Clock animTimer; friend class cControl; friend class cContainer; };