| 1 |
// |
| 2 |
// Copyright(C) 2020 by Ryan Krafnick |
| 3 |
// |
| 4 |
// This program is free software; you can redistribute it and/or |
| 5 |
// modify it under the terms of the GNU General Public License |
| 6 |
// as published by the Free Software Foundation; either version 2 |
| 7 |
// of the License, or (at your option) any later version. |
| 8 |
// |
| 9 |
// This program is distributed in the hope that it will be useful, |
| 10 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 |
// GNU General Public License for more details. |
| 13 |
// |
| 14 |
// DESCRIPTION: |
| 15 |
// DSDA Time |
| 16 |
// |
| 17 |
|
| 18 |
#include <time.h> |
| 19 |
#include <string.h> |
| 20 |
|
| 21 |
#include "i_system.h" |
| 22 |
#include "lprintf.h" |
| 23 |
|
| 24 |
#include "dsda/args.h" |
| 25 |
#include "dsda/configuration.h" |
| 26 |
|
| 27 |
#include "time.h" |
| 28 |
|
| 29 |
// clock_gettime implementation for msvc |
| 30 |
// NOTE: Only supports CLOCK_MONOTONIC |
| 31 |
#ifdef _MSC_VER |
| 32 |
|
| 33 |
#include <windows.h> |
| 34 |
|
| 35 |
#define CLOCK_MONOTONIC -1 |
| 36 |
|
| 37 |
static int clock_gettime(int clockid, struct timespec *tp) { |
| 38 |
static unsigned long long timer_frequency = 0; |
| 39 |
unsigned long long time; |
| 40 |
|
| 41 |
// Get number of timer counts per second |
| 42 |
if (!timer_frequency) |
| 43 |
QueryPerformanceFrequency((LARGE_INTEGER*) &timer_frequency); |
| 44 |
|
| 45 |
// Get timer counts |
| 46 |
QueryPerformanceCounter((LARGE_INTEGER*) &time); |
| 47 |
|
| 48 |
// Convert timer counts to timespec (that is, nanoseconds and seconds) |
| 49 |
tp->tv_nsec = time % timer_frequency * 1000000000 / timer_frequency; |
| 50 |
tp->tv_sec = time / timer_frequency; |
| 51 |
|
| 52 |
return 0; |
| 53 |
} |
| 54 |
|
| 55 |
#endif //_MSC_VER |
| 56 |
|
| 57 |
static struct timespec dsda_time[DSDA_TIMER_COUNT]; |
| 58 |
|
| 59 |
void dsda_StartTimer(int timer) { |
| 60 |
clock_gettime(CLOCK_MONOTONIC, &dsda_time[timer]); |
| 61 |
} |
| 62 |
|
| 63 |
unsigned long long dsda_ElapsedTime(int timer) { |
| 64 |
struct timespec now; |
| 65 |
|
| 66 |
clock_gettime(CLOCK_MONOTONIC, &now); |
| 67 |
|
| 68 |
return (unsigned long long) ( |
| 69 |
(signed long long) (now.tv_nsec - dsda_time[timer].tv_nsec) / 1000 + |
| 70 |
(signed long long) (now.tv_sec - dsda_time[timer].tv_sec) * 1000000 |
| 71 |
); |
| 72 |
} |
| 73 |
|
| 74 |
unsigned long long dsda_ElapsedTimeMS(int timer) { |
| 75 |
return dsda_ElapsedTime(timer) / 1000; |
| 76 |
} |
| 77 |
|
| 78 |
void dsda_PrintElapsedTime(int timer, const char* message) { |
| 79 |
unsigned long long result; |
| 80 |
|
| 81 |
result = dsda_ElapsedTime(timer); |
| 82 |
lprintf(LO_INFO, "%s: %lf\n", message, (double) result / 1000); |
| 83 |
} |
| 84 |
|
| 85 |
static void dsda_Throttle(int timer, unsigned long long target_time) { |
| 86 |
unsigned long long elapsed_time; |
| 87 |
unsigned long long remaining_time; |
| 88 |
|
| 89 |
while (1) { |
| 90 |
elapsed_time = dsda_ElapsedTime(timer); |
| 91 |
|
| 92 |
if (elapsed_time >= target_time) { |
| 93 |
dsda_StartTimer(timer); |
| 94 |
return; |
| 95 |
} |
| 96 |
|
| 97 |
// Sleeping doesn't have high accuracy |
| 98 |
remaining_time = target_time - elapsed_time; |
| 99 |
if (remaining_time > 1000) |
| 100 |
I_uSleep(remaining_time - 1000); |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
void dsda_LimitFPS(void) { |
| 105 |
extern int movement_smooth; |
| 106 |
extern int window_focused; |
| 107 |
|
| 108 |
int allow_limit; |
| 109 |
int fps_limit; |
| 110 |
|
| 111 |
allow_limit = (movement_smooth || !window_focused) && !dsda_Flag(dsda_arg_timedemo) && !dsda_Flag(dsda_arg_fastdemo); |
| 112 |
fps_limit = window_focused ? dsda_IntConfig(dsda_config_fps_limit) |
| 113 |
: dsda_IntConfig(dsda_config_background_fps_limit); |
| 114 |
|
| 115 |
if (allow_limit && fps_limit) { |
| 116 |
unsigned long long target_time; |
| 117 |
|
| 118 |
target_time = 1000000 / fps_limit; |
| 119 |
|
| 120 |
dsda_Throttle(dsda_timer_fps, target_time); |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
#define TICRATE 35 |
| 125 |
|
| 126 |
int dsda_GameSpeed(void); |
| 127 |
|
| 128 |
static unsigned long long dsda_RealTime(void) { |
| 129 |
static dboolean started = false; |
| 130 |
|
| 131 |
if (!started) |
| 132 |
{ |
| 133 |
started = true; |
| 134 |
dsda_StartTimer(dsda_timer_realtime); |
| 135 |
} |
| 136 |
|
| 137 |
return dsda_ElapsedTime(dsda_timer_realtime); |
| 138 |
} |
| 139 |
|
| 140 |
static unsigned long long dsda_ScaledTime(void) { |
| 141 |
return dsda_RealTime() * dsda_GameSpeed() / 100; |
| 142 |
} |
| 143 |
|
| 144 |
extern int ms_to_next_tick; |
| 145 |
|
| 146 |
// During a fast demo, each call yields a new tick |
| 147 |
static int dsda_GetTickFastDemo(void) |
| 148 |
{ |
| 149 |
static int tick; |
| 150 |
return tick++; |
| 151 |
} |
| 152 |
|
| 153 |
int dsda_GetTickRealTime(void) { |
| 154 |
int i; |
| 155 |
unsigned long long t; |
| 156 |
|
| 157 |
t = dsda_RealTime(); |
| 158 |
|
| 159 |
i = t * TICRATE / 1000000; |
| 160 |
ms_to_next_tick = (i + 1) * 1000 / TICRATE - t / 1000; |
| 161 |
if (ms_to_next_tick > 1000 / TICRATE) ms_to_next_tick = 1; |
| 162 |
if (ms_to_next_tick < 1) ms_to_next_tick = 0; |
| 163 |
return i; |
| 164 |
} |
| 165 |
|
| 166 |
static int dsda_TickMS(int n) { |
| 167 |
return n * 1000 * 100 / dsda_GameSpeed() / TICRATE; |
| 168 |
} |
| 169 |
|
| 170 |
static int dsda_GetTickScaledTime(void) { |
| 171 |
int i; |
| 172 |
unsigned long long t; |
| 173 |
|
| 174 |
t = dsda_RealTime(); |
| 175 |
|
| 176 |
i = t * TICRATE * dsda_GameSpeed() / 100 / 1000000; |
| 177 |
ms_to_next_tick = dsda_TickMS(i + 1) - t / 1000; |
| 178 |
if (ms_to_next_tick > dsda_TickMS(1)) ms_to_next_tick = 1; |
| 179 |
if (ms_to_next_tick < 1) ms_to_next_tick = 0; |
| 180 |
return i; |
| 181 |
} |
| 182 |
|
| 183 |
// During a fast demo, no time elapses in between ticks |
| 184 |
static unsigned long long dsda_TickElapsedTimeFastDemo(void) { |
| 185 |
return 0; |
| 186 |
} |
| 187 |
|
| 188 |
static unsigned long long dsda_TickElapsedRealTime(void) { |
| 189 |
int tick = dsda_GetTick(); |
| 190 |
|
| 191 |
return dsda_RealTime() - (unsigned long long) tick * 1000000 / TICRATE; |
| 192 |
} |
| 193 |
|
| 194 |
static unsigned long long dsda_TickElapsedScaledTime(void) { |
| 195 |
int tick = dsda_GetTick(); |
| 196 |
|
| 197 |
return dsda_ScaledTime() - (unsigned long long) tick * 1000000 / TICRATE; |
| 198 |
} |
| 199 |
|
| 200 |
int (*dsda_GetTick)(void) = dsda_GetTickRealTime; |
| 201 |
unsigned long long (*dsda_TickElapsedTime)(void) = dsda_TickElapsedRealTime; |
| 202 |
|
| 203 |
void dsda_ResetTimeFunctions(int fastdemo) { |
| 204 |
if (fastdemo) { |
| 205 |
dsda_GetTick = dsda_GetTickFastDemo; |
| 206 |
dsda_TickElapsedTime = dsda_TickElapsedTimeFastDemo; |
| 207 |
} |
| 208 |
else if (dsda_GameSpeed() != 100) { |
| 209 |
dsda_GetTick = dsda_GetTickScaledTime; |
| 210 |
dsda_TickElapsedTime = dsda_TickElapsedScaledTime; |
| 211 |
} |
| 212 |
else { |
| 213 |
dsda_GetTick = dsda_GetTickRealTime; |
| 214 |
dsda_TickElapsedTime = dsda_TickElapsedRealTime; |
| 215 |
} |
| 216 |
} |