-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.html
192 lines (165 loc) · 5.15 KB
/
main.html
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<!-- Copyright © 2018 Ali Deym <[email protected]> (http://alideym.net) -->
<html>
<meta charset="utf-8">
<head>
<title>Lua SDL2 Emscripten C99 WebAssembly - By Ali Deym</title>
<script src="res/jquery-3.3.1.min.js"></script>
<script src="codemirror/codemirror.js"></script>
<link rel="stylesheet" href="codemirror/codemirror.css">
<script src="codemirror/lua.js"></script>
<link rel="stylesheet" href="codemirror/ambiance.css">
</head>
<body>
<canvas id="canvas" width="100%" height="30%" oncontextmenu="event.preventDefault()"></canvas>
<script type="text/javascript">
var myCodeMirror = CodeMirror(document.body, {
value: `
local rand_index = 0
local rand_x = 0
local rand_y = 0
local i = 0
local going_up = false
local going_right = true
local going_down = false
local going_left = false
local offset_x = 0
local offset_y = 0
function SDL2_Loop()
\tSDL_SetRenderDrawColor(30, 50, 50, 255)
\tSDL_RenderClear()
\tSDL_SetRenderDrawColor(14, 225, 155, 255)
\tif going_right then
\t\ti = i + 1
\t\toffset_x = i
\t\t
\t\tSDL_RenderFillRect(300 + i, 250 + offset_y, 75, 75)
\t\t
\t\tif i > 120 then
\t\t\tgoing_right = false
\t\t\tgoing_up = true
\t\t\ti = 0
\t\tend
\tend
\tif going_up then
\t\ti = i + 1
\t\toffset_y = i * -1
\t\t
\t\tSDL_RenderFillRect(300 + offset_x, 250 - i, 75, 75)
\t\t
\t\tif i > 120 then
\t\t\tgoing_up = false
\t\t\tgoing_left = true
\t\t\ti = 0
\t\tend
\tend
\tif going_left then
\t\ti = i + 1
\t\toffset_x = offset_x - 1
\t
\t\tSDL_RenderFillRect(300 + offset_x, 250 + offset_y, 75, 75)
\t
\t\tif i > 120 then
\t\t\tgoing_left = false
\t\t\tgoing_down = true
\t\t\ti = 0
\t\tend
\tend
\tif going_down then
\t\ti = i + 1
\t\toffset_y = offset_y + 1
\t
\t\tSDL_RenderFillRect(300 + offset_x, 250 + offset_y, 75, 75)
\t
\t\tif i > 120 then
\t\t\tgoing_down = false
\t\t\tgoing_right = true
\t\t\ti = 0
\t\tend
\tend
\trand_index = rand_index + 1
\tif rand_index > 20 then
\t\trand_index = 0
\t\trand_x = Random(1, 800)
\t\trand_y = Random(1, 600)
\tend
\tSDL_RenderFillRect(rand_x, rand_y, 5, 5)
end
function SDL2_Main()
\tSDL_Init()
\tSDL_CreateWindowAndRenderer()
\t
\tSDL_Loop("SDL2_Loop")
\t
\tSDL_Quit()
\t
end
SDL2_Main()`,
mode: "lua",
lineNumbers: true,
theme: "ambiance",
indentWithTabs: true,
tabSize: 2
/*extraKeys: {
Tab: (cm) => cm.execCommand("indentMore").execCommand("indentMore"),
"Shift-Tab": (cm) => cm.execCommand("indentLess"),
}*/
});
myCodeMirror.on("keyup", function (cm, event) {
text_changed ();
});
</script>
<!--<pre><code id="edit" class="lua" style="width: 800px; height: 480px;" onkeyup="text_changed();">
</code></pre>-->
<div id="result" style="width: 800px; float: right"></div>
</div>
<script type='text/javascript'>
var Module = {
canvas: (function() {
var canvas = document.getElementById('canvas');
return canvas;
})(),
postRun: [
text_changed
],
print: (function() {
return function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
console.log(text);
if(text != "emsc") {
document.getElementById("result").innerHTML += "<br>\n" + text;
}
};
})(),
printErr: function(text) {
if (arguments.length > 1) text = Array.prototype.slice.call(arguments).join(' ');
if (0) {
dump(text + '\n');
} else {
console.error(text);
}
}
};
var xhr = new XMLHttpRequest();
xhr.open('GET', 'main.wasm', true);
xhr.responseType = 'arraybuffer';
xhr.overrideMimeType("application/javascript");
xhr.onload = function() {
Module.wasmBinary = xhr.response;
var script = document.createElement('script');
script.src = "main.js";
document.body.appendChild(script);
};
xhr.send(null);
var timer;
function text_changed() {
clearTimeout(timer);
input = myCodeMirror.getValue();
timer = setTimeout(function() {
document.getElementById("result").innerHTML = "";
Module.ccall("run_lua", 'number', ['string'], [input]);
},
750);
}
</script>
</body>
</html>