diff --git a/OpenHellion/HiResTime.cs b/OpenHellion/HiResTime.cs new file mode 100644 index 0000000..5fbc4c8 --- /dev/null +++ b/OpenHellion/HiResTime.cs @@ -0,0 +1,57 @@ +// HiResTime.cs +// +// Copyright (C) 2023, OpenHellion contributors +// +// SPDX-License-Identifier: GPL-3.0-or-later +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +using System; +using System.Diagnostics; + +namespace OpenHellion; + +/// +/// High-Resolution time. Counts milliseconds and uses a long. +/// This should make it be able to run continuously for 300 000 000 years. +/// Perhaps slightly overkill. +/// +public static class HiResTime +{ + private static Stopwatch _stopwatch; + + public static ulong Milliseconds + { + get + { + if (_stopwatch == null) + { + throw new InvalidOperationException("The HiResTime class has not been started. Call the Start() method first."); + } + + return (ulong)_stopwatch.ElapsedMilliseconds; + } + } + + public static void Start() + { + _stopwatch = Stopwatch.StartNew(); + } + + public static void Stop() + { + _stopwatch.Stop(); + _stopwatch = null; + } +} diff --git a/ZeroGravity/HiResTime.cs b/ZeroGravity/HiResTime.cs deleted file mode 100644 index 5bfe0ca..0000000 --- a/ZeroGravity/HiResTime.cs +++ /dev/null @@ -1,61 +0,0 @@ -using System.Runtime.InteropServices; - -namespace ZeroGravity; - -public static class HiResTime -{ - private static bool msTimerSupported; - - private static uint msHigh = 0u; - - private static uint msLastLow = 0u; - - public static ulong Milliseconds - { - get - { - if (!msTimerSupported) - { - return GetTickCount64(); - } - uint msTickCount = timeGetTime(); - if (msTickCount < msLastLow) - { - msHigh++; - } - msLastLow = msTickCount; - return msTickCount | ((ulong)msHigh << 32); - } - } - - [DllImport("kernel32")] - public static extern ulong GetTickCount64(); - - [DllImport("winmm.dll")] - private static extern int timeBeginPeriod(int msec); - - [DllImport("winmm.dll")] - private static extern int timeEndPeriod(int msec); - - [DllImport("winmm.dll")] - private static extern uint timeGetTime(); - - [DllImport("Kernel32")] - private static extern bool QueryPerformanceFrequency(out ulong freq); - - [DllImport("Kernel32")] - private static extern bool QueryPerformanceCounter(out ulong count); - - public static void Start() - { - msTimerSupported = timeBeginPeriod(1) == 0; - } - - public static void Stop() - { - if (msTimerSupported) - { - timeEndPeriod(1); - } - } -} diff --git a/ZeroGravity/Objects/SolarSystem.cs b/ZeroGravity/Objects/SolarSystem.cs index 2ea85a8..2c86afa 100644 --- a/ZeroGravity/Objects/SolarSystem.cs +++ b/ZeroGravity/Objects/SolarSystem.cs @@ -5,6 +5,7 @@ using System.Threading; using System.Threading.Tasks; using OpenHellion.Networking; +using OpenHellion; using ZeroGravity.Data; using ZeroGravity.Math; using ZeroGravity.Network;