-
Notifications
You must be signed in to change notification settings - Fork 0
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
5 changed files
with
166 additions
and
2 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#include "Timer.h" | ||
|
||
Timer::Timer() | ||
{ | ||
//Initialize the variables | ||
mStartTicks = 0; | ||
mPausedTicks = 0; | ||
|
||
mPaused = false; | ||
mStarted = false; | ||
} | ||
|
||
void Timer::start() | ||
{ | ||
//Start the timer | ||
mStarted = true; | ||
|
||
//Unpause the timer | ||
mPaused = false; | ||
|
||
//Get the current clock time | ||
mStartTicks = SDL_GetTicks(); | ||
mPausedTicks = 0; | ||
} | ||
|
||
void Timer::stop() | ||
{ | ||
//Stop the timer | ||
mStarted = false; | ||
|
||
//Unpause the timer | ||
mPaused = false; | ||
|
||
//Clear tick variables | ||
mStartTicks = 0; | ||
mPausedTicks = 0; | ||
} | ||
|
||
void Timer::pause() | ||
{ | ||
//If the timer is running and isn't already paused | ||
if( mStarted && !mPaused ) | ||
{ | ||
//Pause the timer | ||
mPaused = true; | ||
|
||
//Calculate the paused ticks | ||
mPausedTicks = SDL_GetTicks() - mStartTicks; | ||
mStartTicks = 0; | ||
} | ||
} | ||
|
||
void Timer::unpause() | ||
{ | ||
//If the timer is running and paused | ||
if( mStarted && mPaused ) | ||
{ | ||
//Unpause the timer | ||
mPaused = false; | ||
|
||
//Reset the starting ticks | ||
mStartTicks = SDL_GetTicks() - mPausedTicks; | ||
|
||
//Reset the paused ticks | ||
mPausedTicks = 0; | ||
} | ||
} | ||
|
||
Uint64 Timer::getTicks() | ||
{ | ||
//The actual timer time | ||
Uint64 time = 0; | ||
|
||
//If the timer is running | ||
if( mStarted ) | ||
{ | ||
//If the timer is paused | ||
if( mPaused ) | ||
{ | ||
//Return the number of ticks when the timer was paused | ||
time = mPausedTicks; | ||
} | ||
else | ||
{ | ||
//Return the current time minus the start time | ||
time = SDL_GetTicks() - mStartTicks; | ||
} | ||
} | ||
|
||
return time; | ||
} | ||
|
||
bool Timer::isStarted() | ||
{ | ||
//Timer is running and paused or unpaused | ||
return mStarted; | ||
} | ||
|
||
bool Timer::isPaused() | ||
{ | ||
//Timer is running and paused | ||
return mPaused && mStarted; | ||
} |
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,36 @@ | ||
#ifndef TIMER_H | ||
#define TIMER_H | ||
|
||
#include <SDL3/SDL.h> | ||
|
||
class Timer { | ||
public: | ||
//Initializes variables | ||
Timer(); | ||
|
||
//The various clock actions | ||
void start(); | ||
void stop(); | ||
void pause(); | ||
void unpause(); | ||
|
||
//Gets the timer's time | ||
Uint64 getTicks(); | ||
|
||
//Checks the status of the timer | ||
bool isStarted(); | ||
bool isPaused(); | ||
|
||
private: | ||
//The clock time when the timer started | ||
Uint64 mStartTicks; | ||
|
||
//The ticks stored when the timer was paused | ||
Uint64 mPausedTicks; | ||
|
||
//The timer status | ||
bool mPaused; | ||
bool mStarted; | ||
}; | ||
|
||
#endif |
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