test/testviewport.c
changeset 9278 8900afb78a19
parent 8149 681eb46b8ac4
child 9607 7746ab058d12
equal deleted inserted replaced
9277:f0bd40782fc7 9278:8900afb78a19
    13 
    13 
    14 #include <stdlib.h>
    14 #include <stdlib.h>
    15 #include <stdio.h>
    15 #include <stdio.h>
    16 #include <time.h>
    16 #include <time.h>
    17 
    17 
       
    18 #ifdef __EMSCRIPTEN__
       
    19 #include <emscripten/emscripten.h>
       
    20 #endif
       
    21 
    18 #include "SDL_test.h"
    22 #include "SDL_test.h"
    19 #include "SDL_test_common.h"
    23 #include "SDL_test_common.h"
    20 
    24 
    21 
    25 
    22 static SDLTest_CommonState *state;
    26 static SDLTest_CommonState *state;
       
    27 
       
    28 SDL_Rect viewport;
       
    29 int done, j;
       
    30 SDL_bool use_target = SDL_FALSE;
       
    31 #ifdef __EMSCRIPTEN__
       
    32 Uint32 wait_start;
       
    33 #endif
    23 
    34 
    24 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    35 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    25 static void
    36 static void
    26 quit(int rc)
    37 quit(int rc)
    27 {
    38 {
    75     rect.x = (viewport.w - rect.w) / 2;
    86     rect.x = (viewport.w - rect.w) / 2;
    76     rect.y = 0;
    87     rect.y = 0;
    77     SDL_RenderFillRect(renderer, &rect);
    88     SDL_RenderFillRect(renderer, &rect);
    78 }
    89 }
    79 
    90 
       
    91 void
       
    92 loop()
       
    93 {
       
    94 #ifdef __EMSCRIPTEN__
       
    95     /* Avoid using delays */
       
    96     if(SDL_GetTicks() - wait_start < 1000)
       
    97         return;
       
    98     wait_start = SDL_GetTicks();
       
    99 #endif
       
   100     SDL_Event event;
       
   101     int i;
       
   102     /* Check for events */
       
   103     while (SDL_PollEvent(&event)) {
       
   104         SDLTest_CommonEvent(state, &event, &done);
       
   105     }
       
   106 
       
   107     /* Move a viewport box in steps around the screen */
       
   108     viewport.x = j * 100;
       
   109     viewport.y = viewport.x;
       
   110     viewport.w = 100 + j * 50;
       
   111     viewport.h = 100 + j * 50;
       
   112     j = (j + 1) % 4;
       
   113     SDL_Log("Current Viewport x=%i y=%i w=%i h=%i", viewport.x, viewport.y, viewport.w, viewport.h);
       
   114 
       
   115     for (i = 0; i < state->num_windows; ++i) {
       
   116         if (state->windows[i] == NULL)
       
   117             continue;
       
   118 
       
   119         /* Draw using viewport */
       
   120         DrawOnViewport(state->renderers[i], viewport);
       
   121 
       
   122         /* Update the screen! */
       
   123         if (use_target) {
       
   124             SDL_SetRenderTarget(state->renderers[i], NULL);
       
   125             SDL_RenderCopy(state->renderers[i], state->targets[i], NULL, NULL);
       
   126             SDL_RenderPresent(state->renderers[i]);
       
   127             SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
       
   128         } else {
       
   129             SDL_RenderPresent(state->renderers[i]);
       
   130         }
       
   131     }
       
   132 }
       
   133 
    80 int
   134 int
    81 main(int argc, char *argv[])
   135 main(int argc, char *argv[])
    82 {
   136 {
    83     int i, j, done;
   137     int i;
    84     SDL_Event event;
       
    85     Uint32 then, now, frames;
   138     Uint32 then, now, frames;
    86     SDL_Rect viewport;
       
    87     SDL_bool use_target = SDL_FALSE;
       
    88 
   139 
    89     /* Initialize test framework */
   140     /* Initialize test framework */
    90     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
   141     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
    91     if (!state) {
   142     if (!state) {
    92         return 1;
   143         return 1;
   133     /* Main render loop */
   184     /* Main render loop */
   134     frames = 0;
   185     frames = 0;
   135     then = SDL_GetTicks();
   186     then = SDL_GetTicks();
   136     done = 0;
   187     done = 0;
   137     j = 0;
   188     j = 0;
       
   189 
       
   190 #ifdef __EMSCRIPTEN__
       
   191     wait_start = SDL_GetTicks();
       
   192     emscripten_set_main_loop(loop, 0, 1);
       
   193 #else
   138     while (!done) {
   194     while (!done) {
   139         /* Check for events */
       
   140         ++frames;
   195         ++frames;
   141         while (SDL_PollEvent(&event)) {
   196         loop();
   142             SDLTest_CommonEvent(state, &event, &done);
       
   143         }
       
   144         
       
   145         /* Move a viewport box in steps around the screen */                
       
   146         viewport.x = j * 100;
       
   147         viewport.y = viewport.x;
       
   148         viewport.w = 100 + j * 50;
       
   149         viewport.h = 100 + j * 50;
       
   150         j = (j + 1) % 4;            
       
   151         SDL_Log("Current Viewport x=%i y=%i w=%i h=%i", viewport.x, viewport.y, viewport.w, viewport.h);
       
   152         
       
   153         for (i = 0; i < state->num_windows; ++i) {
       
   154             if (state->windows[i] == NULL)
       
   155                 continue;
       
   156                 
       
   157             /* Draw using viewport */        
       
   158             DrawOnViewport(state->renderers[i], viewport);
       
   159 
       
   160             /* Update the screen! */
       
   161             if (use_target) {
       
   162                 SDL_SetRenderTarget(state->renderers[i], NULL);
       
   163                 SDL_RenderCopy(state->renderers[i], state->targets[i], NULL, NULL);
       
   164                 SDL_RenderPresent(state->renderers[i]);
       
   165                 SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
       
   166             } else {
       
   167                 SDL_RenderPresent(state->renderers[i]);
       
   168             }
       
   169         }
       
   170         
       
   171         SDL_Delay(1000);
   197         SDL_Delay(1000);
   172     }
   198     }
       
   199 #endif
   173 
   200 
   174     /* Print out some timing information */
   201     /* Print out some timing information */
   175     now = SDL_GetTicks();
   202     now = SDL_GetTicks();
   176     if (now > then) {
   203     if (now > then) {
   177         double fps = ((double) frames * 1000) / (now - then);
   204         double fps = ((double) frames * 1000) / (now - then);