forked from 42henry/herb-engine-C-3D
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb-platform.c
More file actions
174 lines (151 loc) · 5.67 KB
/
web-platform.c
File metadata and controls
174 lines (151 loc) · 5.67 KB
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
#include <emscripten.h>
#include <emscripten/html5.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "herbengineC3D.c"
/* The engine writes 0x00RRGGBB into pixels[].
Canvas ImageData expects bytes [R, G, B, A] in memory order.
On little-endian that means each uint32 must be 0xFFBBGGRR.
We keep two buffers: engine writes to engine_pixels, we convert
to rgba_pixels before handing to the canvas. */
static uint32_t *engine_pixels = NULL;
static uint32_t *rgba_pixels = NULL;
static void convert_to_rgba(void) {
for (int i = 0; i < WIDTH * HEIGHT; i++) {
uint32_t p = engine_pixels[i];
uint8_t r = (p >> 16) & 0xFF;
uint8_t g = (p >> 8) & 0xFF;
uint8_t b = p & 0xFF;
/* canvas uint32 (LE) = [R, G, B, A] = 0xFF_BB_GG_RR */
rgba_pixels[i] = (0xFF000000u) | ((uint32_t)b << 16) | ((uint32_t)g << 8) | r;
}
}
/* ---- mouse ---- */
static EM_BOOL on_mousemove(int type, const EmscriptenMouseEvent *e, void *ud) {
(void)type; (void)ud;
if (holding_mouse) {
mouse.x = WIDTH / 2 + e->movementX;
mouse.y = HEIGHT / 2 + e->movementY;
}
return EM_TRUE;
}
static EM_BOOL on_mousedown(int type, const EmscriptenMouseEvent *e, void *ud) {
(void)type; (void)ud;
if (e->button == 0) mouse_left_click = 1;
if (e->button == 2) mouse_right_click = 1;
emscripten_request_pointerlock("#canvas", 0);
holding_mouse = 1;
return EM_TRUE;
}
static EM_BOOL on_mouseup(int type, const EmscriptenMouseEvent *e, void *ud) {
(void)type; (void)ud;
if (e->button == 0) mouse_left_click = 0;
if (e->button == 2) mouse_right_click = 0;
return EM_TRUE;
}
/* ---- keyboard ---- */
static int browser_key_to_index(const char *key) {
if (strcmp(key, "KeyW") == 0) return 'w';
if (strcmp(key, "KeyA") == 0) return 'a';
if (strcmp(key, "KeyS") == 0) return 's';
if (strcmp(key, "KeyD") == 0) return 'd';
if (strcmp(key, "Space") == 0) return ' ';
if (strcmp(key, "ShiftLeft") == 0 ||
strcmp(key, "ShiftRight") == 0) return 128;
if (strcmp(key, "ControlLeft") == 0 ||
strcmp(key, "ControlRight") == 0) return 129;
if (strcmp(key, "Escape") == 0) return 130;
if (strcmp(key, "Digit1") == 0) return '1';
if (strcmp(key, "Digit2") == 0) return '2';
if (strcmp(key, "Digit3") == 0) return '3';
if (strcmp(key, "Digit4") == 0) return '4';
if (strcmp(key, "Digit5") == 0) return '5';
if (strcmp(key, "Digit6") == 0) return '6';
if (strcmp(key, "Digit7") == 0) return '7';
if (strcmp(key, "Digit8") == 0) return '8';
if (strcmp(key, "Digit9") == 0) return '9';
return -1;
}
static EM_BOOL on_keydown(int type, const EmscriptenKeyboardEvent *e, void *ud) {
(void)type; (void)ud;
int idx = browser_key_to_index(e->code);
if (idx >= 0) keys[idx] = 1;
return EM_TRUE;
}
static EM_BOOL on_keyup(int type, const EmscriptenKeyboardEvent *e, void *ud) {
(void)type; (void)ud;
int idx = browser_key_to_index(e->code);
if (idx >= 0) keys[idx] = 0;
return EM_TRUE;
}
/* called from JS when pointer lock changes */
EMSCRIPTEN_KEEPALIVE
void set_holding_mouse(int val) {
holding_mouse = val;
}
/* ---- main loop ---- */
static void main_loop(void) {
update();
convert_to_rgba();
mouse.x = WIDTH / 2;
mouse.y = HEIGHT / 2;
EM_ASM({
var buf = new Uint8ClampedArray(HEAPU8.buffer, $0, $1 * $2 * 4);
var idata = new ImageData(buf, $1, $2);
Module._ctx.putImageData(idata, 0, 0);
}, rgba_pixels, WIDTH, HEIGHT);
}
int main(void) {
EM_ASM({
var canvas = document.getElementById('canvas');
if (!canvas) {
canvas = document.createElement('canvas');
canvas.id = 'canvas';
document.body.appendChild(canvas);
}
canvas.width = $0;
canvas.height = $1;
canvas.style.display = 'block';
canvas.style.margin = '0 auto';
document.body.style.background = '#000';
document.body.style.margin = '0';
canvas.addEventListener('contextmenu', function(e) { e.preventDefault(); });
document.addEventListener('pointerlockchange', function() {
var locked = (document.pointerLockElement === canvas);
Module.ccall('set_holding_mouse', null, ['number'], [locked ? 1 : 0]);
});
Module._ctx = canvas.getContext('2d');
}, WIDTH, HEIGHT);
engine_pixels = (uint32_t *)malloc(WIDTH * HEIGHT * 4);
rgba_pixels = (uint32_t *)malloc(WIDTH * HEIGHT * 4);
memset(engine_pixels, 0, WIDTH * HEIGHT * 4);
memset(rgba_pixels, 0, WIDTH * HEIGHT * 4);
/* point the engine at our buffer */
pixels = engine_pixels;
/* key indices */
w = 'w';
a = 'a';
s = 's';
d = 'd';
space = ' ';
shift = 128;
control = 129;
escape = 130;
one = '1'; two = '2'; three = '3';
four = '4'; five = '5'; six = '6';
seven = '7'; eight = '8'; nine = '9';
emscripten_set_mousemove_callback("#canvas", NULL, 1, on_mousemove);
emscripten_set_mousedown_callback("#canvas", NULL, 1, on_mousedown);
emscripten_set_mouseup_callback ("#canvas", NULL, 1, on_mouseup);
emscripten_set_keydown_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, 1, on_keydown);
emscripten_set_keyup_callback (EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, 1, on_keyup);
holding_mouse = 0;
mouse_defined = 1;
init_stuff();
emscripten_set_main_loop(main_loop, TARGET_FPS, 1);
cleanup();
free(engine_pixels);
free(rgba_pixels);
return 0;
}