Hello,
I have this error when porting a game written in C to HTML with emscripten :
SDL_Delay called on the main thread! Potential infinite loop, quitting.
Can you help me to resolve it.
Thank you very match.
Hello,
I have this error when porting a game written in C to HTML with emscripten :
SDL_Delay called on the main thread! Potential infinite loop, quitting.
Can you help me to resolve it.
Thank you very match.
Don't call SDL_Delay on the main thread.
...if you want more specific answers, you will have to provide some actual code.
Hello to all my stalkers.
Thank you for your response
My first c code:
int main ( int argc, char** argv )
{
after some code...
playgame(); //function
}
int playgame()
{
while(1)
{
if (collision_with_enemy==1 )
{
SDL_BlitSurface(image1, NULL, screen, &position);
SDL_Flip(screen);
SDL_Delay(10);
SDL_BlitSurface(image2, NULL, screen, &position);
SDL_Flip(screen);
}
}
}
My second code with emscripten
int main ( int argc, char** argv )
{
#ifdef EMSCRIPTEN
emscripten_set_main_loop(playgame, 0, 1);
#endif
}
void playgame()
{
if (collision_with_enemy==1 )
{
SDL_BlitSurface(image1, NULL, screen, &position);
SDL_Flip(screen);
// SDL_Delay(10);
SDL_BlitSurface(image2, NULL, screen, &position);
SDL_Flip(screen);
}
}
If i delete the delay between the display of the two images I will see only the last image because there is not a delay I do not know how to replace the SDL_Delay();
I want to do a little animation during the collision between the player and the enemy.
Thank you.
Have you done any regular web/Javascript development?
The basic issue is that JS is a single threaded environment, and that thread is also used for all the normal HTML/GUI interactions (normally in SDL/Desktop you might have something like SDL_PollEvent that does that work, but the browser does it for you), so you can't keep that thread in your program forever because it would just hang the browser tab, and blocking operations are essentially never good/allowed.
Generally Javascript will always use callback functions when something would block, executing a function when the desired event occurs (e.g. "do this in 20ms", or "do this when the file is downloaded"). A JavaScript game will often use something like window.requestAnimationFrame() instead of a main loop to push frames ("The number of callbacks is usually 60 times per second, but will generally match the display refresh rate in most web browsers as per W3C recommendation.").
As such emscripten provides some tools to help with this, in the form of a callback, although you still need to be careful of any other blocking (e.g. to load lots of images, sounds, etc. which will take a lot longer than off a local disk). https://kripken.github.io/emscripten-site/docs/porting/emscripten-runtime-environment.html#browser-main-loop