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