Optional updates for the loading screen (#87)

- added support for WAPI_DXGI (now only WAPI_DUMMY does not render the loading screen)
This commit is contained in:
Radek Krzyśków 2024-07-01 05:34:20 +02:00 committed by GitHub
parent fe114133ff
commit 75ce4e1d1d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 43 additions and 34 deletions

View File

@ -1,8 +1,11 @@
#include "loading.h"
#ifdef LOADING_SCREEN_SUPPORTED
#include "djui/djui.h" #include "djui/djui.h"
#include "pc/djui/djui_unicode.h" #include "pc/djui/djui_unicode.h"
#include "pc_main.h" #include "pc_main.h"
#include "loading.h"
#include "pc/utils/misc.h" #include "pc/utils/misc.h"
#include "pc/cliopts.h" #include "pc/cliopts.h"
#include "rom_checker.h" #include "rom_checker.h"
@ -19,13 +22,12 @@ struct LoadingScreen {
}; };
static struct LoadingScreen* sLoading = NULL; static struct LoadingScreen* sLoading = NULL;
pthread_t gLoadingThreadId; pthread_t gLoadingThreadId;
pthread_mutex_t gLoadingThreadMutex = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t gLoadingThreadMutex = PTHREAD_MUTEX_INITIALIZER;
bool gIsThreaded = false; bool gIsThreaded = false;
#define RUN_THREADED if (gIsThreaded)
void loading_screen_set_segment_text(const char* text) { void loading_screen_set_segment_text(const char* text) {
snprintf(gCurrLoadingSegment.str, 256, text); snprintf(gCurrLoadingSegment.str, 256, text);
} }
@ -39,7 +41,7 @@ static void loading_screen_produce_one_frame(void) {
} }
static bool loading_screen_on_render(struct DjuiBase* base) { static bool loading_screen_on_render(struct DjuiBase* base) {
RUN_THREADED { pthread_mutex_lock(&gLoadingThreadMutex); } if (gIsThreaded) { pthread_mutex_lock(&gLoadingThreadMutex); }
u32 windowWidth, windowHeight; u32 windowWidth, windowHeight;
WAPI.get_dimensions(&windowWidth, &windowHeight); WAPI.get_dimensions(&windowWidth, &windowHeight);
@ -76,7 +78,7 @@ static bool loading_screen_on_render(struct DjuiBase* base) {
djui_base_compute(base); djui_base_compute(base);
RUN_THREADED { pthread_mutex_unlock(&gLoadingThreadMutex); } if (gIsThreaded) { pthread_mutex_unlock(&gLoadingThreadMutex); }
return true; return true;
} }
@ -87,7 +89,7 @@ static void loading_screen_destroy(struct DjuiBase* base) {
sLoading = NULL; sLoading = NULL;
} }
void init_loading_screen(void) { static void init_loading_screen(void) {
struct LoadingScreen* load = calloc(1, sizeof(struct LoadingScreen)); struct LoadingScreen* load = calloc(1, sizeof(struct LoadingScreen));
struct DjuiBase* base = &load->base; struct DjuiBase* base = &load->base;
@ -134,7 +136,10 @@ void init_loading_screen(void) {
} }
void loading_screen_reset(void) { void loading_screen_reset(void) {
if (sLoading) {
djui_base_destroy(&sLoading->base); djui_base_destroy(&sLoading->base);
sLoading = NULL;
}
djui_shutdown(); djui_shutdown();
alloc_display_list_reset(); alloc_display_list_reset();
gDisplayListHead = NULL; gDisplayListHead = NULL;
@ -151,9 +156,6 @@ void render_loading_screen(void) {
} }
pthread_join(gLoadingThreadId, NULL); pthread_join(gLoadingThreadId, NULL);
gIsThreaded = false;
loading_screen_reset();
} }
void render_rom_setup_screen(void) { void render_rom_setup_screen(void) {
@ -165,3 +167,5 @@ void render_rom_setup_screen(void) {
WAPI.main_loop(loading_screen_produce_one_frame); WAPI.main_loop(loading_screen_produce_one_frame);
} }
} }
#endif

View File

@ -1,6 +1,12 @@
#ifndef LOADING_HEADER #ifndef LOADING_HEADER
#define LOADING_HEADER #define LOADING_HEADER
#if !defined(WAPI_DUMMY)
#define LOADING_SCREEN_SUPPORTED
#endif
#ifdef LOADING_SCREEN_SUPPORTED
#include <pthread.h> #include <pthread.h>
#include "cliopts.h" #include "cliopts.h"
@ -13,21 +19,12 @@ struct LoadingSegment {
extern struct LoadingSegment gCurrLoadingSegment; extern struct LoadingSegment gCurrLoadingSegment;
extern bool gIsThreaded; #define LOADING_SCREEN_MUTEX(...) \
if (!gCLIOpts.hideLoadingScreen && gIsThreaded) { \
#if !defined(WAPI_DXGI) && !defined(WAPI_DUMMY)
#define LOADING_SCREEN_SUPPORTED
#endif
#ifdef LOADING_SCREEN_SUPPORTED
#define LOADING_SCREEN_MUTEX(...) if (!gCLIOpts.hideLoadingScreen && gIsThreaded) { \
pthread_mutex_lock(&gLoadingThreadMutex); \ pthread_mutex_lock(&gLoadingThreadMutex); \
__VA_ARGS__; \ __VA_ARGS__; \
pthread_mutex_unlock(&gLoadingThreadMutex); \ pthread_mutex_unlock(&gLoadingThreadMutex); \
} }
#else
#define LOADING_SCREEN_MUTEX(...)
#endif
extern pthread_t gLoadingThreadId; extern pthread_t gLoadingThreadId;
extern pthread_mutex_t gLoadingThreadMutex; extern pthread_mutex_t gLoadingThreadMutex;
@ -36,6 +33,13 @@ extern bool gIsThreaded;
void loading_screen_set_segment_text(const char* text); void loading_screen_set_segment_text(const char* text);
void render_loading_screen(void); void render_loading_screen(void);
void loading_screen_reset(void);
void render_rom_setup_screen(void); void render_rom_setup_screen(void);
#else // LOADING_SCREEN_SUPPORTED
#define LOADING_SCREEN_MUTEX(...)
#endif // LOADING_SCREEN_SUPPORTED
#endif #endif

View File

@ -308,9 +308,7 @@ void game_exit(void) {
exit(0); exit(0);
} }
void* main_game_init(void* isThreaded) { void* main_game_init(UNUSED void*) {
gIsThreaded = isThreaded != NULL;
// load language // load language
if (!djui_language_init(configLanguage)) { snprintf(configLanguage, MAX_CONFIG_STRING, "%s", ""); } if (!djui_language_init(configLanguage)) { snprintf(configLanguage, MAX_CONFIG_STRING, "%s", ""); }
@ -337,11 +335,6 @@ void* main_game_init(void* isThreaded) {
if (gCLIOpts.fullscreen == 1) { configWindow.fullscreen = true; } if (gCLIOpts.fullscreen == 1) { configWindow.fullscreen = true; }
else if (gCLIOpts.fullscreen == 2) { configWindow.fullscreen = false; } else if (gCLIOpts.fullscreen == 2) { configWindow.fullscreen = false; }
if (!gGfxInited) {
gfx_init(&WAPI, &RAPI, TITLE);
WAPI.set_keyboard_callbacks(keyboard_on_key_down, keyboard_on_key_up, keyboard_on_all_keys_up, keyboard_on_text_input);
}
audio_init(); audio_init();
sound_init(); sound_init();
smlua_audio_custom_init(); smlua_audio_custom_init();
@ -403,10 +396,12 @@ int main(int argc, char *argv[]) {
#ifdef LOADING_SCREEN_SUPPORTED #ifdef LOADING_SCREEN_SUPPORTED
bool threadSuccess = false; bool threadSuccess = false;
if (!gCLIOpts.hideLoadingScreen && pthread_mutex_init(&gLoadingThreadMutex, NULL) == 0) { if (!gCLIOpts.hideLoadingScreen && pthread_mutex_init(&gLoadingThreadMutex, NULL) == 0) {
if (pthread_create(&gLoadingThreadId, NULL, main_game_init, (void*) 1) == 0) { gIsThreaded = true;
if (pthread_create(&gLoadingThreadId, NULL, main_game_init, NULL) == 0) {
render_loading_screen(); // render the loading screen while the game is setup render_loading_screen(); // render the loading screen while the game is setup
threadSuccess = true; threadSuccess = true;
} }
gIsThreaded = false;
pthread_mutex_destroy(&gLoadingThreadMutex); pthread_mutex_destroy(&gLoadingThreadMutex);
} }
if (!threadSuccess) if (!threadSuccess)
@ -424,6 +419,10 @@ int main(int argc, char *argv[]) {
#endif #endif
if (!audio_api) { audio_api = &audio_null; } if (!audio_api) { audio_api = &audio_null; }
#ifdef LOADING_SCREEN_SUPPORTED
loading_screen_reset();
#endif
// initialize djui // initialize djui
djui_init(); djui_init();
djui_unicode_init(); djui_unicode_init();

View File

@ -100,6 +100,7 @@ bool main_rom_handler(void) {
return gRomIsValid; return gRomIsValid;
} }
#ifdef LOADING_SCREEN_SUPPORTED
void rom_on_drop_file(const char *path) { void rom_on_drop_file(const char *path) {
static bool hasDroppedInvalidFile = false; static bool hasDroppedInvalidFile = false;
if (strlen(path) > 0) { if (strlen(path) > 0) {
@ -109,4 +110,5 @@ void rom_on_drop_file(const char *path) {
} }
} }
} }
#endif
} }