test/testviewport.c
changeset 9278 8900afb78a19
parent 8149 681eb46b8ac4
child 9607 7746ab058d12
     1.1 --- a/test/testviewport.c	Sat Dec 13 02:33:52 2014 -0500
     1.2 +++ b/test/testviewport.c	Thu Dec 18 00:19:52 2014 -0500
     1.3 @@ -15,12 +15,23 @@
     1.4  #include <stdio.h>
     1.5  #include <time.h>
     1.6  
     1.7 +#ifdef __EMSCRIPTEN__
     1.8 +#include <emscripten/emscripten.h>
     1.9 +#endif
    1.10 +
    1.11  #include "SDL_test.h"
    1.12  #include "SDL_test_common.h"
    1.13  
    1.14  
    1.15  static SDLTest_CommonState *state;
    1.16  
    1.17 +SDL_Rect viewport;
    1.18 +int done, j;
    1.19 +SDL_bool use_target = SDL_FALSE;
    1.20 +#ifdef __EMSCRIPTEN__
    1.21 +Uint32 wait_start;
    1.22 +#endif
    1.23 +
    1.24  /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    1.25  static void
    1.26  quit(int rc)
    1.27 @@ -77,14 +88,54 @@
    1.28      SDL_RenderFillRect(renderer, &rect);
    1.29  }
    1.30  
    1.31 +void
    1.32 +loop()
    1.33 +{
    1.34 +#ifdef __EMSCRIPTEN__
    1.35 +    /* Avoid using delays */
    1.36 +    if(SDL_GetTicks() - wait_start < 1000)
    1.37 +        return;
    1.38 +    wait_start = SDL_GetTicks();
    1.39 +#endif
    1.40 +    SDL_Event event;
    1.41 +    int i;
    1.42 +    /* Check for events */
    1.43 +    while (SDL_PollEvent(&event)) {
    1.44 +        SDLTest_CommonEvent(state, &event, &done);
    1.45 +    }
    1.46 +
    1.47 +    /* Move a viewport box in steps around the screen */
    1.48 +    viewport.x = j * 100;
    1.49 +    viewport.y = viewport.x;
    1.50 +    viewport.w = 100 + j * 50;
    1.51 +    viewport.h = 100 + j * 50;
    1.52 +    j = (j + 1) % 4;
    1.53 +    SDL_Log("Current Viewport x=%i y=%i w=%i h=%i", viewport.x, viewport.y, viewport.w, viewport.h);
    1.54 +
    1.55 +    for (i = 0; i < state->num_windows; ++i) {
    1.56 +        if (state->windows[i] == NULL)
    1.57 +            continue;
    1.58 +
    1.59 +        /* Draw using viewport */
    1.60 +        DrawOnViewport(state->renderers[i], viewport);
    1.61 +
    1.62 +        /* Update the screen! */
    1.63 +        if (use_target) {
    1.64 +            SDL_SetRenderTarget(state->renderers[i], NULL);
    1.65 +            SDL_RenderCopy(state->renderers[i], state->targets[i], NULL, NULL);
    1.66 +            SDL_RenderPresent(state->renderers[i]);
    1.67 +            SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
    1.68 +        } else {
    1.69 +            SDL_RenderPresent(state->renderers[i]);
    1.70 +        }
    1.71 +    }
    1.72 +}
    1.73 +
    1.74  int
    1.75  main(int argc, char *argv[])
    1.76  {
    1.77 -    int i, j, done;
    1.78 -    SDL_Event event;
    1.79 +    int i;
    1.80      Uint32 then, now, frames;
    1.81 -    SDL_Rect viewport;
    1.82 -    SDL_bool use_target = SDL_FALSE;
    1.83  
    1.84      /* Initialize test framework */
    1.85      state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
    1.86 @@ -135,41 +186,17 @@
    1.87      then = SDL_GetTicks();
    1.88      done = 0;
    1.89      j = 0;
    1.90 +
    1.91 +#ifdef __EMSCRIPTEN__
    1.92 +    wait_start = SDL_GetTicks();
    1.93 +    emscripten_set_main_loop(loop, 0, 1);
    1.94 +#else
    1.95      while (!done) {
    1.96 -        /* Check for events */
    1.97          ++frames;
    1.98 -        while (SDL_PollEvent(&event)) {
    1.99 -            SDLTest_CommonEvent(state, &event, &done);
   1.100 -        }
   1.101 -        
   1.102 -        /* Move a viewport box in steps around the screen */                
   1.103 -        viewport.x = j * 100;
   1.104 -        viewport.y = viewport.x;
   1.105 -        viewport.w = 100 + j * 50;
   1.106 -        viewport.h = 100 + j * 50;
   1.107 -        j = (j + 1) % 4;            
   1.108 -        SDL_Log("Current Viewport x=%i y=%i w=%i h=%i", viewport.x, viewport.y, viewport.w, viewport.h);
   1.109 -        
   1.110 -        for (i = 0; i < state->num_windows; ++i) {
   1.111 -            if (state->windows[i] == NULL)
   1.112 -                continue;
   1.113 -                
   1.114 -            /* Draw using viewport */        
   1.115 -            DrawOnViewport(state->renderers[i], viewport);
   1.116 -
   1.117 -            /* Update the screen! */
   1.118 -            if (use_target) {
   1.119 -                SDL_SetRenderTarget(state->renderers[i], NULL);
   1.120 -                SDL_RenderCopy(state->renderers[i], state->targets[i], NULL, NULL);
   1.121 -                SDL_RenderPresent(state->renderers[i]);
   1.122 -                SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
   1.123 -            } else {
   1.124 -                SDL_RenderPresent(state->renderers[i]);
   1.125 -            }
   1.126 -        }
   1.127 -        
   1.128 +        loop();
   1.129          SDL_Delay(1000);
   1.130      }
   1.131 +#endif
   1.132  
   1.133      /* Print out some timing information */
   1.134      now = SDL_GetTicks();