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