diff --git a/CMakeLists.txt b/CMakeLists.txt index 8ee2d8d..7295ce4 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -461,7 +461,7 @@ SET(EMS_FLAGS " -s USE_SDL=2 -s USE_SDL_IMAGE=2 -s USE_ZLIB=1 -s USE_OGG=1 -s US SET(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} ${EMS_FLAGS} ${ERR_FLAGS}") SET(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${EMS_FLAGS} ${ERR_FLAGS}") -SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${EMS_FLAGS} -s TOTAL_MEMORY=1073741824 -lopenal --preload-file game -s DISABLE_EXCEPTION_CATCHING=1 -s ASSERTIONS=0 -s SAFE_HEAP=0 -s MINIFY_HTML=0 -s 'ASYNCIFY_IMPORTS=[\"load_file_async\"]'") +SET(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${EMS_FLAGS} -s TOTAL_MEMORY=1073741824 -lopenal --preload-file game -s DISABLE_EXCEPTION_CATCHING=1 -s ASSERTIONS=0 -s SAFE_HEAP=0 -s MINIFY_HTML=0 -s 'ASYNCIFY_IMPORTS=[\"load_file_async_js\"]'") set_target_properties( ${PROJECT_NAME} diff --git a/src/alstream.cpp b/src/alstream.cpp index 2eb74c9..b37e99c 100644 --- a/src/alstream.cpp +++ b/src/alstream.cpp @@ -35,6 +35,10 @@ #include #include +#ifdef __EMSCRIPTEN__ +#include "emscripten.hpp" +#endif + ALStream::ALStream(LoopMode loopMode, const std::string &threadId) : looped(loopMode == Looped), @@ -256,6 +260,10 @@ struct ALStreamOpenHandler : FileSystem::OpenHandler void ALStream::openSource(const std::string &filename) { +#ifdef __EMSCRIPTEN__ + load_file_async(filename.c_str()); +#endif + ALStreamOpenHandler handler(srcOps, looped); shState->fileSystem().openRead(handler, filename.c_str()); source = handler.source; diff --git a/src/bitmap.cpp b/src/bitmap.cpp index f61d3c9..efd6025 100644 --- a/src/bitmap.cpp +++ b/src/bitmap.cpp @@ -248,7 +248,7 @@ struct BitmapOpenHandler : FileSystem::OpenHandler bool tryRead(SDL_RWops &ops, const char *ext, const char * fullPath) { #ifdef __EMSCRIPTEN__ - load_file_async(fullPath); + load_file_async_js(fullPath); surf = IMG_Load(fullPath); #else surf = IMG_LoadTyped_RW(&ops, 1, ext); diff --git a/src/emscripten.cpp b/src/emscripten.cpp index fefa90c..030247b 100644 --- a/src/emscripten.cpp +++ b/src/emscripten.cpp @@ -1,6 +1,8 @@ #include "emscripten.hpp" +#include "filesystem.h" +#include "sharedstate.h" -EM_JS(void, load_file_async, (const char* fullPathC), { +EM_JS(void, load_file_async_js, (const char* fullPathC), { Asyncify.handleSleep(function(wakeUp) { const fullPath = UTF8ToString(fullPathC); @@ -31,3 +33,20 @@ EM_JS(void, load_file_async, (const char* fullPathC), { }); }); +struct LoadOpenHandler : FileSystem::OpenHandler +{ + LoadOpenHandler() + {} + + bool tryRead(SDL_RWops &ops, const char *ext, const char *fullPath) + { + load_file_async_js(fullPath); + return true; + } +}; + +void load_file_async(const char * filename) { + LoadOpenHandler handler; + shState->fileSystem().openRead(handler, filename); +} + diff --git a/src/emscripten.hpp b/src/emscripten.hpp index a44e991..a814968 100644 --- a/src/emscripten.hpp +++ b/src/emscripten.hpp @@ -5,7 +5,9 @@ #include extern "C" { - void load_file_async(const char* fullPathC); + void load_file_async_js(const char* fullPathC); + + void load_file_async(const char* filename); } #endif diff --git a/src/soundemitter.cpp b/src/soundemitter.cpp index 5e5dba4..fb6795a 100644 --- a/src/soundemitter.cpp +++ b/src/soundemitter.cpp @@ -30,6 +30,10 @@ #include +#ifdef __EMSCRIPTEN__ +#include "emscripten.hpp" +#endif + #define SE_CACHE_MEM (10*1024*1024) // 10 MB struct SoundBuffer @@ -238,6 +242,10 @@ SoundBuffer *SoundEmitter::allocateBuffer(const std::string &filename) else { /* Buffer not in cache, needs to be loaded */ +#ifdef __EMSCRIPTEN__ + load_file_async(filename.c_str()); +#endif + SoundOpenHandler handler; shState->fileSystem().openRead(handler, filename.c_str()); buffer = handler.buffer;