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