timer class functions

so i decided to put this here since i this is one of the few places i can actually get good and functional help with my C++/SDL2 developer journey, but anyway, something like 5 or 6 months ago i was doing my online C++ studies and there was the a variation of a timer class:

class Timer
{
private:
	using Clock_t = std::chrono::high_resolution_clock;
	using Second_t = std::chrono::duration<double, std::ratio<1>>;
	std::chrono::time_point<Clock_t>m_begin;
public:
	//Timer() : m_begin{ Clock_t::now() } {}
	void reset() { m_begin = Clock_t::now(); }
	const double elapsed() { return std::chrono::duration_cast<Second_t>(Clock_t::now() - m_begin).count(); }
};

i didnt think to do much with modding it to my liking but now that i have alot more knowledge of things i like toying around with code and doing tons on interesting things. I’m in the process of making a flappy bird variation and want to create a “simulated pause”(in other words, when the player “pauses” i want everything to simply stop by simply setting the game speed to 0) and want this timer to be able to pause as well. If i could find some way to pause the timer, this would change everything i’ve ever put this timer into. I’ve looked around many places online for answers on how to create a pause function for this specific type of timer, but to no avail. Am i missing something? What would be a good solution if i cant get this pause function in?

So the timer can’t be paused (the same as with SDL_GetTicks()), but you can note when you paused it and adjust for that.

This isn’t tested, but here’s the idea:

What you would do is add “pause” and “unpause” methods to that class. In pause(), you note the currrent time (m_pausetime = Clock_t::now();). In “unpause”, you figure out how long you were paused for (pause_duration = Clock_t::now() - m_pausetime;) and then adjust m_begin forward by that much (m_begin += pause_duration;).

(including whatever tapdancing needs to be done for all that duration_cast nonsense. C++ is terrible sometimes.)

The idea is that elapsed tells you the time between m_begin and now, so if m_begin is (whatever the actual resolution is, milliseconds, seconds, idk) 5 and now is 10, then 5 units have elapsed.

So say you call pause while now is 8 and call unpause when it’s at 10, and call elapsed when it’s 12.

So in reality at the end:

  • now = 12
  • we paused for 2 (m_pausetime was 8, subtracted from 10 during unpause).
  • m_begin started at 5, had two added during unpause, is now 7.
  • elapsed returns 12 - 7, for a result of 5.
  • Actual elapsed time is (12 - 5) for a result of 7, but since we paused for 2, 5 is the desired result.
  • This works if you pause and unpause the thing multiple times, since we’re just adjusting the start time to reflect the pausing.

Improvements left to the reader:

  • This will give a wrong result if you call elapsed while still paused.
  • This will fail if you pause when already paused or unpause when not paused, as it will mess up the results.
  • This will fail if you ever extend this class to tell you when the timer was started, since we’re adjusting m_begin.

All of this is just a matter of adding a few more variables to the class, just be aware of it.

okay, thank you. Sorry for the late replay but i just saw this. I’m trying to implement this kind of pause system now, but is there any chance i could make this class more flexible to such semi-important additions like pause and still get the exact same functionality i am now? If you need a reference, try using the class as is above to make a basic stopwatch that in form of “elapsed time”/“set stopwatch length”.

Yes, that’s what my post tells you how to do.