mirror of
https://github.com/lov3b/Pong.git
synced 2025-04-18 12:20:17 +02:00
52 lines
1.6 KiB
C++
52 lines
1.6 KiB
C++
// SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
#include "OptionScreen.h"
|
|
#include <chrono>
|
|
|
|
|
|
int_least64_t getCurrentEpochTimeMillis() {
|
|
using namespace std::chrono;
|
|
|
|
auto now = system_clock::now();
|
|
auto now_ms = time_point_cast<std::chrono::milliseconds>(now);
|
|
auto epoch = now_ms.time_since_epoch();
|
|
auto value = duration_cast<std::chrono::milliseconds>(epoch);
|
|
return value.count();
|
|
}
|
|
|
|
OptionScreen::OptionScreen(const std::string &text, SDL_Point *screenSize, int seconds) : TextScreen(text, screenSize,
|
|
std::nullopt),
|
|
stepsToDo(seconds) {
|
|
secondaryFont = TTF_OpenFont(getDefaultFontPath(), 100);
|
|
if (secondaryFont == nullptr) {
|
|
std::cerr << "Failed to load secondary font: " << TTF_GetError() << std::endl;
|
|
exit(-1);
|
|
}
|
|
|
|
}
|
|
|
|
void OptionScreen::update() {
|
|
auto now = getCurrentEpochTimeMillis();
|
|
if (hasStartedCounting_ && nextMsEpoch <= now) {
|
|
if (stepsDone < stepsToDo) {
|
|
std::string s = std::to_string(stepsToDo - stepsDone);
|
|
setText(s);
|
|
nextMsEpoch = now + 1000;
|
|
hasUpdated = false;
|
|
stepsDone++;
|
|
} else {
|
|
isDone_ = true;
|
|
std::swap(font, secondaryFont);
|
|
}
|
|
}
|
|
|
|
TextScreen::update();
|
|
}
|
|
|
|
void OptionScreen::startCountDown() {
|
|
auto epochMs = getCurrentEpochTimeMillis();
|
|
nextMsEpoch = epochMs;
|
|
hasStartedCounting_ = true;
|
|
stepsDone = 0;
|
|
std::swap(font, secondaryFont);
|
|
} |