SDL 2.0
testviewport.c
Go to the documentation of this file.
1/*
2 Copyright (C) 1997-2019 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
29int done, j;
31#ifdef __EMSCRIPTEN__
32Uint32 wait_start;
33#endif
34
35/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
36static void
37quit(int rc)
38{
40 exit(rc);
41}
42
43void
45{
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
91void
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;
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
140int
141main(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 static const char *options[] = { "[--target]", NULL };
165 SDLTest_CommonLogUsage(state, argv[0], options);
166 quit(1);
167 }
168 i += consumed;
169 }
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: */
#define SDL_INIT_VIDEO
Definition: SDL.h:79
#define SDL_RenderPresent
#define SDL_GetWindowSize
#define SDL_PollEvent
#define SDL_RenderFillRect
#define SDL_SetRenderDrawColor
#define SDL_RenderDrawLine
#define SDL_CreateTexture
#define SDL_SetRenderTarget
#define SDL_strcasecmp
#define SDL_RenderDrawPoint
#define SDL_RenderClear
#define SDL_Delay
#define SDL_RenderCopy
#define SDL_Log
#define SDL_RenderSetViewport
struct _cl_event * event
GLfloat GLfloat GLfloat GLfloat h
GLubyte GLubyte GLubyte GLubyte w
@ SDL_PIXELFORMAT_RGBA8888
Definition: SDL_pixels.h:251
@ SDL_TEXTUREACCESS_TARGET
Definition: SDL_render.h:95
SDL_bool
Definition: SDL_stdinc.h:162
@ SDL_TRUE
Definition: SDL_stdinc.h:164
@ SDL_FALSE
Definition: SDL_stdinc.h:163
uint32_t Uint32
Definition: SDL_stdinc.h:203
void SDLTest_CommonQuit(SDLTest_CommonState *state)
Close test window.
SDLTest_CommonState * SDLTest_CommonCreateState(char **argv, Uint32 flags)
Parse command line parameters and create common state.
void SDLTest_CommonEvent(SDLTest_CommonState *state, SDL_Event *event, int *done)
Common event handler for test windows.
int SDLTest_CommonArg(SDLTest_CommonState *state, int index)
Process one common argument.
SDL_bool SDLTest_CommonInit(SDLTest_CommonState *state)
Open test window.
void SDLTest_CommonLogUsage(SDLTest_CommonState *state, const char *argv0, const char **options)
Logs command line usage info.
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
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
#define NULL
Definition: begin_code.h:167
A rectangle, with the origin at the upper left (integer).
Definition: SDL_rect.h:78
int h
Definition: SDL_rect.h:80
int w
Definition: SDL_rect.h:80
int y
Definition: SDL_rect.h:79
int x
Definition: SDL_rect.h:79
SDL_Window ** windows
SDL_Texture ** targets
SDL_Renderer ** renderers
static SDL_Renderer * renderer
SDL_Rect rect
Definition: testrelative.c:27
static Uint32 frames
Definition: testsprite2.c:40
int main(int argc, char *argv[])
Definition: testviewport.c:141
static void quit(int rc)
Definition: testviewport.c:37
int j
Definition: testviewport.c:29
SDL_Rect viewport
Definition: testviewport.c:28
SDL_bool use_target
Definition: testviewport.c:30
int done
Definition: testviewport.c:29
void DrawOnViewport(SDL_Renderer *renderer, SDL_Rect viewport)
Definition: testviewport.c:44
static SDLTest_CommonState * state
Definition: testviewport.c:26
void loop()
Definition: testviewport.c:92
General event structure.
Definition: SDL_events.h:558