Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
emscripten-core
GitHub Repository: emscripten-core/emscripten
Path: blob/main/test/emscripten_api_browser.c
4128 views
1
// Copyright 2012 The Emscripten Authors. All rights reserved.
2
// Emscripten is available under two separate licenses, the MIT license and the
3
// University of Illinois/NCSA Open Source License. Both these licenses can be
4
// found in the LICENSE file.
5
6
#include <stdbool.h>
7
#include <stdio.h>
8
#include <math.h>
9
#include <stdlib.h>
10
#include <SDL/SDL.h>
11
#include <emscripten.h>
12
#include <assert.h>
13
14
bool exit_ok = false;
15
int last = 0;
16
17
bool pre1ed = false;
18
bool pre2ed = false;
19
void pre1(void *arg) {
20
assert(!pre1ed);
21
assert(!pre2ed);
22
assert((long)arg == 123);
23
pre1ed = true;
24
}
25
void pre2(void *arg) {
26
assert(pre1ed);
27
assert(!pre2ed);
28
assert((long)arg == 98);
29
pre2ed = true;
30
}
31
32
bool fived = false;
33
void five(void *arg) {
34
assert((long)arg == 55);
35
fived = true;
36
emscripten_resume_main_loop();
37
}
38
39
void argey(void* arg) {
40
static int counter = 0;
41
assert((long)arg == 17);
42
counter++;
43
printf("argey: %d\n", counter);
44
if (counter == 5) {
45
emscripten_cancel_main_loop();
46
// The main loop is now done so its ok to run atexit handlers.
47
exit_ok = true;
48
exit(0);
49
}
50
}
51
52
void mainey() {
53
static int counter = 0;
54
printf("mainey: %d\n", counter++);
55
if (counter == 20) {
56
emscripten_pause_main_loop();
57
emscripten_async_call(five, (void*)55, 1000);
58
} else if (counter == 22) { // very soon after 20, so without pausing we fail
59
assert(fived);
60
emscripten_push_main_loop_blocker(pre1, (void*)123);
61
emscripten_push_main_loop_blocker(pre2, (void*)98);
62
} else if (counter == 23) {
63
assert(pre1ed);
64
assert(pre2ed);
65
printf("Good!\n");
66
emscripten_cancel_main_loop();
67
emscripten_set_main_loop_arg(argey, (void*)17, 0, 0);
68
}
69
}
70
71
void four(void *arg) {
72
assert((long)arg == 43);
73
printf("four!\n");
74
emscripten_set_main_loop(mainey, 0, 0);
75
}
76
77
EMSCRIPTEN_KEEPALIVE void third() {
78
int now = SDL_GetTicks();
79
printf("thard! %d\n", now);
80
assert(abs(now - last - 1000) < 500);
81
emscripten_async_call(four, (void*)43, -1); // triggers requestAnimationFrame
82
}
83
84
void second(void *arg) {
85
int now = SDL_GetTicks();
86
printf("sacond! %d\n", now);
87
assert(abs(now - last - 500) < 250);
88
last = now;
89
emscripten_async_run_script("Module._third()", 1000);
90
}
91
92
// Should not be called when main return but only once the
93
// main loops is stopped and the runtime shuts down.
94
void check_exit_ok() {
95
assert(exit_ok == true);
96
}
97
98
int main() {
99
SDL_Init(0);
100
last = SDL_GetTicks();
101
printf("frist! %d\n", last);
102
103
double ratio = emscripten_get_device_pixel_ratio();
104
double ratio2 = EM_ASM_DOUBLE({
105
return window.devicePixelRatio || 1.0;
106
});
107
108
assert(ratio == ratio2);
109
110
atexit(check_exit_ok);
111
112
emscripten_async_call(second, (void*)0, 500);
113
114
return 1;
115
}
116
117
118