emscripten/tests/emscripten_api_browser.cpp

93 строки
1.9 KiB
C++
Исходник Обычный вид История

2012-04-18 07:32:14 +04:00
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
2012-04-18 07:32:14 +04:00
#include<SDL.h>
#include<emscripten.h>
#include<assert.h>
int last = 0;
extern "C" {
2012-07-13 05:26:46 +04:00
bool pre1ed = false;
bool pre2ed = false;
void pre1(void *arg) {
2012-07-13 05:26:46 +04:00
assert(!pre1ed);
assert(!pre2ed);
assert((int)arg == 123);
2012-07-13 05:26:46 +04:00
pre1ed = true;
}
void pre2(void *arg) {
2012-07-13 05:26:46 +04:00
assert(pre1ed);
assert(!pre2ed);
assert((int)arg == 98);
2012-07-13 05:26:46 +04:00
pre2ed = true;
}
2012-05-07 05:04:12 +04:00
bool fived = false;
void five(void *arg) {
assert((int)arg == 55);
2012-05-07 05:04:12 +04:00
fived = true;
emscripten_resume_main_loop();
}
2012-05-02 00:28:40 +04:00
void mainey() {
static int counter = 0;
printf("mainey: %d\n", counter++);
2012-05-07 05:04:12 +04:00
if (counter == 20) {
emscripten_pause_main_loop();
emscripten_async_call(five, (void*)55, 1000);
2012-05-07 05:04:12 +04:00
} else if (counter == 22) { // very soon after 20, so without pausing we fail
2012-07-13 05:26:46 +04:00
assert(fived);
emscripten_push_main_loop_blocker(pre1, (void*)123);
emscripten_push_main_loop_blocker(pre2, (void*)98);
2012-07-13 05:26:46 +04:00
} else if (counter == 23) {
assert(pre1ed);
assert(pre2ed);
printf("Good!\n");
int result = 1;
2012-05-02 00:28:40 +04:00
REPORT_RESULT();
}
}
void four(void *arg) {
assert((int)arg == 43);
2012-05-02 00:28:40 +04:00
printf("four!\n");
emscripten_set_main_loop(mainey, 0, 0);
2012-05-02 00:28:40 +04:00
}
2012-04-19 20:50:32 +04:00
void __attribute__((used)) third() {
2012-04-18 07:32:14 +04:00
int now = SDL_GetTicks();
printf("thard! %d\n", now);
assert(fabs(now - last - 1000) < 500);
emscripten_async_call(four, (void*)43, -1); // triggers requestAnimationFrame
2012-04-18 07:32:14 +04:00
}
void second(void *arg) {
2012-04-18 07:32:14 +04:00
int now = SDL_GetTicks();
printf("sacond! %d\n", now);
assert(fabs(now - last - 500) < 250);
last = now;
2013-01-20 09:23:31 +04:00
emscripten_async_run_script("Module._third()", 1000);
2012-04-18 07:32:14 +04:00
}
}
void never() {
int result = 0;
REPORT_RESULT();
}
2012-04-18 07:32:14 +04:00
int main() {
SDL_Init(0);
last = SDL_GetTicks();
printf("frist! %d\n", last);
atexit(never); // should never be called - it is wrong to exit the runtime orderly if we have async calls!
emscripten_async_call(second, (void*)0, 500);
2012-04-18 07:32:14 +04:00
return 1;
}