Home | History | Annotate | Download | only in test
      1 /*
      2   Copyright (C) 1997-2014 Sam Lantinga <slouken (at) 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         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "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         SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "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 	/* Enable standard application logging */
    143     SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
    144 
    145     /* Initialize test framework */
    146     state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
    147     if (!state) {
    148         return 1;
    149     }
    150     for (i = 1; i < argc;) {
    151         int consumed;
    152 
    153         consumed = SDLTest_CommonArg(state, i);
    154         if (consumed == 0) {
    155             SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
    156             return 1;
    157         }
    158         i += consumed;
    159     }
    160     if (!SDLTest_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             SDLTest_CommonEvent(state, &event, &done);
    189         }
    190         for (i = 0; i < state->num_windows; ++i) {
    191             if (state->windows[i] == NULL)
    192                 continue;
    193             Draw(&drawstates[i]);
    194         }
    195     }
    196 
    197     /* Print out some timing information */
    198     now = SDL_GetTicks();
    199     if (now > then) {
    200         double fps = ((double) frames * 1000) / (now - then);
    201         SDL_Log("%2.2f frames per second\n", fps);
    202     }
    203 
    204     SDL_stack_free(drawstates);
    205 
    206     quit(0);
    207     return 0;
    208 }
    209 
    210 /* vi: set ts=4 sw=4 expandtab: */
    211