Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use chrono steady_clock for time measurement #394

Merged
merged 2 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Descent3/init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,6 @@ void InitIOSystems(bool editor) {

// do io init stuff
io_info.obj = Descent;
io_info.use_lo_res_time = (bool)(FindArg("-lorestimer") != 0);
io_info.joy_emulation = (bool)((FindArg("-alternatejoy") == 0) && (FindArg("-directinput") == 0));
io_info.key_emulation = true; //(bool)(FindArg("-slowkey")!=0); WIN95: DirectInput is flaky for some keys.
INIT_MESSAGE(("Initializing DDIO systems."));
Expand Down Expand Up @@ -2229,7 +2228,6 @@ void RestartD3() {

// startup io
io_info.obj = Descent;
io_info.use_lo_res_time = (bool)(FindArg("-lorestimer") != 0);
io_info.key_emulation = true; //(bool)(FindArg("-slowkey")!=0);
io_info.joy_emulation = (bool)((FindArg("-alternatejoy") == 0) && (FindArg("-directinput") == 0));
if (!ddio_Init(&io_info)) {
Expand Down
3 changes: 1 addition & 2 deletions ddio/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
set(CPPS
chrono_timer.cpp
ddio.cpp
key.cpp
$<$<PLATFORM_ID:Darwin,Linux>:
Expand All @@ -9,7 +10,6 @@ set(CPPS
lnxkey.cpp
lnxkey_null.cpp
lnxmouse.cpp
lnxtimer.cpp
lnxcdrom.cpp
lnxkey_sdl.cpp
>
Expand All @@ -20,7 +20,6 @@ set(CPPS
winjoy.cpp
winkey.cpp
winmouse.cpp
wintimer.cpp
>
)
add_library(ddio STATIC ${CPPS})
Expand Down
36 changes: 36 additions & 0 deletions ddio/chrono_timer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Descent 3
* Copyright (C) 2024 Descent Developers
*
* 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 <http://www.gnu.org/licenses/>.
*/

#include <chrono>
#include "chrono_timer.h"

namespace D3 {

void ChronoTimer::Initialize() { m_start_tstamp = std::chrono::steady_clock::now(); }

float ChronoTimer::GetTime() {
return std::chrono::duration_cast<std::chrono::duration<float>>(std::chrono::steady_clock::now() - m_start_tstamp)
.count();
}

int64_t ChronoTimer::GetTimeMS() {
return std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - m_start_tstamp)
.count();
}

} // namespace D3
44 changes: 44 additions & 0 deletions ddio/chrono_timer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Descent 3
* Copyright (C) 2024 Descent Developers
*
* 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 <http://www.gnu.org/licenses/>.
*/

#pragma once

#include <chrono>

namespace D3 {

static std::chrono::time_point<std::chrono::steady_clock> m_start_tstamp;

/**
* Static class for handling timers and time durations
*/
class ChronoTimer {
public:

/// Initialize internal timestamp
static void Initialize();

/// Get time in seconds after class initialization (i.e. application start)
static float GetTime();

/// Get time in milliseconds after class initialization (i.e. application start)
static int64_t GetTimeMS();

};

}
2 changes: 1 addition & 1 deletion ddio/ddio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ bool ddio_Init(ddio_init_info *init_info) {
res = ddio_InternalInit(init_info);
if (res) {
if (first_time) { // initialize once and only once.
timer_Init(0, init_info->use_lo_res_time);
timer_Init();
}
if (!ddio_KeyInit(init_info))
Error("Failed to initialize keyboard system.");
Expand Down
20 changes: 5 additions & 15 deletions ddio/ddio.h
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,9 @@
#include <cstdio>
#include <cstdint>

#include "pstypes.h"
#include "chrono_timer.h"
#include "ddio_common.h"
#include "pstypes.h"

// ----------------------------------------------------------------------------
// Initialization and destruction functions
Expand Down Expand Up @@ -230,20 +231,9 @@ void ddio_InternalResetKey(uint8_t key);
// Device Dependent Timer Interface
// ----------------------------------------------------------------------------

bool timer_Init(int preemptive, bool force_lores);
void timer_Close();

// returns time in seconds
float timer_GetTime();

// returns time in milliseconds
int64_t timer_GetMSTime();

// hook in timer function at certain period. returns a handle to this function
//@@int timer_HookFunction(void (*fncptr)(), int period);

// clears function from hook list specified by a handle returned from HookFunction
//@@void timer_ReleaseFunction(int func);
#define timer_Init D3::ChronoTimer::Initialize
#define timer_GetTime D3::ChronoTimer::GetTime
#define timer_GetMSTime D3::ChronoTimer::GetTimeMS

// ----------------------------------------------------------------------------
// Device Dependent Mouse Interface
Expand Down
1 change: 0 additions & 1 deletion ddio/ddio_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@

typedef struct ddio_init_info {
oeApplication *obj; // App object used to initialize to IO system
bool use_lo_res_time;
bool key_emulation; // keyboard emulation
bool joy_emulation; // joystick emulation
} ddio_init_info;
Expand Down
158 changes: 0 additions & 158 deletions ddio/lnxtimer.cpp

This file was deleted.

Loading