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

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