The Battle for Wesnoth  1.15.1+dev
countdown_clock.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2017-2018 by the Battle for Wesnoth Project https://www.wesnoth.org/
3 
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY.
10 
11  See the COPYING file for more details.
12 */
13 
14 #include "countdown_clock.hpp"
15 
16 #include "team.hpp"
17 #include "saved_game.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 }
bool turn_bell()
Definition: general.cpp:615
void set_countdown_time(const int amount) const
Definition: team.hpp:211
int countdown_time() const
Definition: team.hpp:210
countdown_clock(team &team)
int update_timestamp(int new_timestamp)
logger & info()
Definition: log.cpp:90
int ticks(unsigned *refresh_counter=nullptr, unsigned refresh_rate=1)
Definition: events.cpp:835
-file sdl_utils.hpp
bool sound_on()
Definition: general.cpp:668
std::string timer_bell
This class stores all the data for a single &#39;side&#39; (in game nomenclature).
Definition: team.hpp:44
bool UI_sound_on()
Definition: general.cpp:639
void process(events::pump_info &info)
void stop_bell()
Definition: sound.cpp:548
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:1011