The Battle for Wesnoth  1.17.0-dev
countdown_clock.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2021
3  Part of the Battle for Wesnoth Project https://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include "countdown_clock.hpp"
16 
17 #include "team.hpp"
18 #include "saved_game.hpp"
19 #include "preferences/general.hpp"
20 #include "sound.hpp"
21 
22 namespace {
23  const int WARNTIME = 20000; //start beeping when 20 seconds are left (20,000ms)
24  unsigned timer_refresh = 0;
25  const unsigned timer_refresh_rate = 50; // prevents calling SDL_GetTicks() too frequently
26 }
27 
28 
30  : team_(team)
31  , last_timestamp_(SDL_GetTicks())
32  , playing_sound_(false)
33 {
34 }
35 
36 
38 {
39  if(playing_sound_)
40  {
42  }
43 }
44 
45 int countdown_clock::update_timestamp(int new_timestamp)
46 {
47  int res = std::max<int>(0, new_timestamp - last_timestamp_);
48  last_timestamp_ = new_timestamp;
49  return res;
50 }
51 
52 void countdown_clock::update_team(int new_timestamp)
53 {
54  int time_passed = update_timestamp(new_timestamp);
55  team_.set_countdown_time(std::max<int>(0, team_.countdown_time() - time_passed));
56 }
57 
58 //make sure we think about countdown even while dialogs are open
60 {
61  if(info.ticks(&timer_refresh, timer_refresh_rate)) {
62  update(info.ticks());
63  }
64 }
65 
66 bool countdown_clock::update(int new_timestamp)
67 {
68  update_team(new_timestamp);
70  return team_.countdown_time() > 0;
71 }
72 
74 {
75  if(!playing_sound_ && team_.countdown_time() < WARNTIME )
76  {
78  {
79  const int loop_ticks = team_.countdown_time();
80  const int fadein_ticks = (loop_ticks > WARNTIME / 2) ? loop_ticks - WARNTIME / 2 : 0;
81  sound::play_timer(game_config::sounds::timer_bell, loop_ticks, fadein_ticks);
82  playing_sound_ = true;
83  }
84  }
85 }
bool turn_bell()
Definition: general.cpp:645
void set_countdown_time(const int amount) const
Definition: team.hpp:223
int countdown_time() const
Definition: team.hpp:222
countdown_clock(team &team)
int update_timestamp(int new_timestamp)
logger & info()
Definition: log.cpp:89
int ticks(unsigned *refresh_counter=nullptr, unsigned refresh_rate=1)
Definition: events.cpp:833
bool sound_on()
Definition: general.cpp:698
std::string timer_bell
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:72
bool UI_sound_on()
Definition: general.cpp:669
void process(events::pump_info &info)
void stop_bell()
Definition: sound.cpp:580
void update_team(int new_timestamp)
bool update(int new_timestamp=SDL_GetTicks())
void play_timer(const std::string &files, int loop_ticks, int fadein_ticks)
Definition: sound.cpp:1044