persona-3-dual
A DS demake of Persona 3
Loading...
Searching...
No Matches
timer.cpp
Go to the documentation of this file.
1#include "timer.hpp"
2#include "logger.hpp"
4
5void Timer::start(ae::q20_12_t durationSeconds)
6{
7 if (durationSeconds.raw_value() < 0)
8 {
9 logMessage(LogSeverity::WARNING, "Called with negative duration");
10 durationSeconds = ae::q20_12_t{0};
11 }
13 durationFrames = static_cast<uint32_t>((static_cast<uint64_t>(durationSeconds.raw_value()) * TARGET_FPS) >> 12);
14 running = true;
15}
16
18{
19 if (!running)
20 {
21 return true;
22 }
23 return (frame - startFrame) >= durationFrames;
24}
25
26ae::q20_12_t Timer::getDuration()
27{
28 return ae::q20_12_t::from_raw_value(
29 static_cast<int32_t>((static_cast<uint64_t>(durationFrames) << 12) / TARGET_FPS));
30}
31
32ae::q20_12_t Timer::getElapsed()
33{
34 uint32_t elapsedFrames = frame - startFrame;
35 return ae::q20_12_t::from_raw_value(
36 static_cast<int32_t>((static_cast<uint64_t>(elapsedFrames) << 12) / TARGET_FPS));
37}
38
39ae::q20_12_t Timer::getProgress()
40{
41 ae::q20_12_t duration = getDuration();
42 if (duration.raw_value() == 0)
43 {
44 return ae::q20_12_t{1};
45 }
46 return MathManager::getInstance().div(getElapsed(), duration);
47}
uint32_t startFrame
Definition timer.hpp:54
bool isFinished()
Checks if the timer expired.
Definition timer.cpp:17
ae::q20_12_t getDuration()
Gets duration of the timer.
Definition timer.cpp:26
bool running
Definition timer.hpp:56
ae::q20_12_t getProgress()
Gets progress of how much of the time has passed.
Definition timer.cpp:39
uint32_t durationFrames
Definition timer.hpp:55
void start(ae::q20_12_t durationSeconds)
Starts a timer.
Definition timer.cpp:5
ae::q20_12_t getElapsed()
Gets elapsed time since the timer was started.
Definition timer.cpp:32
void logMessage(LogSeverity severity, std::string_view message, std::source_location location)
Logs a diagnostic message and halts only for fatal messages.
Definition logger.cpp:110
Util to handle error logging.
Timer util to track if a duration has passed.
constexpr uint32_t TARGET_FPS
Definition timer.hpp:13
volatile int32_t frame
Definition main.cpp:35