|
| 1 | +/* |
| 2 | +* |
| 3 | +* rtime 0.1.0 |
| 4 | +
|
| 5 | +* Copyright (C) 2025 Riley Mabb (@ColleagueRiley) |
| 6 | +* |
| 7 | +* libpng license |
| 8 | +* |
| 9 | +* This software is provided 'as-is', without any express or implied |
| 10 | +* warranty. In no event will the authors be held liable for any damages |
| 11 | +* arising from the use of this software. |
| 12 | +
|
| 13 | +* Permission is granted to anyone to use this software for any purpose, |
| 14 | +* including commercial applications, and to alter it and redistribute it |
| 15 | +* freely, subject to the following restrictions: |
| 16 | +* |
| 17 | +* 1. The origin of this software must not be misrepresented; you must not |
| 18 | +* claim that you wrote the original software. If you use this software |
| 19 | +* in a product, an acknowledgment in the product documentation would be |
| 20 | +* appreciated but is not required. |
| 21 | +* 2. Altered source versions must be plainly marked as such, and must not be |
| 22 | +* misrepresented as being the original software. |
| 23 | +* 3. This notice may not be removed or altered from any source distribution. |
| 24 | +* |
| 25 | +* |
| 26 | +*/ |
| 27 | + |
| 28 | +/* |
| 29 | + (MAKE SURE RT_IMPLEMENTATION is in exactly one header or you use -D RT_IMPLEMENTATION) |
| 30 | + #define RT_IMPLEMENTATION - makes it so source code is included with header |
| 31 | +*/ |
| 32 | + |
| 33 | +/* |
| 34 | + #define RT_IMPLEMENTATION - (required) makes it so the source code is included |
| 35 | +
|
| 36 | + #define RT_EXPORT - use when building RTIME |
| 37 | + #define RT_IMPORT - use when linking with RTIME (not as a single-header) |
| 38 | +*/ |
| 39 | + |
| 40 | +#if defined(RT_EXPORT) || defined(RT_IMPORT) |
| 41 | + #if defined(_WIN32) |
| 42 | + #if defined(__TINYC__) && (defined(RT_EXPORT) || defined(RT_IMPORT)) |
| 43 | + #define __declspec(x) __attribute__((x)) |
| 44 | + #endif |
| 45 | + |
| 46 | + #if defined(RT_EXPORT) |
| 47 | + #define RT_API __declspec(dllexport) |
| 48 | + #else |
| 49 | + #define RT_API __declspec(dllimport) |
| 50 | + #endif |
| 51 | + #else |
| 52 | + #if defined(RT_EXPORT) |
| 53 | + #define RT_API __attribute__((visibility("default"))) |
| 54 | + #endif |
| 55 | + #endif |
| 56 | + #ifndef RT_API |
| 57 | + #define RT_API |
| 58 | + #endif |
| 59 | +#endif |
| 60 | + |
| 61 | +#ifndef RT_API |
| 62 | + #define RT_API inline |
| 63 | +#endif |
| 64 | + |
| 65 | +#ifdef __EMSCRIPTEN__ |
| 66 | + #define RT_WASM |
| 67 | +#endif |
| 68 | + |
| 69 | +#if defined(_WIN32) && !defined(RT_UNIX) && !defined(RT_WASM) |
| 70 | + #define RT_WINDOWS |
| 71 | +#elif !defined(RT_UNIX) && defined(__unix__) && !defined(RT_WASM) && !defined(__APPLE__) |
| 72 | + #define RT_UNIX |
| 73 | +#elif defined(__APPLE__) && !defined(RT_UNIX) && !defined(RT_WASM) |
| 74 | + #define RT_MACOS |
| 75 | +#endif |
| 76 | + |
| 77 | +#if defined(__cplusplus) && !defined(__EMSCRIPTEN__) |
| 78 | + extern "C" { |
| 79 | +#endif |
| 80 | + |
| 81 | +/* makes sure the header file part is only defined once by default */ |
| 82 | +#ifndef RT_HEADER |
| 83 | + |
| 84 | +#define RT_HEADER |
| 85 | + |
| 86 | +#include <stdint.h> |
| 87 | + |
| 88 | +/**! |
| 89 | + * @brief Set starting time for getTime to use |
| 90 | + * @return the time in seconds |
| 91 | +*/ |
| 92 | +RT_API void rt_setTime(double time); /*!< set timer in seconds */ |
| 93 | + |
| 94 | +/**! |
| 95 | + * @brief Retrieves delta time between the setTime value and the current time in seconds |
| 96 | + * @return the time in seconds |
| 97 | +*/ |
| 98 | +RT_API double rt_getTime(void); |
| 99 | + |
| 100 | +/**! |
| 101 | + * @brief Retrieves delta time between the setTime value and the current time in nanoseconds |
| 102 | + * @return the time in nanosecnds |
| 103 | +*/ |
| 104 | +RT_API uint64_t rt_getTimeNS(void); |
| 105 | + |
| 106 | +/**! |
| 107 | + * @brief sleep, pausing the thread for a given amount of time |
| 108 | + * @return time length of the pause in miliseconds |
| 109 | +*/ |
| 110 | +RT_API void rt_sleep(uint64_t milisecond); |
| 111 | + |
| 112 | +/**! |
| 113 | + * @brief Retrieves get source API timer value. |
| 114 | + * @return The source API timer value. |
| 115 | +*/ |
| 116 | +RT_API uint64_t rt_getTimerValue(void); |
| 117 | + |
| 118 | +/**! |
| 119 | + * @brief Retrieves the source api time frequency |
| 120 | + * @return The source API timer frequency. |
| 121 | +*/ |
| 122 | +RT_API uint64_t rt_getTimerFreq(void); |
| 123 | + |
| 124 | +#endif |
| 125 | + |
| 126 | +#ifdef RT_IMPLEMENTATION |
| 127 | + |
| 128 | +#ifdef RT_WINDOWS |
| 129 | + #define WIN32_LEAN_AND_MEAN |
| 130 | + #define OEMRESOURCE |
| 131 | + #include <windows.h> |
| 132 | +#elif defined(RT_WASM) |
| 133 | + #include <emscripten/html5.h> |
| 134 | +#else |
| 135 | + #include <time.h> |
| 136 | +#endif |
| 137 | + |
| 138 | +#ifdef RT_MACOS |
| 139 | + #include <mach/mach_time.h> |
| 140 | +#endif |
| 141 | + |
| 142 | +uint64_t rt_timerOffset = 0; |
| 143 | +void rt_setTime(double time) { |
| 144 | + rt_timerOffset = rt_getTimerValue() - (uint64_t)(time * (double)rt_getTimerFreq()); |
| 145 | +} |
| 146 | + |
| 147 | +double rt_getTime(void) { |
| 148 | + return (double) ((double)(rt_getTimerValue() - rt_timerOffset) / (double)rt_getTimerFreq()); |
| 149 | +} |
| 150 | + |
| 151 | +uint64_t rt_getTimeNS(void) { |
| 152 | + return (uint64_t)(((double)((rt_getTimerValue() - rt_timerOffset)) * 1e9) / (double)rt_getTimerFreq()); |
| 153 | +} |
| 154 | + |
| 155 | +void rt_sleep(uint64_t ms) { |
| 156 | +#ifdef RT_WINDOWS |
| 157 | + Sleep((uint32_t)ms); |
| 158 | +#elif defined(RT_WASM) |
| 159 | + emscripten_sleep(ms); |
| 160 | +#else |
| 161 | + struct timespec time; |
| 162 | + time.tv_sec = 0; |
| 163 | + time.tv_nsec = (long int)((double)ms * 1e+6); |
| 164 | + |
| 165 | + nanosleep(&time, NULL); |
| 166 | +#endif |
| 167 | +} |
| 168 | + |
| 169 | +uint64_t rt_getTimerFreq(void) { |
| 170 | +#ifdef RT_WINDOWS |
| 171 | + static uint64_t frequency = 0; |
| 172 | + if (frequency == 0) QueryPerformanceFrequency((LARGE_INTEGER*)&frequency); |
| 173 | + |
| 174 | + return frequency; |
| 175 | +#elif defined(RT_WASM) |
| 176 | + return emscripten_get_now() * 1e+6; |
| 177 | +#elif defined(RT_MACOS) |
| 178 | + static uint64_t freq = 0; |
| 179 | + if (freq == 0) { |
| 180 | + mach_timebase_info_data_t info; |
| 181 | + mach_timebase_info(&info); |
| 182 | + freq = (uint64_t)((info.denom * 1e9) / info.numer); |
| 183 | + } |
| 184 | + |
| 185 | + return freq; |
| 186 | +#else |
| 187 | + static int32_t clock = -1; |
| 188 | + if (clock == -1) { |
| 189 | + #if defined(_POSIX_MONOTONIC_CLOCK) |
| 190 | + struct timespec ts; |
| 191 | + if (clock_gettime(CLOCK_MONOTONIC, &ts) == 0) |
| 192 | + clock = CLOCK_MONOTONIC; |
| 193 | + #else |
| 194 | + clock = CLOCK_REALTIME; |
| 195 | + #endif |
| 196 | + } |
| 197 | + |
| 198 | + struct timespec ts; |
| 199 | + clock_gettime(CLOCK_REALTIME, &ts); |
| 200 | + return (uint64_t)ts.tv_sec * rt_getTimerFreq() + (uint64_t)ts.tv_nsec; |
| 201 | +#endif |
| 202 | +} |
| 203 | + |
| 204 | +uint64_t rt_getTimerValue(void) { |
| 205 | +#ifdef RT_WINDOWS |
| 206 | + uint64_t value; |
| 207 | + QueryPerformanceCounter((LARGE_INTEGER*)&value); |
| 208 | + return value; |
| 209 | +#elif defined(RT_WASM) |
| 210 | + return (uint64_t)1000; |
| 211 | +#elif defined(RT_MACOS) |
| 212 | + return (uint64_t)mach_absolute_time(); |
| 213 | +#else |
| 214 | + return 1000000000LLU; |
| 215 | +#endif |
| 216 | +} |
| 217 | + |
| 218 | +#endif /* RT_IMPLEMENTATION */ |
| 219 | + |
| 220 | +#if defined(__cplusplus) && !defined(__EMSCRIPTEN__) |
| 221 | +} |
| 222 | +#endif |
| 223 | + |
0 commit comments