aboutsummaryrefslogtreecommitdiff
path: root/vendor/wgpu/examples/sdl2/os_js.odin
blob: 9634f4afedf32b49f644cc8824d99829413f64c6 (plain)
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
package vendor_wgpu_example_triangle

import "vendor:wgpu"
import "vendor:wasm/js"

OS :: struct {
	initialized: bool,
}

@(private="file")
g_os: ^OS

os_init :: proc(os: ^OS) {
	g_os = os
	assert(js.add_window_event_listener(.Resize, nil, size_callback))
}

// NOTE: frame loop is done by the runtime.js repeatedly calling `step`.
os_run :: proc(os: ^OS) {
	os.initialized = true
}

os_get_render_bounds :: proc(os: ^OS) -> (width, height: u32) {
	rect := js.get_bounding_client_rect("body")
	return u32(rect.width), u32(rect.height)
}

os_get_surface :: proc(os: ^OS, instance: wgpu.Instance) -> wgpu.Surface {
	return wgpu.InstanceCreateSurface(
		instance,
		&wgpu.SurfaceDescriptor{
			nextInChain = &wgpu.SurfaceDescriptorFromCanvasHTMLSelector{
				sType = .SurfaceDescriptorFromCanvasHTMLSelector,
				selector = "#wgpu-canvas",
			},
		},
	)
}

@(private="file", export)
step :: proc(dt: f32) -> bool {
	if !g_os.initialized {
		return true
	}

	frame(dt)
	return true
}

@(private="file", fini)
os_fini :: proc() {
	js.remove_window_event_listener(.Resize, nil, size_callback)

	finish()
}

@(private="file")
size_callback :: proc(e: js.Event) {
	resize()
}