Path: blob/main/test/emscripten_api_browser.c
4128 views
// Copyright 2012 The Emscripten Authors. All rights reserved.1// Emscripten is available under two separate licenses, the MIT license and the2// University of Illinois/NCSA Open Source License. Both these licenses can be3// found in the LICENSE file.45#include <stdbool.h>6#include <stdio.h>7#include <math.h>8#include <stdlib.h>9#include <SDL/SDL.h>10#include <emscripten.h>11#include <assert.h>1213bool exit_ok = false;14int last = 0;1516bool pre1ed = false;17bool pre2ed = false;18void pre1(void *arg) {19assert(!pre1ed);20assert(!pre2ed);21assert((long)arg == 123);22pre1ed = true;23}24void pre2(void *arg) {25assert(pre1ed);26assert(!pre2ed);27assert((long)arg == 98);28pre2ed = true;29}3031bool fived = false;32void five(void *arg) {33assert((long)arg == 55);34fived = true;35emscripten_resume_main_loop();36}3738void argey(void* arg) {39static int counter = 0;40assert((long)arg == 17);41counter++;42printf("argey: %d\n", counter);43if (counter == 5) {44emscripten_cancel_main_loop();45// The main loop is now done so its ok to run atexit handlers.46exit_ok = true;47exit(0);48}49}5051void mainey() {52static int counter = 0;53printf("mainey: %d\n", counter++);54if (counter == 20) {55emscripten_pause_main_loop();56emscripten_async_call(five, (void*)55, 1000);57} else if (counter == 22) { // very soon after 20, so without pausing we fail58assert(fived);59emscripten_push_main_loop_blocker(pre1, (void*)123);60emscripten_push_main_loop_blocker(pre2, (void*)98);61} else if (counter == 23) {62assert(pre1ed);63assert(pre2ed);64printf("Good!\n");65emscripten_cancel_main_loop();66emscripten_set_main_loop_arg(argey, (void*)17, 0, 0);67}68}6970void four(void *arg) {71assert((long)arg == 43);72printf("four!\n");73emscripten_set_main_loop(mainey, 0, 0);74}7576EMSCRIPTEN_KEEPALIVE void third() {77int now = SDL_GetTicks();78printf("thard! %d\n", now);79assert(abs(now - last - 1000) < 500);80emscripten_async_call(four, (void*)43, -1); // triggers requestAnimationFrame81}8283void second(void *arg) {84int now = SDL_GetTicks();85printf("sacond! %d\n", now);86assert(abs(now - last - 500) < 250);87last = now;88emscripten_async_run_script("Module._third()", 1000);89}9091// Should not be called when main return but only once the92// main loops is stopped and the runtime shuts down.93void check_exit_ok() {94assert(exit_ok == true);95}9697int main() {98SDL_Init(0);99last = SDL_GetTicks();100printf("frist! %d\n", last);101102double ratio = emscripten_get_device_pixel_ratio();103double ratio2 = EM_ASM_DOUBLE({104return window.devicePixelRatio || 1.0;105});106107assert(ratio == ratio2);108109atexit(check_exit_ok);110111emscripten_async_call(second, (void*)0, 500);112113return 1;114}115116117118