Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
127 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
|
||
#include "countdown_clock.hpp" | ||
|
||
#include "team.hpp" | ||
#include "saved_game.hpp" | ||
#include "preferences.hpp" | ||
#include "sound.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 | ||
} | ||
|
||
|
||
countdown_clock::countdown_clock(team& team, const mp_game_settings& settings) | ||
: team_(team) | ||
, settings_(settings) | ||
, last_timestamp_(SDL_GetTicks()) | ||
, playing_sound_(false) | ||
{ | ||
} | ||
|
||
|
||
countdown_clock::~countdown_clock() | ||
{ | ||
if(playing_sound_) | ||
{ | ||
sound::stop_bell(); | ||
} | ||
} | ||
|
||
int countdown_clock::update_timestamp(int new_timestamp) | ||
{ | ||
int res = std::max<int>(0, new_timestamp - last_timestamp_); | ||
last_timestamp_ = new_timestamp; | ||
return res; | ||
} | ||
|
||
void countdown_clock::update_team(int new_timestamp) | ||
{ | ||
int time_passed = update_timestamp(new_timestamp); | ||
team_.set_countdown_time(std::max<int>(0, team_.countdown_time() - time_passed)); | ||
} | ||
|
||
//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)) { | ||
update(info.ticks()); | ||
} | ||
} | ||
|
||
bool countdown_clock::update(int new_timestamp) | ||
{ | ||
update_team(new_timestamp); | ||
maybe_play_sound(); | ||
return team_.countdown_time() > 0; | ||
} | ||
|
||
void countdown_clock::maybe_play_sound() | ||
{ | ||
if(!playing_sound_ && team_.countdown_time() < WARNTIME ) | ||
{ | ||
if(preferences::turn_bell() || preferences::sound_on() || preferences::UI_sound_on()) | ||
{ | ||
const int loop_ticks = team_.countdown_time(); | ||
const int fadein_ticks = (loop_ticks > WARNTIME / 2) ? loop_ticks - WARNTIME / 2 : 0; | ||
sound::play_timer(game_config::sounds::timer_bell, loop_ticks, fadein_ticks); | ||
playing_sound_ = true; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#pragma once | ||
#include "events.hpp" | ||
#include <SDL_timer.h> | ||
|
||
class team; | ||
struct mp_game_settings; | ||
class countdown_clock : public events::pump_monitor | ||
{ | ||
public: | ||
countdown_clock(team& team, const mp_game_settings& settings); | ||
~countdown_clock(); | ||
/// @returns ticks passed since last update | ||
/// @param new_timestamp latest result of SDL_GetTicks() | ||
int update_timestamp(int new_timestamp); | ||
/// @param new_timestamp latest result of SDL_GetTicks() | ||
void update_team(int new_timestamp); | ||
void process(events::pump_info &info); | ||
/// @return whether there is time left | ||
/// @param new_timestamp latest result of SDL_GetTicks() | ||
bool update(int new_timestamp = SDL_GetTicks()); | ||
void maybe_play_sound(); | ||
private: | ||
team& team_; | ||
const mp_game_settings& settings_; | ||
int last_timestamp_; | ||
bool playing_sound_; | ||
}; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters