ManaPlus
Namespaces | Functions | Variables
timer.cpp File Reference

(986a3bf)

#include "utils/timer.h"
#include "const/utils/timer.h"
#include <SDL_timer.h>
#include <climits>
#include "debug.h"

Go to the source code of this file.

Namespaces

 anonymous_namespace{timer.cpp}
 

Functions

SDL_TimerID anonymous_namespace{timer.cpp}::mLogicCounterId (0)
 
SDL_TimerID anonymous_namespace{timer.cpp}::mSecondsCounterId (0)
 
static uint32_t nextTick (uint32_t interval, void *param)
 
static uint32_t nextSecond (uint32_t interval, void *param)
 
int get_elapsed_time (const int startTime)
 
int get_elapsed_time1 (const int startTime)
 
void startTimers ()
 
void stopTimers ()
 

Variables

static const int MAX_TICK_VALUE = INT_MAX / 2
 
volatile int tick_time
 
volatile int fps = 0
 
volatile int lps = 0
 
volatile int frame_count = 0
 
volatile int logic_count = 0
 
volatile time_t cur_time = 0
 

Function Documentation

◆ get_elapsed_time()

int get_elapsed_time ( const int  startTime)
Returns
the elapsed time in milliseconds between two tick values.

Definition at line 94 of file timer.cpp.

95 {
96  const int time = tick_time;
97  if (startTime <= time)
98  {
99  return (time - startTime) * MILLISECONDS_IN_A_TICK;
100  }
101  return (time + (MAX_TICK_VALUE - startTime))
103 }
static const int MILLISECONDS_IN_A_TICK
Definition: timer.h:30
static const int MAX_TICK_VALUE
Definition: timer.cpp:51
volatile int tick_time
Definition: timer.cpp:53

References MAX_TICK_VALUE, MILLISECONDS_IN_A_TICK, and tick_time.

Referenced by Palette::advanceGradient(), Being::getOffset(), Being::logic(), LocalPlayer::logic(), SkillDialog::slowLogic(), and Map::updateAmbientLayers().

◆ get_elapsed_time1()

int get_elapsed_time1 ( const int  startTime)

Definition at line 105 of file timer.cpp.

106 {
107  const int time = tick_time;
108  if (startTime <= time)
109  return time - startTime;
110  return time + (MAX_TICK_VALUE - startTime);
111 }

References MAX_TICK_VALUE, and tick_time.

Referenced by Being::logic(), Game::slowLogic(), and CompoundSprite::updateImages().

◆ nextSecond()

static uint32_t nextSecond ( uint32_t  interval,
void *  param 
)
static

Updates fps. Called every seconds by SDL_AddTimer()

Definition at line 80 of file timer.cpp.

81 {
82  fps = frame_count;
83  lps = logic_count;
84  frame_count = 0;
85  logic_count = 0;
86 
87  return interval;
88 }
volatile int lps
Definition: timer.cpp:55
volatile int logic_count
Definition: timer.cpp:57
volatile int frame_count
Definition: timer.cpp:56
volatile int fps
Definition: timer.cpp:54

References fps, frame_count, logic_count, and lps.

Referenced by startTimers().

◆ nextTick()

static uint32_t nextTick ( uint32_t  interval,
void *  param 
)
static

Advances game logic counter. Called every 10 milliseconds by SDL_AddTimer()

See also
MILLISECONDS_IN_A_TICK value

Definition at line 68 of file timer.cpp.

69 {
70  tick_time = tick_time + 1;
72  tick_time = 0;
73  return interval;
74 }

References MAX_TICK_VALUE, and tick_time.

Referenced by startTimers().

◆ startTimers()

void startTimers ( )

Definition at line 113 of file timer.cpp.

114 {
115  // Initialize logic and seconds counters
116  tick_time = 0;
117  mLogicCounterId = SDL_AddTimer(MILLISECONDS_IN_A_TICK, nextTick, nullptr);
118  mSecondsCounterId = SDL_AddTimer(1000, nextSecond, nullptr);
119 }
static uint32_t nextTick(uint32_t interval, void *param)
Definition: timer.cpp:68
static uint32_t nextSecond(uint32_t interval, void *param)
Definition: timer.cpp:80

References MILLISECONDS_IN_A_TICK, anonymous_namespace{timer.cpp}::mLogicCounterId(), anonymous_namespace{timer.cpp}::mSecondsCounterId(), nextSecond(), nextTick(), and tick_time.

Referenced by Client::gameInit().

◆ stopTimers()

void stopTimers ( )

Definition at line 121 of file timer.cpp.

122 {
123  SDL_RemoveTimer(mLogicCounterId);
124  SDL_RemoveTimer(mSecondsCounterId);
125 }

References anonymous_namespace{timer.cpp}::mLogicCounterId(), and anonymous_namespace{timer.cpp}::mSecondsCounterId().

Referenced by Client::gameClear().

Variable Documentation

◆ cur_time

volatile time_t cur_time = 0

◆ fps

volatile int fps = 0

Frames counted in the last second

Definition at line 54 of file timer.cpp.

Referenced by Game::adjustPerfomance(), MapDebugTab::logic(), and nextSecond().

◆ frame_count

volatile int frame_count = 0

Counts the frames during one second

Definition at line 56 of file timer.cpp.

Referenced by Client::gameExec(), and nextSecond().

◆ logic_count

volatile int logic_count = 0

Counts the logic during one second

Definition at line 57 of file timer.cpp.

Referenced by Client::gameExec(), and nextSecond().

◆ lps

volatile int lps = 0

Logic processed per second

Definition at line 55 of file timer.cpp.

Referenced by StatDebugTab::logic(), and nextSecond().

◆ MAX_TICK_VALUE

const int MAX_TICK_VALUE = INT_MAX / 2
static

Tells the max tick value, setting it back to zero (and start again).

Definition at line 51 of file timer.cpp.

Referenced by get_elapsed_time(), get_elapsed_time1(), and nextTick().

◆ tick_time

volatile int tick_time