Replaced all calls to clock() in Qt TAS editor. Clock was giving inconsistent time stamps since it actually returns CPU use time, not actual time. Instead use SDL_GetTicks() to get actual time in milliseconds.

This commit is contained in:
mjbudd77 2022-01-08 05:31:15 -05:00
parent ed29139b87
commit 94eab8f8ef
16 changed files with 78 additions and 58 deletions

View File

@ -99,6 +99,7 @@ enum DRAG_MODES
static char pianoRollSaveID[PIANO_ROLL_ID_LEN] = "PIANO_ROLL";
static char pianoRollSkipSaveID[PIANO_ROLL_ID_LEN] = "PIANO_ROLX";
static TasFindNoteWindow *findWin = NULL;
static uint64_t tasEditorTimeStamp = 0;
//----------------------------------------------------------------------------
//---- Main TAS Editor Window
@ -131,6 +132,11 @@ bool isTaseditorRecording(void)
return true; // record
}
uint64_t getTasEditorTime(void)
{
return tasEditorTimeStamp;
}
void recordInputByTaseditor(void)
{
if ( recorder )
@ -1443,6 +1449,11 @@ QPoint TasEditorWindow::getPreviewPopupCoordinates(void)
//----------------------------------------------------------------------------
int TasEditorWindow::initModules(void)
{
#if SDL_VERSION_ATLEAST(2, 0, 18)
tasEditorTimeStamp = SDL_GetTicks64();
#else
tasEditorTimeStamp = SDL_GetTicks();
#endif
// init modules
//editor.init();
//pianoRoll.init();
@ -1505,7 +1516,13 @@ void TasEditorWindow::frameUpdate(void)
{
FCEU_WRAPPER_LOCK();
//printf("TAS Frame Update: %zi\n", currMovieData.records.size());
#if SDL_VERSION_ATLEAST(2, 0, 18)
tasEditorTimeStamp = SDL_GetTicks64();
#else
tasEditorTimeStamp = SDL_GetTicks();
#endif
//printf("TAS Frame Update: %zi %u\n", currMovieData.records.size(), tasEditorTimeStamp);
//taseditorWindow.update();
greenzone.update();
@ -4634,7 +4651,7 @@ void QPianoRoll::mouseDoubleClickEvent(QMouseEvent * event)
// clicked on Input
if (headerClicked)
{
drawingStartTimestamp = clock();
drawingStartTimestamp = getTasEditorTime();
int joy = (column_index - COLUMN_JOYPAD1_A) / NUM_JOYPAD_BUTTONS;
int button = (column_index - COLUMN_JOYPAD1_A) % NUM_JOYPAD_BUTTONS;
int selection_beginning = selection->getCurrentRowsSelectionBeginning();
@ -4654,7 +4671,7 @@ void QPianoRoll::mouseDoubleClickEvent(QMouseEvent * event)
selection->setRowSelection(row_index);
}
// toggle Input
drawingStartTimestamp = clock();
drawingStartTimestamp = getTasEditorTime();
int joy = (column_index - COLUMN_JOYPAD1_A) / NUM_JOYPAD_BUTTONS;
int button = (column_index - COLUMN_JOYPAD1_A) % NUM_JOYPAD_BUTTONS;
int selection_beginning = selection->getCurrentRowsSelectionBeginning();
@ -4908,7 +4925,7 @@ void QPianoRoll::mousePressEvent(QMouseEvent * event)
// clicked on Input
if (headerClicked)
{
drawingStartTimestamp = clock();
drawingStartTimestamp = getTasEditorTime();
int joy = (column_index - COLUMN_JOYPAD1_A) / NUM_JOYPAD_BUTTONS;
int button = (column_index - COLUMN_JOYPAD1_A) % NUM_JOYPAD_BUTTONS;
int selection_beginning = selection->getCurrentRowsSelectionBeginning();
@ -4928,7 +4945,7 @@ void QPianoRoll::mousePressEvent(QMouseEvent * event)
selection->setRowSelection(row_index);
}
// toggle Input
drawingStartTimestamp = clock();
drawingStartTimestamp = getTasEditorTime();
int joy = (column_index - COLUMN_JOYPAD1_A) / NUM_JOYPAD_BUTTONS;
int button = (column_index - COLUMN_JOYPAD1_A) % NUM_JOYPAD_BUTTONS;
int selection_beginning = selection->getCurrentRowsSelectionBeginning();
@ -5805,9 +5822,9 @@ void QPianoRoll::periodicUpdate(void)
updateDrag();
// once per 40 milliseconds update colors alpha in the Header
if (clock() > nextHeaderUpdateTime)
if (getTasEditorTime() > nextHeaderUpdateTime)
{
nextHeaderUpdateTime = clock() + HEADER_LIGHT_UPDATE_TICK;
nextHeaderUpdateTime = getTasEditorTime() + HEADER_LIGHT_UPDATE_TICK;
bool changes_made = false;
int light_value = 0;
// 1 - update Frame# columns' heads
@ -5873,7 +5890,7 @@ void QPianoRoll::setLightInHeaderColumn(int column, int level)
{
headerColors[column] = level;
//redrawHeader();
nextHeaderUpdateTime = clock() + HEADER_LIGHT_UPDATE_TICK;
nextHeaderUpdateTime = getTasEditorTime() + HEADER_LIGHT_UPDATE_TICK;
}
}
//----------------------------------------------------------------------------

View File

@ -3,6 +3,7 @@
#pragma once
#include <stdint.h>
#include <time.h>
#include <string>
#include <list>
@ -104,7 +105,7 @@ enum PIANO_ROLL_COLUMNS
#define HEADER_LIGHT_HOLD 5
#define HEADER_LIGHT_MOUSEOVER_SEL 3
#define HEADER_LIGHT_MOUSEOVER 0
#define HEADER_LIGHT_UPDATE_TICK (CLOCKS_PER_SEC / 25) // 25FPS
#define HEADER_LIGHT_UPDATE_TICK (40) // 25FPS
struct NewProjectParameters
{
@ -298,8 +299,8 @@ class QPianoRoll : public QWidget
int mouse_x;
int mouse_y;
int gridPixelWidth;
clock_t drawingStartTimestamp;
clock_t nextHeaderUpdateTime;
uint64_t drawingStartTimestamp;
uint64_t nextHeaderUpdateTime;
int playbackCursorPos;
@ -654,4 +655,6 @@ void tasWindowSetFocus(bool val);
bool isTaseditorRecording(void);
void recordInputByTaseditor(void);
uint64_t getTasEditorTime(void);
extern TasEditorWindow *tasWin;

View File

@ -119,7 +119,7 @@ void BOOKMARKS::reset_vars()
mouseOverBranchesBitmap = false;
mustCheckItemUnderMouse = true;
bookmarkLeftclicked = bookmarkRightclicked = ITEM_UNDER_MOUSE_NONE;
nextFlashUpdateTime = clock() + BOOKMARKS_FLASH_TICK;
nextFlashUpdateTime = getTasEditorTime() + BOOKMARKS_FLASH_TICK;
imageItem = 0;
}
@ -201,9 +201,9 @@ void BOOKMARKS::update()
commands.resize(0);
// once per 100 milliseconds update bookmark flashes
if (clock() > nextFlashUpdateTime)
if (getTasEditorTime() > nextFlashUpdateTime)
{
nextFlashUpdateTime = clock() + BOOKMARKS_FLASH_TICK;
nextFlashUpdateTime = getTasEditorTime() + BOOKMARKS_FLASH_TICK;
for (int i = 0; i < TOTAL_BOOKMARKS; ++i)
{
if (bookmarkRightclicked == i || bookmarkLeftclicked == i)

View File

@ -42,7 +42,7 @@ enum BOOKMARK_COMMANDS
#define ITEM_UNDER_MOUSE_CLOUD (-1)
#define ITEM_UNDER_MOUSE_FIREBALL (TOTAL_BOOKMARKS)
#define BOOKMARKS_FLASH_TICK (CLOCKS_PER_SEC / 10) // 10 Hz
#define BOOKMARKS_FLASH_TICK 100 // in milliseconds
// listview columns
enum
@ -128,7 +128,7 @@ private:
std::vector<int> commands;
int selectedSlot;
int mouseX, mouseY;
clock_t nextFlashUpdateTime;
uint64_t nextFlashUpdateTime;
// GUI stuff
QFont font;

View File

@ -237,19 +237,19 @@ void BRANCHES::resetVars()
playbackCursorX = playbackCursorY = 0;
branchRightclicked = lastItemUnderMouse = -1;
mustRecalculateBranchesTree = mustRedrawBranchesBitmap = true;
nextAnimationTime = clock() + BRANCHES_ANIMATION_TICK;
nextAnimationTime = getTasEditorTime() + BRANCHES_ANIMATION_TICK;
}
void BRANCHES::update()
{
clock_t currentTime;
unsigned int currentTime;
if (mustRecalculateBranchesTree)
{
recalculateBranchesTree();
}
currentTime = clock();
currentTime = getTasEditorTime();
// once per 40 milliseconds update branches_bitmap
if (currentTime > nextAnimationTime)

View File

@ -9,7 +9,7 @@
#include <QTimer>
#include <QWidget>
#define BRANCHES_ANIMATION_TICK (CLOCKS_PER_SEC / 25) // animate at 25FPS
#define BRANCHES_ANIMATION_TICK (40) // animate at 25FPS
#define BRANCHES_TRANSITION_MAX 12
#define CURSOR_MIN_DISTANCE 1.0
#define CURSOR_MAX_DISTANCE 256.0
@ -179,7 +179,7 @@ private:
// not saved vars
int transitionPhase;
int currentAnimationFrame;
clock_t nextAnimationTime;
uint64_t nextAnimationTime;
int playbackCursorX, playbackCursorY;
double cornersCursorX, cornersCursorY;
std::vector<int> branchX; // in pixels

View File

@ -41,7 +41,7 @@ GREENZONE::GREENZONE()
void GREENZONE::init()
{
reset();
nextCleaningTime = clock() + TIME_BETWEEN_CLEANINGS;
nextCleaningTime = getTasEditorTime() + TIME_BETWEEN_CLEANINGS;
}
void GREENZONE::free()
{
@ -67,7 +67,7 @@ void GREENZONE::update()
}
// run cleaning from time to time
if (clock() > nextCleaningTime)
if (getTasEditorTime() > nextCleaningTime)
runGreenzoneCleaning();
// also log lag frames
@ -180,7 +180,7 @@ finish:
bookmarks->redrawBookmarksList();
}
// shedule next cleaning
nextCleaningTime = clock() + TIME_BETWEEN_CLEANINGS;
nextCleaningTime = getTasEditorTime() + TIME_BETWEEN_CLEANINGS;
}
// returns true if actually cleared savestate data

View File

@ -7,7 +7,7 @@
#define GREENZONE_ID_LEN 10
#define TIME_BETWEEN_CLEANINGS (10 * CLOCKS_PER_SEC)
#define TIME_BETWEEN_CLEANINGS (10000)
// Greenzone cleaning masks
#define EVERY16TH 0xFFFFFFF0
@ -61,6 +61,6 @@ private:
std::vector<std::vector<uint8_t>> savestates;
// not saved data
clock_t nextCleaningTime;
uint64_t nextCleaningTime;
};

View File

@ -101,7 +101,7 @@ void HISTORY::init(void)
{
updateScheduled = false;
// shedule first autocompression
nextAutocompressTime = clock() + TIME_BETWEEN_AUTOCOMPRESSIONS;
nextAutocompressTime = getTasEditorTime() + TIME_BETWEEN_AUTOCOMPRESSIONS;
}
void HISTORY::free()
{
@ -144,7 +144,7 @@ void HISTORY::update()
showUndoHint = false;
if (undoHintPos >= 0)
{
if ((int)clock() < undoHintTimer)
if (getTasEditorTime() < undoHintTimer)
showUndoHint = true;
else
undoHintPos = -1; // finished hinting
@ -159,7 +159,7 @@ void HISTORY::update()
// pianoRoll.redrawRow(undoHintPos);
// When CPU is idle, compress items from time to time
if (clock() > nextAutocompressTime)
if (getTasEditorTime() > nextAutocompressTime)
{
if (FCEUI_EmulationPaused())
{
@ -179,7 +179,7 @@ void HISTORY::update()
}
}
}
nextAutocompressTime = clock() + TIME_BETWEEN_AUTOCOMPRESSIONS;
nextAutocompressTime = getTasEditorTime() + TIME_BETWEEN_AUTOCOMPRESSIONS;
}
}
@ -347,7 +347,7 @@ int HISTORY::jumpInTime(int new_pos)
undoHintPos = getCurrentSnapshot().keyFrame; // redo
else
undoHintPos = getNextToCurrentSnapshot().keyFrame; // undo
undoHintTimer = clock() + UNDO_HINT_TIME;
undoHintTimer = getTasEditorTime() + UNDO_HINT_TIME;
showUndoHint = true;
real_pos = (historyStartPos + historyCursorPos) % historySize;

View File

@ -8,10 +8,10 @@
#include "Qt/TasEditor/bookmark.h"
#include "Qt/TasEditor/snapshot.h"
#define UNDO_HINT_TIME (CLOCKS_PER_SEC / 5) // in milliseconds
#define UNDO_HINT_TIME (200) // in milliseconds
#define SAVING_HISTORY_PROGRESSBAR_UPDATE_RATE 10
#define TIME_BETWEEN_AUTOCOMPRESSIONS (CLOCKS_PER_SEC / 2) // in milliseconds
#define TIME_BETWEEN_AUTOCOMPRESSIONS (500) // in milliseconds
#define HISTORY_LIST_WIDTH 500
@ -157,10 +157,10 @@ private:
int historySize;
int undoHintPos, oldUndoHintPos;
clock_t undoHintTimer;
uint64_t undoHintTimer;
bool showUndoHint, oldShowUndoHint;
bool updateScheduled;
clock_t nextAutocompressTime;
uint64_t nextAutocompressTime;
};

View File

@ -71,10 +71,10 @@ void PLAYBACK::update()
{
if (!rewindButtonOldState)
{
buttonHoldTimer = clock();
buttonHoldTimer = getTasEditorTime();
//handleRewindFrame();
}
else if (buttonHoldTimer + BUTTON_HOLD_REPEAT_DELAY < clock())
else if (buttonHoldTimer + BUTTON_HOLD_REPEAT_DELAY < getTasEditorTime())
{
handleRewindFrame();
}
@ -85,10 +85,10 @@ void PLAYBACK::update()
{
if (!forwardButtonOldState)
{
buttonHoldTimer = clock();
buttonHoldTimer = getTasEditorTime();
//handleForwardFrame();
}
else if (buttonHoldTimer + BUTTON_HOLD_REPEAT_DELAY < clock())
else if (buttonHoldTimer + BUTTON_HOLD_REPEAT_DELAY < getTasEditorTime())
{
handleForwardFrame();
}
@ -100,10 +100,10 @@ void PLAYBACK::update()
{
if (!rewindFullButtonOldState)
{
buttonHoldTimer = clock();
buttonHoldTimer = getTasEditorTime();
//handleRewindFull();
}
else if (buttonHoldTimer + BUTTON_HOLD_REPEAT_DELAY < clock())
else if (buttonHoldTimer + BUTTON_HOLD_REPEAT_DELAY < getTasEditorTime())
{
handleRewindFull();
}
@ -114,10 +114,10 @@ void PLAYBACK::update()
{
if (!forwardFullButtonOldState)
{
buttonHoldTimer = clock();
buttonHoldTimer = getTasEditorTime();
//handleForwardFull();
}
else if (buttonHoldTimer + BUTTON_HOLD_REPEAT_DELAY < clock())
else if (buttonHoldTimer + BUTTON_HOLD_REPEAT_DELAY < getTasEditorTime())
{
handleForwardFull();
}
@ -178,9 +178,9 @@ void PLAYBACK::update()
if (pauseFrame)
{
if (emuPausedState)
showPauseFrame = (int)(clock() / PAUSEFRAME_BLINKING_PERIOD_WHEN_PAUSED) & 1;
showPauseFrame = (int)(getTasEditorTime() / PAUSEFRAME_BLINKING_PERIOD_WHEN_PAUSED) & 1;
else
showPauseFrame = (int)(clock() / PAUSEFRAME_BLINKING_PERIOD_WHEN_SEEKING) & 1;
showPauseFrame = (int)(getTasEditorTime() / PAUSEFRAME_BLINKING_PERIOD_WHEN_SEEKING) & 1;
}
else
{

View File

@ -6,10 +6,10 @@
#define PROGRESSBAR_WIDTH 200
#define PAUSEFRAME_BLINKING_PERIOD_WHEN_SEEKING (CLOCKS_PER_SEC / 10)
#define PAUSEFRAME_BLINKING_PERIOD_WHEN_PAUSED (CLOCKS_PER_SEC / 4)
#define PAUSEFRAME_BLINKING_PERIOD_WHEN_SEEKING (100)
#define PAUSEFRAME_BLINKING_PERIOD_WHEN_PAUSED (250)
#define BUTTON_HOLD_REPEAT_DELAY (CLOCKS_PER_SEC / 4) // in milliseconds
#define BUTTON_HOLD_REPEAT_DELAY (250) // in milliseconds
class UpperMarkerNoteEdit : public QLineEdit
{
@ -90,7 +90,7 @@ private:
bool forwardButtonState, forwardButtonOldState;
bool rewindFullButtonState, rewindFullButtonOldState;
bool forwardFullButtonState, forwardFullButtonOldState;
clock_t buttonHoldTimer;
uint64_t buttonHoldTimer;
int seekingBeginningFrame;
};

View File

@ -93,9 +93,9 @@ void SELECTION::update()
{
if (!previousMarkerButtonOldState)
{
buttonHoldTimer = clock();
buttonHoldTimer = getTasEditorTime();
jumpToPreviousMarker();
} else if (buttonHoldTimer + BUTTON_HOLD_REPEAT_DELAY < clock())
} else if (buttonHoldTimer + BUTTON_HOLD_REPEAT_DELAY < getTasEditorTime())
{
jumpToPreviousMarker();
}
@ -106,10 +106,10 @@ void SELECTION::update()
{
if (!nextMarkerButtonOldState)
{
buttonHoldTimer = clock();
buttonHoldTimer = getTasEditorTime();
jumpToNextMarker();
}
else if (buttonHoldTimer + BUTTON_HOLD_REPEAT_DELAY < clock())
else if (buttonHoldTimer + BUTTON_HOLD_REPEAT_DELAY < getTasEditorTime())
{
jumpToNextMarker();
}

View File

@ -104,7 +104,7 @@ private:
bool previousMarkerButtonState, previousMarkerButtonOldState;
bool nextMarkerButtonState, nextMarkerButtonOldState;
clock_t buttonHoldTimer;
uint64_t buttonHoldTimer;
std::vector<RowsSelection> rowsSelectionHistory;

View File

@ -52,7 +52,7 @@ void TASEDITOR_PROJECT::reset()
void TASEDITOR_PROJECT::update()
{
// if it's time to autosave - pop Save As dialog
if (changed && /*taseditorWindow.TASEditorIsInFocus &&*/ taseditorConfig->autosaveEnabled && !projectFile.empty() && clock() >= nextSaveShedule /*&& pianoRoll.dragMode == DRAG_MODE_NONE*/)
if (changed && /*taseditorWindow.TASEditorIsInFocus &&*/ taseditorConfig->autosaveEnabled && !projectFile.empty() && getTasEditorTime() >= nextSaveShedule /*&& pianoRoll.dragMode == DRAG_MODE_NONE*/)
{
if (taseditorConfig->autosaveSilent)
{
@ -411,7 +411,7 @@ bool TASEDITOR_PROJECT::getProjectChanged()
void TASEDITOR_PROJECT::sheduleNextAutosave()
{
nextSaveShedule = clock() + taseditorConfig->autosavePeriod * AUTOSAVE_PERIOD_SCALE;
nextSaveShedule = getTasEditorTime() + taseditorConfig->autosavePeriod * AUTOSAVE_PERIOD_SCALE;
}

View File

@ -24,7 +24,7 @@
#define LVS_EX_DOUBLEBUFFER 0x00010000
#endif
#define AUTOSAVE_PERIOD_SCALE (60 * CLOCKS_PER_SEC) // = 1 minute in milliseconds
#define AUTOSAVE_PERIOD_SCALE (60000) // = 1 minute in milliseconds
#define MARKERS_SAVED 1
#define BOOKMARKS_SAVED 2
@ -69,7 +69,7 @@ public:
private:
bool changed;
bool updateCaptionFlag;
clock_t nextSaveShedule;
uint64_t nextSaveShedule;
std::string projectFile; // full path
std::string projectName; // file name only