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