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