test/testrendertarget.c
changeset 7502 6ff02ff3cf06
parent 7013 fe82d15464d1
child 7513 ec0c3f1eb2de
equal deleted inserted replaced
7501:b27825bb5879 7502:6ff02ff3cf06
    86     /* We're ready to roll. :) */
    86     /* We're ready to roll. :) */
    87     return texture;
    87     return texture;
    88 }
    88 }
    89 
    89 
    90 void
    90 void
    91 Draw(DrawState *s)
    91 DrawComposite(DrawState *s)
    92 {
    92 {
    93     SDL_Rect viewport;
    93     SDL_Rect viewport, R;
    94     SDL_Texture *target;
    94     SDL_Texture *target, *A, *B;
       
    95 
       
    96     static SDL_bool blend_tested = SDL_FALSE;
       
    97     if (!blend_tested) {
       
    98         SDL_Texture *A, *B;
       
    99         Uint32 P;
       
   100 
       
   101         A = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, 1, 1);
       
   102         SDL_SetTextureBlendMode(A, SDL_BLENDMODE_BLEND);
       
   103 
       
   104         B = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, 1, 1);
       
   105         SDL_SetTextureBlendMode(B, SDL_BLENDMODE_BLEND);
       
   106 
       
   107         SDL_SetRenderTarget(s->renderer, A);
       
   108         SDL_SetRenderDrawColor(s->renderer, 0x00, 0x00, 0x00, 0x80);
       
   109         SDL_RenderFillRect(s->renderer, NULL);
       
   110 
       
   111         SDL_SetRenderTarget(s->renderer, B);
       
   112         SDL_SetRenderDrawColor(s->renderer, 0x00, 0x00, 0x00, 0x00);
       
   113         SDL_RenderFillRect(s->renderer, NULL);
       
   114         SDL_RenderCopy(s->renderer, A, NULL, NULL);
       
   115         SDL_RenderReadPixels(s->renderer, NULL, SDL_PIXELFORMAT_ARGB8888, &P, sizeof(P));
       
   116 
       
   117         printf("Blended pixel: 0x%8.8X\n", P);
       
   118 
       
   119         SDL_DestroyTexture(A);
       
   120         SDL_DestroyTexture(B);
       
   121         blend_tested = SDL_TRUE;
       
   122     }
    95 
   123 
    96     SDL_RenderGetViewport(s->renderer, &viewport);
   124     SDL_RenderGetViewport(s->renderer, &viewport);
    97 
   125 
    98     target = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, viewport.w, viewport.h);
   126     target = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, viewport.w, viewport.h);
       
   127     SDL_SetTextureBlendMode(target, SDL_BLENDMODE_BLEND);
    99     SDL_SetRenderTarget(s->renderer, target);
   128     SDL_SetRenderTarget(s->renderer, target);
   100 
   129 
   101     /* Draw the background */
   130     /* Draw the background.
   102     SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
   131        This is solid black so when the sprite is copied to it, any per-pixel alpha will be blended through.
       
   132      */
       
   133     SDL_SetRenderDrawColor(s->renderer, 0x00, 0x00, 0x00, 0x00);
       
   134     SDL_RenderFillRect(s->renderer, NULL);
   103 
   135 
   104     /* Scale and draw the sprite */
   136     /* Scale and draw the sprite */
   105     s->sprite_rect.w += s->scale_direction;
   137     s->sprite_rect.w += s->scale_direction;
   106     s->sprite_rect.h += s->scale_direction;
   138     s->sprite_rect.h += s->scale_direction;
   107     if (s->scale_direction > 0) {
   139     if (s->scale_direction > 0) {
   117     s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
   149     s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
   118 
   150 
   119     SDL_RenderCopy(s->renderer, s->sprite, NULL, &s->sprite_rect);
   151     SDL_RenderCopy(s->renderer, s->sprite, NULL, &s->sprite_rect);
   120 
   152 
   121     SDL_SetRenderTarget(s->renderer, NULL);
   153     SDL_SetRenderTarget(s->renderer, NULL);
       
   154     SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
       
   155 
       
   156     SDL_SetRenderDrawBlendMode(s->renderer, SDL_BLENDMODE_BLEND);
       
   157     SDL_SetRenderDrawColor(s->renderer, 0xff, 0x00, 0x00, 0x80);
       
   158     R.x = 0;
       
   159     R.y = 0;
       
   160     R.w = 100;
       
   161     R.h = 100;
       
   162     SDL_RenderFillRect(s->renderer, &R);
       
   163     SDL_SetRenderDrawBlendMode(s->renderer, SDL_BLENDMODE_NONE);
       
   164 
       
   165     SDL_RenderCopy(s->renderer, target, NULL, NULL);
       
   166     SDL_DestroyTexture(target);
       
   167 
       
   168     /* Update the screen! */
       
   169     SDL_RenderPresent(s->renderer);
       
   170 }
       
   171 
       
   172 void
       
   173 Draw(DrawState *s)
       
   174 {
       
   175     SDL_Rect viewport;
       
   176     SDL_Texture *target;
       
   177 
       
   178     SDL_RenderGetViewport(s->renderer, &viewport);
       
   179 
       
   180     target = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, viewport.w, viewport.h);
       
   181     SDL_SetRenderTarget(s->renderer, target);
       
   182 
       
   183     /* Draw the background */
       
   184     SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
       
   185 
       
   186     /* Scale and draw the sprite */
       
   187     s->sprite_rect.w += s->scale_direction;
       
   188     s->sprite_rect.h += s->scale_direction;
       
   189     if (s->scale_direction > 0) {
       
   190         if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
       
   191             s->scale_direction = -1;
       
   192         }
       
   193     } else {
       
   194         if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
       
   195             s->scale_direction = 1;
       
   196         }
       
   197     }
       
   198     s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
       
   199     s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
       
   200 
       
   201     SDL_RenderCopy(s->renderer, s->sprite, NULL, &s->sprite_rect);
       
   202 
       
   203     SDL_SetRenderTarget(s->renderer, NULL);
   122     SDL_RenderCopy(s->renderer, target, NULL, NULL);
   204     SDL_RenderCopy(s->renderer, target, NULL, NULL);
   123     SDL_DestroyTexture(target);
   205     SDL_DestroyTexture(target);
   124 
   206 
   125     /* Update the screen! */
   207     /* Update the screen! */
   126     SDL_RenderPresent(s->renderer);
   208     SDL_RenderPresent(s->renderer);
   132     DrawState *drawstates;
   214     DrawState *drawstates;
   133     int i, done;
   215     int i, done;
   134     SDL_Event event;
   216     SDL_Event event;
   135     int frames;
   217     int frames;
   136     Uint32 then, now;
   218     Uint32 then, now;
       
   219     SDL_bool test_composite = SDL_FALSE;
   137 
   220 
   138     /* Initialize test framework */
   221     /* Initialize test framework */
   139     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
   222     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
   140     if (!state) {
   223     if (!state) {
   141         return 1;
   224         return 1;
   143     for (i = 1; i < argc;) {
   226     for (i = 1; i < argc;) {
   144         int consumed;
   227         int consumed;
   145 
   228 
   146         consumed = SDLTest_CommonArg(state, i);
   229         consumed = SDLTest_CommonArg(state, i);
   147         if (consumed == 0) {
   230         if (consumed == 0) {
   148             fprintf(stderr, "Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
   231             consumed = -1;
   149             return 1;
   232             if (SDL_strcasecmp(argv[i], "--composite") == 0) {
       
   233                 test_composite = SDL_TRUE;
       
   234                 consumed = 1;
       
   235             }
       
   236         }
       
   237         if (consumed < 0) {
       
   238             fprintf(stderr,
       
   239                     "Usage: %s %s [--composite]\n",
       
   240                     argv[0], SDLTest_CommonUsage(state));
       
   241             quit(1);
   150         }
   242         }
   151         i += consumed;
   243         i += consumed;
   152     }
   244     }
   153     if (!SDLTest_CommonInit(state)) {
   245     if (!SDLTest_CommonInit(state)) {
   154         quit(2);
   246         quit(2);
   158     for (i = 0; i < state->num_windows; ++i) {
   250     for (i = 0; i < state->num_windows; ++i) {
   159         DrawState *drawstate = &drawstates[i];
   251         DrawState *drawstate = &drawstates[i];
   160 
   252 
   161         drawstate->window = state->windows[i];
   253         drawstate->window = state->windows[i];
   162         drawstate->renderer = state->renderers[i];
   254         drawstate->renderer = state->renderers[i];
   163         drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
   255         if (test_composite) {
       
   256             drawstate->sprite = LoadTexture(drawstate->renderer, "icon-alpha.bmp", SDL_TRUE);
       
   257         } else {
       
   258             drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
       
   259         }
   164         drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
   260         drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
   165         if (!drawstate->sprite || !drawstate->background) {
   261         if (!drawstate->sprite || !drawstate->background) {
   166             quit(2);
   262             quit(2);
   167         }
   263         }
   168         SDL_QueryTexture(drawstate->sprite, NULL, NULL,
   264         SDL_QueryTexture(drawstate->sprite, NULL, NULL,
   179         ++frames;
   275         ++frames;
   180         while (SDL_PollEvent(&event)) {
   276         while (SDL_PollEvent(&event)) {
   181             SDLTest_CommonEvent(state, &event, &done);
   277             SDLTest_CommonEvent(state, &event, &done);
   182         }
   278         }
   183         for (i = 0; i < state->num_windows; ++i) {
   279         for (i = 0; i < state->num_windows; ++i) {
   184             Draw(&drawstates[i]);
   280             if (test_composite) {
       
   281                 DrawComposite(&drawstates[i]);
       
   282             } else {
       
   283                 Draw(&drawstates[i]);
       
   284             }
   185         }
   285         }
   186     }
   286     }
   187 
   287 
   188     /* Print out some timing information */
   288     /* Print out some timing information */
   189     now = SDL_GetTicks();
   289     now = SDL_GetTicks();