From 2b9ad0fdfa3a13345268461286c339b30b24e493 Mon Sep 17 00:00:00 2001 From: Ivan Savenko Date: Tue, 30 Apr 2024 15:31:35 +0300 Subject: [PATCH] Fix time handling --- .../eventsSDL/InputSourceGameController.cpp | 21 +++++++++---------- client/eventsSDL/InputSourceGameController.h | 7 +++---- client/renderSDL/CursorHardware.h | 1 - 3 files changed, 13 insertions(+), 16 deletions(-) diff --git a/client/eventsSDL/InputSourceGameController.cpp b/client/eventsSDL/InputSourceGameController.cpp index 2e58a7e9e..38cc8fe31 100644 --- a/client/eventsSDL/InputSourceGameController.cpp +++ b/client/eventsSDL/InputSourceGameController.cpp @@ -26,7 +26,6 @@ void InputSourceGameController::gameControllerDeleter(SDL_GameController * gameC } InputSourceGameController::InputSourceGameController(): - lastCheckTime(0), cursorAxisValueX(0), cursorAxisValueY(0), cursorPlanDisX(0.0), @@ -236,31 +235,31 @@ int InputSourceGameController::getMoveDis(float planDis) void InputSourceGameController::handleUpdate() { - auto now = std::chrono::high_resolution_clock::now(); - auto nowMs = std::chrono::duration_cast(now.time_since_epoch()).count(); - if(lastCheckTime == 0) + std::chrono::steady_clock::time_point nowMs = std::chrono::steady_clock::now(); + + if(lastCheckTime == std::chrono::steady_clock::time_point()) { lastCheckTime = nowMs; return; } - long long deltaTime = nowMs - lastCheckTime; + int32_t deltaTime = std::chrono::duration_cast(nowMs - lastCheckTime).count(); handleCursorUpdate(deltaTime); handleScrollUpdate(deltaTime); lastCheckTime = nowMs; } -void InputSourceGameController::handleCursorUpdate(long long deltaTime) +void InputSourceGameController::handleCursorUpdate(int32_t deltaTimeMs) { if(cursorAxisValueX == 0) cursorPlanDisX = 0; else - cursorPlanDisX += ((float)deltaTime / 1000) * ((float)cursorAxisValueX / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED; + cursorPlanDisX += ((float)deltaTimeMs / 1000) * ((float)cursorAxisValueX / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED; if(cursorAxisValueY == 0) cursorPlanDisY = 0; else - cursorPlanDisY += ((float)deltaTime / 1000) * ((float)cursorAxisValueY / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED; + cursorPlanDisY += ((float)deltaTimeMs / 1000) * ((float)cursorAxisValueY / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED; int moveDisX = getMoveDis(cursorPlanDisX); int moveDisY = getMoveDis(cursorPlanDisY); @@ -269,7 +268,7 @@ void InputSourceGameController::handleCursorUpdate(long long deltaTime) doCursorMove(moveDisX, moveDisY); } -void InputSourceGameController::handleScrollUpdate(long long deltaTime) +void InputSourceGameController::handleScrollUpdate(int32_t deltaTimeMs) { if(!scrollAxisMoved && isScrollAxisReleased()) { @@ -288,8 +287,8 @@ void InputSourceGameController::handleScrollUpdate(long long deltaTime) scrollPlanDisX = scrollPlanDisY = 0; return; } - scrollPlanDisX += ((float)deltaTime / 1000) * ((float)scrollAxisValueX / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED; - scrollPlanDisY += ((float)deltaTime / 1000) * ((float)scrollAxisValueY / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED; + scrollPlanDisX += ((float)deltaTimeMs / 1000) * ((float)scrollAxisValueX / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED; + scrollPlanDisY += ((float)deltaTimeMs / 1000) * ((float)scrollAxisValueY / AXIS_MAX_ZOOM) * AXIS_MOVE_SPEED; int moveDisX = getMoveDis(scrollPlanDisX); int moveDisY = getMoveDis(scrollPlanDisY); if(moveDisX != 0 || moveDisY != 0) diff --git a/client/eventsSDL/InputSourceGameController.h b/client/eventsSDL/InputSourceGameController.h index 6692ad8ed..03564670b 100644 --- a/client/eventsSDL/InputSourceGameController.h +++ b/client/eventsSDL/InputSourceGameController.h @@ -19,7 +19,6 @@ constexpr int AXIS_DEAD_ZOOM = 6000; constexpr int AXIS_MAX_ZOOM = 32000; constexpr int AXIS_MOVE_SPEED = 500; -constexpr int AXIS_CURSOR_MOVE_INTERVAL = 1000; constexpr int TRIGGER_PRESS_THRESHOLD = 8000; enum class AxisType @@ -38,7 +37,7 @@ class InputSourceGameController std::map gameControllerMap; std::set pressedAxes; - long long lastCheckTime; + std::chrono::steady_clock::time_point lastCheckTime; int cursorAxisValueX; int cursorAxisValueY; float cursorPlanDisX; @@ -59,8 +58,8 @@ class InputSourceGameController void tryToConvertCursor(); void doCursorMove(int deltaX, int deltaY); int getMoveDis(float planDis); - void handleCursorUpdate(long long deltaTime); - void handleScrollUpdate(long long deltaTime); + void handleCursorUpdate(int32_t deltaTimeMs); + void handleScrollUpdate(int32_t deltaTimeMs); bool isScrollAxisReleased(); public: diff --git a/client/renderSDL/CursorHardware.h b/client/renderSDL/CursorHardware.h index 3c5509aa2..c4e311778 100644 --- a/client/renderSDL/CursorHardware.h +++ b/client/renderSDL/CursorHardware.h @@ -23,7 +23,6 @@ class CursorHardware : public ICursor std::shared_ptr cursorImage; SDL_Cursor * cursor; - Point pos; public: CursorHardware();