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