From 56e1696cd42a3c5cf01d695360b67f24dc518cd6 Mon Sep 17 00:00:00 2001 From: Dominic Masters Date: Wed, 4 Feb 2026 21:52:14 -0600 Subject: [PATCH] Asset loading --- CMakeLists.txt | 4 +-- src/asset/asset.c | 50 ++++++++++++++++++++++++++++++++- src/asset/asset.h | 59 ++++++++++++++++++++++++++------------- src/debug/debug.c | 8 +++++- src/display/display.c | 21 +------------- src/display/framebuffer.c | 14 ++++++++++ src/engine/engine.c | 1 + src/input/input.c | 5 ++-- src/input/inputbutton.c | 23 ++++++++------- src/ui/ui.c | 2 ++ 10 files changed, 130 insertions(+), 57 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5fd6c77..da19778 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -119,12 +119,9 @@ elseif(DUSK_TARGET_SYSTEM STREQUAL "psp") elseif(DUSK_TARGET_SYSTEM STREQUAL "gamecube" OR DUSK_TARGET_SYSTEM STREQUAL "wii") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-rtti -fno-exceptions") - configure_file(opengl.pc.in opengl.pc @ONLY) - find_package(PkgConfig REQUIRED) pkg_check_modules(zip IMPORTED_TARGET libzip) - target_compile_definitions(${DUSK_LIBRARY_TARGET_NAME} PUBLIC DOLPHIN ) @@ -159,6 +156,7 @@ elseif(DUSK_TARGET_SYSTEM STREQUAL "gamecube" OR DUSK_TARGET_SYSTEM STREQUAL "wi cglm liblua m + fat PkgConfig::zip ) endif() diff --git a/src/asset/asset.c b/src/asset/asset.c index e57c5b8..b862608 100644 --- a/src/asset/asset.c +++ b/src/asset/asset.c @@ -12,14 +12,62 @@ #include "asset/assettype.h" #include "engine/engine.h" #include "debug/debug.h" +#include "util/string.h" errorret_t assetInit(void) { memoryZero(&ASSET, sizeof(asset_t)); #if DOLPHIN + // Init FAT driver. + if(!fatInitDefault()) errorThrow("Failed to initialize FAT filesystem."); + + char_t **dolphinSearchPath = (char_t **)ASSET_DOLPHIN_PATHS; + char_t foundPath[FILENAME_MAX]; + foundPath[0] = '\0'; + do { + // Try open dir + DIR *pdir = opendir(*dolphinSearchPath); + if(pdir == NULL) continue; + + // Scan if file is present + while(true) { + struct dirent* pent = readdir(pdir); + if(pent == NULL) break; + + if(stringCompareInsensitive(pent->d_name, ASSET_FILE) != 0) { + continue; + } + + // Copy out filename + snprintf( + foundPath, + FILENAME_MAX, + "%s%s", + *dolphinSearchPath, + ASSET_FILE + ); + break; + } + + // Close dir. + closedir(pdir); + + // Did we find the file here? + if(foundPath[0] != '\0') break; + } while(*(++dolphinSearchPath) != NULL); + + // Did we find the asset file? + if(foundPath[0] == '\0') { + errorThrow("Failed to find asset file on FAT filesystem."); + } + + ASSET.zip = zip_open(foundPath, ZIP_RDONLY, NULL); + if(ASSET.zip == NULL) { + errorThrow("Failed to open asset file on FAT filesystem."); + } errorOk(); #endif - + // Engine may have been provided the launch path if(ENGINE.argc > 0) { // Get the directory of the executable diff --git a/src/asset/asset.h b/src/asset/asset.h index b56866f..ff7103c 100644 --- a/src/asset/asset.h +++ b/src/asset/asset.h @@ -9,27 +9,46 @@ #include "error/error.h" #include "assettype.h" -#if ASSET_TYPE == wad - #if PSP - #define ASSET_PBP_READ_PBP_FROM_HOST 0 - #define ASSET_PBP_SIGNATURE_SIZE 4 - #define ASSET_PBP_SIGNATURE "\0PBP" +#if PSP + #define ASSET_PBP_READ_PBP_FROM_HOST 0 + #define ASSET_PBP_SIGNATURE_SIZE 4 + #define ASSET_PBP_SIGNATURE "\0PBP" + + typedef struct { + char_t signature[ASSET_PBP_SIGNATURE_SIZE]; + uint32_t version; + uint32_t sfoOffset; + uint32_t icon0Offset; + uint32_t icon1Offset; + uint32_t pic0Offset; + uint32_t pic1Offset; + uint32_t snd0Offset; + uint32_t pspOffset; + uint32_t psarOffset; + } assetpbp_t; + +#elif DOLPHIN + #include + #include + #include + #include + #include + #include + #include + #include + + static const char_t *ASSET_DOLPHIN_PATHS[] = { + "/", + "/Dusk", + "%s/dusk", + "%s/DUSK", + "%s/apps", + "%s/apps/Dusk", + "%s/apps/dusk", + "%s/apps/DUSK", + NULL + }; - typedef struct { - char_t signature[ASSET_PBP_SIGNATURE_SIZE]; - uint32_t version; - uint32_t sfoOffset; - uint32_t icon0Offset; - uint32_t icon1Offset; - uint32_t pic0Offset; - uint32_t pic1Offset; - uint32_t snd0Offset; - uint32_t pspOffset; - uint32_t psarOffset; - } assetpbp_t; - #endif -#else - #error "Unsupported ASSET_TYPE" #endif #define ASSET_FILE "dusk.dsk" diff --git a/src/debug/debug.c b/src/debug/debug.c index 9297346..807471a 100644 --- a/src/debug/debug.c +++ b/src/debug/debug.c @@ -27,7 +27,13 @@ void debugPrint(const char_t *message, ...) { } #elif DOLPHIN - if(!DISPLAY.frameBuffer) return; + if(!DISPLAY.frameBuffer) { + errorret_t ret = displayInit(); + if(ret.code != ERROR_OK) { + abort(); + } + } + console_init( DISPLAY.frameBuffer, 20, diff --git a/src/display/display.c b/src/display/display.c index 48d679e..2c8373b 100644 --- a/src/display/display.c +++ b/src/display/display.c @@ -86,26 +86,6 @@ errorret_t displayInit(void) { VIDEO_WaitVSync(); if(DISPLAY.screenMode->viTVMode & VI_NON_INTERLACE) VIDEO_WaitVSync(); - // DISPLAY.fifoBuffer = MEM_K0_TO_K1(memalign(32,FIFO_SIZE)); - // memset(DISPLAY.fifoBuffer, 0, FIFO_SIZE); - - // GX_Init(DISPLAY.fifoBuffer, FIFO_SIZE); - // GXColor backgroundColor = {0, 0, 0, 255}; - // GX_SetCopyClear(backgroundColor, 0x00ffffff); - // GX_SetViewport(0,0,DISPLAY.screenMode->fbWidth,DISPLAY.screenMode->efbHeight,0,1); - // GX_SetDispCopyYScale((f32)DISPLAY.screenMode->xfbHeight/(f32)DISPLAY.screenMode->efbHeight); - // GX_SetScissor(0,0,DISPLAY.screenMode->fbWidth,DISPLAY.screenMode->efbHeight); - // GX_SetDispCopySrc(0,0,DISPLAY.screenMode->fbWidth,DISPLAY.screenMode->efbHeight); - // GX_SetDispCopyDst(DISPLAY.screenMode->fbWidth,DISPLAY.screenMode->xfbHeight); - // GX_SetCopyFilter(DISPLAY.screenMode->aa,DISPLAY.screenMode->sample_pattern, - // GX_TRUE,DISPLAY.screenMode->vfilter); - // GX_SetFieldMode(DISPLAY.screenMode->field_rendering, - // ((DISPLAY.screenMode->viHeight==2*DISPLAY.screenMode->xfbHeight)?GX_ENABLE:GX_DISABLE)); - - // GX_SetCullMode(GX_CULL_NONE); - // GX_CopyDisp(DISPLAY.frameBuffer,GX_TRUE); - // GX_SetDispCopyGamma(GX_GM_1_0); - #endif quadInit(); @@ -147,6 +127,7 @@ errorret_t displayUpdate(void) { } SDL_GL_MakeCurrent(DISPLAY.window, DISPLAY.glContext); + #elif DOLPHIN diff --git a/src/display/framebuffer.c b/src/display/framebuffer.c index 7b7e340..26339e2 100644 --- a/src/display/framebuffer.c +++ b/src/display/framebuffer.c @@ -68,6 +68,13 @@ int32_t frameBufferGetWidth(const framebuffer_t *framebuffer) { } return framebuffer->texture.width; + + #elif DOLPHIN + return DISPLAY.screenMode->fbWidth; + + #else + #error "Unsupported DISPLAY_TYPE." + #endif } @@ -84,6 +91,13 @@ int32_t frameBufferGetHeight(const framebuffer_t *framebuffer) { } return framebuffer->texture.height; + + #elif DOLPHIN + return DISPLAY.screenMode->efbHeight; + + #else + #error "Unsupported DISPLAY_TYPE." + #endif } diff --git a/src/engine/engine.c b/src/engine/engine.c index a197d51..281d0da 100644 --- a/src/engine/engine.c +++ b/src/engine/engine.c @@ -18,6 +18,7 @@ #include "script/scriptmanager.h" #include "debug/debug.h" #include "item/backpack.h" +#include "assert/assert.h" engine_t ENGINE; diff --git a/src/input/input.c b/src/input/input.c index 7db8c8f..e714c14 100644 --- a/src/input/input.c +++ b/src/input/input.c @@ -11,6 +11,7 @@ #include "util/string.h" #include "util/math.h" #include "time/time.h" +#include "debug/debug.h" input_t INPUT; @@ -78,7 +79,7 @@ void inputUpdate(void) { // For each button... inputbuttondata_t *cur = &INPUT_BUTTON_DATA[0]; - do { + while(cur->name) { cur->lastVal = cur->curVal; cur->curVal = inputButtonGetValue(cur->button); @@ -104,7 +105,7 @@ void inputUpdate(void) { #endif cur++; - } while(cur->name); + } // Do we need to fire off events? #if TIME_FIXED == 0 diff --git a/src/input/inputbutton.c b/src/input/inputbutton.c index d252bef..a9ffa03 100644 --- a/src/input/inputbutton.c +++ b/src/input/inputbutton.c @@ -174,18 +174,18 @@ inputbuttondata_t INPUT_BUTTON_DATA[] = { #elif DOLPHIN #if INPUT_GAMEPAD == 1 - { .name = "a", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = PAD_BUTTON_A } }, - { .name = "b", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = PAD_BUTTON_B } }, - { .name = "x", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = PAD_BUTTON_X } }, - { .name = "y", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = PAD_BUTTON_Y } }, - { .name = "start", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = PAD_BUTTON_START } }, + { .name = "a", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = PAD_BUTTON_A } }, + { .name = "b", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = PAD_BUTTON_B } }, + { .name = "x", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = PAD_BUTTON_X } }, + { .name = "y", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = PAD_BUTTON_Y } }, + { .name = "start", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = PAD_BUTTON_START } }, { .name = "dpad_up", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = PAD_BUTTON_UP } }, { .name = "dpad_down", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = PAD_BUTTON_DOWN } }, { .name = "dpad_left", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = PAD_BUTTON_LEFT } }, { .name = "dpad_right", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = PAD_BUTTON_RIGHT } }, - { .name = "l", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = PAD_TRIGGER_L } }, - { .name = "r", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = PAD_TRIGGER_R } }, - { .name = "z", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = PAD_TRIGGER_Z } }, + { .name = "l", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = PAD_TRIGGER_L } }, + { .name = "r", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = PAD_TRIGGER_R } }, + { .name = "z", { .type = INPUT_BUTTON_TYPE_GAMEPAD, .gpButton = PAD_TRIGGER_Z } }, { .name = "lstick_positive_x", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = INPUT_GAMEPAD_AXIS_LEFT_X, .positive = true } } }, { .name = "lstick_negative_x", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = INPUT_GAMEPAD_AXIS_LEFT_X, .positive = false } } }, { .name = "lstick_positive_y", { .type = INPUT_BUTTON_TYPE_GAMEPAD_AXIS, .gpAxis = { .axis = INPUT_GAMEPAD_AXIS_LEFT_Y, .positive = true } } }, @@ -220,9 +220,9 @@ float_t inputButtonGetValue(const inputbutton_t button) { case INPUT_BUTTON_TYPE_KEYBOARD: { #if INPUT_SDL2 == 1 return INPUT.keyboardState[button.scancode] ? 1.0f : 0.0f; - #else - return 0.0f; #endif + + return 0.0f; } #endif @@ -233,6 +233,7 @@ float_t inputButtonGetValue(const inputbutton_t button) { return 1.0f; } #endif + return 0.0f; } @@ -245,6 +246,8 @@ float_t inputButtonGetValue(const inputbutton_t button) { if(value < INPUT.deadzone) return 0.0f; return value; #endif + + return 0.0f; } #endif diff --git a/src/ui/ui.c b/src/ui/ui.c index 1f9bf3b..f602b22 100644 --- a/src/ui/ui.c +++ b/src/ui/ui.c @@ -11,6 +11,8 @@ #include "display/spritebatch.h" #include "display/screen.h" +#include "debug/debug.h" + ui_t UI; errorret_t uiInit(void) {