1 /*
2 Copyright (C) 1997-2016 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
quit(int rc)37 quit(int rc)
38 {
39 SDLTest_CommonQuit(state);
40 exit(rc);
41 }
42
43 void
DrawOnViewport(SDL_Renderer * renderer,SDL_Rect viewport)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
loop()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 #ifdef __EMSCRIPTEN__
134 if (done) {
135 emscripten_cancel_main_loop();
136 }
137 #endif
138 }
139
140 int
main(int argc,char * argv[])141 main(int argc, char *argv[])
142 {
143 int i;
144 Uint32 then, now, frames;
145
146 /* Initialize test framework */
147 state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
148 if (!state) {
149 return 1;
150 }
151
152 for (i = 1; i < argc;) {
153 int consumed;
154
155 consumed = SDLTest_CommonArg(state, i);
156 if (consumed == 0) {
157 consumed = -1;
158 if (SDL_strcasecmp(argv[i], "--target") == 0) {
159 use_target = SDL_TRUE;
160 consumed = 1;
161 }
162 }
163 if (consumed < 0) {
164 SDL_Log("Usage: %s %s [--target]\n",
165 argv[0], SDLTest_CommonUsage(state));
166 quit(1);
167 }
168 i += consumed;
169 }
170 if (!SDLTest_CommonInit(state)) {
171 quit(2);
172 }
173
174 if (use_target) {
175 int w, h;
176
177 for (i = 0; i < state->num_windows; ++i) {
178 SDL_GetWindowSize(state->windows[i], &w, &h);
179 state->targets[i] = SDL_CreateTexture(state->renderers[i], SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
180 SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
181 }
182 }
183
184 for (i = 0; i < state->num_windows; ++i) {
185 SDL_Renderer *renderer = state->renderers[i];
186 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
187 SDL_RenderClear(renderer);
188 }
189
190 /* Main render loop */
191 frames = 0;
192 then = SDL_GetTicks();
193 done = 0;
194 j = 0;
195
196 #ifdef __EMSCRIPTEN__
197 wait_start = SDL_GetTicks();
198 emscripten_set_main_loop(loop, 0, 1);
199 #else
200 while (!done) {
201 ++frames;
202 loop();
203 SDL_Delay(1000);
204 }
205 #endif
206
207 /* Print out some timing information */
208 now = SDL_GetTicks();
209 if (now > then) {
210 double fps = ((double) frames * 1000) / (now - then);
211 SDL_Log("%2.2f frames per second\n", fps);
212 }
213 quit(0);
214 return 0;
215 }
216
217 /* vi: set ts=4 sw=4 expandtab: */
218