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

Manages time-related functionality for frame rate independence and timing. More...

#include <SDL.h>

Go to the source code of this file.

Classes

struct  TimeSystem
 Structure to handle time-related functionality. More...
 

Typedefs

typedef struct TimeSystem TimeSystem
 Structure to handle time-related functionality.
 

Functions

void Time_PreUpdate ()
 [PreUpdate] Updates the time system values
 
void Time_UpdateFPS ()
 [Utility] Updates the FPS counter
 
void Time_SetTimeScale (float scale)
 [Utility] Sets the time scale.
 
void Time_ResetTotalTime ()
 [Utility] Resets the total time elapsed in the program.
 

Variables

const TimeSystem *const Time
 Global read-only time system pointer for other modules.
 

Detailed Description

Manages time-related functionality for frame rate independence and timing.

The time system struct is used to keep track of the program's time, delta time, and time scale. Time delta is the time between the current frame and the previous frame. It's super useful for making sure that the game runs at the same speed on different fps.

Usage Examples

Usage in updating the position of an object that's moving up with the speed of 5 units per second.

It is quite tempting to move an object something like this:

// This line is called every frame
object.y -= 5; // btw in SDL reducing y moves the object up

Since this is called every frame, the speed will be 5 units/frame. Which causes the speed to be dependent on the frame rate.

To make the speed independent of the frame rate, we can use the delta time like this:

// This line is called every frame
object.y -= 5 * Time->deltaTimeSeconds; // Do this instead!
float deltaTimeSeconds
Time between current frame and previous frame in seconds.
Definition time_system.h:37
const TimeSystem *const Time
Global read-only time system pointer for other modules.
Definition time_system.c:29

This way, the object will move up 5 units per second regardless of the frame rate.

Author
Mango
Date
2025-01-03

Typedef Documentation

◆ TimeSystem

typedef struct TimeSystem TimeSystem

Structure to handle time-related functionality.

Function Documentation

◆ Time_PreUpdate()

void Time_PreUpdate ( )

[PreUpdate] Updates the time system values

Called at the beginning of each frame to calculate delta time and other time-related metrics. Caps deltaTime to prevent spiral of death if the game freezes temporarily.

◆ Time_ResetTotalTime()

void Time_ResetTotalTime ( )

[Utility] Resets the total time elapsed in the program.

Zeroes out the accumulated program time, useful for timing specific operations or resetting game timers.

◆ Time_SetTimeScale()

void Time_SetTimeScale ( float  scale)

[Utility] Sets the time scale.

Adjusts the game's time scale, which can speed up or slow down the entire game. A value of 1.0 is normal speed, while 2.0 would be double speed and 0.5 would be half speed.

Parameters
scaleThe scale to set the time to.

◆ Time_UpdateFPS()

void Time_UpdateFPS ( )

[Utility] Updates the FPS counter

Calculates average FPS over different time scales to monitor performance metrics for the game.

Variable Documentation

◆ Time

const TimeSystem* const Time
extern

Global read-only time system pointer for other modules.