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

Reduce duration of delays when the game is running slowly #560

Merged
merged 2 commits into from
Nov 26, 2023
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
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 @@ -188,11 +200,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 @@ -279,10 +279,24 @@ static void _gameLoop() {
SDL_Quit();
}

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 @@ -296,8 +310,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 @@ -307,8 +319,6 @@ static void _nextKeyOrMouseEvent(rogueEvent *returnEvent, boolean textInput, boo
}

while (true) {
tstart = SDL_GetTicks();

if (colorsDance) {
shuffleTerrainColors(3, true);
commitDraws();
Expand All @@ -318,10 +328,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