test/testintersections.c
changeset 9278 8900afb78a19
parent 8149 681eb46b8ac4
child 9607 7746ab058d12
     1.1 --- a/test/testintersections.c	Sat Dec 13 02:33:52 2014 -0500
     1.2 +++ b/test/testintersections.c	Thu Dec 18 00:19:52 2014 -0500
     1.3 @@ -16,6 +16,10 @@
     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_common.h"
    1.12  
    1.13  #define SWAP(typ,a,b) do{typ t=a;a=b;b=t;}while(0)
    1.14 @@ -30,6 +34,9 @@
    1.15  static int current_color = 255;
    1.16  static SDL_BlendMode blendMode = SDL_BLENDMODE_NONE;
    1.17  
    1.18 +int mouse_begin_x = -1, mouse_begin_y = -1;
    1.19 +int done;
    1.20 +
    1.21  void
    1.22  DrawPoints(SDL_Renderer * renderer)
    1.23  {
    1.24 @@ -191,12 +198,71 @@
    1.25          }
    1.26  }
    1.27  
    1.28 +void
    1.29 +loop()
    1.30 +{
    1.31 +    int i;
    1.32 +    SDL_Event event;
    1.33 +
    1.34 +    /* Check for events */
    1.35 +    while (SDL_PollEvent(&event)) {
    1.36 +        SDLTest_CommonEvent(state, &event, &done);
    1.37 +        switch (event.type) {
    1.38 +        case SDL_MOUSEBUTTONDOWN:
    1.39 +            mouse_begin_x = event.button.x;
    1.40 +            mouse_begin_y = event.button.y;
    1.41 +            break;
    1.42 +        case SDL_MOUSEBUTTONUP:
    1.43 +            if (event.button.button == 3)
    1.44 +                add_line(mouse_begin_x, mouse_begin_y, event.button.x,
    1.45 +                         event.button.y);
    1.46 +            if (event.button.button == 1)
    1.47 +                add_rect(mouse_begin_x, mouse_begin_y, event.button.x,
    1.48 +                         event.button.y);
    1.49 +            break;
    1.50 +        case SDL_KEYDOWN:
    1.51 +            switch (event.key.keysym.sym) {
    1.52 +            case 'l':
    1.53 +                if (event.key.keysym.mod & KMOD_SHIFT)
    1.54 +                    num_lines = 0;
    1.55 +                else
    1.56 +                    add_line(rand() % 640, rand() % 480, rand() % 640,
    1.57 +                             rand() % 480);
    1.58 +                break;
    1.59 +            case 'r':
    1.60 +                if (event.key.keysym.mod & KMOD_SHIFT)
    1.61 +                    num_rects = 0;
    1.62 +                else
    1.63 +                    add_rect(rand() % 640, rand() % 480, rand() % 640,
    1.64 +                             rand() % 480);
    1.65 +                break;
    1.66 +            }
    1.67 +            break;
    1.68 +        default:
    1.69 +            break;
    1.70 +        }
    1.71 +    }
    1.72 +    for (i = 0; i < state->num_windows; ++i) {
    1.73 +        SDL_Renderer *renderer = state->renderers[i];
    1.74 +        if (state->windows[i] == NULL)
    1.75 +            continue;
    1.76 +        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
    1.77 +        SDL_RenderClear(renderer);
    1.78 +
    1.79 +        DrawRects(renderer);
    1.80 +        DrawPoints(renderer);
    1.81 +        DrawRectRectIntersections(renderer);
    1.82 +        DrawLines(renderer);
    1.83 +        DrawRectLineIntersections(renderer);
    1.84 +
    1.85 +        SDL_RenderPresent(renderer);
    1.86 +    }
    1.87 +}
    1.88 +
    1.89  int
    1.90  main(int argc, char *argv[])
    1.91  {
    1.92 -    int mouse_begin_x = -1, mouse_begin_y = -1;
    1.93 -    int i, done;
    1.94 -    SDL_Event event;
    1.95 +    int i;
    1.96      Uint32 then, now, frames;
    1.97  
    1.98      /* Enable standard application logging */
    1.99 @@ -268,62 +334,15 @@
   1.100      frames = 0;
   1.101      then = SDL_GetTicks();
   1.102      done = 0;
   1.103 +
   1.104 +#ifdef __EMSCRIPTEN__
   1.105 +    emscripten_set_main_loop(loop, 0, 1);
   1.106 +#else
   1.107      while (!done) {
   1.108 -        /* Check for events */
   1.109          ++frames;
   1.110 -        while (SDL_PollEvent(&event)) {
   1.111 -            SDLTest_CommonEvent(state, &event, &done);
   1.112 -            switch (event.type) {
   1.113 -            case SDL_MOUSEBUTTONDOWN:
   1.114 -                mouse_begin_x = event.button.x;
   1.115 -                mouse_begin_y = event.button.y;
   1.116 -                break;
   1.117 -            case SDL_MOUSEBUTTONUP:
   1.118 -                if (event.button.button == 3)
   1.119 -                    add_line(mouse_begin_x, mouse_begin_y, event.button.x,
   1.120 -                             event.button.y);
   1.121 -                if (event.button.button == 1)
   1.122 -                    add_rect(mouse_begin_x, mouse_begin_y, event.button.x,
   1.123 -                             event.button.y);
   1.124 -                break;
   1.125 -            case SDL_KEYDOWN:
   1.126 -                switch (event.key.keysym.sym) {
   1.127 -                case 'l':
   1.128 -                    if (event.key.keysym.mod & KMOD_SHIFT)
   1.129 -                        num_lines = 0;
   1.130 -                    else
   1.131 -                        add_line(rand() % 640, rand() % 480, rand() % 640,
   1.132 -                                 rand() % 480);
   1.133 -                    break;
   1.134 -                case 'r':
   1.135 -                    if (event.key.keysym.mod & KMOD_SHIFT)
   1.136 -                        num_rects = 0;
   1.137 -                    else
   1.138 -                        add_rect(rand() % 640, rand() % 480, rand() % 640,
   1.139 -                                 rand() % 480);
   1.140 -                    break;
   1.141 -                }
   1.142 -                break;
   1.143 -            default:
   1.144 -                break;
   1.145 -            }
   1.146 -        }
   1.147 -        for (i = 0; i < state->num_windows; ++i) {
   1.148 -            SDL_Renderer *renderer = state->renderers[i];
   1.149 -            if (state->windows[i] == NULL)
   1.150 -                continue;
   1.151 -            SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
   1.152 -            SDL_RenderClear(renderer);
   1.153 -
   1.154 -            DrawRects(renderer);
   1.155 -            DrawPoints(renderer);
   1.156 -            DrawRectRectIntersections(renderer);
   1.157 -            DrawLines(renderer);
   1.158 -            DrawRectLineIntersections(renderer);
   1.159 -
   1.160 -            SDL_RenderPresent(renderer);
   1.161 -        }
   1.162 +        loop();
   1.163      }
   1.164 +#endif
   1.165  
   1.166      SDLTest_CommonQuit(state);
   1.167