ManaPlus
timer.cpp
Go to the documentation of this file.
1 /*
2  * The ManaPlus Client
3  * Copyright (C) 2011-2017 The ManaPlus Developers
4  *
5  * This file is part of The ManaPlus Client.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
21 #include "utils/timer.h"
22 
23 #include "const/utils/timer.h"
24 
25 PRAGMA48(GCC diagnostic push)
26 PRAGMA48(GCC diagnostic ignored "-Wshadow")
27 #include <SDL_timer.h>
28 PRAGMA48(GCC diagnostic pop)
29 
30 #include <climits>
31 
32 #include "debug.h"
33 
34 namespace
35 {
36 #ifdef USE_SDL2
37  SDL_TimerID mLogicCounterId(0);
38  SDL_TimerID mSecondsCounterId(0);
39 #else // USE_SDL2
40 
41  SDL_TimerID mLogicCounterId(nullptr);
42  SDL_TimerID mSecondsCounterId(nullptr);
43 #endif // USE_SDL2
44 } // namespace
45 
50 static const int MAX_TICK_VALUE = INT_MAX / 2;
51 
52 volatile int tick_time;
53 volatile int fps = 0;
54 volatile int lps = 0;
55 volatile int frame_count = 0;
56 volatile int logic_count = 0;
57 volatile time_t cur_time = 0;
58 
59 static uint32_t nextTick(uint32_t interval, void *param A_UNUSED);
60 static uint32_t nextSecond(uint32_t interval, void *param A_UNUSED);
61 
67 static uint32_t nextTick(uint32_t interval, void *param A_UNUSED)
68 {
69  tick_time++;
70  if (tick_time == MAX_TICK_VALUE)
71  tick_time = 0;
72  return interval;
73 }
74 
79 static uint32_t nextSecond(uint32_t interval, void *param A_UNUSED)
80 {
81  fps = frame_count;
82  lps = logic_count;
83  frame_count = 0;
84  logic_count = 0;
85 
86  return interval;
87 }
88 
93 int get_elapsed_time(const int startTime)
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 }
103 
104 int get_elapsed_time1(const int startTime)
105 {
106  const int time = tick_time;
107  if (startTime <= time)
108  return time - startTime;
109  return time + (MAX_TICK_VALUE - startTime);
110 }
111 
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 }
119 
121 {
122  SDL_RemoveTimer(mLogicCounterId);
123  SDL_RemoveTimer(mSecondsCounterId);
124 }
static uint32_t nextTick(uint32_t interval, void *param)
Definition: timer.cpp:67
volatile int lps
Definition: timer.cpp:54
volatile int frame_count
Definition: timer.cpp:55
volatile int fps
Definition: timer.cpp:53
int get_elapsed_time(const int startTime)
Definition: timer.cpp:93
volatile int logic_count
Definition: timer.cpp:56
int get_elapsed_time1(const int startTime)
Definition: timer.cpp:104
void startTimers()
Definition: timer.cpp:112
volatile int tick_time
Definition: timer.cpp:52
static const int MAX_TICK_VALUE
Definition: timer.cpp:50
static const int MILLISECONDS_IN_A_TICK
Definition: timer.h:29
#define PRAGMA48(str)
Definition: localconsts.h:214
#define A_UNUSED
Definition: localconsts.h:171
void stopTimers()
Definition: timer.cpp:120
static uint32_t nextSecond(uint32_t interval, void *param)
Definition: timer.cpp:79
volatile time_t cur_time
Definition: timer.cpp:57