test/testrendercopyex.c
changeset 6320 6077a1310907
child 6785 c094c438c0a6
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testrendercopyex.c	Fri Jun 01 19:51:08 2012 -0300
     1.3 @@ -0,0 +1,208 @@
     1.4 +/*
     1.5 +  Copyright (C) 1997-2011 Sam Lantinga <slouken@libsdl.org>
     1.6 +
     1.7 +  This software is provided 'as-is', without any express or implied
     1.8 +  warranty.  In no event will the authors be held liable for any damages
     1.9 +  arising from the use of this software.
    1.10 +
    1.11 +  Permission is granted to anyone to use this software for any purpose,
    1.12 +  including commercial applications, and to alter it and redistribute it
    1.13 +  freely.
    1.14 +*/
    1.15 +/* Simple program:  Move N sprites around on the screen as fast as possible */
    1.16 +
    1.17 +#include <stdlib.h>
    1.18 +#include <stdio.h>
    1.19 +#include <time.h>
    1.20 +
    1.21 +#include "SDL.h"
    1.22 +#include "common.h"
    1.23 +
    1.24 +#define WINDOW_WIDTH    640
    1.25 +#define WINDOW_HEIGHT   480
    1.26 +
    1.27 +static CommonState *state;
    1.28 +
    1.29 +typedef struct {
    1.30 +    SDL_Window *window;
    1.31 +    SDL_Renderer *renderer;
    1.32 +    SDL_Texture *background;
    1.33 +    SDL_Texture *sprite;
    1.34 +    SDL_Rect sprite_rect;
    1.35 +    int scale_direction;
    1.36 +} DrawState;
    1.37 +
    1.38 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    1.39 +static void
    1.40 +quit(int rc)
    1.41 +{
    1.42 +    CommonQuit(state);
    1.43 +    exit(rc);
    1.44 +}
    1.45 +
    1.46 +SDL_Texture *
    1.47 +LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
    1.48 +{
    1.49 +    SDL_Surface *temp;
    1.50 +    SDL_Texture *texture;
    1.51 +
    1.52 +    /* Load the sprite image */
    1.53 +    temp = SDL_LoadBMP(file);
    1.54 +    if (temp == NULL) {
    1.55 +        fprintf(stderr, "Couldn't load %s: %s", file, SDL_GetError());
    1.56 +        return NULL;
    1.57 +    }
    1.58 +
    1.59 +    /* Set transparent pixel as the pixel at (0,0) */
    1.60 +    if (transparent) {
    1.61 +        if (temp->format->palette) {
    1.62 +            SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
    1.63 +        } else {
    1.64 +            switch (temp->format->BitsPerPixel) {
    1.65 +            case 15:
    1.66 +                SDL_SetColorKey(temp, SDL_TRUE,
    1.67 +                                (*(Uint16 *) temp->pixels) & 0x00007FFF);
    1.68 +                break;
    1.69 +            case 16:
    1.70 +                SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
    1.71 +                break;
    1.72 +            case 24:
    1.73 +                SDL_SetColorKey(temp, SDL_TRUE,
    1.74 +                                (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
    1.75 +                break;
    1.76 +            case 32:
    1.77 +                SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
    1.78 +                break;
    1.79 +            }
    1.80 +        }
    1.81 +    }
    1.82 +
    1.83 +    /* Create textures from the image */
    1.84 +    texture = SDL_CreateTextureFromSurface(renderer, temp);
    1.85 +    if (!texture) {
    1.86 +        fprintf(stderr, "Couldn't create texture: %s\n", SDL_GetError());
    1.87 +        SDL_FreeSurface(temp);
    1.88 +        return NULL;
    1.89 +    }
    1.90 +    SDL_FreeSurface(temp);
    1.91 +
    1.92 +    /* We're ready to roll. :) */
    1.93 +    return texture;
    1.94 +}
    1.95 +
    1.96 +void
    1.97 +Draw(DrawState *s)
    1.98 +{
    1.99 +    SDL_Rect viewport;
   1.100 +    SDL_Texture *target;
   1.101 +    SDL_Point *center=NULL;
   1.102 +    SDL_Point origin = {0,0};
   1.103 +
   1.104 +    SDL_RenderGetViewport(s->renderer, &viewport);
   1.105 +
   1.106 +    target = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, viewport.w, viewport.h);
   1.107 +    SDL_SetRenderTarget(s->renderer, target);
   1.108 +
   1.109 +    /* Draw the background */
   1.110 +    SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
   1.111 +
   1.112 +    /* Scale and draw the sprite */
   1.113 +    s->sprite_rect.w += s->scale_direction;
   1.114 +    s->sprite_rect.h += s->scale_direction;
   1.115 +    if (s->scale_direction > 0) {
   1.116 +        center = &origin;
   1.117 +        if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
   1.118 +            s->scale_direction = -1;
   1.119 +        }
   1.120 +    } else {
   1.121 +        if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
   1.122 +            s->scale_direction = 1;
   1.123 +        }
   1.124 +    }
   1.125 +    s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
   1.126 +    s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
   1.127 +
   1.128 +    SDL_RenderCopyEx(s->renderer, s->sprite, NULL, &s->sprite_rect, (double)s->sprite_rect.w, center, s->scale_direction);
   1.129 +
   1.130 +    SDL_SetRenderTarget(s->renderer, NULL);
   1.131 +    SDL_RenderCopy(s->renderer, target, NULL, NULL);
   1.132 +    SDL_DestroyTexture(target);
   1.133 +
   1.134 +    /* Update the screen! */
   1.135 +    SDL_RenderPresent(s->renderer);
   1.136 +    //SDL_Delay(10);
   1.137 +}
   1.138 +
   1.139 +int
   1.140 +main(int argc, char *argv[])
   1.141 +{
   1.142 +    DrawState *drawstates;
   1.143 +    int i, done;
   1.144 +    SDL_Event event;
   1.145 +    int frames;
   1.146 +    Uint32 then, now;
   1.147 +
   1.148 +    /* Initialize test framework */
   1.149 +    state = CommonCreateState(argv, SDL_INIT_VIDEO);
   1.150 +    if (!state) {
   1.151 +        return 1;
   1.152 +    }
   1.153 +    for (i = 1; i < argc;) {
   1.154 +        int consumed;
   1.155 +
   1.156 +        consumed = CommonArg(state, i);
   1.157 +        if (consumed == 0) {
   1.158 +            fprintf(stderr, "Usage: %s %s\n", argv[0], CommonUsage(state));
   1.159 +            return 1;
   1.160 +        }
   1.161 +        i += consumed;
   1.162 +    }
   1.163 +    if (!CommonInit(state)) {
   1.164 +        quit(2);
   1.165 +    }
   1.166 +
   1.167 +    drawstates = SDL_stack_alloc(DrawState, state->num_windows);
   1.168 +    for (i = 0; i < state->num_windows; ++i) {
   1.169 +        DrawState *drawstate = &drawstates[i];
   1.170 +
   1.171 +        drawstate->window = state->windows[i];
   1.172 +        drawstate->renderer = state->renderers[i];
   1.173 +        drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
   1.174 +        drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
   1.175 +        if (!drawstate->sprite || !drawstate->background) {
   1.176 +            quit(2);
   1.177 +        }
   1.178 +        SDL_QueryTexture(drawstate->sprite, NULL, NULL,
   1.179 +                         &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
   1.180 +        drawstate->scale_direction = 1;
   1.181 +    }
   1.182 +
   1.183 +    /* Main render loop */
   1.184 +    frames = 0;
   1.185 +    then = SDL_GetTicks();
   1.186 +    done = 0;
   1.187 +    while (!done) {
   1.188 +        /* Check for events */
   1.189 +        ++frames;
   1.190 +        while (SDL_PollEvent(&event)) {
   1.191 +            CommonEvent(state, &event, &done);
   1.192 +        }
   1.193 +        for (i = 0; i < state->num_windows; ++i) {
   1.194 +            Draw(&drawstates[i]);
   1.195 +        }
   1.196 +    }
   1.197 +
   1.198 +    /* Print out some timing information */
   1.199 +    now = SDL_GetTicks();
   1.200 +    if (now > then) {
   1.201 +        double fps = ((double) frames * 1000) / (now - then);
   1.202 +        printf("%2.2f frames per second\n", fps);
   1.203 +    }
   1.204 +
   1.205 +    SDL_stack_free(drawstates);
   1.206 +
   1.207 +    quit(0);
   1.208 +    return 0;
   1.209 +}
   1.210 +
   1.211 +/* vi: set ts=4 sw=4 expandtab: */