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