Skip to content

Commit

Permalink
add file resolver for runtime FS access of emscripten builds
Browse files Browse the repository at this point in the history
  • Loading branch information
Moros1138 committed May 27, 2024
1 parent 9151ac3 commit ff632a5
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions olcPixelGameEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,13 @@ int main()
#endif
#endif

// File resolver for runtime FS access of emscripten builds
#if defined(__EMSCRIPTEN__)
#include <emscripten.h>
#define FILE_RESOLVE(url, file) emscripten_wget(url, file); emscripten_sleep(0)
#else
#define FILE_RESOLVE(url, file)
#endif

// O------------------------------------------------------------------------------O
// | PLATFORM-SPECIFIC DEPENDENCIES |
Expand Down

1 comment on commit ff632a5

@Moros1138
Copy link
Collaborator Author

@Moros1138 Moros1138 commented on ff632a5 Sep 2, 2024

Choose a reason for hiding this comment

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

On further contemplation, this will break several tinkers on PGEtinker. There currently exists a macro with the same name.... and to complicate it further, it has since been brought to my attention that this macro sucks and is prone to ... well lots of bad things because it makes multiple function calls without encapsulating them in something that makes it work in situations like

if(some_predicate)
    FILE_RESOLVE(..., ...);

where the code would expand and cause errors unexpected logic errors that are difficult to debug. To mitigate this, I switched to a function using the same name, implemented like so:

void FILE_RESOLVE(const char* url, const char* file)
{
    #if defined(__EMSCRIPTEN__)
    emscripten_wget(url, file);
    emscripten_sleep(0);
    #endif
}

not only do I recommend the function instead of the macro solution, I also recommend putting it into the olc namespace in order to prevent collision with existing tinkers... if adopted, in PGEtinker next I will remove the function from the examples and point them to olc::FILE_RESOLVE or whatever it ultimately gets called. I suck at names as well..

Or.. and I know you're gonna hate me for this because of how hard i pushed for it years ago... we could just drop it from PGE and let PGEtinker handle it however it wants. Thoughts?

Please sign in to comment.