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
"src/audio_engine.cc"
"src/audio_engine.h"
"src/delay.cc"
"src/delay.h"
"src/fps_limiter.cc"
"src/fps_limiter.h"
"src/platform_compat.cc"

View File

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

View File

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

View File

@ -9,6 +9,7 @@
#include "character_editor.h"
#include "color.h"
#include "debug.h"
#include "delay.h"
#include "draw.h"
#include "game.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;
while (getTicksSince(scrollTick) < delay) {
}
delay_ms(delay - (getTicks() - scrollTick));
if (_game_user_wants_to_quit != 0) {
rc = 1;
@ -909,8 +910,7 @@ int showLoadFileDialog(char* title, char** fileList, char* dest, int fileListLen
doubleClickSelectedFileIndex = -2;
}
while (getTicksSince(tick) < (1000 / 24)) {
}
delay_ms(1000 / 24 - (getTicks() - tick));
}
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.
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) {
rc = 1;
@ -1369,8 +1368,7 @@ int showSaveFileDialog(char* title, char** fileList, char* dest, int fileListLen
doubleClickSelectedFileIndex = -2;
}
while (getTicksSince(tick) < (1000 / 24)) {
}
delay_ms(1000 / 24 - (getTicks() - tick));
}
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 "cycle.h"
#include "debug.h"
#include "delay.h"
#include "draw.h"
#include "game_mouse.h"
#include "game_sound.h"
@ -453,8 +454,7 @@ int elevatorSelectLevel(int elevator, int* mapPtr, int* elevationPtr, int* tileP
windowRefresh(gElevatorWindow);
while (getTicksSince(tick) < delay) {
}
delay_ms(delay - (getTicks() - tick));
renderPresent();
sharedFpsLimiter.throttle();

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -7,6 +7,7 @@
#include "combat.h"
#include "combat_ai.h"
#include "debug.h"
#include "delay.h"
#include "draw.h"
#include "game.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);
windowRefresh(gPreferencesWindow);
while (getTicksSince(tick) < 35)
;
delay_ms(35 - (getTicks() - tick));
renderPresent();
sharedFpsLimiter.throttle();

View File

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