diff options
-rw-r--r-- | src/library.js | 6 | ||||
-rw-r--r-- | src/library_browser.js | 24 | ||||
-rw-r--r-- | system/include/emscripten/emscripten.h | 22 | ||||
-rwxr-xr-x | tests/runner.py | 5 | ||||
-rw-r--r-- | tests/sdl_image_prepare.c | 49 |
5 files changed, 99 insertions, 7 deletions
diff --git a/src/library.js b/src/library.js index 6662e9b7..1bb58833 100644 --- a/src/library.js +++ b/src/library.js @@ -304,12 +304,14 @@ LibraryManager.library = { // You can also call this with a typed array instead of a url. It will then // do preloading for the Image/Audio part, as if the typed array were the // result of an XHR that you did manually. - createPreloadedFile: function(parent, name, url, canRead, canWrite, onload, onerror) { + createPreloadedFile: function(parent, name, url, canRead, canWrite, onload, onerror, dontCreateFile) { Browser.ensureObjects(); var fullname = FS.joinPath([parent, name], true); function processData(byteArray) { function finish(byteArray) { - FS.createDataFile(parent, name, byteArray, canRead, canWrite); + if (!dontCreateFile) { + FS.createDataFile(parent, name, byteArray, canRead, canWrite); + } if (onload) onload(); removeRunDependency('cp ' + fullname); } diff --git a/src/library_browser.js b/src/library_browser.js index 27bf4a0c..6985e04a 100644 --- a/src/library_browser.js +++ b/src/library_browser.js @@ -364,14 +364,34 @@ mergeInto(LibraryManager.library, { _file.substr(index +1), _url, true, true, function() { - FUNCTION_TABLE[onload](file); + if (onload) FUNCTION_TABLE[onload](file); }, function() { - FUNCTION_TABLE[onerror](file); + if (onerror) FUNCTION_TABLE[onerror](file); } ); }, + emscripten_async_prepare: function(file, onload, onerror) { + var _file = Pointer_stringify(file); + var data = FS.analyzePath(_file); + if (!data.exists) return -1; + var index = _file.lastIndexOf('/'); + FS.createPreloadedFile( + _file.substr(0, index), + _file.substr(index +1), + new Uint8Array(data.object.contents), true, true, + function() { + if (onload) FUNCTION_TABLE[onload](file); + }, + function() { + if (onerror) FUNCTION_TABLE[onerror](file); + }, + true // don'tCreateFile - it's already there + ); + return 0; + }, + emscripten_async_run_script__deps: ['emscripten_run_script'], emscripten_async_run_script: function(script, millis) { Module['noExitRuntime'] = true; diff --git a/system/include/emscripten/emscripten.h b/system/include/emscripten/emscripten.h index 3e69044a..1b1310c4 100644 --- a/system/include/emscripten/emscripten.h +++ b/system/include/emscripten/emscripten.h @@ -157,14 +157,30 @@ float emscripten_random(); */ /* - * Load file from url in asynchronous way. - * When file is loaded then 'onload' callback will called. + * Load file from url in asynchronous way. In addition to + * fetching the URL from the network, the contents are + * prepared so that the data is usable in IMG_Load and + * so forth (we asynchronously do the work to make the + * browser decode the image or audio and so forth). + * When file is ready then 'onload' callback will called. * If any error occurred 'onerror' will called. * The callbacks are called with the file as their argument. - */ + */ void emscripten_async_wget(const char* url, const char* file, void (*onload)(const char*), void (*onerror)(const char*)); /* + * Prepare a file in asynchronous way. This does just the + * preparation part of emscripten_async_wget, that is, it + * works on file data already present, and asynchronously + * prepares it for use in IMG_Load, Mix_LoadWAV, etc. + * When file is loaded then 'onload' callback will called. + * If any error occurred 'onerror' will called. + * The callbacks are called with the file as their argument. + * @return 0 if successful, -1 if the file does not exist + */ +int emscripten_async_prepare(const char* file, void (*onload)(const char*), void (*onerror)(const char*)); + +/* * Profiling tools. * INIT must be called first, with the maximum identifier that * will be used. BEGIN will add some code that marks diff --git a/tests/runner.py b/tests/runner.py index dc2dab6a..56f06db2 100755 --- a/tests/runner.py +++ b/tests/runner.py @@ -8339,6 +8339,11 @@ elif 'browser' in str(sys.argv): shutil.move(os.path.join(self.get_dir(), basename), basename + '.renamedsoitcannotbefound'); self.run_browser('page.html', '', '/report_result?' + str(width)) + def test_sdl_image_prepare(self): + # load an image file, get pixel data. Also O2 coverage for --preload-file + shutil.copyfile(path_from_root('tests', 'screenshot.jpg'), os.path.join(self.get_dir(), 'screenshot.not')) + self.btest('sdl_image_prepare.c', reference='screenshot.jpg', args=['--preload-file', 'screenshot.not']) + def test_sdl_canvas(self): open(os.path.join(self.get_dir(), 'sdl_canvas.c'), 'w').write(self.with_report_result(open(path_from_root('tests', 'sdl_canvas.c')).read())) diff --git a/tests/sdl_image_prepare.c b/tests/sdl_image_prepare.c new file mode 100644 index 00000000..1c4d7218 --- /dev/null +++ b/tests/sdl_image_prepare.c @@ -0,0 +1,49 @@ +#include <stdio.h> +#include <SDL/SDL.h> +#include <SDL/SDL_image.h> +#include <assert.h> +#include <emscripten.h> + +SDL_Surface* screen; + +int testImage(const char* fileName) { + SDL_Surface *image = IMG_Load(fileName); + if (!image) + { + printf("IMG_Load: %s\n", IMG_GetError()); + return 0; + } + assert(image->format->BitsPerPixel == 32); + assert(image->format->BytesPerPixel == 4); + assert(image->pitch == 4*image->w); + int result = image->w; + + SDL_BlitSurface (image, NULL, screen, NULL); + SDL_FreeSurface (image); + + return result; +} + +void ready(const char *f) { + printf("ready!\n"); + + testImage("screenshot.jpg"); // relative path + + SDL_Flip(screen); +} + +int main() { + SDL_Init(SDL_INIT_VIDEO); + screen = SDL_SetVideoMode(600, 450, 32, SDL_SWSURFACE); + + printf("rename..\n"); + + rename("screenshot.not", "screenshot.jpg"); + + printf("prepare..\n"); + + assert(emscripten_async_prepare("screenshot.jpg", ready, NULL) == 0); + + return 0; +} + |