mirror of
git://git.tartarus.org/simon/puzzles.git
synced 2025-04-21 08:01:30 -07:00
Support for saving games in Javascript puzzles.
This is done by getting midend_serialise to produce the complete saved-game file as an in-memory string buffer, and then encoding that into a data: URI which we provide to the user as a hyperlink in a dialog box. The hyperlink has the 'download' attribute, which means clicking on it should automatically offer to save the file, and also lets me specify a not-too-silly default file name.
This commit is contained in:
26
emccpre.js
26
emccpre.js
@ -296,6 +296,32 @@ function initPuzzle() {
|
||||
command(9);
|
||||
};
|
||||
|
||||
// 'number' is used for C pointers
|
||||
get_save_file = Module.cwrap('get_save_file', 'number', []);
|
||||
free_save_file = Module.cwrap('free_save_file', 'void', ['number']);
|
||||
|
||||
document.getElementById("save").onclick = function(event) {
|
||||
if (dlg_dimmer === null) {
|
||||
var savefile_ptr = get_save_file();
|
||||
var savefile_text = Pointer_stringify(savefile_ptr);
|
||||
free_save_file(savefile_ptr);
|
||||
dialog_init("Download saved-game file");
|
||||
dlg_form.appendChild(document.createTextNode(
|
||||
"Click to download the "));
|
||||
var a = document.createElement("a");
|
||||
a.download = "puzzle.sav";
|
||||
a.href = "data:application/octet-stream," +
|
||||
encodeURIComponent(savefile_text);
|
||||
a.appendChild(document.createTextNode("saved-game file"));
|
||||
dlg_form.appendChild(a);
|
||||
dlg_form.appendChild(document.createTextNode("."));
|
||||
dlg_form.appendChild(document.createElement("br"));
|
||||
dialog_launch(function(event) {
|
||||
dialog_cleanup();
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
gametypelist = document.getElementById("gametype");
|
||||
gametypesubmenus.push(gametypelist);
|
||||
|
||||
|
Reference in New Issue
Block a user