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