-
Notifications
You must be signed in to change notification settings - Fork 0
/
worldgen_init.js
72 lines (64 loc) · 1.71 KB
/
worldgen_init.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
Module['print'] = console.log;
Module['printErr'] = function(text) { console.log('stderr: ' + text) };
Module['preInit'] = worldGenPreInit;
Module['preRun'] = worldGenPreRun;
function worldGenPreInit()
{
FS.init(readStdin, writeStdout, writeStderr);
console.log("preinit");
}
function worldGenPreRun()
{
console.log("preRun");
Module['postRun'].push(worldGenPostRun);
}
function worldGenPostRun()
{
console.log("postRun");
var gifdata = FS.readFile('foo.gif');
var b64 = Base64.encode(gifdata);
if (typeof document != 'undefined') {
var wgimg = document.createElement("img");
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
console.log("drawing...");
wgimg.src = "data:image/gif;base64," + b64;
wgimg.onload = function () { ctx.drawImage(wgimg, 0, 0); };
}
}
var stdinBuffer = "50\n50\n20\n30\nfoo\n";
var stdinCatbuf = "";
function readStdin(stream, buffer, offset, length, pos /* ignored */)
{
code = stdinBuffer.charCodeAt(0);
if (code == 10) {
writeStdout(code);
writeStderr(code);
} else {
stdinCatbuf = stdinCatbuf.concat(stdinBuffer.charAt(0));
}
stdinBuffer = stdinBuffer.substr(1);
return code;
}
var stdoutBuffer = "";
function writeStdout(ch)
{
if (ch != 10) {
stdoutBuffer = stdoutBuffer.concat(String.fromCharCode(ch));
} else if (stdoutBuffer.length > 0) {
console.log(stdoutBuffer + stdinCatbuf);
stdinCatbuf = "";
stdoutBuffer = "";
}
}
var stderrBuffer = "";
function writeStderr(ch)
{
if (ch != 10) {
stderrBuffer = stderrBuffer.concat(String.fromCharCode(ch));
} else if (stderrBuffer.length > 0) {
console.log(stderrBuffer + stdinCatbuf);
stdinCatbuf = "";
stderrBuffer = "";
}
}