test/testsprite2.c
changeset 5147 ad50b3db78bd
parent 5140 e743b9c3f6d6
child 5158 b3ccd1947786
     1.1 --- a/test/testsprite2.c	Tue Feb 01 19:15:42 2011 -0800
     1.2 +++ b/test/testsprite2.c	Tue Feb 01 19:19:43 2011 -0800
     1.3 @@ -6,8 +6,8 @@
     1.4  
     1.5  #include "common.h"
     1.6  
     1.7 -#define NUM_SPRITES	100
     1.8 -#define MAX_SPEED 	1
     1.9 +#define NUM_SPRITES    100
    1.10 +#define MAX_SPEED     1
    1.11  
    1.12  static CommonState *state;
    1.13  static int num_sprites;
    1.14 @@ -76,12 +76,12 @@
    1.15  
    1.16      /* Create textures from the image */
    1.17      for (i = 0; i < state->num_windows; ++i) {
    1.18 -        SDL_SelectRenderer(state->windows[i]);
    1.19 -        sprites[i] = SDL_CreateTextureFromSurface(0, temp);
    1.20 +        SDL_Renderer *renderer = state->renderers[i];
    1.21 +        sprites[i] = SDL_CreateTextureFromSurface(renderer, 0, temp);
    1.22          if (!sprites[i]) {
    1.23 -		SDL_SetColorKey(temp, 0, 0);
    1.24 -		sprites[i] = SDL_CreateTextureFromSurface(0, temp);
    1.25 -	}
    1.26 +            SDL_SetColorKey(temp, 0, 0);
    1.27 +            sprites[i] = SDL_CreateTextureFromSurface(renderer, 0, temp);
    1.28 +        }
    1.29          if (!sprites[i]) {
    1.30              fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
    1.31              SDL_FreeSurface(temp);
    1.32 @@ -96,15 +96,13 @@
    1.33  }
    1.34  
    1.35  void
    1.36 -MoveSprites(SDL_Window * window, SDL_Texture * sprite)
    1.37 +MoveSprites(SDL_Window * window, SDL_Renderer * renderer, SDL_Texture * sprite)
    1.38  {
    1.39      int i, n;
    1.40      int window_w, window_h;
    1.41      SDL_Rect temp;
    1.42      SDL_Rect *position, *velocity;
    1.43  
    1.44 -    SDL_SelectRenderer(window);
    1.45 -
    1.46      /* Query the sizes */
    1.47      SDL_GetWindowSize(window, &window_w, &window_h);
    1.48  
    1.49 @@ -136,55 +134,55 @@
    1.50      }
    1.51  
    1.52      /* Draw a gray background */
    1.53 -    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
    1.54 -    SDL_RenderClear();
    1.55 +    SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
    1.56 +    SDL_RenderClear(renderer);
    1.57  
    1.58      /* Test points */
    1.59 -    SDL_SetRenderDrawColor(0xFF, 0x00, 0x00, 0xFF);
    1.60 -    SDL_RenderDrawPoint(0, 0);
    1.61 -    SDL_RenderDrawPoint(window_w-1, 0);
    1.62 -    SDL_RenderDrawPoint(0, window_h-1);
    1.63 -    SDL_RenderDrawPoint(window_w-1, window_h-1);
    1.64 +    SDL_SetRenderDrawColor(renderer, 0xFF, 0x00, 0x00, 0xFF);
    1.65 +    SDL_RenderDrawPoint(renderer, 0, 0);
    1.66 +    SDL_RenderDrawPoint(renderer, window_w-1, 0);
    1.67 +    SDL_RenderDrawPoint(renderer, 0, window_h-1);
    1.68 +    SDL_RenderDrawPoint(renderer, window_w-1, window_h-1);
    1.69  
    1.70      /* Test horizontal and vertical lines */
    1.71 -    SDL_SetRenderDrawColor(0x00, 0xFF, 0x00, 0xFF);
    1.72 -    SDL_RenderDrawLine(1, 0, window_w-2, 0);
    1.73 -    SDL_RenderDrawLine(1, window_h-1, window_w-2, window_h-1);
    1.74 -    SDL_RenderDrawLine(0, 1, 0, window_h-2);
    1.75 -    SDL_RenderDrawLine(window_w-1, 1, window_w-1, window_h-2);
    1.76 +    SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
    1.77 +    SDL_RenderDrawLine(renderer, 1, 0, window_w-2, 0);
    1.78 +    SDL_RenderDrawLine(renderer, 1, window_h-1, window_w-2, window_h-1);
    1.79 +    SDL_RenderDrawLine(renderer, 0, 1, 0, window_h-2);
    1.80 +    SDL_RenderDrawLine(renderer, window_w-1, 1, window_w-1, window_h-2);
    1.81  
    1.82      /* Test fill and copy */
    1.83 -    SDL_SetRenderDrawColor(0xFF, 0xFF, 0xFF, 0xFF);
    1.84 +    SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
    1.85      temp.x = 1;
    1.86      temp.y = 1;
    1.87      temp.w = sprite_w;
    1.88      temp.h = sprite_h;
    1.89 -    SDL_RenderFillRect(&temp);
    1.90 -    SDL_RenderCopy(sprite, NULL, &temp);
    1.91 +    SDL_RenderFillRect(renderer, &temp);
    1.92 +    SDL_RenderCopy(renderer, sprite, NULL, &temp);
    1.93      temp.x = window_w-sprite_w-1;
    1.94      temp.y = 1;
    1.95      temp.w = sprite_w;
    1.96      temp.h = sprite_h;
    1.97 -    SDL_RenderFillRect(&temp);
    1.98 -    SDL_RenderCopy(sprite, NULL, &temp);
    1.99 +    SDL_RenderFillRect(renderer, &temp);
   1.100 +    SDL_RenderCopy(renderer, sprite, NULL, &temp);
   1.101      temp.x = 1;
   1.102      temp.y = window_h-sprite_h-1;
   1.103      temp.w = sprite_w;
   1.104      temp.h = sprite_h;
   1.105 -    SDL_RenderFillRect(&temp);
   1.106 -    SDL_RenderCopy(sprite, NULL, &temp);
   1.107 +    SDL_RenderFillRect(renderer, &temp);
   1.108 +    SDL_RenderCopy(renderer, sprite, NULL, &temp);
   1.109      temp.x = window_w-sprite_w-1;
   1.110      temp.y = window_h-sprite_h-1;
   1.111      temp.w = sprite_w;
   1.112      temp.h = sprite_h;
   1.113 -    SDL_RenderFillRect(&temp);
   1.114 -    SDL_RenderCopy(sprite, NULL, &temp);
   1.115 +    SDL_RenderFillRect(renderer, &temp);
   1.116 +    SDL_RenderCopy(renderer, sprite, NULL, &temp);
   1.117  
   1.118      /* Test diagonal lines */
   1.119 -    SDL_SetRenderDrawColor(0x00, 0xFF, 0x00, 0xFF);
   1.120 -    SDL_RenderDrawLine(sprite_w, sprite_h,
   1.121 +    SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
   1.122 +    SDL_RenderDrawLine(renderer, sprite_w, sprite_h,
   1.123                         window_w-sprite_w-2, window_h-sprite_h-2);
   1.124 -    SDL_RenderDrawLine(window_w-sprite_w-2, sprite_h,
   1.125 +    SDL_RenderDrawLine(renderer, window_w-sprite_w-2, sprite_h,
   1.126                         sprite_w, window_h-sprite_h-2);
   1.127  
   1.128      /* Move the sprite, bounce at the wall, and draw */
   1.129 @@ -204,11 +202,11 @@
   1.130          }
   1.131  
   1.132          /* Blit the sprite onto the screen */
   1.133 -        SDL_RenderCopy(sprite, NULL, position);
   1.134 +        SDL_RenderCopy(renderer, sprite, NULL, position);
   1.135      }
   1.136  
   1.137      /* Update the screen! */
   1.138 -    SDL_RenderPresent();
   1.139 +    SDL_RenderPresent(renderer);
   1.140  }
   1.141  
   1.142  int
   1.143 @@ -276,9 +274,9 @@
   1.144          quit(2);
   1.145      }
   1.146      for (i = 0; i < state->num_windows; ++i) {
   1.147 -        SDL_SelectRenderer(state->windows[i]);
   1.148 -        SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   1.149 -        SDL_RenderClear();
   1.150 +        SDL_Renderer *renderer = state->renderers[i];
   1.151 +        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
   1.152 +        SDL_RenderClear(renderer);
   1.153      }
   1.154      if (LoadSprite("icon.bmp") < 0) {
   1.155          quit(2);
   1.156 @@ -314,22 +312,9 @@
   1.157          ++frames;
   1.158          while (SDL_PollEvent(&event)) {
   1.159              CommonEvent(state, &event, &done);
   1.160 -            switch (event.type) {
   1.161 -            case SDL_WINDOWEVENT:
   1.162 -                switch (event.window.event) {
   1.163 -                case SDL_WINDOWEVENT_EXPOSED:
   1.164 -                    SDL_SelectRenderer(SDL_GetWindowFromID(event.window.windowID));
   1.165 -                    SDL_SetRenderDrawColor(0xA0, 0xA0, 0xA0, 0xFF);
   1.166 -                    SDL_RenderClear();
   1.167 -                    break;
   1.168 -                }
   1.169 -                break;
   1.170 -            default:
   1.171 -                break;
   1.172 -            }
   1.173          }
   1.174          for (i = 0; i < state->num_windows; ++i) {
   1.175 -            MoveSprites(state->windows[i], sprites[i]);
   1.176 +            MoveSprites(state->windows[i], state->renderers[i], sprites[i]);
   1.177          }
   1.178      }
   1.179  
   1.180 @@ -340,7 +325,7 @@
   1.181          printf("%2.2f frames per second\n", fps);
   1.182      }
   1.183      quit(0);
   1.184 -	return 0;
   1.185 +    return 0;
   1.186  }
   1.187  
   1.188  /* vi: set ts=4 sw=4 expandtab: */