Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
authorSam Lantinga <slouken@libsdl.org>
Fri, 15 Nov 2013 22:05:26 -0800
changeset 799437c48b4ecb0c
parent 7993 771138df6c62
child 7995 0eed14683885
Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
If it works correctly you should see a square moving from the upper left to the lower right, with a little yellow box at the top of the moving square.

You can pass --target as a command line option to have it use a render target instead of rendering directly to the screen. The output should be identical in this case.
test/Makefile.in
test/testviewport.c
     1.1 --- a/test/Makefile.in	Fri Nov 15 22:01:58 2013 -0800
     1.2 +++ b/test/Makefile.in	Fri Nov 15 22:05:26 2013 -0800
     1.3 @@ -49,6 +49,7 @@
     1.4  	teststreaming$(EXE) \
     1.5  	testtimer$(EXE) \
     1.6  	testver$(EXE) \
     1.7 +	testviewport$(EXE) \
     1.8  	testwm2$(EXE) \
     1.9  	torturethread$(EXE) \
    1.10  	testrendercopyex$(EXE) \
    1.11 @@ -218,6 +219,9 @@
    1.12  testver$(EXE): $(srcdir)/testver.c
    1.13  	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
    1.14  
    1.15 +testviewport$(EXE): $(srcdir)/testviewport.c
    1.16 +	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
    1.17 +
    1.18  testwm2$(EXE): $(srcdir)/testwm2.c
    1.19  	$(CC) -o $@ $^ $(CFLAGS) $(LIBS)
    1.20  
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/test/testviewport.c	Fri Nov 15 22:05:26 2013 -0800
     2.3 @@ -0,0 +1,184 @@
     2.4 +/*
     2.5 +  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
     2.6 +
     2.7 +  This software is provided 'as-is', without any express or implied
     2.8 +  warranty.  In no event will the authors be held liable for any damages
     2.9 +  arising from the use of this software.
    2.10 +
    2.11 +  Permission is granted to anyone to use this software for any purpose,
    2.12 +  including commercial applications, and to alter it and redistribute it
    2.13 +  freely.
    2.14 +*/
    2.15 +/* Simple program:  Check viewports */
    2.16 +
    2.17 +#include <stdlib.h>
    2.18 +#include <stdio.h>
    2.19 +#include <time.h>
    2.20 +
    2.21 +#include "SDL_test.h"
    2.22 +#include "SDL_test_common.h"
    2.23 +
    2.24 +
    2.25 +static SDLTest_CommonState *state;
    2.26 +
    2.27 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    2.28 +static void
    2.29 +quit(int rc)
    2.30 +{
    2.31 +    SDLTest_CommonQuit(state);
    2.32 +    exit(rc);
    2.33 +}
    2.34 +
    2.35 +void
    2.36 +DrawOnViewport(SDL_Renderer * renderer, SDL_Rect viewport)
    2.37 +{    
    2.38 +    SDL_Rect rect;
    2.39 +
    2.40 +    /* Set the viewport */
    2.41 +    SDL_RenderSetViewport(renderer, &viewport);
    2.42 +    
    2.43 +    /* Draw a gray background */
    2.44 +    SDL_SetRenderDrawColor(renderer, 0x80, 0x80, 0x80, 0xFF);
    2.45 +    SDL_RenderClear(renderer);
    2.46 +
    2.47 +    /* Test inside points */
    2.48 +    SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xF, 0xFF);
    2.49 +    SDL_RenderDrawPoint(renderer, viewport.h/2 + 10, viewport.w/2);
    2.50 +    SDL_RenderDrawPoint(renderer, viewport.h/2 - 10, viewport.w/2);
    2.51 +    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 - 10);
    2.52 +    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 + 10);
    2.53 +
    2.54 +    /* Test horizontal and vertical lines */
    2.55 +    SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
    2.56 +    SDL_RenderDrawLine(renderer, 1, 0, viewport.w-2, 0);
    2.57 +    SDL_RenderDrawLine(renderer, 1, viewport.h-1, viewport.w-2, viewport.h-1);
    2.58 +    SDL_RenderDrawLine(renderer, 0, 1, 0, viewport.h-2);
    2.59 +    SDL_RenderDrawLine(renderer, viewport.w-1, 1, viewport.w-1, viewport.h-2);
    2.60 +
    2.61 +    /* Test diagonal lines */
    2.62 +    SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0xFF, 0xFF);
    2.63 +    SDL_RenderDrawLine(renderer, 0, 0,
    2.64 +                       viewport.w-1, viewport.h-1);
    2.65 +    SDL_RenderDrawLine(renderer, viewport.w-1, 0,
    2.66 +                       0, viewport.h-1);                      
    2.67 +
    2.68 +    /* Test outside points */
    2.69 +    SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xF, 0xFF);
    2.70 +    SDL_RenderDrawPoint(renderer, viewport.h/2 + viewport.h, viewport.w/2);
    2.71 +    SDL_RenderDrawPoint(renderer, viewport.h/2 - viewport.h, viewport.w/2);
    2.72 +    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 - viewport.w);
    2.73 +    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 + viewport.w);
    2.74 +
    2.75 +    /* Add a box at the top */
    2.76 +    rect.w = 8;
    2.77 +    rect.h = 8;
    2.78 +    rect.x = (viewport.w - rect.w) / 2;
    2.79 +    rect.y = 0;
    2.80 +    SDL_RenderFillRect(renderer, &rect);
    2.81 +}
    2.82 +
    2.83 +int
    2.84 +main(int argc, char *argv[])
    2.85 +{
    2.86 +    int i, j, done;
    2.87 +    SDL_Event event;
    2.88 +    Uint32 then, now, frames;
    2.89 +    SDL_Rect viewport;
    2.90 +    SDL_bool use_target = SDL_FALSE;
    2.91 +
    2.92 +    /* Initialize test framework */
    2.93 +    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
    2.94 +    if (!state) {
    2.95 +        return 1;
    2.96 +    }
    2.97 +
    2.98 +    for (i = 1; i < argc;) {
    2.99 +        int consumed;
   2.100 +
   2.101 +        consumed = SDLTest_CommonArg(state, i);
   2.102 +        if (consumed == 0) {
   2.103 +            consumed = -1;
   2.104 +            if (SDL_strcasecmp(argv[i], "--target") == 0) {
   2.105 +                use_target = SDL_TRUE;
   2.106 +                consumed = 1;
   2.107 +            }
   2.108 +        }
   2.109 +        if (consumed < 0) {
   2.110 +            SDL_Log("Usage: %s %s [--target]\n",
   2.111 +                    argv[0], SDLTest_CommonUsage(state));
   2.112 +            quit(1);
   2.113 +        }
   2.114 +        i += consumed;
   2.115 +    }
   2.116 +    if (!SDLTest_CommonInit(state)) {
   2.117 +        quit(2);
   2.118 +    }
   2.119 +
   2.120 +    if (use_target) {
   2.121 +        int w, h;
   2.122 +
   2.123 +        for (i = 0; i < state->num_windows; ++i) {
   2.124 +            SDL_GetWindowSize(state->windows[i], &w, &h);
   2.125 +            state->targets[i] = SDL_CreateTexture(state->renderers[i], SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
   2.126 +            SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
   2.127 +        }
   2.128 +    }
   2.129 +
   2.130 +    for (i = 0; i < state->num_windows; ++i) {
   2.131 +        SDL_Renderer *renderer = state->renderers[i];
   2.132 +        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
   2.133 +        SDL_RenderClear(renderer);
   2.134 +    }
   2.135 +
   2.136 +    /* Main render loop */
   2.137 +    frames = 0;
   2.138 +    then = SDL_GetTicks();
   2.139 +    done = 0;
   2.140 +    j = 0;
   2.141 +    while (!done) {
   2.142 +        /* Check for events */
   2.143 +        ++frames;
   2.144 +        while (SDL_PollEvent(&event)) {
   2.145 +            SDLTest_CommonEvent(state, &event, &done);
   2.146 +        }
   2.147 +        
   2.148 +        /* Move a viewport box in steps around the screen */                
   2.149 +        viewport.x = j * 100;
   2.150 +        viewport.y = viewport.x;
   2.151 +        viewport.w = 100 + j * 50;
   2.152 +        viewport.h = 100 + j * 50;
   2.153 +        j = (j + 1) % 4;            
   2.154 +        SDL_Log("Current Viewport x=%i y=%i w=%i h=%i", viewport.x, viewport.y, viewport.w, viewport.h);
   2.155 +        
   2.156 +        for (i = 0; i < state->num_windows; ++i) {
   2.157 +            if (state->windows[i] == NULL)
   2.158 +                continue;
   2.159 +                
   2.160 +            /* Draw using viewport */        
   2.161 +            DrawOnViewport(state->renderers[i], viewport);
   2.162 +
   2.163 +            /* Update the screen! */
   2.164 +            if (use_target) {
   2.165 +                SDL_SetRenderTarget(state->renderers[i], NULL);
   2.166 +                SDL_RenderCopy(state->renderers[i], state->targets[i], NULL, NULL);
   2.167 +                SDL_RenderPresent(state->renderers[i]);
   2.168 +                SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
   2.169 +            } else {
   2.170 +                SDL_RenderPresent(state->renderers[i]);
   2.171 +            }
   2.172 +        }
   2.173 +        
   2.174 +        SDL_Delay(1000);
   2.175 +    }
   2.176 +
   2.177 +    /* Print out some timing information */
   2.178 +    now = SDL_GetTicks();
   2.179 +    if (now > then) {
   2.180 +        double fps = ((double) frames * 1000) / (now - then);
   2.181 +        SDL_Log("%2.2f frames per second\n", fps);
   2.182 +    }
   2.183 +    quit(0);
   2.184 +    return 0;
   2.185 +}
   2.186 +
   2.187 +/* vi: set ts=4 sw=4 expandtab: */