/* * emccpre.js: one of the Javascript components of an Emscripten-based * web/Javascript front end for Puzzles. * * The other parts of this system live in emcc.c and emcclib.js. It * also depends on being run in the context of a web page containing * an appropriate collection of bits and pieces (a canvas, some * buttons and links etc), which is generated for each puzzle by the * script html/jspage.pl. * * This file contains the Javascript code which is prefixed unmodified * to Emscripten's output via the --pre-js option. It declares all our * global variables, and provides the puzzle init function and a * couple of other helper functions. */ // To avoid flicker while doing complicated drawing, we use two // canvases, the same size. One is actually on the web page, and the // other is off-screen. We do all our drawing on the off-screen one // first, and then copy rectangles of it to the on-screen canvas in // response to draw_update() calls by the game backend. var onscreen_canvas, offscreen_canvas; // A persistent drawing context for the offscreen canvas, to save // requesting it for each individual graphics operation. var ctx; // Bounding rectangle for the copy to the onscreen canvas that will be // done at drawing end time. Updated by js_canvas_draw_update and used // by js_canvas_end_draw. var update_xmin, update_xmax, update_ymin, update_ymax; // Module object for Emscripten. We fill in these parameters to ensure // that Module.run() won't be called until we're ready (we want to do // our own init stuff first), and that when main() returns nothing // will get cleaned up so we remain able to call the puzzle's various // callbacks. // // // Page loading order: // // 1. The browser starts reading *.html (which comes from jspage.pl) // 2. It finds the var envscript = document.getElementById("environment"); var k, v; if (envscript !== null) for ([k, v] of Object.entries(JSON.parse(envscript.textContent))) ENV[k] = v; }; Module.onRuntimeInitialized = function() { // Run the C setup function, passing argv[1] as the fragment // identifier (so that permalinks of the form puzzle.html#game-id // can launch the specified id). Module.callMain([decodeURIComponent(location.hash)]); update_pixel_ratio(); // And if we get here with everything having gone smoothly, i.e. // we haven't crashed for one reason or another during setup, then // it's probably safe to hide the 'sorry, no puzzle here' div and // show the div containing the actual puzzle. var apology = document.getElementById("apology"); if (apology !== null) apology.style.display = "none"; document.getElementById("puzzle").style.display = ""; // Default to giving keyboard focus to the puzzle. onscreen_canvas.focus(); }; }