Add emscripten async bitmap

This commit is contained in:
Varun Patil 2020-05-04 16:22:32 +05:30
parent 3c39f14484
commit ee39e65061
1 changed files with 38 additions and 0 deletions

View File

@ -44,6 +44,10 @@
#include "font.h"
#include "eventthread.h"
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#endif
#define GUARD_MEGA \
{ \
if (p->megaSurface) \
@ -233,6 +237,39 @@ struct BitmapPrivate
}
};
#ifdef __EMSCRIPTEN__
EM_JS(void, load_file_async, (const char* fullPathC), {
Asyncify.handleSleep(function(wakeUp) {
const fullPath = UTF8ToString(fullPathC);
// Make cache object
if (!window.fileAsyncCache) window.fileAsyncCache = {};
// Check if already loaded
if (window.fileAsyncCache.hasOwnProperty(fullPath)) return wakeUp();
// Get full destination
const file = "game/" + fullPath;
// Get path and filename
const path = "/" + file.substring(0, file.lastIndexOf("/"));
const filename = file.substring(file.lastIndexOf("/") + 1);
// Get target URL
const iurl = "gameasync/" + fullPath;
// Delete original file
FS.unlink(path + "/" + filename);
// Get the new file
FS.createPreloadedFile(path, filename, iurl, true, true, function() {
window.fileAsyncCache[fullPath] = 1;
wakeUp();
}, console.error);
});
});
#endif;
struct BitmapOpenHandler : FileSystem::OpenHandler
{
SDL_Surface *surf;
@ -244,6 +281,7 @@ struct BitmapOpenHandler : FileSystem::OpenHandler
bool tryRead(SDL_RWops &ops, const char *ext, const char * fullPath)
{
#ifdef __EMSCRIPTEN__
load_file_async(fullPath);
surf = IMG_Load(fullPath);
#else
surf = IMG_LoadTyped_RW(&ops, 1, ext);