ManaPlus
Namespaces | Functions | Variables
timer.cpp File Reference

(642df39)

#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 93 of file timer.cpp.

94 {
95  const int time = tick_time;
96  if (startTime <= time)
97  {
98  return (time - startTime) * MILLISECONDS_IN_A_TICK;
99  }
100  return (time + (MAX_TICK_VALUE - startTime))
102 }

References MAX_TICK_VALUE, MILLISECONDS_IN_A_TICK, and tick_time.

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

◆ get_elapsed_time1()

int get_elapsed_time1 ( const int  startTime)

Definition at line 104 of file timer.cpp.

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

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 79 of file timer.cpp.

80 {
81  fps = frame_count;
82  lps = logic_count;
83  frame_count = 0;
84  logic_count = 0;
85 
86  return interval;
87 }

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 67 of file timer.cpp.

68 {
69  tick_time = tick_time + 1;
71  tick_time = 0;
72  return interval;
73 }

References MAX_TICK_VALUE, and tick_time.

Referenced by startTimers().

◆ startTimers()

void startTimers ( )

Definition at line 112 of file timer.cpp.

113 {
114  // Initialize logic and seconds counters
115  tick_time = 0;
116  mLogicCounterId = SDL_AddTimer(MILLISECONDS_IN_A_TICK, nextTick, nullptr);
117  mSecondsCounterId = SDL_AddTimer(1000, nextSecond, nullptr);
118 }

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 120 of file timer.cpp.

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

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 53 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 55 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 56 of file timer.cpp.

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

◆ lps

volatile int lps = 0

Logic processed per second

Definition at line 54 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 50 of file timer.cpp.

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

◆ tick_time

volatile int tick_time
fps
volatile int fps
Definition: timer.cpp:53
anonymous_namespace{timer.cpp}::mSecondsCounterId
SDL_TimerID mSecondsCounterId(0)
lps
volatile int lps
Definition: timer.cpp:54
MAX_TICK_VALUE
static const int MAX_TICK_VALUE
Definition: timer.cpp:50
frame_count
volatile int frame_count
Definition: timer.cpp:55
logic_count
volatile int logic_count
Definition: timer.cpp:56
MILLISECONDS_IN_A_TICK
static const int MILLISECONDS_IN_A_TICK
Definition: timer.h:29
nextSecond
static uint32_t nextSecond(uint32_t interval, void *param)
Definition: timer.cpp:79
nextTick
static uint32_t nextTick(uint32_t interval, void *param)
Definition: timer.cpp:67
tick_time
volatile int tick_time
Definition: timer.cpp:52
anonymous_namespace{timer.cpp}::mLogicCounterId
SDL_TimerID mLogicCounterId(0)