limit framerate of controls' nested event loops

This commit is contained in:
2024-08-09 16:26:24 -05:00
committed by Celtic Minstrel
parent fe4821cb4e
commit 39c85e7f3c
22 changed files with 147 additions and 137 deletions

View File

@@ -1102,7 +1102,7 @@ static void handle_party_death() {
}
}
bool handle_action(const sf::Event& event) {
bool handle_action(const sf::Event& event, cFramerateLimiter& fps_limiter) {
long item_hit;
bool are_done = false;
bool need_redraw = false, did_something = false, need_reprint = false;
@@ -1129,12 +1129,12 @@ bool handle_action(const sf::Event& event) {
// Now split off the extra stuff, like talking and shopping.
if(overall_mode == MODE_TALKING) {
handle_talk_event(the_point);
handle_talk_event(the_point, fps_limiter);
if(overall_mode != MODE_TALKING)
return false;
}
if(overall_mode == MODE_SHOPPING) {
handle_shop_event(the_point);
handle_shop_event(the_point, fps_limiter);
if(overall_mode != MODE_SHOPPING)
return false;
}
@@ -1630,7 +1630,7 @@ void initiate_outdoor_combat(short i) {
draw_terrain();
}
bool handle_keystroke(const sf::Event& event){
bool handle_keystroke(const sf::Event& event, cFramerateLimiter& fps_limiter){
bool are_done = false;
location pass_point; // TODO: This isn't needed
std::ostringstream sout;
@@ -1708,7 +1708,7 @@ bool handle_keystroke(const sf::Event& event){
pass_point = mainPtr.mapCoordsToPixel(pass_point, mainView);
pass_event.mouseButton.x = pass_point.x;
pass_event.mouseButton.y = pass_point.y;
are_done = handle_action(pass_event);
are_done = handle_action(pass_event, fps_limiter);
}
}
else if(overall_mode == MODE_SHOPPING) { // shopping keystrokes
@@ -1725,7 +1725,7 @@ bool handle_keystroke(const sf::Event& event){
pass_point = mainPtr.mapCoordsToPixel(pass_point, mainView);
pass_event.mouseButton.x = pass_point.x;
pass_event.mouseButton.y = pass_point.y;
are_done = handle_action(pass_event);
are_done = handle_action(pass_event, fps_limiter);
}
} else {
for(short i = 0; i < 10; i++)
@@ -1737,7 +1737,7 @@ bool handle_keystroke(const sf::Event& event){
pass_point = mainPtr.mapCoordsToPixel(terrain_click[i], mainView);
pass_event.mouseButton.x = pass_point.x;
pass_event.mouseButton.y = pass_point.y;
are_done = handle_action(pass_event);
are_done = handle_action(pass_event, fps_limiter);
return are_done;
}
}

View File

@@ -5,17 +5,18 @@
#include <SFML/Window/Event.hpp>
#include "location.hpp"
#include "dialogxml/keycodes.hpp"
#include "tools/framerate_limiter.hpp"
void init_screen_locs();
bool prime_time();
bool handle_action(const sf::Event& event);
bool handle_action(const sf::Event& event, cFramerateLimiter& fps_limiter);
void advance_time(bool did_something, bool need_redraw, bool need_reprint);
void handle_move(location destination, bool& did_something, bool& need_redraw, bool& need_reprint);
void handle_monster_actions(bool& need_redraw, bool& need_reprint);
bool someone_awake();
void handle_menu_spell(short spell_picked,short spell_type) ;
void initiate_outdoor_combat(short i);
bool handle_keystroke(const sf::Event& event);
bool handle_keystroke(const sf::Event& event, cFramerateLimiter& fps_limiter);
bool handle_scroll(const sf::Event& event);
void do_load();
void post_load();

View File

@@ -206,16 +206,16 @@ void end_shop_mode() {
}
}
void handle_shop_event(location p) {
void handle_shop_event(location p, cFramerateLimiter& fps_limiter) {
if(p.in(talk_help_rect)) {
if(!help_btn->handleClick(p))
if(!help_btn->handleClick(p, fps_limiter))
return;
give_help(226,27);
return;
}
if(p.in(shop_done_rect)) {
if(done_btn->handleClick(p))
if(done_btn->handleClick(p, fps_limiter))
end_shop_mode();
return;
}
@@ -706,7 +706,7 @@ static void show_job_bank(int which_bank, std::string title) {
job_dlg.run();
}
void handle_talk_event(location p) {
void handle_talk_event(location p, cFramerateLimiter& fps_limiter) {
short get_pc,s1 = -1,s2 = -1;
char asked[4];
@@ -714,7 +714,7 @@ void handle_talk_event(location p) {
eTalkNode ttype;
if(p.in(talk_help_rect)) {
if(!help_btn->handleClick(p))
if(!help_btn->handleClick(p, fps_limiter))
return;
give_help(205,6);
return;

View File

@@ -7,13 +7,13 @@
void start_shop_mode(short which,short cost_adj,std::string store_name);
void end_shop_mode();
void handle_shop_event(location p);
void handle_shop_event(location p, cFramerateLimiter& fps_limiter);
void handle_sale(cShopItem item, int i);
void handle_info_request(cShopItem item);
void set_up_shop_array();
void start_talk_mode(short m_num,short personality,mon_num_t monst_type,short store_face_pic);
void end_talk_mode();
void handle_talk_event(location p);
void handle_talk_event(location p, cFramerateLimiter& fps_limiter);
void handle_talk_spec(short ttype,char* place_string1,char* place_string2);
void store_responses();
void do_sign(short town_num, short which_sign, short sign_type);

View File

@@ -414,9 +414,9 @@ void handle_events() {
while(!fake_event_queue.empty()){
const sf::Event& next_event = fake_event_queue.front();
fake_event_queue.pop_front();
handle_one_event(next_event);
handle_one_event(next_event, fps_limiter);
}
while(mainPtr.pollEvent(currentEvent)) handle_one_event(currentEvent);
while(mainPtr.pollEvent(currentEvent)) handle_one_event(currentEvent, fps_limiter);
// It would be nice to have minimap inside the main game window (we have lots of screen space in fullscreen mode).
// Alternatively, minimap could live on its own thread.
@@ -474,7 +474,7 @@ void handle_quit_event() {
All_Done = true;
}
void handle_one_event(const sf::Event& event) {
void handle_one_event(const sf::Event& event, cFramerateLimiter& fps_limiter) {
// What does this do and should it be here?
through_sending();
@@ -493,12 +493,12 @@ void handle_one_event(const sf::Event& event) {
switch(event.type) {
case sf::Event::KeyPressed:
if(flushingInput) return;
if(!(event.key.*systemKey)) handle_keystroke(event);
if(!(event.key.*systemKey)) handle_keystroke(event, fps_limiter);
break;
case sf::Event::MouseButtonPressed:
if(flushingInput) return;
Mouse_Pressed(event);
Mouse_Pressed(event, fps_limiter);
break;
case sf::Event::MouseLeft:
@@ -581,7 +581,7 @@ void redraw_everything() {
if(map_visible) draw_map(false);
}
void Mouse_Pressed(const sf::Event& event) {
void Mouse_Pressed(const sf::Event& event, cFramerateLimiter& fps_limiter) {
// What is this stuff? Why is it here?
if(had_text_freeze > 0) {
@@ -592,7 +592,7 @@ void Mouse_Pressed(const sf::Event& event) {
if(overall_mode == MODE_STARTUP) {
All_Done = handle_startup_press({event.mouseButton.x, event.mouseButton.y});
} else {
All_Done = handle_action(event);
All_Done = handle_action(event, fps_limiter);
}
// Why does every mouse click activate a menu?
@@ -772,11 +772,11 @@ void handle_menu_choice(eMenu item_hit) {
case eMenu::ACTIONS_ALCHEMY:
dummyEvent.key.code = sf::Keyboard::A;
dummyEvent.key.shift = true;
handle_keystroke(dummyEvent);
queue_fake_event(dummyEvent);
break;
case eMenu::ACTIONS_WAIT:
dummyEvent.key.code = sf::Keyboard::W;
handle_keystroke(dummyEvent);
queue_fake_event(dummyEvent);
break;
case eMenu::ACTIONS_AUTOMAP:
if(!prime_time()) {

View File

@@ -1,6 +1,5 @@
#include <SFML/Graphics.hpp>
#include "tools/framerate_limiter.hpp"
#ifdef __APPLE__
extern eMenuChoice menuChoice;
@@ -9,8 +8,8 @@ extern short menuChoiceId;
int main(int argc, char* argv[]);
void update_everything();
void redraw_everything();
void Mouse_Pressed(const sf::Event&);
eKeyMod current_key_mod();
void Mouse_Pressed(const sf::Event&, cFramerateLimiter& fps_limiter);
void close_program();
void change_cursor(location where_curs);
void set_up_apple_events();
@@ -19,12 +18,12 @@ void incidental_noises(bool on_surface);
void pause(short length);
bool handle_startup_press(location the_point);
void handle_splash_events();
void show_logo(cFramerateLimiter& framerate_limiter);
void plop_fancy_startup(cFramerateLimiter& framerate_limiter);
void show_logo(cFramerateLimiter& fps_limiter);
void plop_fancy_startup(cFramerateLimiter& fps_limiter);
void update_terrain_animation();
void update_startup_animation();
void handle_events();
void handle_one_event(const sf::Event&);
void handle_one_event(const sf::Event&, cFramerateLimiter& fps_limiter);
void queue_fake_event(const sf::Event&);
void handle_one_minimap_event(const sf::Event &);

View File

@@ -6,6 +6,8 @@
//
//
#include "tools/framerate_limiter.hpp"
#ifndef BoE_boe_menus_h
#define BoE_boe_menus_h

View File

@@ -128,13 +128,13 @@ bool handle_startup_press(location the_point) {
return false;
}
void handle_splash_events(cFramerateLimiter& framerate_limiter) {
void handle_splash_events(cFramerateLimiter& fps_limiter) {
sf::Event event;
while(mainPtr.pollEvent(event)) {
if(event.type == sf::Event::GainedFocus || event.type == sf::Event::MouseMoved)
set_cursor(sword_curs);
}
framerate_limiter.frame_finished();
fps_limiter.frame_finished();
}
static rectangle view_rect() {
@@ -142,7 +142,7 @@ static rectangle view_rect() {
return rectangle(0, 0, size.y, size.x);
}
void show_logo(cFramerateLimiter& framerate_limiter) {
void show_logo(cFramerateLimiter& fps_limiter) {
rectangle whole_window = view_rect();
if(get_int_pref("DisplayMode") != 5)
@@ -157,17 +157,17 @@ void show_logo(cFramerateLimiter& framerate_limiter) {
play_sound(-95);
while(sound_going(95)) {
draw_splash(pict_to_draw, mainPtr, logo_from);
handle_splash_events(framerate_limiter);
handle_splash_events(fps_limiter);
}
if(!get_int_pref("ShowStartupSplash", true)) {
sf::Time delay = time_in_ticks(60);
sf::Clock timer;
while(timer.getElapsedTime() < delay)
handle_splash_events(framerate_limiter);
handle_splash_events(fps_limiter);
}
}
void plop_fancy_startup(cFramerateLimiter& framerate_limiter) {
void plop_fancy_startup(cFramerateLimiter& fps_limiter) {
rectangle whole_window = view_rect();
float ui_scale = get_float_pref("UIScale", 1.0);
@@ -183,7 +183,7 @@ void plop_fancy_startup(cFramerateLimiter& framerate_limiter) {
while(timer.getElapsedTime() < delay) {
draw_splash(pict_to_draw, mainPtr, intro_from);
handle_splash_events(framerate_limiter);
handle_splash_events(fps_limiter);
}
}