-
Notifications
You must be signed in to change notification settings - Fork 77
/
Copy pathweb.c
80 lines (63 loc) · 2.28 KB
/
web.c
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
#include <emscripten/emscripten.h>
#include <emscripten/html5.h>
#include <stdint.h>
#include "../../shared/constants.h"
#include "../../shared/platform-interface.h"
#include "../../shared/debug.h"
#include "web-input.h"
#include "web-audio.h"
static struct {
int32_t windowWidth;
int32_t windowHeight;
float lastFrameTime;
} state = {};
static EM_BOOL gameLoop(double time, void *userData) {
if (state.lastFrameTime == 0.0f) {
state.lastFrameTime = (float) time;
}
float dt = time - state.lastFrameTime;
state.lastFrameTime = time;
web_updateAudio();
game_update(dt);
game_draw();
return EM_TRUE;
}
static EM_BOOL onResize(int eventType, const EmscriptenUiEvent *uiEvent, void *userData) {
state.windowWidth = uiEvent->windowInnerWidth;
state.windowHeight = uiEvent->windowInnerHeight;
game_resize(state.windowWidth, state.windowHeight);
emscripten_set_canvas_element_size("#canvas", (double) state.windowWidth, (double) state.windowHeight);
return EM_TRUE;
}
int32_t main() {
double windowWidth = 0.0;
double windowHeight = 0.0;
emscripten_get_element_css_size("#canvas", &windowWidth, &windowHeight);
emscripten_set_canvas_element_size("#canvas", windowWidth, windowHeight);
state.windowWidth = (int32_t) windowWidth;
state.windowHeight = (int32_t) windowHeight;
EMSCRIPTEN_WEBGL_CONTEXT_HANDLE gl = emscripten_webgl_create_context("#canvas", &(EmscriptenWebGLContextAttributes) {
.majorVersion = 2,
.minorVersion = 0
});
if (gl == 0) {
platform_userMessage("Failed to initialize WebGL.");
return 1;
}
emscripten_webgl_make_context_current(gl);
web_initInputHandlers();
emscripten_set_resize_callback(EMSCRIPTEN_EVENT_TARGET_WINDOW, NULL, EM_FALSE, onResize);
if (!game_init(&(Game_InitOptions) {
.showInputToStartScreen = true,
.hideSystemInstructions = true,
.noAudio = true
})) {
return 1;
}
game_resize(state.windowWidth, state.windowHeight);
emscripten_request_animation_frame_loop(gameLoop, NULL);
return 0;
}
EM_JS(void, platform_userMessage, (const char* message), {
alert(UTF8ToString(message));
})