Skip to content

Commit

Permalink
high precision Timer
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasByr committed Jun 20, 2023
1 parent e359fdd commit 8a14c70
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 20 deletions.
4 changes: 3 additions & 1 deletion inc/core/entity.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,10 @@ class Entity {
* @brief add some time to the internal timer
*
* @param sec seconds to add
* @param ms milliseconds to add
* @param us microseconds to add
*/
void add_timer_time(unsigned sec);
void add_timer_time(unsigned sec=0, unsigned ms=0, unsigned us=0);
};

#endif // __inc_core_entity_H__
6 changes: 4 additions & 2 deletions inc/core/game.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ class Game {

/// @brief Run the game
void run(void);
/// @brief pause for 1 second and delay timers
void pause(void);
/// @brief delay timeres
void delay(unsigned sec = 0, unsigned ms = 0, unsigned us = 0);
/// @brief pause for given duration and delay timers
void pause(unsigned sec = 0, unsigned ms = 0, unsigned us = 0);
};

#endif // __inc_core_game_H__
2 changes: 1 addition & 1 deletion inc/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ class Timer {
///@brief reset the timer
void reset_timer(void);
///@brief add a given duration to the timer
void add_time(unsigned sec);
void add_time(unsigned sec=0, unsigned ms=0, unsigned us=0);
};

} // namespace sys_pause
Expand Down
4 changes: 3 additions & 1 deletion src/core/entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,6 @@ bool Entity::ate_entity(double other_cx, double other_cy) const {
bool Entity::is_dead() const { return m_lives <= 0; }
void Entity::die() { m_lives--; }

void Entity::add_timer_time(unsigned sec) { m_timer.add_time(sec); }
void Entity::add_timer_time(unsigned sec, unsigned ms, unsigned us) {
m_timer.add_time(sec, ms, us);
}
37 changes: 24 additions & 13 deletions src/core/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,17 @@ Game::Game(const std::string &config_path)

Game::~Game() = default;

void Game::pause() {
void Game::delay(unsigned sec, unsigned ms, unsigned us) {
m_pacman->add_timer_time(sec, ms, us);
for (auto &ghost : m_ghosts) { ghost->add_timer_time(sec, ms, us); }
m_map->get_power_timer()->add_time(sec, ms, us);
}

void Game::pause(unsigned sec, unsigned ms, unsigned us) {
// todo: tell other threads to also sleep
m_pacman->add_timer_time(1);
for (auto &ghost : m_ghosts) { ghost->add_timer_time(1); }
m_map->get_power_timer()->add_time(1);
std::this_thread::sleep_for(std::chrono::seconds(1));
std::this_thread::sleep_for(std::chrono::seconds(sec) +
std::chrono::milliseconds(ms) +
std::chrono::microseconds(us));
}

void Game::run() {
Expand All @@ -98,8 +103,9 @@ void Game::run() {

while (m_running) {
// handle quit event
SDL_Event event;
bool quit = false;
SDL_Event event; // NOLINT
bool quit = false; // quit game

while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT || quit) { m_running = false; }

Expand Down Expand Up @@ -137,9 +143,9 @@ void Game::run() {

// handle key events in game mode
// basically just move pacman
if (m_state == GameState::GAME && event.type == SDL_KEYDOWN) {
if (m_state == GameState::GAME &&
event.type == SDL_KEYDOWN) {
switch (event.key.keysym.sym) {

case SDLK_UP: m_pacman->set_direction(Direction::UP); break;
case SDLK_DOWN: m_pacman->set_direction(Direction::DOWN); break;
case SDLK_LEFT: m_pacman->set_direction(Direction::LEFT); break;
Expand All @@ -157,7 +163,7 @@ void Game::run() {
x / m_map->get_size(), y, y / m_map->get_size());
}
}
}
} // end of event handling

// update
fps = fps_counter.tick();
Expand Down Expand Up @@ -212,7 +218,7 @@ void Game::run() {
case true:
m_pacman->eat_ghost();
ghost->eat(m_map);
pause();
pause(1);
break;
}
}
Expand All @@ -223,13 +229,18 @@ void Game::run() {
for (auto &ghost : m_ghosts) { ghost->reset(); }
m_map->reset();

pause();
pause(1);
}
/* FALLTHROUGH */
case GameState::WAITING:
if (m_state == GameState::WAITING) { /* ready */
m_renderer->text("READY!", 175, 335);
}
/* FALLTHROUGH */
case GameState::PAUSE:
if (m_state == GameState::PAUSE) { /* pause */
m_renderer->text("PRESS P TO RESUME", 75, 335);
}

m_map->show(m_renderer);

Expand All @@ -247,7 +258,7 @@ void Game::run() {
for (auto &ghost : m_ghosts) { ghost->reset(); }
m_state = GameState::WAITING;

pause();
pause(1);
break;
default: break;
}
Expand Down
2 changes: 1 addition & 1 deletion src/helper/renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ void Renderer::blit(SDL_Rect src, int x, int y, double scale) {
void Renderer::text(const std::string &text, int x, int y) {
// as we do not use sdl_ttf, we have to render the text ourselves
// we can use sprites for that
// we have to render each character separately (each is 7)
// we have to render each character separately

static const double custom_scale = 0.7;
SDL_Rect src = m_assets->get_sprite_alpha_numerical(' ');
Expand Down
4 changes: 3 additions & 1 deletion src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,11 @@ bool Timer::is_expired() {

void Timer::reset_timer() { running = false; }

void Timer::add_time(unsigned sec) {
void Timer::add_time(unsigned sec, unsigned ms, unsigned us) {
if (!running) { return; }
end += std::chrono::seconds(sec);
end += std::chrono::milliseconds(ms);
end += std::chrono::microseconds(us);
}

} // namespace sys_pause

0 comments on commit 8a14c70

Please sign in to comment.