test/testrendercopyex.c
author Sam Lantinga <slouken@libsdl.org>
Wed, 05 Jun 2013 22:33:10 -0700
changeset 7286 04087a99e3f4
parent 7013 fe82d15464d1
child 7517 965d57022c01
permissions -rw-r--r--
Added testautomation to the Visual Studio 2008 project.
     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 
    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 Draw(DrawState *s)
    92 {
    93     SDL_Rect viewport;
    94     SDL_Texture *target;
    95     SDL_Point *center=NULL;
    96     SDL_Point origin = {0,0};
    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         center = &origin;
   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_RenderCopyEx(s->renderer, s->sprite, NULL, &s->sprite_rect, (double)s->sprite_rect.w, center, s->scale_direction);
   123 
   124     SDL_SetRenderTarget(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     //SDL_Delay(10);
   131 }
   132 
   133 int
   134 main(int argc, char *argv[])
   135 {
   136     DrawState *drawstates;
   137     int i, done;
   138     SDL_Event event;
   139     int frames;
   140     Uint32 then, now;
   141 
   142     /* Initialize test framework */
   143     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
   144     if (!state) {
   145         return 1;
   146     }
   147     for (i = 1; i < argc;) {
   148         int consumed;
   149 
   150         consumed = SDLTest_CommonArg(state, i);
   151         if (consumed == 0) {
   152             fprintf(stderr, "Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
   153             return 1;
   154         }
   155         i += consumed;
   156     }
   157     if (!SDLTest_CommonInit(state)) {
   158         quit(2);
   159     }
   160 
   161     drawstates = SDL_stack_alloc(DrawState, state->num_windows);
   162     for (i = 0; i < state->num_windows; ++i) {
   163         DrawState *drawstate = &drawstates[i];
   164 
   165         drawstate->window = state->windows[i];
   166         drawstate->renderer = state->renderers[i];
   167         drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
   168         drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
   169         if (!drawstate->sprite || !drawstate->background) {
   170             quit(2);
   171         }
   172         SDL_QueryTexture(drawstate->sprite, NULL, NULL,
   173                          &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
   174         drawstate->scale_direction = 1;
   175     }
   176 
   177     /* Main render loop */
   178     frames = 0;
   179     then = SDL_GetTicks();
   180     done = 0;
   181     while (!done) {
   182         /* Check for events */
   183         ++frames;
   184         while (SDL_PollEvent(&event)) {
   185             SDLTest_CommonEvent(state, &event, &done);
   186         }
   187         for (i = 0; i < state->num_windows; ++i) {
   188             Draw(&drawstates[i]);
   189         }
   190     }
   191 
   192     /* Print out some timing information */
   193     now = SDL_GetTicks();
   194     if (now > then) {
   195         double fps = ((double) frames * 1000) / (now - then);
   196         printf("%2.2f frames per second\n", fps);
   197     }
   198 
   199     SDL_stack_free(drawstates);
   200 
   201     quit(0);
   202     return 0;
   203 }
   204 
   205 /* vi: set ts=4 sw=4 expandtab: */