test/testscale.c
author Sam Lantinga <slouken@libsdl.org>
Tue, 15 Feb 2011 13:59:59 -0800
changeset 5297 1800dc39b74c
parent 5295 b88d05603e4b
child 5535 96594ac5fd1a
permissions -rw-r--r--
Changed the concept of a render clip rect to a render viewport.
The render viewport is automatically re-centered when the window changes size, so applications that don't care will not have to handle recalculating their rendering coordinates.

Fixed API for drawing and filling multiple rectangles - the parameter should be an array of rects, not an array of pointers to rects.

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