test/testrendertarget.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 25 Jul 2013 09:51:21 -0700
changeset 7517 965d57022c01
parent 7513 ec0c3f1eb2de
child 7639 9406b7dd2f2d
permissions -rw-r--r--
Updated the copyright year for the test programs
     1 /*
     2   Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     3 
     4   This software is provided 'as-is', without any express or implied
     5   warranty.  In no event will the authors be held liable for any damages
     6   arising from the use of this software.
     7 
     8   Permission is granted to anyone to use this software for any purpose,
     9   including commercial applications, and to alter it and redistribute it
    10   freely.
    11 */
    12 /* Simple program:  Move N sprites around on the screen as fast as possible */
    13 
    14 #include <stdlib.h>
    15 #include <stdio.h>
    16 #include <time.h>
    17 
    18 #include "SDL_test_common.h"
    19 
    20 
    21 static SDLTest_CommonState *state;
    22 
    23 typedef struct {
    24     SDL_Window *window;
    25     SDL_Renderer *renderer;
    26     SDL_Texture *background;
    27     SDL_Texture *sprite;
    28     SDL_Rect sprite_rect;
    29     int scale_direction;
    30 } DrawState;
    31 
    32 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    33 static void
    34 quit(int rc)
    35 {
    36     SDLTest_CommonQuit(state);
    37     exit(rc);
    38 }
    39 
    40 SDL_Texture *
    41 LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
    42 {
    43     SDL_Surface *temp;
    44     SDL_Texture *texture;
    45 
    46     /* Load the sprite image */
    47     temp = SDL_LoadBMP(file);
    48     if (temp == NULL) {
    49         fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
    50         return NULL;
    51     }
    52 
    53     /* Set transparent pixel as the pixel at (0,0) */
    54     if (transparent) {
    55         if (temp->format->palette) {
    56             SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
    57         } else {
    58             switch (temp->format->BitsPerPixel) {
    59             case 15:
    60                 SDL_SetColorKey(temp, SDL_TRUE,
    61                                 (*(Uint16 *) temp->pixels) & 0x00007FFF);
    62                 break;
    63             case 16:
    64                 SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
    65                 break;
    66             case 24:
    67                 SDL_SetColorKey(temp, SDL_TRUE,
    68                                 (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
    69                 break;
    70             case 32:
    71                 SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
    72                 break;
    73             }
    74         }
    75     }
    76 
    77     /* Create textures from the image */
    78     texture = SDL_CreateTextureFromSurface(renderer, temp);
    79     if (!texture) {
    80         fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
    81         SDL_FreeSurface(temp);
    82         return NULL;
    83     }
    84     SDL_FreeSurface(temp);
    85 
    86     /* We're ready to roll. :) */
    87     return texture;
    88 }
    89 
    90 void
    91 DrawComposite(DrawState *s)
    92 {
    93     SDL_Rect viewport, R;
    94     SDL_Texture *target;
    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     }
   123 
   124     SDL_RenderGetViewport(s->renderer, &viewport);
   125 
   126     target = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, viewport.w, viewport.h);
   127     SDL_SetTextureBlendMode(target, SDL_BLENDMODE_BLEND);
   128     SDL_SetRenderTarget(s->renderer, target);
   129 
   130     /* Draw the background.
   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);
   135 
   136     /* Scale and draw the sprite */
   137     s->sprite_rect.w += s->scale_direction;
   138     s->sprite_rect.h += s->scale_direction;
   139     if (s->scale_direction > 0) {
   140         if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
   141             s->scale_direction = -1;
   142         }
   143     } else {
   144         if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
   145             s->scale_direction = 1;
   146         }
   147     }
   148     s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
   149     s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
   150 
   151     SDL_RenderCopy(s->renderer, s->sprite, NULL, &s->sprite_rect);
   152 
   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);
   204     SDL_RenderCopy(s->renderer, target, NULL, NULL);
   205     SDL_DestroyTexture(target);
   206 
   207     /* Update the screen! */
   208     SDL_RenderPresent(s->renderer);
   209 }
   210 
   211 int
   212 main(int argc, char *argv[])
   213 {
   214     DrawState *drawstates;
   215     int i, done;
   216     SDL_Event event;
   217     int frames;
   218     Uint32 then, now;
   219     SDL_bool test_composite = SDL_FALSE;
   220 
   221     /* Initialize test framework */
   222     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
   223     if (!state) {
   224         return 1;
   225     }
   226     for (i = 1; i < argc;) {
   227         int consumed;
   228 
   229         consumed = SDLTest_CommonArg(state, i);
   230         if (consumed == 0) {
   231             consumed = -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);
   242         }
   243         i += consumed;
   244     }
   245     if (!SDLTest_CommonInit(state)) {
   246         quit(2);
   247     }
   248 
   249     drawstates = SDL_stack_alloc(DrawState, state->num_windows);
   250     for (i = 0; i < state->num_windows; ++i) {
   251         DrawState *drawstate = &drawstates[i];
   252 
   253         drawstate->window = state->windows[i];
   254         drawstate->renderer = state->renderers[i];
   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         }
   260         drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
   261         if (!drawstate->sprite || !drawstate->background) {
   262             quit(2);
   263         }
   264         SDL_QueryTexture(drawstate->sprite, NULL, NULL,
   265                          &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
   266         drawstate->scale_direction = 1;
   267     }
   268 
   269     /* Main render loop */
   270     frames = 0;
   271     then = SDL_GetTicks();
   272     done = 0;
   273     while (!done) {
   274         /* Check for events */
   275         ++frames;
   276         while (SDL_PollEvent(&event)) {
   277             SDLTest_CommonEvent(state, &event, &done);
   278         }
   279         for (i = 0; i < state->num_windows; ++i) {
   280             if (test_composite) {
   281                 DrawComposite(&drawstates[i]);
   282             } else {
   283                 Draw(&drawstates[i]);
   284             }
   285         }
   286     }
   287 
   288     /* Print out some timing information */
   289     now = SDL_GetTicks();
   290     if (now > then) {
   291         double fps = ((double) frames * 1000) / (now - then);
   292         printf("%2.2f frames per second\n", fps);
   293     }
   294 
   295     SDL_stack_free(drawstates);
   296 
   297     quit(0);
   298     return 0;
   299 }
   300 
   301 /* vi: set ts=4 sw=4 expandtab: */