test/testrendertarget.c
author Andreas Schiffler <aschiffler@ferzkopp.net>
Wed, 14 Aug 2013 23:30:10 -0700
changeset 7639 9406b7dd2f2d
parent 7517 965d57022c01
child 7781 3023b0270c94
permissions -rw-r--r--
Fix bug 2034: replace printf by SDL_Log in tests; update loopwave VS solution: copy missing dependency
     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         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "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         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "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         SDL_Log("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     /* Enable standard application logging */
   222     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
   223 
   224     /* Initialize test framework */
   225     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
   226     if (!state) {
   227         return 1;
   228     }
   229     for (i = 1; i < argc;) {
   230         int consumed;
   231 
   232         consumed = SDLTest_CommonArg(state, i);
   233         if (consumed == 0) {
   234             consumed = -1;
   235             if (SDL_strcasecmp(argv[i], "--composite") == 0) {
   236                 test_composite = SDL_TRUE;
   237                 consumed = 1;
   238             }
   239         }
   240         if (consumed < 0) {
   241             SDL_Log("Usage: %s %s [--composite]\n",
   242                     argv[0], SDLTest_CommonUsage(state));
   243             quit(1);
   244         }
   245         i += consumed;
   246     }
   247     if (!SDLTest_CommonInit(state)) {
   248         quit(2);
   249     }
   250 
   251     drawstates = SDL_stack_alloc(DrawState, state->num_windows);
   252     for (i = 0; i < state->num_windows; ++i) {
   253         DrawState *drawstate = &drawstates[i];
   254 
   255         drawstate->window = state->windows[i];
   256         drawstate->renderer = state->renderers[i];
   257         if (test_composite) {
   258             drawstate->sprite = LoadTexture(drawstate->renderer, "icon-alpha.bmp", SDL_TRUE);
   259         } else {
   260             drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
   261         }
   262         drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
   263         if (!drawstate->sprite || !drawstate->background) {
   264             quit(2);
   265         }
   266         SDL_QueryTexture(drawstate->sprite, NULL, NULL,
   267                          &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
   268         drawstate->scale_direction = 1;
   269     }
   270 
   271     /* Main render loop */
   272     frames = 0;
   273     then = SDL_GetTicks();
   274     done = 0;
   275     while (!done) {
   276         /* Check for events */
   277         ++frames;
   278         while (SDL_PollEvent(&event)) {
   279             SDLTest_CommonEvent(state, &event, &done);
   280         }
   281         for (i = 0; i < state->num_windows; ++i) {
   282             if (test_composite) {
   283                 DrawComposite(&drawstates[i]);
   284             } else {
   285                 Draw(&drawstates[i]);
   286             }
   287         }
   288     }
   289 
   290     /* Print out some timing information */
   291     now = SDL_GetTicks();
   292     if (now > then) {
   293         double fps = ((double) frames * 1000) / (now - then);
   294         SDL_Log("%2.2f frames per second\n", fps);
   295     }
   296 
   297     SDL_stack_free(drawstates);
   298 
   299     quit(0);
   300     return 0;
   301 }
   302 
   303 /* vi: set ts=4 sw=4 expandtab: */