test/testviewport.c
author Alex Szpakowski <slime73@gmail.com>
Wed, 06 May 2015 12:54:51 -0300
changeset 9589 a583c42c51d7
parent 9278 8900afb78a19
child 9607 7746ab058d12
permissions -rw-r--r--
Fixed SDL_GL_GetAttribute queries for framebuffer component sizes in Core Profile OpenGL contexts.

Fixes bugzilla #2060.
slouken@7994
     1
/*
slouken@8149
     2
  Copyright (C) 1997-2014 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
icculus@9278
    18
#ifdef __EMSCRIPTEN__
icculus@9278
    19
#include <emscripten/emscripten.h>
icculus@9278
    20
#endif
icculus@9278
    21
slouken@7994
    22
#include "SDL_test.h"
slouken@7994
    23
#include "SDL_test_common.h"
slouken@7994
    24
slouken@7994
    25
slouken@7994
    26
static SDLTest_CommonState *state;
slouken@7994
    27
icculus@9278
    28
SDL_Rect viewport;
icculus@9278
    29
int done, j;
icculus@9278
    30
SDL_bool use_target = SDL_FALSE;
icculus@9278
    31
#ifdef __EMSCRIPTEN__
icculus@9278
    32
Uint32 wait_start;
icculus@9278
    33
#endif
icculus@9278
    34
slouken@7994
    35
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
slouken@7994
    36
static void
slouken@7994
    37
quit(int rc)
slouken@7994
    38
{
slouken@7994
    39
    SDLTest_CommonQuit(state);
slouken@7994
    40
    exit(rc);
slouken@7994
    41
}
slouken@7994
    42
slouken@7994
    43
void
slouken@7994
    44
DrawOnViewport(SDL_Renderer * renderer, SDL_Rect viewport)
slouken@7994
    45
{    
slouken@7994
    46
    SDL_Rect rect;
slouken@7994
    47
slouken@7994
    48
    /* Set the viewport */
slouken@7994
    49
    SDL_RenderSetViewport(renderer, &viewport);
slouken@7994
    50
    
slouken@7994
    51
    /* Draw a gray background */
slouken@7994
    52
    SDL_SetRenderDrawColor(renderer, 0x80, 0x80, 0x80, 0xFF);
slouken@7994
    53
    SDL_RenderClear(renderer);
slouken@7994
    54
slouken@7994
    55
    /* Test inside points */
slouken@7994
    56
    SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xF, 0xFF);
slouken@7994
    57
    SDL_RenderDrawPoint(renderer, viewport.h/2 + 10, viewport.w/2);
slouken@7994
    58
    SDL_RenderDrawPoint(renderer, viewport.h/2 - 10, viewport.w/2);
slouken@7994
    59
    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 - 10);
slouken@7994
    60
    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 + 10);
slouken@7994
    61
slouken@7994
    62
    /* Test horizontal and vertical lines */
slouken@7994
    63
    SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
slouken@7994
    64
    SDL_RenderDrawLine(renderer, 1, 0, viewport.w-2, 0);
slouken@7994
    65
    SDL_RenderDrawLine(renderer, 1, viewport.h-1, viewport.w-2, viewport.h-1);
slouken@7994
    66
    SDL_RenderDrawLine(renderer, 0, 1, 0, viewport.h-2);
slouken@7994
    67
    SDL_RenderDrawLine(renderer, viewport.w-1, 1, viewport.w-1, viewport.h-2);
slouken@7994
    68
slouken@7994
    69
    /* Test diagonal lines */
slouken@7994
    70
    SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0xFF, 0xFF);
slouken@7994
    71
    SDL_RenderDrawLine(renderer, 0, 0,
slouken@7994
    72
                       viewport.w-1, viewport.h-1);
slouken@7994
    73
    SDL_RenderDrawLine(renderer, viewport.w-1, 0,
slouken@7994
    74
                       0, viewport.h-1);                      
slouken@7994
    75
slouken@7994
    76
    /* Test outside points */
slouken@7994
    77
    SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xF, 0xFF);
slouken@7994
    78
    SDL_RenderDrawPoint(renderer, viewport.h/2 + viewport.h, viewport.w/2);
slouken@7994
    79
    SDL_RenderDrawPoint(renderer, viewport.h/2 - viewport.h, viewport.w/2);
slouken@7994
    80
    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 - viewport.w);
slouken@7994
    81
    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 + viewport.w);
slouken@7994
    82
slouken@7994
    83
    /* Add a box at the top */
slouken@7994
    84
    rect.w = 8;
slouken@7994
    85
    rect.h = 8;
slouken@7994
    86
    rect.x = (viewport.w - rect.w) / 2;
slouken@7994
    87
    rect.y = 0;
slouken@7994
    88
    SDL_RenderFillRect(renderer, &rect);
slouken@7994
    89
}
slouken@7994
    90
icculus@9278
    91
void
icculus@9278
    92
loop()
icculus@9278
    93
{
icculus@9278
    94
#ifdef __EMSCRIPTEN__
icculus@9278
    95
    /* Avoid using delays */
icculus@9278
    96
    if(SDL_GetTicks() - wait_start < 1000)
icculus@9278
    97
        return;
icculus@9278
    98
    wait_start = SDL_GetTicks();
icculus@9278
    99
#endif
icculus@9278
   100
    SDL_Event event;
icculus@9278
   101
    int i;
icculus@9278
   102
    /* Check for events */
icculus@9278
   103
    while (SDL_PollEvent(&event)) {
icculus@9278
   104
        SDLTest_CommonEvent(state, &event, &done);
icculus@9278
   105
    }
icculus@9278
   106
icculus@9278
   107
    /* Move a viewport box in steps around the screen */
icculus@9278
   108
    viewport.x = j * 100;
icculus@9278
   109
    viewport.y = viewport.x;
icculus@9278
   110
    viewport.w = 100 + j * 50;
icculus@9278
   111
    viewport.h = 100 + j * 50;
icculus@9278
   112
    j = (j + 1) % 4;
icculus@9278
   113
    SDL_Log("Current Viewport x=%i y=%i w=%i h=%i", viewport.x, viewport.y, viewport.w, viewport.h);
icculus@9278
   114
icculus@9278
   115
    for (i = 0; i < state->num_windows; ++i) {
icculus@9278
   116
        if (state->windows[i] == NULL)
icculus@9278
   117
            continue;
icculus@9278
   118
icculus@9278
   119
        /* Draw using viewport */
icculus@9278
   120
        DrawOnViewport(state->renderers[i], viewport);
icculus@9278
   121
icculus@9278
   122
        /* Update the screen! */
icculus@9278
   123
        if (use_target) {
icculus@9278
   124
            SDL_SetRenderTarget(state->renderers[i], NULL);
icculus@9278
   125
            SDL_RenderCopy(state->renderers[i], state->targets[i], NULL, NULL);
icculus@9278
   126
            SDL_RenderPresent(state->renderers[i]);
icculus@9278
   127
            SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
icculus@9278
   128
        } else {
icculus@9278
   129
            SDL_RenderPresent(state->renderers[i]);
icculus@9278
   130
        }
icculus@9278
   131
    }
icculus@9278
   132
}
icculus@9278
   133
slouken@7994
   134
int
slouken@7994
   135
main(int argc, char *argv[])
slouken@7994
   136
{
icculus@9278
   137
    int i;
slouken@7994
   138
    Uint32 then, now, frames;
slouken@7994
   139
slouken@7994
   140
    /* Initialize test framework */
slouken@7994
   141
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@7994
   142
    if (!state) {
slouken@7994
   143
        return 1;
slouken@7994
   144
    }
slouken@7994
   145
slouken@7994
   146
    for (i = 1; i < argc;) {
slouken@7994
   147
        int consumed;
slouken@7994
   148
slouken@7994
   149
        consumed = SDLTest_CommonArg(state, i);
slouken@7994
   150
        if (consumed == 0) {
slouken@7994
   151
            consumed = -1;
slouken@7994
   152
            if (SDL_strcasecmp(argv[i], "--target") == 0) {
slouken@7994
   153
                use_target = SDL_TRUE;
slouken@7994
   154
                consumed = 1;
slouken@7994
   155
            }
slouken@7994
   156
        }
slouken@7994
   157
        if (consumed < 0) {
slouken@7994
   158
            SDL_Log("Usage: %s %s [--target]\n",
slouken@7994
   159
                    argv[0], SDLTest_CommonUsage(state));
slouken@7994
   160
            quit(1);
slouken@7994
   161
        }
slouken@7994
   162
        i += consumed;
slouken@7994
   163
    }
slouken@7994
   164
    if (!SDLTest_CommonInit(state)) {
slouken@7994
   165
        quit(2);
slouken@7994
   166
    }
slouken@7994
   167
slouken@7994
   168
    if (use_target) {
slouken@7994
   169
        int w, h;
slouken@7994
   170
slouken@7994
   171
        for (i = 0; i < state->num_windows; ++i) {
slouken@7994
   172
            SDL_GetWindowSize(state->windows[i], &w, &h);
slouken@7994
   173
            state->targets[i] = SDL_CreateTexture(state->renderers[i], SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
slouken@7994
   174
            SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
slouken@7994
   175
        }
slouken@7994
   176
    }
slouken@7994
   177
slouken@7994
   178
    for (i = 0; i < state->num_windows; ++i) {
slouken@7994
   179
        SDL_Renderer *renderer = state->renderers[i];
slouken@7994
   180
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@7994
   181
        SDL_RenderClear(renderer);
slouken@7994
   182
    }
slouken@7994
   183
slouken@7994
   184
    /* Main render loop */
slouken@7994
   185
    frames = 0;
slouken@7994
   186
    then = SDL_GetTicks();
slouken@7994
   187
    done = 0;
slouken@7994
   188
    j = 0;
icculus@9278
   189
icculus@9278
   190
#ifdef __EMSCRIPTEN__
icculus@9278
   191
    wait_start = SDL_GetTicks();
icculus@9278
   192
    emscripten_set_main_loop(loop, 0, 1);
icculus@9278
   193
#else
slouken@7994
   194
    while (!done) {
slouken@7994
   195
        ++frames;
icculus@9278
   196
        loop();
slouken@7994
   197
        SDL_Delay(1000);
slouken@7994
   198
    }
icculus@9278
   199
#endif
slouken@7994
   200
slouken@7994
   201
    /* Print out some timing information */
slouken@7994
   202
    now = SDL_GetTicks();
slouken@7994
   203
    if (now > then) {
slouken@7994
   204
        double fps = ((double) frames * 1000) / (now - then);
slouken@7994
   205
        SDL_Log("%2.2f frames per second\n", fps);
slouken@7994
   206
    }
slouken@7994
   207
    quit(0);
slouken@7994
   208
    return 0;
slouken@7994
   209
}
slouken@7994
   210
slouken@7994
   211
/* vi: set ts=4 sw=4 expandtab: */