GCC Code Coverage Report
Directory: src/ Exec Total Coverage
File: src/utils/timer.cpp Lines: 15 30 50.0 %
Date: 2021-03-17 Branches: 4 6 66.7 %

Line Branch Exec Source
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
47
/**
48
 * Tells the max tick value,
49
 * setting it back to zero (and start again).
50
 */
51
static const int MAX_TICK_VALUE = INT_MAX / 2;
52
53
volatile int tick_time;       /**< Tick counter */
54
volatile int fps = 0;         /**< Frames counted in the last second */
55
volatile int lps = 0;         /**< Logic processed per second */
56
volatile int frame_count = 0; /**< Counts the frames during one second */
57
volatile int logic_count = 0; /**< Counts the logic during one second */
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
63
/**
64
 * Advances game logic counter.
65
 * Called every 10 milliseconds by SDL_AddTimer()
66
 * @see MILLISECONDS_IN_A_TICK value
67
 */
68
static uint32_t nextTick(uint32_t interval, void *param A_UNUSED)
69
{
70
    tick_time = tick_time + 1;
71
    if (tick_time == MAX_TICK_VALUE)
72
        tick_time = 0;
73
    return interval;
74
}
75
76
/**
77
 * Updates fps.
78
 * Called every seconds by SDL_AddTimer()
79
 */
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
90
/**
91
 * @return the elapsed time in milliseconds
92
 * between two tick values.
93
 */
94
14
int get_elapsed_time(const int startTime)
95
{
96
14
    const int time = tick_time;
97
14
    if (startTime <= time)
98
    {
99
8
        return (time - startTime) * MILLISECONDS_IN_A_TICK;
100
    }
101
6
    return (time + (MAX_TICK_VALUE - startTime))
102
6
        * MILLISECONDS_IN_A_TICK;
103
}
104
105
14
int get_elapsed_time1(const int startTime)
106
{
107
14
    const int time = tick_time;
108
14
    if (startTime <= time)
109
8
        return time - startTime;
110
6
    return time + (MAX_TICK_VALUE - startTime);
111
}
112
113
void startTimers()
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
121
204
void stopTimers()
122
{
123
204
    SDL_RemoveTimer(mLogicCounterId);
124
204
    SDL_RemoveTimer(mSecondsCounterId);
125
204
}