Skip to content

Commit

Permalink
Reduce duration of delays when the game is running slowly (#560)
Browse files Browse the repository at this point in the history
  • Loading branch information
nstoddard authored Nov 26, 2023
1 parent 33de9a1 commit 8a67af2
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 18 deletions.
1 change: 1 addition & 0 deletions changes/autoexplore-delay.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Animations such as autoexplore and using a staff now run at the same speed regardless of game performance (up to a limit).
26 changes: 17 additions & 9 deletions src/platform/curses-platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,24 @@ static uint64_t getTime() {
return (uint64_t)tv.tv_sec * 1000 + tv.tv_usec / 1000;
}

static long lastDelayTime = 0;

// Like SDL_Delay, but reduces the delay if time has passed since the last delay
static void _delayUpTo(short ms) {
long curTime = getTime();
long timeDiff = curTime - lastDelayTime;
ms -= timeDiff;

if (ms > 0) {
Term.wait(ms);
} // else delaying further would go past the time we want to delay until

lastDelayTime = getTime();
}

static boolean curses_pauseForMilliseconds(short milliseconds) {
Term.refresh();
Term.wait(milliseconds);
_delayUpTo(milliseconds);

// hasKey returns true if we have a mouse event, too.
return Term.hasKey();
Expand All @@ -124,14 +139,11 @@ static boolean curses_pauseForMilliseconds(short milliseconds) {
static void curses_nextKeyOrMouseEvent(rogueEvent *returnEvent, boolean textInput, boolean colorsDance) {
int key;
// TCOD_mouse_t mouse;
uint64_t theTime, waitTime;
// short x, y;

Term.refresh();

for (;;) {
theTime = getTime(); //TCOD_sys_elapsed_milli();

/*if (TCOD_console_is_window_closed()) {
rogue.gameHasEnded = true; // causes the game loop to terminate quickly
returnEvent->eventType = KEYSTROKE;
Expand Down Expand Up @@ -189,11 +201,7 @@ static void curses_nextKeyOrMouseEvent(rogueEvent *returnEvent, boolean textInpu
return;
}

waitTime = PAUSE_BETWEEN_EVENT_POLLING + theTime - getTime();

if (waitTime > 0 && waitTime <= PAUSE_BETWEEN_EVENT_POLLING) {
curses_pauseForMilliseconds(waitTime);
}
_delayUpTo(PAUSE_BETWEEN_EVENT_POLLING);
}
}

Expand Down
25 changes: 16 additions & 9 deletions src/platform/sdl2-platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,10 +282,24 @@ static void _gameLoop() {
exit(statusCode);
}

static long lastDelayTime = 0;

// Like SDL_Delay, but reduces the delay if time has passed since the last delay
static void _delayUpTo(short ms) {
long curTime = SDL_GetTicks();
long timeDiff = curTime - lastDelayTime;
ms -= timeDiff;

if (ms > 0) {
SDL_Delay(ms);
} // else delaying further would go past the time we want to delay until

lastDelayTime = SDL_GetTicks();
}

static boolean _pauseForMilliseconds(short ms) {
updateScreen();
SDL_Delay(ms);
_delayUpTo(ms);

if (lastEvent.eventType != EVENT_ERROR
&& lastEvent.eventType != MOUSE_ENTERED_CELL) {
Expand All @@ -299,8 +313,6 @@ static boolean _pauseForMilliseconds(short ms) {


static void _nextKeyOrMouseEvent(rogueEvent *returnEvent, boolean textInput, boolean colorsDance) {
long tstart, dt;

updateScreen();

if (lastEvent.eventType != EVENT_ERROR) {
Expand All @@ -310,8 +322,6 @@ static void _nextKeyOrMouseEvent(rogueEvent *returnEvent, boolean textInput, boo
}

while (true) {
tstart = SDL_GetTicks();

if (colorsDance) {
shuffleTerrainColors(3, true);
commitDraws();
Expand All @@ -321,10 +331,7 @@ static void _nextKeyOrMouseEvent(rogueEvent *returnEvent, boolean textInput, boo

if (pollBrogueEvent(returnEvent, textInput)) break;

dt = PAUSE_BETWEEN_EVENT_POLLING - (SDL_GetTicks() - tstart);
if (dt > 0) {
SDL_Delay(dt);
}
_delayUpTo(PAUSE_BETWEEN_EVENT_POLLING);
}
}

Expand Down

0 comments on commit 8a67af2

Please sign in to comment.