test/testrendertarget.c
changeset 6232 37e8d0736366
child 6247 b6212690f78d
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testrendertarget.c	Wed Jan 18 22:45:49 2012 -0500
     1.3 @@ -0,0 +1,204 @@
     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 +
   1.102 +    SDL_RenderGetViewport(s->renderer, &viewport);
   1.103 +
   1.104 +    target = SDL_CreateTexture(s->renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, viewport.w, viewport.h);
   1.105 +    SDL_SetTargetTexture(s->renderer, target);
   1.106 +
   1.107 +    /* Draw the background */
   1.108 +    SDL_RenderCopy(s->renderer, s->background, NULL, NULL);
   1.109 +
   1.110 +    /* Scale and draw the sprite */
   1.111 +    s->sprite_rect.w += s->scale_direction;
   1.112 +    s->sprite_rect.h += s->scale_direction;
   1.113 +    if (s->scale_direction > 0) {
   1.114 +        if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
   1.115 +            s->scale_direction = -1;
   1.116 +        }
   1.117 +    } else {
   1.118 +        if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
   1.119 +            s->scale_direction = 1;
   1.120 +        }
   1.121 +    }
   1.122 +    s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
   1.123 +    s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
   1.124 +
   1.125 +    SDL_RenderCopy(s->renderer, s->sprite, NULL, &s->sprite_rect);
   1.126 +
   1.127 +    SDL_SetTargetTexture(s->renderer, NULL);
   1.128 +    SDL_RenderCopy(s->renderer, target, NULL, NULL);
   1.129 +    SDL_DestroyTexture(target);
   1.130 +
   1.131 +    /* Update the screen! */
   1.132 +    SDL_RenderPresent(s->renderer);
   1.133 +}
   1.134 +
   1.135 +int
   1.136 +main(int argc, char *argv[])
   1.137 +{
   1.138 +    DrawState *drawstates;
   1.139 +    int i, done;
   1.140 +    SDL_Event event;
   1.141 +    int frames;
   1.142 +    Uint32 then, now;
   1.143 +
   1.144 +    /* Initialize test framework */
   1.145 +    state = CommonCreateState(argv, SDL_INIT_VIDEO);
   1.146 +    if (!state) {
   1.147 +        return 1;
   1.148 +    }
   1.149 +    for (i = 1; i < argc;) {
   1.150 +        int consumed;
   1.151 +
   1.152 +        consumed = CommonArg(state, i);
   1.153 +        if (consumed == 0) {
   1.154 +            fprintf(stderr, "Usage: %s %s\n", argv[0], CommonUsage(state));
   1.155 +            return 1;
   1.156 +        }
   1.157 +        i += consumed;
   1.158 +    }
   1.159 +    if (!CommonInit(state)) {
   1.160 +        quit(2);
   1.161 +    }
   1.162 +
   1.163 +    drawstates = SDL_stack_alloc(DrawState, state->num_windows);
   1.164 +    for (i = 0; i < state->num_windows; ++i) {
   1.165 +        DrawState *drawstate = &drawstates[i];
   1.166 +
   1.167 +        drawstate->window = state->windows[i];
   1.168 +        drawstate->renderer = state->renderers[i];
   1.169 +        drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
   1.170 +        drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
   1.171 +        if (!drawstate->sprite || !drawstate->background) {
   1.172 +            quit(2);
   1.173 +        }
   1.174 +        SDL_QueryTexture(drawstate->sprite, NULL, NULL,
   1.175 +                         &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
   1.176 +        drawstate->scale_direction = 1;
   1.177 +    }
   1.178 +
   1.179 +    /* Main render loop */
   1.180 +    frames = 0;
   1.181 +    then = SDL_GetTicks();
   1.182 +    done = 0;
   1.183 +    while (!done) {
   1.184 +        /* Check for events */
   1.185 +        ++frames;
   1.186 +        while (SDL_PollEvent(&event)) {
   1.187 +            CommonEvent(state, &event, &done);
   1.188 +        }
   1.189 +        for (i = 0; i < state->num_windows; ++i) {
   1.190 +            Draw(&drawstates[i]);
   1.191 +        }
   1.192 +    }
   1.193 +
   1.194 +    /* Print out some timing information */
   1.195 +    now = SDL_GetTicks();
   1.196 +    if (now > then) {
   1.197 +        double fps = ((double) frames * 1000) / (now - then);
   1.198 +        printf("%2.2f frames per second\n", fps);
   1.199 +    }
   1.200 +
   1.201 +    SDL_stack_free(drawstates);
   1.202 +
   1.203 +    quit(0);
   1.204 +    return 0;
   1.205 +}
   1.206 +
   1.207 +/* vi: set ts=4 sw=4 expandtab: */