Events: split off rate counting from pump_info::ticks

This interface was extremely confusing. pum_info is designed to set `ticks_` only once. This can lead to a situation (exemplified by the music code) where if ticks() is called multiple times in a row, the subsequent times with a counter, that counter would never be incremented since the condition that tests them short circuits on the is-ticks-not-set check.

Also, giving ticks() multiple side effects was very confusing.
This commit is contained in:
Charles Dang 2024-10-26 13:29:40 -04:00
parent 44bb36d411
commit 1ba3be778b
5 changed files with 42 additions and 10 deletions

View file

@ -17,11 +17,11 @@
#include "team.hpp"
#include "preferences/preferences.hpp"
#include "sound.hpp"
#include "utils/rate_counter.hpp"
namespace {
const int WARNTIME = 20000; //start beeping when 20 seconds are left (20,000ms)
unsigned timer_refresh = 0;
const unsigned timer_refresh_rate = 50; // prevents calling SDL_GetTicks() too frequently
utils::rate_counter timer_refresh_rate{50}; // prevents calling SDL_GetTicks() too frequently
}
@ -57,7 +57,7 @@ void countdown_clock::update_team(int new_timestamp)
//make sure we think about countdown even while dialogs are open
void countdown_clock::process(events::pump_info &info)
{
if(info.ticks(&timer_refresh, timer_refresh_rate)) {
if(timer_refresh_rate.poll()) {
update(info.ticks());
}
}

View file

@ -778,9 +778,9 @@ void process_tooltip_strings(int mousex, int mousey)
}
}
int pump_info::ticks(unsigned* refresh_counter, unsigned refresh_rate)
int pump_info::ticks()
{
if(!ticks_ && !(refresh_counter && ++*refresh_counter % refresh_rate)) {
if(!ticks_) {
ticks_ = ::SDL_GetTicks();
}

View file

@ -153,7 +153,7 @@ inline void pump_and_draw() { pump(); draw(); }
struct pump_info {
pump_info() : resize_dimensions(), ticks_(0) {}
std::pair<int,int> resize_dimensions;
int ticks(unsigned *refresh_counter=nullptr, unsigned refresh_rate=1);
int ticks();
private:
int ticks_; //0 if not calculated
};

View file

@ -20,6 +20,7 @@
#include "random.hpp"
#include "serialization/string_utils.hpp"
#include "sound_music_track.hpp"
#include "utils/rate_counter.hpp"
#include <SDL2/SDL.h> // Travis doesn't like this, although it works on my machine -> '#include <SDL2/SDL_sound.h>
#include <SDL2/SDL_mixer.h>
@ -50,8 +51,7 @@ namespace
{
bool mix_ok = false;
int music_start_time = 0;
unsigned music_refresh = 0;
unsigned music_refresh_rate = 20;
utils::rate_counter music_refresh_rate{20};
bool want_new_music = false;
int fadingout_time = 5000;
bool no_fading = false;
@ -790,8 +790,8 @@ void music_thinker::process(events::pump_info& info)
fadingout_time = 0;
}
if(music_start_time && info.ticks(&music_refresh, music_refresh_rate) >= music_start_time - fadingout_time) {
want_new_music = true;
if(music_start_time && music_refresh_rate.poll()) {
want_new_music = info.ticks() >= music_start_time - fadingout_time;
}
if(want_new_music) {

View file

@ -0,0 +1,32 @@
/*
Copyright (C) 2003 - 2024
Part of the Battle for Wesnoth Project https://www.wesnoth.org/
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY.
See the COPYING file for more details.
*/
#pragma once
namespace utils
{
class rate_counter
{
public:
explicit rate_counter(unsigned rate) : rate_(rate) {}
/** Increments the counter by one and checks whether it is now a multiple of the chosen rate. */
bool poll() { return (++counter_ % rate_) == 0; }
private:
unsigned counter_ = 0;
unsigned rate_ = 1;
};
} // end namespace utils