test/testviewport.c
author Philipp Wiesemann <philipp.wiesemann@arcor.de>
Wed, 17 Aug 2016 21:04:50 +0200
changeset 10275 ba7dc3ebb81e
parent 9998 f67cf37e9cd4
permissions -rw-r--r--
Emscripten: Fixed opening previously closed joystick.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
7994
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     1
/*
9998
f67cf37e9cd4 Updated copyright to 2016
Sam Lantinga <slouken@libsdl.org>
parents: 9619
diff changeset
     2
  Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
7994
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     3
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     4
  This software is provided 'as-is', without any express or implied
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     5
  warranty.  In no event will the authors be held liable for any damages
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     6
  arising from the use of this software.
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     7
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     8
  Permission is granted to anyone to use this software for any purpose,
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
     9
  including commercial applications, and to alter it and redistribute it
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    10
  freely.
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    11
*/
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    12
/* Simple program:  Check viewports */
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    13
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    14
#include <stdlib.h>
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    15
#include <stdio.h>
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    16
#include <time.h>
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    17
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    18
#ifdef __EMSCRIPTEN__
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    19
#include <emscripten/emscripten.h>
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    20
#endif
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    21
7994
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    22
#include "SDL_test.h"
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    23
#include "SDL_test_common.h"
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    24
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    25
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    26
static SDLTest_CommonState *state;
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    27
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    28
SDL_Rect viewport;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    29
int done, j;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    30
SDL_bool use_target = SDL_FALSE;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    31
#ifdef __EMSCRIPTEN__
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    32
Uint32 wait_start;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    33
#endif
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    34
7994
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    35
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    36
static void
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    37
quit(int rc)
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    38
{
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    39
    SDLTest_CommonQuit(state);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    40
    exit(rc);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    41
}
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    42
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    43
void
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    44
DrawOnViewport(SDL_Renderer * renderer, SDL_Rect viewport)
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    45
{    
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    46
    SDL_Rect rect;
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    47
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    48
    /* Set the viewport */
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    49
    SDL_RenderSetViewport(renderer, &viewport);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    50
    
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    51
    /* Draw a gray background */
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    52
    SDL_SetRenderDrawColor(renderer, 0x80, 0x80, 0x80, 0xFF);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    53
    SDL_RenderClear(renderer);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    54
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    55
    /* Test inside points */
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    56
    SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xF, 0xFF);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    57
    SDL_RenderDrawPoint(renderer, viewport.h/2 + 10, viewport.w/2);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    58
    SDL_RenderDrawPoint(renderer, viewport.h/2 - 10, viewport.w/2);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    59
    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 - 10);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    60
    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 + 10);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    61
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    62
    /* Test horizontal and vertical lines */
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    63
    SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    64
    SDL_RenderDrawLine(renderer, 1, 0, viewport.w-2, 0);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    65
    SDL_RenderDrawLine(renderer, 1, viewport.h-1, viewport.w-2, viewport.h-1);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    66
    SDL_RenderDrawLine(renderer, 0, 1, 0, viewport.h-2);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    67
    SDL_RenderDrawLine(renderer, viewport.w-1, 1, viewport.w-1, viewport.h-2);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    68
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    69
    /* Test diagonal lines */
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    70
    SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0xFF, 0xFF);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    71
    SDL_RenderDrawLine(renderer, 0, 0,
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    72
                       viewport.w-1, viewport.h-1);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    73
    SDL_RenderDrawLine(renderer, viewport.w-1, 0,
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    74
                       0, viewport.h-1);                      
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    75
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    76
    /* Test outside points */
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    77
    SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xF, 0xFF);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    78
    SDL_RenderDrawPoint(renderer, viewport.h/2 + viewport.h, viewport.w/2);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    79
    SDL_RenderDrawPoint(renderer, viewport.h/2 - viewport.h, viewport.w/2);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    80
    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 - viewport.w);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    81
    SDL_RenderDrawPoint(renderer, viewport.h/2     , viewport.w/2 + viewport.w);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    82
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    83
    /* Add a box at the top */
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    84
    rect.w = 8;
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    85
    rect.h = 8;
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    86
    rect.x = (viewport.w - rect.w) / 2;
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    87
    rect.y = 0;
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    88
    SDL_RenderFillRect(renderer, &rect);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    89
}
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
    90
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    91
void
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    92
loop()
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    93
{
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    94
#ifdef __EMSCRIPTEN__
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    95
    /* Avoid using delays */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    96
    if(SDL_GetTicks() - wait_start < 1000)
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    97
        return;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    98
    wait_start = SDL_GetTicks();
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
    99
#endif
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   100
    SDL_Event event;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   101
    int i;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   102
    /* Check for events */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   103
    while (SDL_PollEvent(&event)) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   104
        SDLTest_CommonEvent(state, &event, &done);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   105
    }
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   106
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   107
    /* Move a viewport box in steps around the screen */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   108
    viewport.x = j * 100;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   109
    viewport.y = viewport.x;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   110
    viewport.w = 100 + j * 50;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   111
    viewport.h = 100 + j * 50;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   112
    j = (j + 1) % 4;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   113
    SDL_Log("Current Viewport x=%i y=%i w=%i h=%i", viewport.x, viewport.y, viewport.w, viewport.h);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   114
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   115
    for (i = 0; i < state->num_windows; ++i) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   116
        if (state->windows[i] == NULL)
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   117
            continue;
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   118
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   119
        /* Draw using viewport */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   120
        DrawOnViewport(state->renderers[i], viewport);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   121
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   122
        /* Update the screen! */
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   123
        if (use_target) {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   124
            SDL_SetRenderTarget(state->renderers[i], NULL);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   125
            SDL_RenderCopy(state->renderers[i], state->targets[i], NULL, NULL);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   126
            SDL_RenderPresent(state->renderers[i]);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   127
            SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   128
        } else {
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   129
            SDL_RenderPresent(state->renderers[i]);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   130
        }
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   131
    }
9607
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   132
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   133
#ifdef __EMSCRIPTEN__
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   134
    if (done) {
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   135
        emscripten_cancel_main_loop();
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   136
    }
7746ab058d12 Added missing loop cancel for Emscripten in test programs.
Philipp Wiesemann <philipp.wiesemann@arcor.de>
parents: 9278
diff changeset
   137
#endif
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   138
}
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   139
7994
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   140
int
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   141
main(int argc, char *argv[])
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   142
{
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   143
    int i;
7994
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   144
    Uint32 then, now, frames;
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   145
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   146
    /* Initialize test framework */
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   147
    state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   148
    if (!state) {
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   149
        return 1;
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   150
    }
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   151
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   152
    for (i = 1; i < argc;) {
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   153
        int consumed;
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   154
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   155
        consumed = SDLTest_CommonArg(state, i);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   156
        if (consumed == 0) {
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   157
            consumed = -1;
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   158
            if (SDL_strcasecmp(argv[i], "--target") == 0) {
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   159
                use_target = SDL_TRUE;
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   160
                consumed = 1;
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   161
            }
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   162
        }
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   163
        if (consumed < 0) {
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   164
            SDL_Log("Usage: %s %s [--target]\n",
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   165
                    argv[0], SDLTest_CommonUsage(state));
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   166
            quit(1);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   167
        }
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   168
        i += consumed;
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   169
    }
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   170
    if (!SDLTest_CommonInit(state)) {
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   171
        quit(2);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   172
    }
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   173
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   174
    if (use_target) {
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   175
        int w, h;
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   176
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   177
        for (i = 0; i < state->num_windows; ++i) {
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   178
            SDL_GetWindowSize(state->windows[i], &w, &h);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   179
            state->targets[i] = SDL_CreateTexture(state->renderers[i], SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   180
            SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   181
        }
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   182
    }
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   183
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   184
    for (i = 0; i < state->num_windows; ++i) {
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   185
        SDL_Renderer *renderer = state->renderers[i];
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   186
        SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   187
        SDL_RenderClear(renderer);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   188
    }
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   189
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   190
    /* Main render loop */
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   191
    frames = 0;
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   192
    then = SDL_GetTicks();
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   193
    done = 0;
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   194
    j = 0;
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   195
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   196
#ifdef __EMSCRIPTEN__
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   197
    wait_start = SDL_GetTicks();
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   198
    emscripten_set_main_loop(loop, 0, 1);
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   199
#else
7994
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   200
    while (!done) {
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   201
        ++frames;
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   202
        loop();
7994
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   203
        SDL_Delay(1000);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   204
    }
9278
8900afb78a19 Initial merge of Emscripten port!
Ryan C. Gordon <icculus@icculus.org>
parents: 8149
diff changeset
   205
#endif
7994
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   206
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   207
    /* Print out some timing information */
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   208
    now = SDL_GetTicks();
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   209
    if (now > then) {
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   210
        double fps = ((double) frames * 1000) / (now - then);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   211
        SDL_Log("%2.2f frames per second\n", fps);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   212
    }
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   213
    quit(0);
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   214
    return 0;
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   215
}
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   216
37c48b4ecb0c Added viewport test, contributed by Andreas Schiffler and improved by me and Ryan Gordon.
Sam Lantinga <slouken@libsdl.org>
parents:
diff changeset
   217
/* vi: set ts=4 sw=4 expandtab: */