Use delay_ms instead of spinwait

This commit is contained in:
Vasilii Rogin 2023-04-13 20:20:25 +03:00
parent e23b39abaa
commit ff5f461e6d
14 changed files with 87 additions and 87 deletions

View File

@ -271,6 +271,8 @@ target_sources(${EXECUTABLE_NAME} PUBLIC
"src/sfall_lists.h" "src/sfall_lists.h"
"src/sfall_opcodes.cc" "src/sfall_opcodes.cc"
"src/sfall_opcodes.h" "src/sfall_opcodes.h"
"src/delay.cc"
"src/delay.h"
) )
if(IOS) if(IOS)

View File

@ -45,6 +45,7 @@
#include "window_manager.h" #include "window_manager.h"
#include "word_wrap.h" #include "word_wrap.h"
#include "worldmap.h" #include "worldmap.h"
#include "delay.h"
namespace fallout { namespace fallout {
@ -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

@ -21,6 +21,7 @@
#include "svga.h" #include "svga.h"
#include "text_font.h" #include "text_font.h"
#include "window_manager.h" #include "window_manager.h"
#include "delay.h"
namespace fallout { namespace fallout {
@ -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

@ -21,6 +21,7 @@
#include "text_font.h" #include "text_font.h"
#include "window_manager.h" #include "window_manager.h"
#include "word_wrap.h" #include "word_wrap.h"
#include "delay.h"
namespace fallout { namespace fallout {
@ -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;
@ -908,9 +909,8 @@ int showLoadFileDialog(char* title, char** fileList, char* dest, int fileListLen
doubleClickTimer = FILE_DIALOG_DOUBLE_CLICK_DELAY; doubleClickTimer = FILE_DIALOG_DOUBLE_CLICK_DELAY;
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;
@ -1368,9 +1367,8 @@ int showSaveFileDialog(char* title, char** fileList, char* dest, int fileListLen
doubleClickTimer = FILE_DIALOG_DOUBLE_CLICK_DELAY; doubleClickTimer = FILE_DIALOG_DOUBLE_CLICK_DELAY;
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) {

9
src/delay.cc Normal file
View File

@ -0,0 +1,9 @@
#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

@ -21,6 +21,7 @@
#include "sfall_config.h" #include "sfall_config.h"
#include "svga.h" #include "svga.h"
#include "window_manager.h" #include "window_manager.h"
#include "delay.h"
namespace fallout { namespace fallout {
@ -452,9 +453,8 @@ int elevatorSelectLevel(int elevator, int* mapPtr, int* elevationPtr, int* tileP
_elevatorBackgroundFrmImage.getWidth()); _elevatorBackgroundFrmImage.getWidth());
windowRefresh(gElevatorWindow); windowRefresh(gElevatorWindow);
while (getTicksSince(tick) < delay) { delay_ms(delay - (getTicks() - tick));
}
renderPresent(); renderPresent();
sharedFpsLimiter.throttle(); sharedFpsLimiter.throttle();

View File

@ -40,6 +40,7 @@
#include "text_object.h" #include "text_object.h"
#include "tile.h" #include "tile.h"
#include "window_manager.h" #include "window_manager.h"
#include "delay.h"
namespace fallout { namespace fallout {
@ -2970,10 +2971,8 @@ void _gdialog_scroll_subwin(int win, int a2, unsigned char* a3, unsigned char* a
windowRefreshRect(win, &rect); windowRefreshRect(win, &rect);
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();
@ -3010,10 +3009,8 @@ void _gdialog_scroll_subwin(int win, int a2, unsigned char* a3, unsigned char* a
windowRefreshRect(win, &rect); windowRefreshRect(win, &rect);
rect.top += 10; rect.top += 10;
tick = getTicks(); delay_ms(33);
while (getTicksSince(tick) < 33) {
}
renderPresent(); renderPresent();
sharedFpsLimiter.throttle(); sharedFpsLimiter.throttle();

View File

@ -13,6 +13,7 @@
#include "text_font.h" #include "text_font.h"
#include "vcr.h" #include "vcr.h"
#include "win32.h" #include "win32.h"
#include "delay.h"
namespace fallout { namespace fallout {
@ -633,12 +634,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

@ -55,6 +55,7 @@
#include "window_manager.h" #include "window_manager.h"
#include "word_wrap.h" #include "word_wrap.h"
#include "worldmap.h" #include "worldmap.h"
#include "delay.h"
namespace fallout { namespace fallout {
@ -650,9 +651,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();
@ -696,8 +697,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) {
@ -1153,9 +1153,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();
@ -1205,7 +1205,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) {
@ -2277,8 +2277,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

@ -10,6 +10,7 @@
#include "audio_engine.h" #include "audio_engine.h"
#include "platform_compat.h" #include "platform_compat.h"
#include "delay.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) {
@ -1316,7 +1324,11 @@ static int _syncWaitLevel(int a1)
} }
v2 = _sync_time + a1; v2 = _sync_time + a1;
do { do {
result = v2 + 1000 * compat_timeGetTime();
if (result < 0) {
delay_ms(-result/1000 - 3);
}
result = v2 + 1000 * compat_timeGetTime(); result = v2 + 1000 * compat_timeGetTime();
} while (result < 0); } while (result < 0);

View File

@ -39,6 +39,7 @@
#include "window_manager.h" #include "window_manager.h"
#include "word_wrap.h" #include "word_wrap.h"
#include "worldmap.h" #include "worldmap.h"
#include "delay.h"
namespace fallout { namespace fallout {
@ -2000,9 +2001,8 @@ static bool pipboyRest(int hours, int minutes, int duration)
pipboyDrawNumber(gameTimeGetHour(), 4, PIPBOY_WINDOW_TIME_X, PIPBOY_WINDOW_TIME_Y); pipboyDrawNumber(gameTimeGetHour(), 4, PIPBOY_WINDOW_TIME_X, PIPBOY_WINDOW_TIME_Y);
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();
@ -2368,8 +2367,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

@ -23,6 +23,7 @@
#include "text_font.h" #include "text_font.h"
#include "text_object.h" #include "text_object.h"
#include "window_manager.h" #include "window_manager.h"
#include "delay.h"
namespace fallout { namespace fallout {
@ -1569,9 +1570,8 @@ 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

@ -7,6 +7,7 @@
#include "memory.h" #include "memory.h"
#include "mouse.h" #include "mouse.h"
#include "svga.h" #include "svga.h"
#include "delay.h"
namespace fallout { namespace fallout {
@ -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));
}
} }
} }