test/testrendertarget.c
changeset 7502 6ff02ff3cf06
parent 7013 fe82d15464d1
child 7513 ec0c3f1eb2de
     1.1 --- a/test/testrendertarget.c	Mon Jul 22 02:51:45 2013 -0700
     1.2 +++ b/test/testrendertarget.c	Tue Jul 23 08:06:49 2013 -0700
     1.3 @@ -88,6 +88,88 @@
     1.4  }
     1.5  
     1.6  void
     1.7 +DrawComposite(DrawState *s)
     1.8 +{
     1.9 +    SDL_Rect viewport, R;
    1.10 +    SDL_Texture *target, *A, *B;
    1.11 +
    1.12 +    static SDL_bool blend_tested = SDL_FALSE;
    1.13 +    if (!blend_tested) {
    1.14 +        SDL_Texture *A, *B;
    1.15 +        Uint32 P;
    1.16 +
    1.17 +        A = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, 1, 1);
    1.18 +        SDL_SetTextureBlendMode(A, SDL_BLENDMODE_BLEND);
    1.19 +
    1.20 +        B = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, 1, 1);
    1.21 +        SDL_SetTextureBlendMode(B, SDL_BLENDMODE_BLEND);
    1.22 +
    1.23 +        SDL_SetRenderTarget(s->renderer, A);
    1.24 +        SDL_SetRenderDrawColor(s->renderer, 0x00, 0x00, 0x00, 0x80);
    1.25 +        SDL_RenderFillRect(s->renderer, NULL);
    1.26 +
    1.27 +        SDL_SetRenderTarget(s->renderer, B);
    1.28 +        SDL_SetRenderDrawColor(s->renderer, 0x00, 0x00, 0x00, 0x00);
    1.29 +        SDL_RenderFillRect(s->renderer, NULL);
    1.30 +        SDL_RenderCopy(s->renderer, A, NULL, NULL);
    1.31 +        SDL_RenderReadPixels(s->renderer, NULL, SDL_PIXELFORMAT_ARGB8888, &P, sizeof(P));
    1.32 +
    1.33 +        printf("Blended pixel: 0x%8.8X\n", P);
    1.34 +
    1.35 +        SDL_DestroyTexture(A);
    1.36 +        SDL_DestroyTexture(B);
    1.37 +        blend_tested = SDL_TRUE;
    1.38 +    }
    1.39 +
    1.40 +    SDL_RenderGetViewport(s->renderer, &viewport);
    1.41 +
    1.42 +    target = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, viewport.w, viewport.h);
    1.43 +    SDL_SetTextureBlendMode(target, SDL_BLENDMODE_BLEND);
    1.44 +    SDL_SetRenderTarget(s->renderer, target);
    1.45 +
    1.46 +    /* Draw the background.
    1.47 +       This is solid black so when the sprite is copied to it, any per-pixel alpha will be blended through.
    1.48 +     */
    1.49 +    SDL_SetRenderDrawColor(s->renderer, 0x00, 0x00, 0x00, 0x00);
    1.50 +    SDL_RenderFillRect(s->renderer, NULL);
    1.51 +
    1.52 +    /* Scale and draw the sprite */
    1.53 +    s->sprite_rect.w += s->scale_direction;
    1.54 +    s->sprite_rect.h += s->scale_direction;
    1.55 +    if (s->scale_direction > 0) {
    1.56 +        if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
    1.57 +            s->scale_direction = -1;
    1.58 +        }
    1.59 +    } else {
    1.60 +        if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
    1.61 +            s->scale_direction = 1;
    1.62 +        }
    1.63 +    }
    1.64 +    s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
    1.65 +    s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
    1.66 +
    1.67 +    SDL_RenderCopy(s->renderer, s->sprite, NULL, &s->sprite_rect);
    1.68 +
    1.69 +    SDL_SetRenderTarget(s->renderer, NULL);
    1.70 +    SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
    1.71 +
    1.72 +    SDL_SetRenderDrawBlendMode(s->renderer, SDL_BLENDMODE_BLEND);
    1.73 +    SDL_SetRenderDrawColor(s->renderer, 0xff, 0x00, 0x00, 0x80);
    1.74 +    R.x = 0;
    1.75 +    R.y = 0;
    1.76 +    R.w = 100;
    1.77 +    R.h = 100;
    1.78 +    SDL_RenderFillRect(s->renderer, &R);
    1.79 +    SDL_SetRenderDrawBlendMode(s->renderer, SDL_BLENDMODE_NONE);
    1.80 +
    1.81 +    SDL_RenderCopy(s->renderer, target, NULL, NULL);
    1.82 +    SDL_DestroyTexture(target);
    1.83 +
    1.84 +    /* Update the screen! */
    1.85 +    SDL_RenderPresent(s->renderer);
    1.86 +}
    1.87 +
    1.88 +void
    1.89  Draw(DrawState *s)
    1.90  {
    1.91      SDL_Rect viewport;
    1.92 @@ -134,6 +216,7 @@
    1.93      SDL_Event event;
    1.94      int frames;
    1.95      Uint32 then, now;
    1.96 +    SDL_bool test_composite = SDL_FALSE;
    1.97  
    1.98      /* Initialize test framework */
    1.99      state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
   1.100 @@ -145,8 +228,17 @@
   1.101  
   1.102          consumed = SDLTest_CommonArg(state, i);
   1.103          if (consumed == 0) {
   1.104 -            fprintf(stderr, "Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
   1.105 -            return 1;
   1.106 +            consumed = -1;
   1.107 +            if (SDL_strcasecmp(argv[i], "--composite") == 0) {
   1.108 +                test_composite = SDL_TRUE;
   1.109 +                consumed = 1;
   1.110 +            }
   1.111 +        }
   1.112 +        if (consumed < 0) {
   1.113 +            fprintf(stderr,
   1.114 +                    "Usage: %s %s [--composite]\n",
   1.115 +                    argv[0], SDLTest_CommonUsage(state));
   1.116 +            quit(1);
   1.117          }
   1.118          i += consumed;
   1.119      }
   1.120 @@ -160,7 +252,11 @@
   1.121  
   1.122          drawstate->window = state->windows[i];
   1.123          drawstate->renderer = state->renderers[i];
   1.124 -        drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
   1.125 +        if (test_composite) {
   1.126 +            drawstate->sprite = LoadTexture(drawstate->renderer, "icon-alpha.bmp", SDL_TRUE);
   1.127 +        } else {
   1.128 +            drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
   1.129 +        }
   1.130          drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
   1.131          if (!drawstate->sprite || !drawstate->background) {
   1.132              quit(2);
   1.133 @@ -181,7 +277,11 @@
   1.134              SDLTest_CommonEvent(state, &event, &done);
   1.135          }
   1.136          for (i = 0; i < state->num_windows; ++i) {
   1.137 -            Draw(&drawstates[i]);
   1.138 +            if (test_composite) {
   1.139 +                DrawComposite(&drawstates[i]);
   1.140 +            } else {
   1.141 +                Draw(&drawstates[i]);
   1.142 +            }
   1.143          }
   1.144      }
   1.145