SDL  2.0
testviewport.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 1997-2018 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 
27 
29 int done, j;
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 {
40  exit(rc);
41 }
42 
43 void
45 {
46  SDL_Rect rect;
47 
48  /* Set the viewport */
50 
51  /* Draw a gray background */
52  SDL_SetRenderDrawColor(renderer, 0x80, 0x80, 0x80, 0xFF);
54 
55  /* Test inside points */
56  SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xF, 0xFF);
61 
62  /* Test horizontal and vertical lines */
63  SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
68 
69  /* Test diagonal lines */
70  SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0xFF, 0xFF);
72  viewport.w-1, viewport.h-1);
74  0, viewport.h-1);
75 
76  /* Test outside points */
77  SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xF, 0xFF);
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;
89 }
90 
91 void
93 {
94 #ifdef __EMSCRIPTEN__
95  /* Avoid using delays */
96  if(SDL_GetTicks() - wait_start < 1000)
97  return;
98  wait_start = SDL_GetTicks();
99 #endif
101  int i;
102  /* Check for events */
103  while (SDL_PollEvent(&event)) {
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 */
121 
122  /* Update the screen! */
123  if (use_target) {
128  } else {
130  }
131  }
132 
133 #ifdef __EMSCRIPTEN__
134  if (done) {
135  emscripten_cancel_main_loop();
136  }
137 #endif
138 }
139 
140 int
141 main(int argc, char *argv[])
142 {
143  int i;
144  Uint32 then, now, frames;
145 
146  /* Initialize test framework */
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) {
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) {
181  }
182  }
183 
184  for (i = 0; i < state->num_windows; ++i) {
186  SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
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: */
SDLTest_CommonState::windows
SDL_Window ** windows
Definition: SDL_test_common.h:78
SDL_RenderPresent
#define SDL_RenderPresent
Definition: SDL_dynapi_overrides.h:346
SDL_SetRenderTarget
#define SDL_SetRenderTarget
Definition: SDL_dynapi_overrides.h:320
SDLTest_CommonState::targets
SDL_Texture ** targets
Definition: SDL_test_common.h:85
SDL_RenderDrawPoint
#define SDL_RenderDrawPoint
Definition: SDL_dynapi_overrides.h:335
SDL_RenderSetViewport
#define SDL_RenderSetViewport
Definition: SDL_dynapi_overrides.h:324
SDL_PollEvent
#define SDL_PollEvent
Definition: SDL_dynapi_overrides.h:122
SDL_test.h
NULL
#define NULL
Definition: begin_code.h:164
SDLTest_CommonState
Definition: SDL_test_common.h:52
SDL_RenderFillRect
#define SDL_RenderFillRect
Definition: SDL_dynapi_overrides.h:341
viewport
SDL_Rect viewport
Definition: testviewport.c:28
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:203
loop
void loop()
Definition: testviewport.c:92
h
GLfloat GLfloat GLfloat GLfloat h
Definition: SDL_opengl_glext.h:1946
SDL_Rect::x
int x
Definition: SDL_rect.h:66
SDLTest_CommonState::renderers
SDL_Renderer ** renderers
Definition: SDL_test_common.h:84
SDL_Rect::w
int w
Definition: SDL_rect.h:67
SDLTest_CommonCreateState
SDLTest_CommonState * SDLTest_CommonCreateState(char **argv, Uint32 flags)
Parse command line parameters and create common state.
Definition: SDL_test_common.c:48
SDL_strcasecmp
#define SDL_strcasecmp
Definition: SDL_dynapi_overrides.h:419
SDLTest_CommonQuit
void SDLTest_CommonQuit(SDLTest_CommonState *state)
Close test window.
Definition: SDL_test_common.c:1794
SDL_GetWindowSize
#define SDL_GetWindowSize
Definition: SDL_dynapi_overrides.h:527
SDLTest_CommonState::num_windows
int num_windows
Definition: SDL_test_common.h:77
done
int done
Definition: testviewport.c:29
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
SDL_Renderer
Definition: SDL_sysrender.h:86
SDL_RenderCopy
#define SDL_RenderCopy
Definition: SDL_dynapi_overrides.h:343
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:31
SDL_test_common.h
SDL_Rect::y
int y
Definition: SDL_rect.h:66
SDL_Rect::h
int h
Definition: SDL_rect.h:67
SDLTest_CommonArg
int SDLTest_CommonArg(SDLTest_CommonState *state, int index)
Process one common argument.
Definition: SDL_test_common.c:106
rect
SDL_Rect rect
Definition: testrelative.c:27
SDL_GetTicks
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
j
int j
Definition: testviewport.c:29
SDL_TRUE
@ SDL_TRUE
Definition: SDL_stdinc.h:164
use_target
SDL_bool use_target
Definition: testviewport.c:30
SDL_Delay
#define SDL_Delay
Definition: SDL_dynapi_overrides.h:486
SDL_PIXELFORMAT_RGBA8888
@ SDL_PIXELFORMAT_RGBA8888
Definition: SDL_pixels.h:251
SDLTest_CommonInit
SDL_bool SDLTest_CommonInit(SDLTest_CommonState *state)
Open test window.
Definition: SDL_test_common.c:726
SDL_INIT_VIDEO
#define SDL_INIT_VIDEO
Definition: SDL.h:79
main
int main(int argc, char *argv[])
Definition: testviewport.c:141
renderer
static SDL_Renderer * renderer
Definition: testaudiocapture.c:21
SDL_Rect
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:65
SDL_TEXTUREACCESS_TARGET
@ SDL_TEXTUREACCESS_TARGET
Definition: SDL_render.h:95
SDL_RenderClear
#define SDL_RenderClear
Definition: SDL_dynapi_overrides.h:334
SDL_SetRenderDrawColor
#define SDL_SetRenderDrawColor
Definition: SDL_dynapi_overrides.h:330
DrawOnViewport
void DrawOnViewport(SDL_Renderer *renderer, SDL_Rect viewport)
Definition: testviewport.c:44
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:162
SDL_Event
General event structure.
Definition: SDL_events.h:558
SDL_FALSE
@ SDL_FALSE
Definition: SDL_stdinc.h:163
SDLTest_CommonUsage
const char * SDLTest_CommonUsage(SDLTest_CommonState *state)
Returns common usage information.
Definition: SDL_test_common.c:478
SDLTest_CommonEvent
void SDLTest_CommonEvent(SDLTest_CommonState *state, SDL_Event *event, int *done)
Common event handler for test windows.
Definition: SDL_test_common.c:1463
SDL_RenderDrawLine
#define SDL_RenderDrawLine
Definition: SDL_dynapi_overrides.h:337
state
static SDLTest_CommonState * state
Definition: testviewport.c:26
quit
static void quit(int rc)
Definition: testviewport.c:37
i
return Display return Display Bool Bool int int int return Display XEvent Bool(*) XPointer return Display return Display Drawable _Xconst char unsigned int unsigned int return Display Pixmap Pixmap XColor XColor unsigned int unsigned int return Display _Xconst char char int char return Display Visual unsigned int int int char unsigned int unsigned int in i)
Definition: SDL_x11sym.h:50
SDL_CreateTexture
#define SDL_CreateTexture
Definition: SDL_dynapi_overrides.h:306
w
GLubyte GLubyte GLubyte GLubyte w
Definition: SDL_opengl_glext.h:731