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