Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable asset load redirection #106

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/hello_imgui/hello_imgui_assets.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *)
Expand All @@ -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);

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I have to emit Python bindings for all other code in this library, I cannot use bare C function pointers.

Instead I will need to be replaced by a std::function (which is essentially the same except that it can be bound using Python).

// 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

Expand Down
17 changes: 15 additions & 2 deletions src/hello_imgui/internal/hello_imgui_assets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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__
{
Expand Down Expand Up @@ -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());
Expand All @@ -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


Expand Down
Loading