Skip to content

Commit 1b3f7f8

Browse files
committed
[core] Preload few SFX to avoid first input lag.
1 parent 973dc2c commit 1b3f7f8

File tree

3 files changed

+32
-18
lines changed

3 files changed

+32
-18
lines changed

include/sfx.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,6 @@ typedef enum {
2626
int ds_sfx_play(ds_sfx_t sfx);
2727
int ds_sfx_is_enabled(ds_sfx_t sfx);
2828
void ds_sfx_get_wav(char sfx_path[], ds_sfx_t sfx_sel);
29-
29+
void ds_sfx_preload(void);
3030

3131
#endif /* _DS_SFX_H */

src/main.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -294,25 +294,25 @@ int InitDS() {
294294
return -1;
295295
}
296296

297+
/* Preload some sfx to avoid delay on first input */
298+
ds_sfx_preload();
299+
297300
#ifdef DS_DEBUG
298301
uint64 t_end = timer_ms_gettime64();
299302
dbglog(DBG_INFO, "Initializing time: %ld ms\n", (uint32)(t_end - t_start));
300303
#endif
301304

302305
if(settings->startup[0] == '/') {
303-
304306
snprintf(fn, NAME_MAX, "%s%s", getenv("PATH"), settings->startup);
305307
LuaDo(LUA_DO_FILE, fn, GetLuaState());
306-
307-
} else if(settings->startup[0] == '#') {
308-
308+
}
309+
else if(settings->startup[0] == '#') {
309310
dsystem_buff(settings->startup);
310-
311-
} else if(settings->startup[0] != 0) {
312-
311+
}
312+
else if(settings->startup[0] != 0) {
313313
LuaDo(LUA_DO_STRING, settings->startup, GetLuaState());
314-
315-
} else {
314+
}
315+
else {
316316
snprintf(fn, NAME_MAX, "%s/lua/startup.lua", getenv("PATH"));
317317
LuaDo(LUA_DO_FILE, fn, GetLuaState());
318318
}

src/sfx.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,9 @@ int ds_sfx_is_enabled(ds_sfx_t sfx) {
168168
}
169169
}
170170

171-
static int ds_sfx_get_volume() {
172-
Settings_t *settings = GetSettings();
173-
174-
if(!settings) {
175-
return 230;
176-
}
177-
178-
return settings->audio.volume;
171+
static int ds_sfx_get_volume(void) {
172+
int volume = GetVolumeFromSettings();
173+
return volume < 0 ? 230 : volume;
179174
}
180175

181176
void ds_sfx_get_wav(char sfx_path[], ds_sfx_t sfx_sel) {
@@ -223,6 +218,25 @@ static int ds_sfx_play_stream(ds_sfx_t sfx) {
223218
return 0;
224219
}
225220

221+
void ds_sfx_preload(void) {
222+
ds_sfx_t init_sfx[] = {DS_SFX_CLICK, DS_SFX_CLICK2, DS_SFX_SLIDE};
223+
int init_count = sizeof(init_sfx) / sizeof(init_sfx[0]);
224+
225+
for(int i = 0; i < init_count; i++) {
226+
if(!ds_sfx_is_enabled(init_sfx[i])) {
227+
continue;
228+
}
229+
230+
int sfx_sel = init_sfx[i] - DS_SFX_LAST_STREAM;
231+
232+
if(sys_sfx_hnd[sfx_sel] == SFXHND_INVALID) {
233+
char sfx_path[NAME_MAX];
234+
ds_sfx_get_wav(sfx_path, sfx_sel);
235+
sys_sfx_hnd[sfx_sel] = snd_sfx_load(sfx_path);
236+
}
237+
}
238+
}
239+
226240
int ds_sfx_play(ds_sfx_t sfx) {
227241
if(sfx >= DS_SFX_LAST) {
228242
return -1;

0 commit comments

Comments
 (0)