The Battle for Wesnoth  1.19.0-dev
countdown_clock.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017 - 2024
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 "preferences/general.hpp"
19 #include "sound.hpp"
20 
21 namespace {
22  const int WARNTIME = 20000; //start beeping when 20 seconds are left (20,000ms)
23  unsigned timer_refresh = 0;
24  const unsigned timer_refresh_rate = 50; // prevents calling SDL_GetTicks() too frequently
25 }
26 
27 
29  : team_(team)
30  , last_timestamp_(SDL_GetTicks())
31  , playing_sound_(false)
32 {
33 }
34 
35 
37 {
38  if(playing_sound_)
39  {
41  }
42 }
43 
44 int countdown_clock::update_timestamp(int new_timestamp)
45 {
46  int res = std::max<int>(0, new_timestamp - last_timestamp_);
47  last_timestamp_ = new_timestamp;
48  return res;
49 }
50 
51 void countdown_clock::update_team(int new_timestamp)
52 {
53  int time_passed = update_timestamp(new_timestamp);
54  team_.set_countdown_time(std::max<int>(0, team_.countdown_time() - time_passed));
55 }
56 
57 //make sure we think about countdown even while dialogs are open
59 {
60  if(info.ticks(&timer_refresh, timer_refresh_rate)) {
61  update(info.ticks());
62  }
63 }
64 
65 bool countdown_clock::update(int new_timestamp)
66 {
67  update_team(new_timestamp);
69  return team_.countdown_time() > 0;
70 }
71 
73 {
74  if(!playing_sound_ && team_.countdown_time() < WARNTIME )
75  {
77  {
78  const int loop_ticks = team_.countdown_time();
79  const int fadein_ticks = (loop_ticks > WARNTIME / 2) ? loop_ticks - WARNTIME / 2 : 0;
80  sound::play_timer(game_config::sounds::timer_bell, loop_ticks, fadein_ticks);
81  playing_sound_ = true;
82  }
83  }
84 }
countdown_clock(team &team)
void update_team(int new_timestamp)
void process(events::pump_info &info)
int update_timestamp(int new_timestamp)
bool update(int new_timestamp=SDL_GetTicks())
This class stores all the data for a single 'side' (in game nomenclature).
Definition: team.hpp:74
void set_countdown_time(const int amount) const
Definition: team.hpp:197
int countdown_time() const
Definition: team.hpp:196
std::string timer_bell
logger & info()
Definition: log.cpp:314
bool sound_on()
Definition: general.cpp:728
bool UI_sound_on()
Definition: general.cpp:699
bool turn_bell()
Definition: general.cpp:675
void play_timer(const std::string &files, int loop_ticks, int fadein_ticks)
Definition: sound.cpp:1056
void stop_bell()
Definition: sound.cpp:578