Use delay_ms instead of spinwait (#262)

This commit is contained in:
Vasilii Rogin 2024-01-16 14:24:49 +02:00 committed by GitHub
parent 601f0c7b44
commit e23c4eddda
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 81 additions and 80 deletions

View File

@ -253,6 +253,8 @@ target_sources(${EXECUTABLE_NAME} PUBLIC
target_sources(${EXECUTABLE_NAME} PUBLIC target_sources(${EXECUTABLE_NAME} PUBLIC
"src/audio_engine.cc" "src/audio_engine.cc"
"src/audio_engine.h" "src/audio_engine.h"
"src/delay.cc"
"src/delay.h"
"src/fps_limiter.cc" "src/fps_limiter.cc"
"src/fps_limiter.h" "src/fps_limiter.h"
"src/platform_compat.cc" "src/platform_compat.cc"

View File

@ -16,6 +16,7 @@
#include "db.h" #include "db.h"
#include "dbox.h" #include "dbox.h"
#include "debug.h" #include "debug.h"
#include "delay.h"
#include "draw.h" #include "draw.h"
#include "game.h" #include "game.h"
#include "game_mouse.h" #include "game_mouse.h"
@ -1991,7 +1992,7 @@ static int _get_input_str(int win, int cancelKeyCode, char* text, int maxLength,
windowRefresh(win); windowRefresh(win);
while (getTicksSince(_frame_time) < 1000 / 24) { } delay_ms(1000 / 24 - (getTicks() - _frame_time));
renderPresent(); renderPresent();
sharedFpsLimiter.throttle(); sharedFpsLimiter.throttle();
@ -2281,8 +2282,7 @@ static void characterEditorDrawBigNumber(int x, int y, int flags, int value, int
windowWidth); windowWidth);
windowRefreshRect(windowHandle, &rect); windowRefreshRect(windowHandle, &rect);
renderPresent(); renderPresent();
while (getTicksSince(_frame_time) < BIG_NUM_ANIMATION_DELAY) delay_ms(BIG_NUM_ANIMATION_DELAY - (getTicks() - _frame_time));
;
} }
blitBufferToBuffer(numbersGraphicBufferPtr + BIG_NUM_WIDTH * ones, blitBufferToBuffer(numbersGraphicBufferPtr + BIG_NUM_WIDTH * ones,
@ -2304,8 +2304,7 @@ static void characterEditorDrawBigNumber(int x, int y, int flags, int value, int
windowWidth); windowWidth);
windowRefreshRect(windowHandle, &rect); windowRefreshRect(windowHandle, &rect);
renderPresent(); renderPresent();
while (getTicksSince(_frame_time) < BIG_NUM_ANIMATION_DELAY) delay_ms(BIG_NUM_ANIMATION_DELAY - (getTicks() - _frame_time));
;
} }
blitBufferToBuffer(numbersGraphicBufferPtr + BIG_NUM_WIDTH * tens, blitBufferToBuffer(numbersGraphicBufferPtr + BIG_NUM_WIDTH * tens,
@ -3530,11 +3529,9 @@ static int characterEditorEditAge()
} }
if (v33 > dbl_50170B) { if (v33 > dbl_50170B) {
while (getTicksSince(_frame_time) < 1000 / _repFtime) delay_ms(1000 / _repFtime - (getTicks() - _frame_time));
;
} else { } else {
while (getTicksSince(_frame_time) < 1000 / 24) delay_ms(1000 / 24 - (getTicks() - _frame_time));
;
} }
keyCode = inputGetInput(); keyCode = inputGetInput();
@ -3548,8 +3545,7 @@ static int characterEditorEditAge()
} else { } else {
windowRefresh(win); windowRefresh(win);
while (getTicksSince(_frame_time) < 1000 / 24) delay_ms(1000 / 24 - (getTicks() - _frame_time));
;
} }
renderPresent(); renderPresent();
@ -3699,8 +3695,7 @@ static void characterEditorEditGender()
windowRefresh(win); windowRefresh(win);
while (getTicksSince(_frame_time) < 41) delay_ms(41 - (getTicks() - _frame_time));
;
renderPresent(); renderPresent();
sharedFpsLimiter.throttle(); sharedFpsLimiter.throttle();
@ -3778,12 +3773,9 @@ static void characterEditorAdjustPrimaryStat(int eventCode)
} }
if (v11 >= 19.2) { if (v11 >= 19.2) {
unsigned int delay = 1000 / _repFtime; delay_ms(1000 / _repFtime - (getTicks() - _frame_time));
while (getTicksSince(_frame_time) < delay) {
}
} else { } else {
while (getTicksSince(_frame_time) < 1000 / 24) { delay_ms(1000 / 24 - (getTicks() - _frame_time));
}
} }
renderPresent(); renderPresent();
@ -5279,11 +5271,9 @@ static void characterEditorHandleAdjustSkillButtonPressed(int keyCode)
if (!isUsingKeyboard) { if (!isUsingKeyboard) {
unspentSp = pcGetStat(PC_STAT_UNSPENT_SKILL_POINTS); unspentSp = pcGetStat(PC_STAT_UNSPENT_SKILL_POINTS);
if (repeatDelay >= dbl_5018F0) { if (repeatDelay >= dbl_5018F0) {
while (getTicksSince(_frame_time) < 1000 / _repFtime) { delay_ms(1000 / _repFtime - (getTicks() - _frame_time));
}
} else { } else {
while (getTicksSince(_frame_time) < 1000 / 24) { delay_ms(1000 / 24 - (getTicks() - _frame_time));
}
} }
int keyCode = inputGetInput(); int keyCode = inputGetInput();
@ -6141,11 +6131,9 @@ static int perkDialogHandleInput(int count, void (*refreshProc)())
} }
if (v19 < dbl_5019BE) { if (v19 < dbl_5019BE) {
while (getTicksSince(_frame_time) < 1000 / 24) { delay_ms(1000 / 24 - (getTicks() - _frame_time));
}
} else { } else {
while (getTicksSince(_frame_time) < 1000 / _repFtime) { delay_ms(1000 / _repFtime - (getTicks() - _frame_time));
}
} }
renderPresent(); renderPresent();
@ -6188,11 +6176,9 @@ static int perkDialogHandleInput(int count, void (*refreshProc)())
} }
if (v19 < dbl_5019BE) { if (v19 < dbl_5019BE) {
while (getTicksSince(_frame_time) < 1000 / 24) { delay_ms(1000 / 24 - (getTicks() - _frame_time));
}
} else { } else {
while (getTicksSince(_frame_time) < 1000 / _repFtime) { delay_ms(1000 / _repFtime - (getTicks() - _frame_time));
}
} }
renderPresent(); renderPresent();
@ -6224,11 +6210,9 @@ static int perkDialogHandleInput(int count, void (*refreshProc)())
} }
if (v19 < dbl_5019BE) { if (v19 < dbl_5019BE) {
while (getTicksSince(_frame_time) < 1000 / 24) { delay_ms(1000 / 24 - (getTicks() - _frame_time));
}
} else { } else {
while (getTicksSince(_frame_time) < 1000 / _repFtime) { delay_ms(1000 / _repFtime - (getTicks() - _frame_time));
}
} }
renderPresent(); renderPresent();

View File

@ -9,6 +9,7 @@
#include "cycle.h" #include "cycle.h"
#include "db.h" #include "db.h"
#include "debug.h" #include "debug.h"
#include "delay.h"
#include "draw.h" #include "draw.h"
#include "game_mouse.h" #include "game_mouse.h"
#include "input.h" #include "input.h"
@ -172,8 +173,7 @@ void creditsOpen(const char* filePath, int backgroundFid, bool useReversedStyle)
windowBuffer, windowBuffer,
windowWidth); windowWidth);
while (getTicksSince(tick) < CREDITS_WINDOW_SCROLLING_DELAY) { delay_ms(CREDITS_WINDOW_SCROLLING_DELAY - (getTicks() - tick));
}
tick = getTicks(); tick = getTicks();
@ -215,8 +215,7 @@ void creditsOpen(const char* filePath, int backgroundFid, bool useReversedStyle)
windowBuffer, windowBuffer,
windowWidth); windowWidth);
while (getTicksSince(tick) < CREDITS_WINDOW_SCROLLING_DELAY) { delay_ms(CREDITS_WINDOW_SCROLLING_DELAY - (getTicks() - tick));
}
tick = getTicks(); tick = getTicks();

View File

@ -9,6 +9,7 @@
#include "character_editor.h" #include "character_editor.h"
#include "color.h" #include "color.h"
#include "debug.h" #include "debug.h"
#include "delay.h"
#include "draw.h" #include "draw.h"
#include "game.h" #include "game.h"
#include "game_sound.h" #include "game_sound.h"
@ -885,8 +886,8 @@ int showLoadFileDialog(char* title, char** fileList, char* dest, int fileListLen
} }
unsigned int delay = (scrollCounter > 14.4) ? 1000 / scrollDelay : 1000 / 24; unsigned int delay = (scrollCounter > 14.4) ? 1000 / scrollDelay : 1000 / 24;
while (getTicksSince(scrollTick) < delay) {
} delay_ms(delay - (getTicks() - scrollTick));
if (_game_user_wants_to_quit != 0) { if (_game_user_wants_to_quit != 0) {
rc = 1; rc = 1;
@ -909,8 +910,7 @@ int showLoadFileDialog(char* title, char** fileList, char* dest, int fileListLen
doubleClickSelectedFileIndex = -2; doubleClickSelectedFileIndex = -2;
} }
while (getTicksSince(tick) < (1000 / 24)) { delay_ms(1000 / 24 - (getTicks() - tick));
}
} }
if (_game_user_wants_to_quit) { if (_game_user_wants_to_quit) {
@ -1335,8 +1335,7 @@ int showSaveFileDialog(char* title, char** fileList, char* dest, int fileListLen
// FIXME: Missing windowRefresh makes blinking useless. // FIXME: Missing windowRefresh makes blinking useless.
unsigned int delay = (scrollCounter > 14.4) ? 1000 / scrollDelay : 1000 / 24; unsigned int delay = (scrollCounter > 14.4) ? 1000 / scrollDelay : 1000 / 24;
while (getTicksSince(scrollTick) < delay) { delay_ms(delay - (getTicks() - scrollTick));
}
if (_game_user_wants_to_quit != 0) { if (_game_user_wants_to_quit != 0) {
rc = 1; rc = 1;
@ -1369,8 +1368,7 @@ int showSaveFileDialog(char* title, char** fileList, char* dest, int fileListLen
doubleClickSelectedFileIndex = -2; doubleClickSelectedFileIndex = -2;
} }
while (getTicksSince(tick) < (1000 / 24)) { delay_ms(1000 / 24 - (getTicks() - tick));
}
} }
if (_game_user_wants_to_quit != 0) { if (_game_user_wants_to_quit != 0) {

11
src/delay.cc Normal file
View File

@ -0,0 +1,11 @@
#include "delay.h"
#include <SDL.h>
void delay_ms(int ms)
{
if (ms <= 0) {
return;
}
SDL_Delay(ms);
}

6
src/delay.h Normal file
View File

@ -0,0 +1,6 @@
#ifndef DELAY_H
#define DELAY_H
void delay_ms(int ms);
#endif

View File

@ -8,6 +8,7 @@
#include "art.h" #include "art.h"
#include "cycle.h" #include "cycle.h"
#include "debug.h" #include "debug.h"
#include "delay.h"
#include "draw.h" #include "draw.h"
#include "game_mouse.h" #include "game_mouse.h"
#include "game_sound.h" #include "game_sound.h"
@ -453,8 +454,7 @@ int elevatorSelectLevel(int elevator, int* mapPtr, int* elevationPtr, int* tileP
windowRefresh(gElevatorWindow); windowRefresh(gElevatorWindow);
while (getTicksSince(tick) < delay) { delay_ms(delay - (getTicks() - tick));
}
renderPresent(); renderPresent();
sharedFpsLimiter.throttle(); sharedFpsLimiter.throttle();

View File

@ -13,6 +13,7 @@
#include "critter.h" #include "critter.h"
#include "cycle.h" #include "cycle.h"
#include "debug.h" #include "debug.h"
#include "delay.h"
#include "dialog.h" #include "dialog.h"
#include "display_monitor.h" #include "display_monitor.h"
#include "draw.h" #include "draw.h"
@ -2936,7 +2937,6 @@ void _gdialog_scroll_subwin(int win, int a2, unsigned char* a3, unsigned char* a
int v7; int v7;
unsigned char* v9; unsigned char* v9;
Rect rect; Rect rect;
unsigned int tick;
v7 = a6; v7 = a6;
v9 = a4; v9 = a4;
@ -2971,9 +2971,7 @@ void _gdialog_scroll_subwin(int win, int a2, unsigned char* a3, unsigned char* a
v7 += 10; v7 += 10;
v9 -= 10 * (GAME_DIALOG_WINDOW_WIDTH); v9 -= 10 * (GAME_DIALOG_WINDOW_WIDTH);
tick = getTicks(); delay_ms(33);
while (getTicksSince(tick) < 33) {
}
renderPresent(); renderPresent();
sharedFpsLimiter.throttle(); sharedFpsLimiter.throttle();
@ -3011,9 +3009,7 @@ void _gdialog_scroll_subwin(int win, int a2, unsigned char* a3, unsigned char* a
rect.top += 10; rect.top += 10;
tick = getTicks(); delay_ms(33);
while (getTicksSince(tick) < 33) {
}
renderPresent(); renderPresent();
sharedFpsLimiter.throttle(); sharedFpsLimiter.throttle();

View File

@ -4,6 +4,7 @@
#include "audio_engine.h" #include "audio_engine.h"
#include "color.h" #include "color.h"
#include "delay.h"
#include "dinput.h" #include "dinput.h"
#include "draw.h" #include "draw.h"
#include "kb.h" #include "kb.h"
@ -641,12 +642,7 @@ void inputPauseForTocks(unsigned int delay)
// 0x4C93B8 // 0x4C93B8
void inputBlockForTocks(unsigned int ms) void inputBlockForTocks(unsigned int ms)
{ {
unsigned int start = SDL_GetTicks(); delay_ms(ms);
unsigned int diff;
do {
// NOTE: Uninline
diff = getTicksSince(start);
} while (diff < ms);
} }
// 0x4C93E0 // 0x4C93E0

View File

@ -18,6 +18,7 @@
#include "db.h" #include "db.h"
#include "dbox.h" #include "dbox.h"
#include "debug.h" #include "debug.h"
#include "delay.h"
#include "display_monitor.h" #include "display_monitor.h"
#include "draw.h" #include "draw.h"
#include "file_utils.h" #include "file_utils.h"
@ -672,9 +673,9 @@ int lsgSaveGame(int mode)
} }
if (scrollCounter > 14.4) { if (scrollCounter > 14.4) {
while (getTicksSince(start) < 1000 / scrollVelocity) { } delay_ms(1000 / scrollVelocity - (getTicks() - start));
} else { } else {
while (getTicksSince(start) < 1000 / 24) { } delay_ms(1000 / 24 - (getTicks() - start));
} }
keyCode = inputGetInput(); keyCode = inputGetInput();
@ -718,8 +719,7 @@ int lsgSaveGame(int mode)
doubleClickSlot = -1; doubleClickSlot = -1;
} }
while (getTicksSince(tick) < 1000 / 24) { delay_ms(1000 / 24 - (getTicks() - tick));
}
} }
if (rc == 1) { if (rc == 1) {
@ -1175,9 +1175,9 @@ int lsgLoadGame(int mode)
} }
if (scrollCounter > 14.4) { if (scrollCounter > 14.4) {
while (getTicksSince(start) < 1000 / scrollVelocity) { } delay_ms(1000 / scrollVelocity - (getTicks() - start));
} else { } else {
while (getTicksSince(start) < 1000 / 24) { } delay_ms(1000 / 24 - (getTicks() - start));
} }
keyCode = inputGetInput(); keyCode = inputGetInput();
@ -1227,7 +1227,7 @@ int lsgLoadGame(int mode)
doubleClickSlot = -1; doubleClickSlot = -1;
} }
while (getTicksSince(time) < 1000 / 24) { } delay_ms(1000 / 24 - (getTicks() - time));
} }
if (rc == 1) { if (rc == 1) {
@ -2387,8 +2387,7 @@ static int _get_input_str2(int win, int doneKeyCode, int cancelKeyCode, char* de
windowRefresh(win); windowRefresh(win);
} }
while (getTicksSince(tick) < 1000 / 24) { delay_ms(1000 / 24 - (getTicks() - tick));
}
renderPresent(); renderPresent();
sharedFpsLimiter.throttle(); sharedFpsLimiter.throttle();

View File

@ -9,6 +9,7 @@
#include <string.h> #include <string.h>
#include "audio_engine.h" #include "audio_engine.h"
#include "delay.h"
#include "platform_compat.h" #include "platform_compat.h"
namespace fallout { namespace fallout {
@ -794,6 +795,8 @@ static int _syncWait()
if (_sync_active) { if (_sync_active) {
if (((_sync_time + 1000 * compat_timeGetTime()) & 0x80000000) != 0) { if (((_sync_time + 1000 * compat_timeGetTime()) & 0x80000000) != 0) {
result = 1; result = 1;
delay_ms(-(_sync_time + 1000 * compat_timeGetTime()) / 1000 - 3);
while (((_sync_time + 1000 * compat_timeGetTime()) & 0x80000000) != 0) while (((_sync_time + 1000 * compat_timeGetTime()) & 0x80000000) != 0)
; ;
} }
@ -1148,6 +1151,7 @@ static int _MVE_sndConfigure(int a1, int a2, int a3, int a4, int a5, int a6)
} }
// 0x4F56C0 // 0x4F56C0
// Looks like this function is not used
static void _MVE_syncSync() static void _MVE_syncSync()
{ {
if (_sync_active) { if (_sync_active) {
@ -1294,6 +1298,10 @@ static void _MVE_sndSync()
break; break;
} }
v0 = true; v0 = true;
#ifdef EMSCRIPTEN
delay_ms(1);
#endif
} }
if (dword_6B3660 != dword_6B3AE4) { if (dword_6B3660 != dword_6B3AE4) {
@ -1318,6 +1326,10 @@ static int _syncWaitLevel(int a1)
v2 = _sync_time + a1; v2 = _sync_time + a1;
do { do {
result = v2 + 1000 * compat_timeGetTime(); result = v2 + 1000 * compat_timeGetTime();
if (result < 0) {
delay_ms(-result / 1000 - 3);
}
result = v2 + 1000 * compat_timeGetTime();
} while (result < 0); } while (result < 0);
_sync_time += _sync_wait_quanta; _sync_time += _sync_wait_quanta;

View File

@ -13,6 +13,7 @@
#include "cycle.h" #include "cycle.h"
#include "dbox.h" #include "dbox.h"
#include "debug.h" #include "debug.h"
#include "delay.h"
#include "draw.h" #include "draw.h"
#include "game.h" #include "game.h"
#include "game_mouse.h" #include "game_mouse.h"
@ -2001,8 +2002,7 @@ static bool pipboyRest(int hours, int minutes, int duration)
pipboyDrawDate(); pipboyDrawDate();
windowRefresh(gPipboyWindow); windowRefresh(gPipboyWindow);
while (getTicksSince(start) < 50) { delay_ms(50 - (getTicks() - start));
}
} }
renderPresent(); renderPresent();
@ -2072,8 +2072,7 @@ static bool pipboyRest(int hours, int minutes, int duration)
pipboyDrawHitPoints(); pipboyDrawHitPoints();
windowRefresh(gPipboyWindow); windowRefresh(gPipboyWindow);
while (getTicksSince(start) < 50) { delay_ms(50 - (getTicks() - start));
}
} }
renderPresent(); renderPresent();
@ -2366,8 +2365,7 @@ static int pipboyRenderScreensaver()
v31 -= 1; v31 -= 1;
} else { } else {
windowRefreshRect(gPipboyWindow, &gPipboyWindowContentRect); windowRefreshRect(gPipboyWindow, &gPipboyWindowContentRect);
while (getTicksSince(time) < 50) { delay_ms(50 - (getTicks() - time));
}
} }
renderPresent(); renderPresent();

View File

@ -7,6 +7,7 @@
#include "combat.h" #include "combat.h"
#include "combat_ai.h" #include "combat_ai.h"
#include "debug.h" #include "debug.h"
#include "delay.h"
#include "draw.h" #include "draw.h"
#include "game.h" #include "game.h"
#include "game_mouse.h" #include "game_mouse.h"
@ -1570,8 +1571,7 @@ static void _DoThing(int eventCode)
blitBufferToBufferTrans(_preferencesFrmImages[PREFERENCES_WINDOW_FRM_KNOB_ON].getData(), 21, 12, 21, gPreferencesWindowBuffer + PREFERENCES_WINDOW_WIDTH * meta->knobY + v31, PREFERENCES_WINDOW_WIDTH); blitBufferToBufferTrans(_preferencesFrmImages[PREFERENCES_WINDOW_FRM_KNOB_ON].getData(), 21, 12, 21, gPreferencesWindowBuffer + PREFERENCES_WINDOW_WIDTH * meta->knobY + v31, PREFERENCES_WINDOW_WIDTH);
windowRefresh(gPreferencesWindow); windowRefresh(gPreferencesWindow);
while (getTicksSince(tick) < 35) delay_ms(35 - (getTicks() - tick));
;
renderPresent(); renderPresent();
sharedFpsLimiter.throttle(); sharedFpsLimiter.throttle();

View File

@ -2,6 +2,7 @@
#include <stdlib.h> #include <stdlib.h>
#include "delay.h"
#include "input.h" #include "input.h"
#include "kb.h" #include "kb.h"
#include "memory.h" #include "memory.h"
@ -228,8 +229,7 @@ int vcrUpdate()
* (vcrEntry->time - stru_6AD940.time) * (vcrEntry->time - stru_6AD940.time)
/ (vcrEntry->counter - stru_6AD940.counter); / (vcrEntry->counter - stru_6AD940.counter);
while (getTicksSince(_vcr_start_time) < delay) { delay_ms(delay - (getTicks() - _vcr_start_time));
}
} }
} }