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
slouken@5535
     1
/*
slouken@5535
     2
  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
slouken@5535
     3
slouken@5535
     4
  This software is provided 'as-is', without any express or implied
slouken@5535
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@5535
     6
  arising from the use of this software.
slouken@5535
     7
slouken@5535
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@5535
     9
  including commercial applications, and to alter it and redistribute it
slouken@5535
    10
  freely.
slouken@5535
    11
*/
slouken@5240
    12
/* Simple program:  Move N sprites around on the screen as fast as possible */
slouken@5240
    13
slouken@5240
    14
#include <stdlib.h>
slouken@5240
    15
#include <stdio.h>
slouken@5240
    16
#include <time.h>
slouken@5240
    17
slouken@5240
    18
#include "SDL.h"
slouken@5240
    19
#include "common.h"
slouken@5240
    20
slouken@5240
    21
#define WINDOW_WIDTH    640
slouken@5240
    22
#define WINDOW_HEIGHT   480
slouken@5240
    23
slouken@5240
    24
static CommonState *state;
slouken@5240
    25
slouken@5240
    26
typedef struct {
slouken@5240
    27
    SDL_Window *window;
slouken@5240
    28
    SDL_Renderer *renderer;
slouken@5240
    29
    SDL_Texture *background;
slouken@5240
    30
    SDL_Texture *sprite;
slouken@5240
    31
    SDL_Rect sprite_rect;
slouken@5240
    32
    int scale_direction;
slouken@5240
    33
} DrawState;
slouken@5240
    34
slouken@5240
    35
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@5240
    36
static void
slouken@5240
    37
quit(int rc)
slouken@5240
    38
{
slouken@5240
    39
    CommonQuit(state);
slouken@5240
    40
    exit(rc);
slouken@5240
    41
}
slouken@5240
    42
slouken@5240
    43
SDL_Texture *
slouken@5240
    44
LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
slouken@5240
    45
{
slouken@5240
    46
    SDL_Surface *temp;
slouken@5240
    47
    SDL_Texture *texture;
slouken@5240
    48
slouken@5240
    49
    /* Load the sprite image */
slouken@5240
    50
    temp = SDL_LoadBMP(file);
slouken@5240
    51
    if (temp == NULL) {
slouken@5240
    52
        fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
slouken@5240
    53
        return NULL;
slouken@5240
    54
    }
slouken@5240
    55
slouken@5240
    56
    /* Set transparent pixel as the pixel at (0,0) */
slouken@5240
    57
    if (transparent) {
slouken@5240
    58
        if (temp->format->palette) {
slouken@5240
    59
            SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
slouken@5240
    60
        } else {
slouken@5240
    61
            switch (temp->format->BitsPerPixel) {
slouken@5240
    62
            case 15:
slouken@5240
    63
                SDL_SetColorKey(temp, SDL_TRUE,
slouken@5240
    64
                                (*(Uint16 *) temp->pixels) & 0x00007FFF);
slouken@5240
    65
                break;
slouken@5240
    66
            case 16:
slouken@5240
    67
                SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
slouken@5240
    68
                break;
slouken@5240
    69
            case 24:
slouken@5240
    70
                SDL_SetColorKey(temp, SDL_TRUE,
slouken@5240
    71
                                (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
slouken@5240
    72
                break;
slouken@5240
    73
            case 32:
slouken@5240
    74
                SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
slouken@5240
    75
                break;
slouken@5240
    76
            }
slouken@5240
    77
        }
slouken@5240
    78
    }
slouken@5240
    79
slouken@5240
    80
    /* Create textures from the image */
slouken@5240
    81
    texture = SDL_CreateTextureFromSurface(renderer, temp);
slouken@5240
    82
    if (!texture) {
slouken@5240
    83
        fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
slouken@5240
    84
        SDL_FreeSurface(temp);
slouken@5240
    85
        return NULL;
slouken@5240
    86
    }
slouken@5240
    87
    SDL_FreeSurface(temp);
slouken@5240
    88
slouken@5240
    89
    /* We're ready to roll. :) */
slouken@5240
    90
    return texture;
slouken@5240
    91
}
slouken@5240
    92
slouken@5240
    93
void
slouken@5240
    94
Draw(DrawState *s)
slouken@5240
    95
{
slouken@5297
    96
    SDL_Rect viewport;
slouken@5240
    97
slouken@5297
    98
    SDL_RenderGetViewport(s->renderer, &viewport);
slouken@5240
    99
slouken@5240
   100
    /* Draw the background */
slouken@5240
   101
    SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
slouken@5240
   102
slouken@5240
   103
    /* Scale and draw the sprite */
slouken@5240
   104
    s->sprite_rect.w += s->scale_direction;
slouken@5240
   105
    s->sprite_rect.h += s->scale_direction;
slouken@5240
   106
    if (s->scale_direction > 0) {
slouken@5297
   107
        if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
slouken@5240
   108
            s->scale_direction = -1;
slouken@5240
   109
        }
slouken@5240
   110
    } else {
slouken@5240
   111
        if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
slouken@5240
   112
            s->scale_direction = 1;
slouken@5240
   113
        }
slouken@5240
   114
    }
slouken@5297
   115
    s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
slouken@5297
   116
    s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
slouken@5240
   117
slouken@5240
   118
    SDL_RenderCopy(s->renderer, s->sprite, NULL, &s->sprite_rect);
slouken@5240
   119
slouken@5240
   120
    /* Update the screen! */
slouken@5240
   121
    SDL_RenderPresent(s->renderer);
slouken@5240
   122
}
slouken@5240
   123
slouken@5240
   124
int
slouken@5240
   125
main(int argc, char *argv[])
slouken@5240
   126
{
slouken@5240
   127
    DrawState *drawstates;
slouken@5240
   128
    int i, done;
slouken@5240
   129
    SDL_Event event;
slouken@5240
   130
    int frames;
slouken@5240
   131
    Uint32 then, now;
slouken@5240
   132
slouken@5240
   133
    /* Initialize test framework */
slouken@5240
   134
    state = CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@5240
   135
    if (!state) {
slouken@5240
   136
        return 1;
slouken@5240
   137
    }
slouken@5240
   138
    for (i = 1; i < argc;) {
slouken@5240
   139
        int consumed;
slouken@5240
   140
slouken@5240
   141
        consumed = CommonArg(state, i);
slouken@5240
   142
        if (consumed == 0) {
slouken@5240
   143
            fprintf(stderr, "Usage: %s %s\n", argv[0], CommonUsage(state));
slouken@5240
   144
            return 1;
slouken@5240
   145
        }
slouken@5240
   146
        i += consumed;
slouken@5240
   147
    }
slouken@5240
   148
    if (!CommonInit(state)) {
slouken@5240
   149
        quit(2);
slouken@5240
   150
    }
slouken@5240
   151
slouken@5240
   152
    drawstates = SDL_stack_alloc(DrawState, state->num_windows);
slouken@5240
   153
    for (i = 0; i < state->num_windows; ++i) {
slouken@5240
   154
        DrawState *drawstate = &drawstates[i];
slouken@5240
   155
slouken@5240
   156
        drawstate->window = state->windows[i];
slouken@5240
   157
        drawstate->renderer = state->renderers[i];
slouken@5240
   158
        drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
slouken@5240
   159
        drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
slouken@5240
   160
        if (!drawstate->sprite || !drawstate->background) {
slouken@5240
   161
            quit(2);
slouken@5240
   162
        }
slouken@5240
   163
        SDL_QueryTexture(drawstate->sprite, NULL, NULL,
slouken@5240
   164
                         &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
slouken@5240
   165
        drawstate->scale_direction = 1;
slouken@5240
   166
    }
slouken@5240
   167
slouken@5240
   168
    /* Main render loop */
slouken@5240
   169
    frames = 0;
slouken@5240
   170
    then = SDL_GetTicks();
slouken@5240
   171
    done = 0;
slouken@5240
   172
    while (!done) {
slouken@5240
   173
        /* Check for events */
slouken@5240
   174
        ++frames;
slouken@5240
   175
        while (SDL_PollEvent(&event)) {
slouken@5240
   176
            CommonEvent(state, &event, &done);
slouken@5240
   177
        }
slouken@5240
   178
        for (i = 0; i < state->num_windows; ++i) {
slouken@5240
   179
            Draw(&drawstates[i]);
slouken@5240
   180
        }
slouken@5240
   181
    }
slouken@5240
   182
slouken@5240
   183
    /* Print out some timing information */
slouken@5240
   184
    now = SDL_GetTicks();
slouken@5240
   185
    if (now > then) {
slouken@5240
   186
        double fps = ((double) frames * 1000) / (now - then);
slouken@5240
   187
        printf("%2.2f frames per second\n", fps);
slouken@5240
   188
    }
slouken@5240
   189
slouken@5295
   190
    SDL_stack_free(drawstates);
slouken@5240
   191
slouken@5240
   192
    quit(0);
slouken@5240
   193
    return 0;
slouken@5240
   194
}
slouken@5240
   195
slouken@5240
   196
/* vi: set ts=4 sw=4 expandtab: */