diff options
author | Stephan Vedder <vedder@mbits.info> | 2022-04-29 02:28:01 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-29 02:28:01 +0300 |
commit | dbe069e42e1cea27654a8fb618fdde77f41ac257 (patch) | |
tree | 7a93d8183564852ce61e1348104b8f6e9f5bb853 | |
parent | f8b1f1db48d8e43153f6e0ace97d470bc9fbf141 (diff) |
Use the SDL2 perf counter (#50)
Using SDL2 functions will allow us to be compatible to more platforms
and not having to maintain our own implementations for each platform.
-rw-r--r-- | Projects/CMakeLists.txt | 4 | ||||
-rw-r--r-- | Source/Compat/Mac/mac_time.cpp | 35 | ||||
-rw-r--r-- | Source/Compat/Win/win_time.cpp | 42 | ||||
-rw-r--r-- | Source/Compat/time.cpp (renamed from Source/Compat/Linux/linux_time.cpp) | 19 |
4 files changed, 9 insertions, 91 deletions
diff --git a/Projects/CMakeLists.txt b/Projects/CMakeLists.txt index 21c9fce2..5be55093 100644 --- a/Projects/CMakeLists.txt +++ b/Projects/CMakeLists.txt @@ -1147,6 +1147,7 @@ FILE(GLOB OVERGROWTH_INTERNAL_SRCS RELATIVE ${CMAKE_SOURCE_DIR} ${SRCDIR}/Compat/compat.h ${SRCDIR}/Compat/filepath.h ${SRCDIR}/Compat/hardware_info.h + ${SRCDIR}/Compat/time.cpp ${SRCDIR}/Compat/time.h ${SRCDIR}/Compat/os_dialogs.h ${SRCDIR}/Compat/fileio.cpp @@ -1171,7 +1172,6 @@ IF(LINUX) ${SRCDIR}/Compat/Linux/linux_compat.cpp ${SRCDIR}/Compat/Linux/linux_compat.h ${SRCDIR}/Compat/Linux/linux_hardware_info.cpp - ${SRCDIR}/Compat/Linux/linux_time.cpp ) ENDIF() @@ -1184,7 +1184,6 @@ IF(APPLE) ${SRCDIR}/Compat/Mac/mac_compat.mm ${SRCDIR}/Compat/Mac/mac_compat.h ${SRCDIR}/Compat/Mac/mac_hardware_info.cpp - ${SRCDIR}/Compat/Mac/mac_time.cpp ) ENDIF() @@ -1195,7 +1194,6 @@ IF(WIN32) ${SRCDIR}/Compat/Win/win_compat.cpp ${SRCDIR}/Compat/Win/win_compat.h ${SRCDIR}/Compat/Win/win_hardware_info.cpp - ${SRCDIR}/Compat/Win/win_time.cpp ) IF(MSVC_VERSION LESS 1911) diff --git a/Source/Compat/Mac/mac_time.cpp b/Source/Compat/Mac/mac_time.cpp deleted file mode 100644 index 6607b42a..00000000 --- a/Source/Compat/Mac/mac_time.cpp +++ /dev/null @@ -1,35 +0,0 @@ -//----------------------------------------------------------------------------- -// Name: mac_time.cpp -// Developer: Wolfire Games LLC -// Description: -// License: Read below -//----------------------------------------------------------------------------- -// -// Copyright 2022 Wolfire Games LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -//----------------------------------------------------------------------------- -#include <Compat/time.h> - -#include <CoreServices/CoreServices.h> - -uint64_t GetPrecisionTime() { - AbsoluteTime upTime = UpTime(); - return *(uint64_t*)&upTime; -} - -uint64_t ToNanoseconds(uint64_t time){ - Nanoseconds elapsedNano = AbsoluteToNanoseconds(*(AbsoluteTime*)&time ); - return *(uint64_t*)&elapsedNano; -} diff --git a/Source/Compat/Win/win_time.cpp b/Source/Compat/Win/win_time.cpp deleted file mode 100644 index 6751fe7c..00000000 --- a/Source/Compat/Win/win_time.cpp +++ /dev/null @@ -1,42 +0,0 @@ -//----------------------------------------------------------------------------- -// Name: win_time.cpp -// Developer: Wolfire Games LLC -// Description: -// License: Read below -//----------------------------------------------------------------------------- -// -// Copyright 2022 Wolfire Games LLC -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -// -//----------------------------------------------------------------------------- -#include <Compat/time.h> - -#define NOMINMAX -#include <windows.h> - -uint64_t GetPrecisionTime() { - LARGE_INTEGER tick; - QueryPerformanceCounter(&tick); - return (uint64_t)tick.QuadPart; -} - -uint64_t ToNanoseconds(uint64_t time){ - LARGE_INTEGER ticksPerSecond; - QueryPerformanceFrequency(&ticksPerSecond); - - time *= 1000000000; - time /= ticksPerSecond.QuadPart; - - return time; -} diff --git a/Source/Compat/Linux/linux_time.cpp b/Source/Compat/time.cpp index 8b10f287..a35da9c2 100644 --- a/Source/Compat/Linux/linux_time.cpp +++ b/Source/Compat/time.cpp @@ -1,5 +1,5 @@ //----------------------------------------------------------------------------- -// Name: linux_time.cpp +// Name: time.cpp // Developer: Wolfire Games LLC // Description: // License: Read below @@ -20,19 +20,16 @@ // limitations under the License. // //----------------------------------------------------------------------------- -#include <Compat/time.h> - -#include <sys/time.h> -#include <stdlib.h> +#include "time.h" +#include <SDL.h> uint64_t GetPrecisionTime() { - struct timeval time; - - gettimeofday(&time, NULL); - return time.tv_usec + - (time.tv_sec * 1000 * 1000); + return SDL_GetPerformanceCounter(); } uint64_t ToNanoseconds(uint64_t time){ - return time * 1000; + uint64_t ticksPerSecond = SDL_GetPerformanceFrequency(); + + // Multiply with 1e9 to get nanoseconds + return time * 1e9 / ticksPerSecond; } |