test/testrendercopyex.c
author Sam Lantinga <slouken@libsdl.org>
Mon, 31 Dec 2012 10:26:38 -0800
changeset 6785 c094c438c0a6
parent 6320 6077a1310907
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     SDL_Point *center=NULL;
    98     SDL_Point origin = {0,0};
    99 
   100     SDL_RenderGetViewport(s->renderer, &viewport);
   101 
   102     target = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, viewport.w, viewport.h);
   103     SDL_SetRenderTarget(s->renderer, target);
   104 
   105     /* Draw the background */
   106     SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
   107 
   108     /* Scale and draw the sprite */
   109     s->sprite_rect.w += s->scale_direction;
   110     s->sprite_rect.h += s->scale_direction;
   111     if (s->scale_direction > 0) {
   112         center = &origin;
   113         if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
   114             s->scale_direction = -1;
   115         }
   116     } else {
   117         if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
   118             s->scale_direction = 1;
   119         }
   120     }
   121     s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
   122     s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
   123 
   124     SDL_RenderCopyEx(s->renderer, s->sprite, NULL, &s->sprite_rect, (double)s->sprite_rect.w, center, s->scale_direction);
   125 
   126     SDL_SetRenderTarget(s->renderer, NULL);
   127     SDL_RenderCopy(s->renderer, target, NULL, NULL);
   128     SDL_DestroyTexture(target);
   129 
   130     /* Update the screen! */
   131     SDL_RenderPresent(s->renderer);
   132     //SDL_Delay(10);
   133 }
   134 
   135 int
   136 main(int argc, char *argv[])
   137 {
   138     DrawState *drawstates;
   139     int i, done;
   140     SDL_Event event;
   141     int frames;
   142     Uint32 then, now;
   143 
   144     /* Initialize test framework */
   145     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
   146     if (!state) {
   147         return 1;
   148     }
   149     for (i = 1; i < argc;) {
   150         int consumed;
   151 
   152         consumed = SDLTest_CommonArg(state, i);
   153         if (consumed == 0) {
   154             fprintf(stderr, "Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
   155             return 1;
   156         }
   157         i += consumed;
   158     }
   159     if (!SDLTest_CommonInit(state)) {
   160         quit(2);
   161     }
   162 
   163     drawstates = SDL_stack_alloc(DrawState, state->num_windows);
   164     for (i = 0; i < state->num_windows; ++i) {
   165         DrawState *drawstate = &drawstates[i];
   166 
   167         drawstate->window = state->windows[i];
   168         drawstate->renderer = state->renderers[i];
   169         drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
   170         drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
   171         if (!drawstate->sprite || !drawstate->background) {
   172             quit(2);
   173         }
   174         SDL_QueryTexture(drawstate->sprite, NULL, NULL,
   175                          &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
   176         drawstate->scale_direction = 1;
   177     }
   178 
   179     /* Main render loop */
   180     frames = 0;
   181     then = SDL_GetTicks();
   182     done = 0;
   183     while (!done) {
   184         /* Check for events */
   185         ++frames;
   186         while (SDL_PollEvent(&event)) {
   187             SDLTest_CommonEvent(state, &event, &done);
   188         }
   189         for (i = 0; i < state->num_windows; ++i) {
   190             Draw(&drawstates[i]);
   191         }
   192     }
   193 
   194     /* Print out some timing information */
   195     now = SDL_GetTicks();
   196     if (now > then) {
   197         double fps = ((double) frames * 1000) / (now - then);
   198         printf("%2.2f frames per second\n", fps);
   199     }
   200 
   201     SDL_stack_free(drawstates);
   202 
   203     quit(0);
   204     return 0;
   205 }
   206 
   207 /* vi: set ts=4 sw=4 expandtab: */