-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
179 lines (157 loc) · 4.27 KB
/
index.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
<head>
<title>null0 game</title>
<style>
body {
color: white;
background: black;
font-family: sans-serif;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
gap: 10px;
}
h1 {
margin: 0;
}
</style>
</head>
<body>
<h1>My Game</h1>
<p>This is a minimal null0 runner, so you can check out your game.</p>
<null0-cart src="mygame.null0" />
</body>
<script type="importmap">
{
"imports": {
"cmem_helpers": "https://cdn.jsdelivr.net/npm/[email protected]/+esm",
"unzipit": "https://cdn.jsdelivr.net/npm/[email protected]/+esm",
"mime": "https://cdn.skypack.dev/mime"
}
}
</script>
<script type="module">
// NOTE: I have this here, while I am building things, but will replace it with a simple import, later.
import { unzip } from "unzipit"
import memhelpers from 'cmem_helpers'
import { getType } from 'mime'
const encoder = new TextEncoder()
const decoder = new TextDecoder()
function colorFromWasm (pixelValue) {
const a = pixelValue >> 24 & 0xFF
const b = pixelValue >> 16 & 0xFF
const g = pixelValue >> 8 & 0xFF
const r = pixelValue & 0xFF
return `rgba(${r}, ${g}, ${b}, ${a})`
}
export async function loadCart(bytes, canvas=document.body.appendChild(document.createElement('canvas'))) {
let zip
let wasmBytes
const sig = (new Uint8Array(bytes.slice(0,4))).join(',')
if (sig === '80,75,3,4') {
const z = await unzip(bytes)
zip = z.entries
if (!zip['main.wasm']) {
throw new Error('No main.wasm in your cart.')
}
wasmBytes = await zip['main.wasm'].arrayBuffer()
} else if (sig === '0,97,115,109') {
wasmBytes = bytes
} else {
throw new Error('Unknown cart-type.')
}
// get a file from zip as an ArrayBuffer
const loadFileBuffer = async filenamePtr => {
const f = getString(filenamePtr)
if (!zip[f]) {
throw new Error(`File not found: ${f}`)
}
return zip[f].arrayBuffer()
}
// get a file from zip as a Blob
const loadFileBlob = async filenamePtr => {
const f = getString(filenamePtr)
if (!zip[f]) {
throw new Error(`File not found: ${f}`)
}
const b = await zip[f].blob(getType(f))
return b
}
// resource-counter
const resources = [canvas, null] // screen is 0, default-font is 1
const api = {
null0: {
trace (format) {
// TODO: process printf vargs
console.log(getString(format))
},
load_image(filename) {
const r = resources.length
resources.push(null)
loadFileBlob(filename).then(b => {
const i = new Image()
i.src = URL.createObjectURL(b)
resources[r] = i
})
return r
},
clear(c) {
ctx.rect(0, 0, canvas.width, canvas.height)
ctx.fillStyle = colorFromWasm(c)
ctx.fill()
},
draw_image(src, posX, posY) {
if (!resources[src]) {
return
}
ctx.drawImage(resources[src], posX, posY)
}
},
wasi_snapshot_preview1: {
proc_exit () {
console.error('exit requested.')
}
}
}
const { instance: { exports } } = await WebAssembly.instantiate(wasmBytes, api)
const { struct, setString, getString } = memhelpers(exports.memory.buffer, exports.malloc)
const ctx = canvas.getContext("2d")
if (exports._start) {
exports._start()
}
if (exports.load) {
exports.load()
}
if (exports.update) {
const updateLoop = t => {
exports.update(t)
requestAnimationFrame(updateLoop)
}
requestAnimationFrame(updateLoop)
}
}
export class Null0Cart extends HTMLElement {
constructor() {
super()
this.shadow = this.attachShadow({mode: 'open'});
this.shadow.innerHTML = `
<style>
canvas {
border: 1px solid white;
}
</style>
<canvas width=320 height=240></canvas>
`
}
async connectedCallback() {
this.canvas = this.shadow.querySelector('canvas')
if (!this.attributes.src) {
throw new Error('Set src attribute to your cart.')
}
this.cart = await loadCart(await fetch(this.attributes.src.value).then(r => r.arrayBuffer()), this.canvas)
}
}
window.customElements.define('null0-cart', Null0Cart)
</script>