test/testviewport.c
author David Ludwig <dludwig@pobox.com>
Wed, 25 Dec 2013 21:39:48 -0500
changeset 8563 c0e68f3b6bbb
parent 7994 37c48b4ecb0c
child 8149 681eb46b8ac4
permissions -rw-r--r--
WinRT: compiled the d3d11 renderer's shaders into SDL itself

Previously, the shaders would get compiled separately, the output of which would need to be packaged into the app. This change should make SDL's dll be the only binary needed to include SDL in a WinRT app.
slouken@7994
     1
/*
slouken@7994
     2
  Copyright (C) 1997-2013 Sam Lantinga <slouken@libsdl.org>
slouken@7994
     3
slouken@7994
     4
  This software is provided 'as-is', without any express or implied
slouken@7994
     5
  warranty.  In no event will the authors be held liable for any damages
slouken@7994
     6
  arising from the use of this software.
slouken@7994
     7
slouken@7994
     8
  Permission is granted to anyone to use this software for any purpose,
slouken@7994
     9
  including commercial applications, and to alter it and redistribute it
slouken@7994
    10
  freely.
slouken@7994
    11
*/
slouken@7994
    12
/* Simple program:  Check viewports */
slouken@7994
    13
slouken@7994
    14
#include <stdlib.h>
slouken@7994
    15
#include <stdio.h>
slouken@7994
    16
#include <time.h>
slouken@7994
    17
slouken@7994
    18
#include "SDL_test.h"
slouken@7994
    19
#include "SDL_test_common.h"
slouken@7994
    20
slouken@7994
    21
slouken@7994
    22
static SDLTest_CommonState *state;
slouken@7994
    23
slouken@7994
    24
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@7994
    25
static void
slouken@7994
    26
quit(int rc)
slouken@7994
    27
{
slouken@7994
    28
    SDLTest_CommonQuit(state);
slouken@7994
    29
    exit(rc);
slouken@7994
    30
}
slouken@7994
    31
slouken@7994
    32
void
slouken@7994
    33
DrawOnViewport(SDL_Renderer * renderer, SDL_Rect viewport)
slouken@7994
    34
{    
slouken@7994
    35
    SDL_Rect rect;
slouken@7994
    36
slouken@7994
    37
    /* Set the viewport */
slouken@7994
    38
    SDL_RenderSetViewport(renderer, &viewport);
slouken@7994
    39
    
slouken@7994
    40
    /* Draw a gray background */
slouken@7994
    41
    SDL_SetRenderDrawColor(renderer, 0x80, 0x80, 0x80, 0xFF);
slouken@7994
    42
    SDL_RenderClear(renderer);
slouken@7994
    43
slouken@7994
    44
    /* Test inside points */
slouken@7994
    45
    SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xF, 0xFF);
slouken@7994
    46
    SDL_RenderDrawPoint(renderer, viewport.h/2 + 10, viewport.w/2);
slouken@7994
    47
    SDL_RenderDrawPoint(renderer, viewport.h/2 - 10, viewport.w/2);
slouken@7994
    48
    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 - 10);
slouken@7994
    49
    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 + 10);
slouken@7994
    50
slouken@7994
    51
    /* Test horizontal and vertical lines */
slouken@7994
    52
    SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
slouken@7994
    53
    SDL_RenderDrawLine(renderer, 1, 0, viewport.w-2, 0);
slouken@7994
    54
    SDL_RenderDrawLine(renderer, 1, viewport.h-1, viewport.w-2, viewport.h-1);
slouken@7994
    55
    SDL_RenderDrawLine(renderer, 0, 1, 0, viewport.h-2);
slouken@7994
    56
    SDL_RenderDrawLine(renderer, viewport.w-1, 1, viewport.w-1, viewport.h-2);
slouken@7994
    57
slouken@7994
    58
    /* Test diagonal lines */
slouken@7994
    59
    SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0xFF, 0xFF);
slouken@7994
    60
    SDL_RenderDrawLine(renderer, 0, 0,
slouken@7994
    61
                       viewport.w-1, viewport.h-1);
slouken@7994
    62
    SDL_RenderDrawLine(renderer, viewport.w-1, 0,
slouken@7994
    63
                       0, viewport.h-1);                      
slouken@7994
    64
slouken@7994
    65
    /* Test outside points */
slouken@7994
    66
    SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xF, 0xFF);
slouken@7994
    67
    SDL_RenderDrawPoint(renderer, viewport.h/2 + viewport.h, viewport.w/2);
slouken@7994
    68
    SDL_RenderDrawPoint(renderer, viewport.h/2 - viewport.h, viewport.w/2);
slouken@7994
    69
    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 - viewport.w);
slouken@7994
    70
    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 + viewport.w);
slouken@7994
    71
slouken@7994
    72
    /* Add a box at the top */
slouken@7994
    73
    rect.w = 8;
slouken@7994
    74
    rect.h = 8;
slouken@7994
    75
    rect.x = (viewport.w - rect.w) / 2;
slouken@7994
    76
    rect.y = 0;
slouken@7994
    77
    SDL_RenderFillRect(renderer, &rect);
slouken@7994
    78
}
slouken@7994
    79
slouken@7994
    80
int
slouken@7994
    81
main(int argc, char *argv[])
slouken@7994
    82
{
slouken@7994
    83
    int i, j, done;
slouken@7994
    84
    SDL_Event event;
slouken@7994
    85
    Uint32 then, now, frames;
slouken@7994
    86
    SDL_Rect viewport;
slouken@7994
    87
    SDL_bool use_target = SDL_FALSE;
slouken@7994
    88
slouken@7994
    89
    /* Initialize test framework */
slouken@7994
    90
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@7994
    91
    if (!state) {
slouken@7994
    92
        return 1;
slouken@7994
    93
    }
slouken@7994
    94
slouken@7994
    95
    for (i = 1; i < argc;) {
slouken@7994
    96
        int consumed;
slouken@7994
    97
slouken@7994
    98
        consumed = SDLTest_CommonArg(state, i);
slouken@7994
    99
        if (consumed == 0) {
slouken@7994
   100
            consumed = -1;
slouken@7994
   101
            if (SDL_strcasecmp(argv[i], "--target") == 0) {
slouken@7994
   102
                use_target = SDL_TRUE;
slouken@7994
   103
                consumed = 1;
slouken@7994
   104
            }
slouken@7994
   105
        }
slouken@7994
   106
        if (consumed < 0) {
slouken@7994
   107
            SDL_Log("Usage: %s %s [--target]\n",
slouken@7994
   108
                    argv[0], SDLTest_CommonUsage(state));
slouken@7994
   109
            quit(1);
slouken@7994
   110
        }
slouken@7994
   111
        i += consumed;
slouken@7994
   112
    }
slouken@7994
   113
    if (!SDLTest_CommonInit(state)) {
slouken@7994
   114
        quit(2);
slouken@7994
   115
    }
slouken@7994
   116
slouken@7994
   117
    if (use_target) {
slouken@7994
   118
        int w, h;
slouken@7994
   119
slouken@7994
   120
        for (i = 0; i < state->num_windows; ++i) {
slouken@7994
   121
            SDL_GetWindowSize(state->windows[i], &w, &h);
slouken@7994
   122
            state->targets[i] = SDL_CreateTexture(state->renderers[i], SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
slouken@7994
   123
            SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
slouken@7994
   124
        }
slouken@7994
   125
    }
slouken@7994
   126
slouken@7994
   127
    for (i = 0; i < state->num_windows; ++i) {
slouken@7994
   128
        SDL_Renderer *renderer = state->renderers[i];
slouken@7994
   129
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@7994
   130
        SDL_RenderClear(renderer);
slouken@7994
   131
    }
slouken@7994
   132
slouken@7994
   133
    /* Main render loop */
slouken@7994
   134
    frames = 0;
slouken@7994
   135
    then = SDL_GetTicks();
slouken@7994
   136
    done = 0;
slouken@7994
   137
    j = 0;
slouken@7994
   138
    while (!done) {
slouken@7994
   139
        /* Check for events */
slouken@7994
   140
        ++frames;
slouken@7994
   141
        while (SDL_PollEvent(&event)) {
slouken@7994
   142
            SDLTest_CommonEvent(state, &event, &done);
slouken@7994
   143
        }
slouken@7994
   144
        
slouken@7994
   145
        /* Move a viewport box in steps around the screen */                
slouken@7994
   146
        viewport.x = j * 100;
slouken@7994
   147
        viewport.y = viewport.x;
slouken@7994
   148
        viewport.w = 100 + j * 50;
slouken@7994
   149
        viewport.h = 100 + j * 50;
slouken@7994
   150
        j = (j + 1) % 4;            
slouken@7994
   151
        SDL_Log("Current Viewport x=%i y=%i w=%i h=%i", viewport.x, viewport.y, viewport.w, viewport.h);
slouken@7994
   152
        
slouken@7994
   153
        for (i = 0; i < state->num_windows; ++i) {
slouken@7994
   154
            if (state->windows[i] == NULL)
slouken@7994
   155
                continue;
slouken@7994
   156
                
slouken@7994
   157
            /* Draw using viewport */        
slouken@7994
   158
            DrawOnViewport(state->renderers[i], viewport);
slouken@7994
   159
slouken@7994
   160
            /* Update the screen! */
slouken@7994
   161
            if (use_target) {
slouken@7994
   162
                SDL_SetRenderTarget(state->renderers[i], NULL);
slouken@7994
   163
                SDL_RenderCopy(state->renderers[i], state->targets[i], NULL, NULL);
slouken@7994
   164
                SDL_RenderPresent(state->renderers[i]);
slouken@7994
   165
                SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
slouken@7994
   166
            } else {
slouken@7994
   167
                SDL_RenderPresent(state->renderers[i]);
slouken@7994
   168
            }
slouken@7994
   169
        }
slouken@7994
   170
        
slouken@7994
   171
        SDL_Delay(1000);
slouken@7994
   172
    }
slouken@7994
   173
slouken@7994
   174
    /* Print out some timing information */
slouken@7994
   175
    now = SDL_GetTicks();
slouken@7994
   176
    if (now > then) {
slouken@7994
   177
        double fps = ((double) frames * 1000) / (now - then);
slouken@7994
   178
        SDL_Log("%2.2f frames per second\n", fps);
slouken@7994
   179
    }
slouken@7994
   180
    quit(0);
slouken@7994
   181
    return 0;
slouken@7994
   182
}
slouken@7994
   183
slouken@7994
   184
/* vi: set ts=4 sw=4 expandtab: */