diff options
| author | Simon Tatham <anakin@pobox.com> | 2017-09-05 20:48:42 +0100 |
|---|---|---|
| committer | Simon Tatham <anakin@pobox.com> | 2017-09-05 20:58:05 +0100 |
| commit | 721119e4a61cbb261b456dfd134811d7beb5ce98 (patch) | |
| tree | c31c90e57d7cb002fcfc9f09bc15b505836ae422 /emcc.c | |
| parent | 1bf591a5735068d1853be13c5a4255962835d5fe (diff) | |
| download | puzzles-721119e4a61cbb261b456dfd134811d7beb5ce98.zip puzzles-721119e4a61cbb261b456dfd134811d7beb5ce98.tar.gz puzzles-721119e4a61cbb261b456dfd134811d7beb5ce98.tar.bz2 puzzles-721119e4a61cbb261b456dfd134811d7beb5ce98.tar.xz | |
Support for loading games in Javascript puzzles.
This is done by showing a dialog containing an <input type="file">
through which the user can 'upload' a save file - though, of course,
the 'upload' doesn't go to any HTTP server, but only into the mind of
the Javascript running in the same browser.
It would be even nicer to support drag-and-drop as an alternative UI
for getting the save file into the browser, but that isn't critical to
getting the first version of this feature out of the door.
Diffstat (limited to 'emcc.c')
| -rw-r--r-- | emcc.c | 38 |
1 files changed, 36 insertions, 2 deletions
@@ -766,9 +766,9 @@ struct savefile_write_ctx { size_t pos; }; -static void savefile_write(void *wctx, void *buf, int len) +static void savefile_write(void *vctx, void *buf, int len) { - struct savefile_write_ctx *ctx = (struct savefile_write_ctx *)wctx; + struct savefile_write_ctx *ctx = (struct savefile_write_ctx *)vctx; if (ctx->buffer) memcpy(ctx->buffer + ctx->pos, buf, len); ctx->pos += len; @@ -799,6 +799,40 @@ void free_save_file(char *buffer) sfree(buffer); } +struct savefile_read_ctx { + const char *buffer; + int len_remaining; +}; + +static int savefile_read(void *vctx, void *buf, int len) +{ + struct savefile_read_ctx *ctx = (struct savefile_read_ctx *)vctx; + if (ctx->len_remaining < len) + return FALSE; + memcpy(buf, ctx->buffer, len); + ctx->len_remaining -= len; + ctx->buffer += len; + return TRUE; +} + +void load_game(const char *buffer, int len) +{ + struct savefile_read_ctx ctx; + const char *err; + + ctx.buffer = buffer; + ctx.len_remaining = len; + err = midend_deserialise(me, savefile_read, &ctx); + + if (err) { + js_error_box(err); + } else { + select_appropriate_preset(); + resize(); + midend_redraw(me); + } +} + /* ---------------------------------------------------------------------- * Setup function called at page load time. It's called main() because * that's the most convenient thing in Emscripten, but it's not main() |