Operation Null Mind 0.4.1
A 2D roguelike top down shooter game made with SDL2
 
Loading...
Searching...
No Matches
timer.h File Reference

Provides timing functionality for cooldowns, fire rates, animations, and other time-based features. More...

#include <SDL.h>
#include <stdbool.h>

Go to the source code of this file.

Classes

struct  Timer
 The Timer struct used to keep track of time. More...
 

Typedefs

typedef struct Timer Timer
 The Timer struct used to keep track of time.
 

Functions

Timer * Timer_Create (float duration)
 Creates a new timer with the specified duration.
 
void Timer_Start (Timer *timer)
 Starts the timer.
 
bool Timer_IsFinished (Timer *timer)
 Checks if the timer is finished.
 
float Timer_GetTimeLeft (Timer *timer)
 Gets the time left on the timer.
 
bool Timer_IsInProgress (Timer *timer)
 Checks if the timer is in progress.
 
void Timer_Destroy (Timer *timer)
 Destroys the timer and frees its memory.
 

Detailed Description

Provides timing functionality for cooldowns, fire rates, animations, and other time-based features.

Timer has many different uses - for cooldowns, fire rates, animations, etc. Right now, it's just a simple timer that can only be started, but not paused or stopped.

The way you use the timer is by creating a Timer object, setting its duration, and starting it. You can check if the timer is finished or in progress by using Timer_IsFinished() and Timer_IsInProgress().

Cooldown System Example

Usage in a cooldown system

void foo() {
static Timer* cooldown = NULL; // Make sure this is static
if (cooldown != NULL) { // Check there's a cooldown timer in progress
if (Timer_IsFinished(cooldown)) {
Timer_Destroy(cooldown); // Destroy the cooldown timer if it's finished
// This is important to prevent memory leaks
cooldown = NULL;
} else {
return; // Return if the cooldown is still in progress
}
}
// Do something that requires a cooldown
cooldown = Timer_Create(1); // 1 second cooldown
Timer_Start(cooldown); // Start the cooldown
}
The Timer struct used to keep track of time.
Definition timer.h:48
void Timer_Start(Timer *timer)
[Utility] Starts or restarts a timer
Definition timer.c:45
Timer * Timer_Create(float duration)
[Start/Utility] Creates a timer with the specified duration
Definition timer.c:27
void Timer_Destroy(Timer *timer)
[Utility] Destroys a timer and frees its memory
Definition timer.c:105
bool Timer_IsFinished(Timer *timer)
[Utility] Determines whether a timer is finished
Definition timer.c:61
Author
Mango
Date
2025-03-02

Typedef Documentation

◆ Timer

typedef struct Timer Timer

The Timer struct used to keep track of time.

Used for cooldowns, delays, etc. It has a start time, an elapsed time, a duration, and flags to check if the timer is in progress or finished.

Function Documentation

◆ Timer_Create()

Timer * Timer_Create ( float  duration)

Creates a new timer with the specified duration.

Parameters
durationThe duration of the timer in seconds.
Returns
A pointer to the newly created timer.

Creates a new timer with the specified duration.

Allocates and initializes a new timer with the given duration. The timer starts in a stopped state and must be started with Timer_Start().

Parameters
durationThe duration of the timer in seconds
Returns
Timer* Pointer to the newly created timer

◆ Timer_Destroy()

void Timer_Destroy ( Timer *  timer)

Destroys the timer and frees its memory.

Parameters
timerThe timer to destroy.

Destroys the timer and frees its memory.

Parameters
timerPointer to the timer to destroy

◆ Timer_GetTimeLeft()

float Timer_GetTimeLeft ( Timer *  timer)

Gets the time left on the timer.

Parameters
timerThe timer to check.
Returns
The time left in seconds.

Gets the time left on the timer.

Returns how many seconds are left before the timer completes. Returns 0 if the timer is not running or has already finished.

Parameters
timerPointer to the timer to check
Returns
float Time left in seconds, or 0 if the timer is not running

◆ Timer_IsFinished()

bool Timer_IsFinished ( Timer *  timer)

Checks if the timer is finished.

Parameters
timerThe timer to check.
Returns
true if the timer is finished, false otherwise.

Checks if the timer is finished.

A timer is finished when Current Time - Timer start time > Timer duration. If the timer is not running, returns the cached finished state.

Parameters
timerPointer to the timer to check
Returns
bool True if the timer has finished, false otherwise

◆ Timer_IsInProgress()

bool Timer_IsInProgress ( Timer *  timer)

Checks if the timer is in progress.

Parameters
timerThe timer to check.
Returns
true if the timer is in progress, false otherwise.

Checks if the timer is in progress.

Checks if the timer is in the "in progress" state.

Parameters
timerPointer to the timer to check
Returns
bool True if the timer is in progress, false otherwise

◆ Timer_Start()

void Timer_Start ( Timer *  timer)

Starts the timer.

Parameters
timerThe timer to start.

Starts the timer.

Sets the start time to the current time and marks the timer as in progress. If the timer was already running, this will restart it.

Parameters
timerPointer to the timer to start