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