From be05d9634178f73cf6d738cbcdfabd49aab87386 Mon Sep 17 00:00:00 2001 From: Jorg Neves Bliesener Date: Sat, 4 May 2024 23:13:35 +0200 Subject: [PATCH] Enable asset load redirection --- src/hello_imgui/hello_imgui_assets.h | 13 +++++++++++++ src/hello_imgui/internal/hello_imgui_assets.cpp | 17 +++++++++++++++-- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/hello_imgui/hello_imgui_assets.h b/src/hello_imgui/hello_imgui_assets.h index 37930116..d3f1d3c7 100644 --- a/src/hello_imgui/hello_imgui_assets.h +++ b/src/hello_imgui/hello_imgui_assets.h @@ -38,6 +38,8 @@ struct AssetFileData // You *have* to call FreeAssetFileData to free the memory, except if you use // ImGui::GetIO().Fonts->AddFontFromMemoryTTF, which will take ownership of the // data and free it for you. +// This function can be redirected with setLoadAssetFileDataFunction. If not redirected, +// it calls DefaultLoadAssetFileData. AssetFileData LoadAssetFileData(const char *assetPath); // FreeAssetFileData(AssetFileData *) @@ -47,6 +49,17 @@ AssetFileData LoadAssetFileData(const char *assetPath); void FreeAssetFileData(AssetFileData * assetFileData); // @@md +// Function type to redirect asset loads. Function receives a path and +// returns an AssetFileData structure. By default, it points to +// DefaultLoadAssetFileData. +typedef AssetFileData (*LoadAssetFileDataFunc)(const char *assetPath); + +// Redirect asset loads to user-defined function +void setLoadAssetFileDataFunction(LoadAssetFileDataFunc func); + +// This function actually performs the asset load, as described in +// LoadAssetFileData +AssetFileData DefaultLoadAssetFileData(const char *assetPath); // @@md#assetFileFullPath diff --git a/src/hello_imgui/internal/hello_imgui_assets.cpp b/src/hello_imgui/internal/hello_imgui_assets.cpp index 6d6b4f09..92cd325e 100644 --- a/src/hello_imgui/internal/hello_imgui_assets.cpp +++ b/src/hello_imgui/internal/hello_imgui_assets.cpp @@ -263,7 +263,7 @@ bool AssetExists(const std::string& assetFilename) #ifdef HELLOIMGUI_USE_SDL2 -AssetFileData LoadAssetFileData(const char *assetPath) +AssetFileData DefaultLoadAssetFileData(const char *assetPath) { #ifdef __ANDROID__ { @@ -328,7 +328,7 @@ AssetFileData LoadAssetFileData_Impl(const char *assetPath) } } -AssetFileData LoadAssetFileData(const char *assetPath) +AssetFileData DefaultLoadAssetFileData(const char *assetPath) { std::string fullPath = assetFileFullPath(assetPath); AssetFileData r = LoadAssetFileData_Impl(fullPath.c_str()); @@ -348,6 +348,19 @@ void FreeAssetFileData(AssetFileData * assetFileData) assetFileData = nullptr; } +static LoadAssetFileDataFunc loadAssetFileDataFunc = DefaultLoadAssetFileData; + +AssetFileData LoadAssetFileData(const char *assetPath) +{ + AssetFileData data = loadAssetFileDataFunc(assetPath); + return data; +} + +void setLoadAssetFileDataFunction(LoadAssetFileDataFunc newLoadAssetFileDataFunc) +{ + loadAssetFileDataFunc = newLoadAssetFileDataFunc; +} + #endif // #ifdef HELLOIMGUI_USE_SDL2