test/testviewport.c
changeset 7994 37c48b4ecb0c
child 8149 681eb46b8ac4
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/testviewport.c	Fri Nov 15 22:05:26 2013 -0800
     1.3 @@ -0,0 +1,184 @@
     1.4 +/*
     1.5 +  Copyright (C) 1997-2013 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:  Check viewports */
    1.16 +
    1.17 +#include <stdlib.h>
    1.18 +#include <stdio.h>
    1.19 +#include <time.h>
    1.20 +
    1.21 +#include "SDL_test.h"
    1.22 +#include "SDL_test_common.h"
    1.23 +
    1.24 +
    1.25 +static SDLTest_CommonState *state;
    1.26 +
    1.27 +/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
    1.28 +static void
    1.29 +quit(int rc)
    1.30 +{
    1.31 +    SDLTest_CommonQuit(state);
    1.32 +    exit(rc);
    1.33 +}
    1.34 +
    1.35 +void
    1.36 +DrawOnViewport(SDL_Renderer * renderer, SDL_Rect viewport)
    1.37 +{    
    1.38 +    SDL_Rect rect;
    1.39 +
    1.40 +    /* Set the viewport */
    1.41 +    SDL_RenderSetViewport(renderer, &viewport);
    1.42 +    
    1.43 +    /* Draw a gray background */
    1.44 +    SDL_SetRenderDrawColor(renderer, 0x80, 0x80, 0x80, 0xFF);
    1.45 +    SDL_RenderClear(renderer);
    1.46 +
    1.47 +    /* Test inside points */
    1.48 +    SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xF, 0xFF);
    1.49 +    SDL_RenderDrawPoint(renderer, viewport.h/2 + 10, viewport.w/2);
    1.50 +    SDL_RenderDrawPoint(renderer, viewport.h/2 - 10, viewport.w/2);
    1.51 +    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 - 10);
    1.52 +    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 + 10);
    1.53 +
    1.54 +    /* Test horizontal and vertical lines */
    1.55 +    SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
    1.56 +    SDL_RenderDrawLine(renderer, 1, 0, viewport.w-2, 0);
    1.57 +    SDL_RenderDrawLine(renderer, 1, viewport.h-1, viewport.w-2, viewport.h-1);
    1.58 +    SDL_RenderDrawLine(renderer, 0, 1, 0, viewport.h-2);
    1.59 +    SDL_RenderDrawLine(renderer, viewport.w-1, 1, viewport.w-1, viewport.h-2);
    1.60 +
    1.61 +    /* Test diagonal lines */
    1.62 +    SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0xFF, 0xFF);
    1.63 +    SDL_RenderDrawLine(renderer, 0, 0,
    1.64 +                       viewport.w-1, viewport.h-1);
    1.65 +    SDL_RenderDrawLine(renderer, viewport.w-1, 0,
    1.66 +                       0, viewport.h-1);                      
    1.67 +
    1.68 +    /* Test outside points */
    1.69 +    SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xF, 0xFF);
    1.70 +    SDL_RenderDrawPoint(renderer, viewport.h/2 + viewport.h, viewport.w/2);
    1.71 +    SDL_RenderDrawPoint(renderer, viewport.h/2 - viewport.h, viewport.w/2);
    1.72 +    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 - viewport.w);
    1.73 +    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 + viewport.w);
    1.74 +
    1.75 +    /* Add a box at the top */
    1.76 +    rect.w = 8;
    1.77 +    rect.h = 8;
    1.78 +    rect.x = (viewport.w - rect.w) / 2;
    1.79 +    rect.y = 0;
    1.80 +    SDL_RenderFillRect(renderer, &rect);
    1.81 +}
    1.82 +
    1.83 +int
    1.84 +main(int argc, char *argv[])
    1.85 +{
    1.86 +    int i, j, done;
    1.87 +    SDL_Event event;
    1.88 +    Uint32 then, now, frames;
    1.89 +    SDL_Rect viewport;
    1.90 +    SDL_bool use_target = SDL_FALSE;
    1.91 +
    1.92 +    /* Initialize test framework */
    1.93 +    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
    1.94 +    if (!state) {
    1.95 +        return 1;
    1.96 +    }
    1.97 +
    1.98 +    for (i = 1; i < argc;) {
    1.99 +        int consumed;
   1.100 +
   1.101 +        consumed = SDLTest_CommonArg(state, i);
   1.102 +        if (consumed == 0) {
   1.103 +            consumed = -1;
   1.104 +            if (SDL_strcasecmp(argv[i], "--target") == 0) {
   1.105 +                use_target = SDL_TRUE;
   1.106 +                consumed = 1;
   1.107 +            }
   1.108 +        }
   1.109 +        if (consumed < 0) {
   1.110 +            SDL_Log("Usage: %s %s [--target]\n",
   1.111 +                    argv[0], SDLTest_CommonUsage(state));
   1.112 +            quit(1);
   1.113 +        }
   1.114 +        i += consumed;
   1.115 +    }
   1.116 +    if (!SDLTest_CommonInit(state)) {
   1.117 +        quit(2);
   1.118 +    }
   1.119 +
   1.120 +    if (use_target) {
   1.121 +        int w, h;
   1.122 +
   1.123 +        for (i = 0; i < state->num_windows; ++i) {
   1.124 +            SDL_GetWindowSize(state->windows[i], &w, &h);
   1.125 +            state->targets[i] = SDL_CreateTexture(state->renderers[i], SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
   1.126 +            SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
   1.127 +        }
   1.128 +    }
   1.129 +
   1.130 +    for (i = 0; i < state->num_windows; ++i) {
   1.131 +        SDL_Renderer *renderer = state->renderers[i];
   1.132 +        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
   1.133 +        SDL_RenderClear(renderer);
   1.134 +    }
   1.135 +
   1.136 +    /* Main render loop */
   1.137 +    frames = 0;
   1.138 +    then = SDL_GetTicks();
   1.139 +    done = 0;
   1.140 +    j = 0;
   1.141 +    while (!done) {
   1.142 +        /* Check for events */
   1.143 +        ++frames;
   1.144 +        while (SDL_PollEvent(&event)) {
   1.145 +            SDLTest_CommonEvent(state, &event, &done);
   1.146 +        }
   1.147 +        
   1.148 +        /* Move a viewport box in steps around the screen */                
   1.149 +        viewport.x = j * 100;
   1.150 +        viewport.y = viewport.x;
   1.151 +        viewport.w = 100 + j * 50;
   1.152 +        viewport.h = 100 + j * 50;
   1.153 +        j = (j + 1) % 4;            
   1.154 +        SDL_Log("Current Viewport x=%i y=%i w=%i h=%i", viewport.x, viewport.y, viewport.w, viewport.h);
   1.155 +        
   1.156 +        for (i = 0; i < state->num_windows; ++i) {
   1.157 +            if (state->windows[i] == NULL)
   1.158 +                continue;
   1.159 +                
   1.160 +            /* Draw using viewport */        
   1.161 +            DrawOnViewport(state->renderers[i], viewport);
   1.162 +
   1.163 +            /* Update the screen! */
   1.164 +            if (use_target) {
   1.165 +                SDL_SetRenderTarget(state->renderers[i], NULL);
   1.166 +                SDL_RenderCopy(state->renderers[i], state->targets[i], NULL, NULL);
   1.167 +                SDL_RenderPresent(state->renderers[i]);
   1.168 +                SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
   1.169 +            } else {
   1.170 +                SDL_RenderPresent(state->renderers[i]);
   1.171 +            }
   1.172 +        }
   1.173 +        
   1.174 +        SDL_Delay(1000);
   1.175 +    }
   1.176 +
   1.177 +    /* Print out some timing information */
   1.178 +    now = SDL_GetTicks();
   1.179 +    if (now > then) {
   1.180 +        double fps = ((double) frames * 1000) / (now - then);
   1.181 +        SDL_Log("%2.2f frames per second\n", fps);
   1.182 +    }
   1.183 +    quit(0);
   1.184 +    return 0;
   1.185 +}
   1.186 +
   1.187 +/* vi: set ts=4 sw=4 expandtab: */