test/testviewport.c
author Sam Lantinga <slouken@libsdl.org>
Thu, 07 Dec 2017 16:08:09 -0800
changeset 11730 ac6c607e065c
parent 10737 3406a0f8b041
child 11811 5d94cb6b24d3
permissions -rw-r--r--
Enable building the Metal renderer by default, and weak link the Metal framework so the SDL library is safe to use on older Macs
Also generate iOS versions of the Metal shaders
slouken@7994
     1
/*
slouken@10737
     2
  Copyright (C) 1997-2017 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
    }
philipp@9607
   132
philipp@9607
   133
#ifdef __EMSCRIPTEN__
philipp@9607
   134
    if (done) {
philipp@9607
   135
        emscripten_cancel_main_loop();
philipp@9607
   136
    }
philipp@9607
   137
#endif
icculus@9278
   138
}
icculus@9278
   139
slouken@7994
   140
int
slouken@7994
   141
main(int argc, char *argv[])
slouken@7994
   142
{
icculus@9278
   143
    int i;
slouken@7994
   144
    Uint32 then, now, frames;
slouken@7994
   145
slouken@7994
   146
    /* Initialize test framework */
slouken@7994
   147
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
slouken@7994
   148
    if (!state) {
slouken@7994
   149
        return 1;
slouken@7994
   150
    }
slouken@7994
   151
slouken@7994
   152
    for (i = 1; i < argc;) {
slouken@7994
   153
        int consumed;
slouken@7994
   154
slouken@7994
   155
        consumed = SDLTest_CommonArg(state, i);
slouken@7994
   156
        if (consumed == 0) {
slouken@7994
   157
            consumed = -1;
slouken@7994
   158
            if (SDL_strcasecmp(argv[i], "--target") == 0) {
slouken@7994
   159
                use_target = SDL_TRUE;
slouken@7994
   160
                consumed = 1;
slouken@7994
   161
            }
slouken@7994
   162
        }
slouken@7994
   163
        if (consumed < 0) {
slouken@7994
   164
            SDL_Log("Usage: %s %s [--target]\n",
slouken@7994
   165
                    argv[0], SDLTest_CommonUsage(state));
slouken@7994
   166
            quit(1);
slouken@7994
   167
        }
slouken@7994
   168
        i += consumed;
slouken@7994
   169
    }
slouken@7994
   170
    if (!SDLTest_CommonInit(state)) {
slouken@7994
   171
        quit(2);
slouken@7994
   172
    }
slouken@7994
   173
slouken@7994
   174
    if (use_target) {
slouken@7994
   175
        int w, h;
slouken@7994
   176
slouken@7994
   177
        for (i = 0; i < state->num_windows; ++i) {
slouken@7994
   178
            SDL_GetWindowSize(state->windows[i], &w, &h);
slouken@7994
   179
            state->targets[i] = SDL_CreateTexture(state->renderers[i], SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
slouken@7994
   180
            SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
slouken@7994
   181
        }
slouken@7994
   182
    }
slouken@7994
   183
slouken@7994
   184
    for (i = 0; i < state->num_windows; ++i) {
slouken@7994
   185
        SDL_Renderer *renderer = state->renderers[i];
slouken@7994
   186
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
slouken@7994
   187
        SDL_RenderClear(renderer);
slouken@7994
   188
    }
slouken@7994
   189
slouken@7994
   190
    /* Main render loop */
slouken@7994
   191
    frames = 0;
slouken@7994
   192
    then = SDL_GetTicks();
slouken@7994
   193
    done = 0;
slouken@7994
   194
    j = 0;
icculus@9278
   195
icculus@9278
   196
#ifdef __EMSCRIPTEN__
icculus@9278
   197
    wait_start = SDL_GetTicks();
icculus@9278
   198
    emscripten_set_main_loop(loop, 0, 1);
icculus@9278
   199
#else
slouken@7994
   200
    while (!done) {
slouken@7994
   201
        ++frames;
icculus@9278
   202
        loop();
slouken@7994
   203
        SDL_Delay(1000);
slouken@7994
   204
    }
icculus@9278
   205
#endif
slouken@7994
   206
slouken@7994
   207
    /* Print out some timing information */
slouken@7994
   208
    now = SDL_GetTicks();
slouken@7994
   209
    if (now > then) {
slouken@7994
   210
        double fps = ((double) frames * 1000) / (now - then);
slouken@7994
   211
        SDL_Log("%2.2f frames per second\n", fps);
slouken@7994
   212
    }
slouken@7994
   213
    quit(0);
slouken@7994
   214
    return 0;
slouken@7994
   215
}
slouken@7994
   216
slouken@7994
   217
/* vi: set ts=4 sw=4 expandtab: */