Updated test program for Emscripten.
authorPhilipp Wiesemann <philipp.wiesemann@arcor.de>
Mon, 23 Mar 2015 20:21:45 +0100
changeset 94205ffb5a958c43
parent 9419 9763f689bced
child 9421 fd5dd91ec0ce
Updated test program for Emscripten.
test/loopwavequeue.c
     1.1 --- a/test/loopwavequeue.c	Sun Mar 22 01:25:12 2015 -0400
     1.2 +++ b/test/loopwavequeue.c	Mon Mar 23 20:21:45 2015 +0100
     1.3 @@ -15,6 +15,10 @@
     1.4  #include <stdio.h>
     1.5  #include <stdlib.h>
     1.6  
     1.7 +#ifdef __EMSCRIPTEN__
     1.8 +#include <emscripten/emscripten.h>
     1.9 +#endif
    1.10 +
    1.11  #include "SDL.h"
    1.12  
    1.13  #if HAVE_SIGNAL_H
    1.14 @@ -45,6 +49,29 @@
    1.15      done = 1;
    1.16  }
    1.17  
    1.18 +void
    1.19 +loop()
    1.20 +{
    1.21 +#ifdef __EMSCRIPTEN__
    1.22 +    if (done || (SDL_GetAudioStatus() != SDL_AUDIO_PLAYING)) {
    1.23 +        emscripten_cancel_main_loop();
    1.24 +    }
    1.25 +    else
    1.26 +#endif
    1.27 +    {
    1.28 +        /* The device from SDL_OpenAudio() is always device #1. */
    1.29 +        const Uint32 queued = SDL_GetQueuedAudioSize(1);
    1.30 +        SDL_Log("Device has %u bytes queued.\n", (unsigned int) queued);
    1.31 +        if (queued <= 8192) {  /* time to requeue the whole thing? */
    1.32 +            if (SDL_QueueAudio(1, wave.sound, wave.soundlen) == 0) {
    1.33 +                SDL_Log("Device queued %u more bytes.\n", (unsigned int) wave.soundlen);
    1.34 +            } else {
    1.35 +                SDL_Log("Device FAILED to queue %u more bytes: %s\n", (unsigned int) wave.soundlen, SDL_GetError());
    1.36 +            }
    1.37 +        }
    1.38 +    }
    1.39 +}
    1.40 +
    1.41  int
    1.42  main(int argc, char *argv[])
    1.43  {
    1.44 @@ -96,25 +123,22 @@
    1.45      /* Let the audio run */
    1.46      SDL_PauseAudio(0);
    1.47  
    1.48 +    done = 0;
    1.49 +
    1.50      /* Note that we stuff the entire audio buffer into the queue in one
    1.51         shot. Most apps would want to feed it a little at a time, as it
    1.52         plays, but we're going for simplicity here. */
    1.53      
    1.54 +#ifdef __EMSCRIPTEN__
    1.55 +    emscripten_set_main_loop(loop, 0, 1);
    1.56 +#else
    1.57      while (!done && (SDL_GetAudioStatus() == SDL_AUDIO_PLAYING))
    1.58      {
    1.59 -        /* The device from SDL_OpenAudio() is always device #1. */
    1.60 -        const Uint32 queued = SDL_GetQueuedAudioSize(1);
    1.61 -        SDL_Log("Device has %u bytes queued.\n", (unsigned int) queued);
    1.62 -        if (queued <= 8192) {  /* time to requeue the whole thing? */
    1.63 -            if (SDL_QueueAudio(1, wave.sound, wave.soundlen) == 0) {
    1.64 -                SDL_Log("Device queued %u more bytes.\n", (unsigned int) wave.soundlen);
    1.65 -            } else {
    1.66 -                SDL_Log("Device FAILED to queue %u more bytes: %s\n", (unsigned int) wave.soundlen, SDL_GetError());
    1.67 -            }
    1.68 -        }
    1.69 +        loop();
    1.70  
    1.71          SDL_Delay(100);  /* let it play for awhile. */
    1.72      }
    1.73 +#endif
    1.74  
    1.75      /* Clean up on signal */
    1.76      SDL_CloseAudio();