test/testrendertarget.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 31 Dec 2012 10:26:38 -0800
changeset 6785 c094c438c0a6
parent 6247 b6212690f78d
child 7013 fe82d15464d1
permissions -rw-r--r--
Switched the test code over to use the common functions in the test library.
     1 /*
     2   Copyright (C) 1997-2011 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 #define WINDOW_WIDTH    640
    21 #define WINDOW_HEIGHT   480
    22 
    23 static SDLTest_CommonState *state;
    24 
    25 typedef struct {
    26     SDL_Window *window;
    27     SDL_Renderer *renderer;
    28     SDL_Texture *background;
    29     SDL_Texture *sprite;
    30     SDL_Rect sprite_rect;
    31     int scale_direction;
    32 } DrawState;
    33 
    34 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    35 static void
    36 quit(int rc)
    37 {
    38     SDLTest_CommonQuit(state);
    39     exit(rc);
    40 }
    41 
    42 SDL_Texture *
    43 LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
    44 {
    45     SDL_Surface *temp;
    46     SDL_Texture *texture;
    47 
    48     /* Load the sprite image */
    49     temp = SDL_LoadBMP(file);
    50     if (temp == NULL) {
    51         fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
    52         return NULL;
    53     }
    54 
    55     /* Set transparent pixel as the pixel at (0,0) */
    56     if (transparent) {
    57         if (temp->format->palette) {
    58             SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
    59         } else {
    60             switch (temp->format->BitsPerPixel) {
    61             case 15:
    62                 SDL_SetColorKey(temp, SDL_TRUE,
    63                                 (*(Uint16 *) temp->pixels) & 0x00007FFF);
    64                 break;
    65             case 16:
    66                 SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
    67                 break;
    68             case 24:
    69                 SDL_SetColorKey(temp, SDL_TRUE,
    70                                 (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
    71                 break;
    72             case 32:
    73                 SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
    74                 break;
    75             }
    76         }
    77     }
    78 
    79     /* Create textures from the image */
    80     texture = SDL_CreateTextureFromSurface(renderer, temp);
    81     if (!texture) {
    82         fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
    83         SDL_FreeSurface(temp);
    84         return NULL;
    85     }
    86     SDL_FreeSurface(temp);
    87 
    88     /* We're ready to roll. :) */
    89     return texture;
    90 }
    91 
    92 void
    93 Draw(DrawState *s)
    94 {
    95     SDL_Rect viewport;
    96     SDL_Texture *target;
    97 
    98     SDL_RenderGetViewport(s->renderer, &viewport);
    99 
   100     target = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, viewport.w, viewport.h);
   101     SDL_SetRenderTarget(s->renderer, target);
   102 
   103     /* Draw the background */
   104     SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
   105 
   106     /* Scale and draw the sprite */
   107     s->sprite_rect.w += s->scale_direction;
   108     s->sprite_rect.h += s->scale_direction;
   109     if (s->scale_direction > 0) {
   110         if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
   111             s->scale_direction = -1;
   112         }
   113     } else {
   114         if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
   115             s->scale_direction = 1;
   116         }
   117     }
   118     s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
   119     s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
   120 
   121     SDL_RenderCopy(s->renderer, s->sprite, NULL, &s->sprite_rect);
   122 
   123     SDL_SetRenderTarget(s->renderer, NULL);
   124     SDL_RenderCopy(s->renderer, target, NULL, NULL);
   125     SDL_DestroyTexture(target);
   126 
   127     /* Update the screen! */
   128     SDL_RenderPresent(s->renderer);
   129 }
   130 
   131 int
   132 main(int argc, char *argv[])
   133 {
   134     DrawState *drawstates;
   135     int i, done;
   136     SDL_Event event;
   137     int frames;
   138     Uint32 then, now;
   139 
   140     /* Initialize test framework */
   141     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
   142     if (!state) {
   143         return 1;
   144     }
   145     for (i = 1; i < argc;) {
   146         int consumed;
   147 
   148         consumed = SDLTest_CommonArg(state, i);
   149         if (consumed == 0) {
   150             fprintf(stderr, "Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
   151             return 1;
   152         }
   153         i += consumed;
   154     }
   155     if (!SDLTest_CommonInit(state)) {
   156         quit(2);
   157     }
   158 
   159     drawstates = SDL_stack_alloc(DrawState, state->num_windows);
   160     for (i = 0; i < state->num_windows; ++i) {
   161         DrawState *drawstate = &drawstates[i];
   162 
   163         drawstate->window = state->windows[i];
   164         drawstate->renderer = state->renderers[i];
   165         drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
   166         drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
   167         if (!drawstate->sprite || !drawstate->background) {
   168             quit(2);
   169         }
   170         SDL_QueryTexture(drawstate->sprite, NULL, NULL,
   171                          &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
   172         drawstate->scale_direction = 1;
   173     }
   174 
   175     /* Main render loop */
   176     frames = 0;
   177     then = SDL_GetTicks();
   178     done = 0;
   179     while (!done) {
   180         /* Check for events */
   181         ++frames;
   182         while (SDL_PollEvent(&event)) {
   183             SDLTest_CommonEvent(state, &event, &done);
   184         }
   185         for (i = 0; i < state->num_windows; ++i) {
   186             Draw(&drawstates[i]);
   187         }
   188     }
   189 
   190     /* Print out some timing information */
   191     now = SDL_GetTicks();
   192     if (now > then) {
   193         double fps = ((double) frames * 1000) / (now - then);
   194         printf("%2.2f frames per second\n", fps);
   195     }
   196 
   197     SDL_stack_free(drawstates);
   198 
   199     quit(0);
   200     return 0;
   201 }
   202 
   203 /* vi: set ts=4 sw=4 expandtab: */