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