The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
countdown_clock.cpp
Go to the documentation of this file.
1 
2 #include "countdown_clock.hpp"
3 
4 #include "team.hpp"
5 #include "saved_game.hpp"
6 #include "preferences.hpp"
7 #include "sound.hpp"
8 
9 namespace {
10  const int WARNTIME = 20000; //start beeping when 20 seconds are left (20,000ms)
11  unsigned timer_refresh = 0;
12  const unsigned timer_refresh_rate = 50; // prevents calling SDL_GetTicks() too frequently
13 }
14 
15 
17  : team_(team)
18  , last_timestamp_(SDL_GetTicks())
19  , playing_sound_(false)
20 {
21 }
22 
23 
25 {
26  if(playing_sound_)
27  {
29  }
30 }
31 
32 int countdown_clock::update_timestamp(int new_timestamp)
33 {
34  int res = std::max<int>(0, new_timestamp - last_timestamp_);
35  last_timestamp_ = new_timestamp;
36  return res;
37 }
38 
39 void countdown_clock::update_team(int new_timestamp)
40 {
41  int time_passed = update_timestamp(new_timestamp);
42  team_.set_countdown_time(std::max<int>(0, team_.countdown_time() - time_passed));
43 }
44 
45 //make sure we think about countdown even while dialogs are open
47 {
48  if(info.ticks(&timer_refresh, timer_refresh_rate)) {
49  update(info.ticks());
50  }
51 }
52 
53 bool countdown_clock::update(int new_timestamp)
54 {
55  update_team(new_timestamp);
57  return team_.countdown_time() > 0;
58 }
59 
61 {
62  if(!playing_sound_ && team_.countdown_time() < WARNTIME )
63  {
65  {
66  const int loop_ticks = team_.countdown_time();
67  const int fadein_ticks = (loop_ticks > WARNTIME / 2) ? loop_ticks - WARNTIME / 2 : 0;
68  sound::play_timer(game_config::sounds::timer_bell, loop_ticks, fadein_ticks);
69  playing_sound_ = true;
70  }
71  }
72 }
bool turn_bell()
countdown_clock(team &team)
int update_timestamp(int new_timestamp)
logger & info()
Definition: log.cpp:91
int ticks(unsigned *refresh_counter=nullptr, unsigned refresh_rate=1)
Definition: events.cpp:640
void set_countdown_time(const int amount) const
Definition: team.hpp:216
-file sdl_utils.hpp
bool sound_on()
std::string timer_bell
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:50
bool UI_sound_on()
GLuint res
Definition: glew.h:9258
void process(events::pump_info &info)
void stop_bell()
Definition: sound.cpp:441
void update_team(int new_timestamp)
bool update(int new_timestamp=SDL_GetTicks())
int countdown_time() const
Definition: team.hpp:215
void play_timer(const std::string &files, int loop_ticks, int fadein_ticks)
Definition: sound.cpp:834