testviewport.c File Reference
`#include <stdlib.h>`
`#include <stdio.h>`
`#include <time.h>`
`#include "SDL_test.h"`
`#include "SDL_test_common.h"`
Include dependency graph for testviewport.c:

Go to the source code of this file.

## Functions

static void | quit (int rc)
void | DrawOnViewport (SDL_Renderer *renderer, SDL_Rect viewport)
void | loop ()
int | main (int argc, char *argv[])

## Variables

static SDLTest_CommonState * | state
SDL_Rect | viewport
int | done
int | j
SDL_bool | use_target = SDL_FALSE

## Function Documentation

void DrawOnViewport | ( | SDL_Renderer * | renderer,
|  | SDL_Rect | viewport
| ) |  |

Definition at line 44 of file testviewport.c.

References SDL_Rect::h, rect, SDL_RenderClear, SDL_RenderDrawLine, SDL_RenderDrawPoint, SDL_RenderFillRect, SDL_RenderSetViewport, SDL_SetRenderDrawColor, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by loop().

{
SDL_Rect rect;
/* Set the viewport */
SDL_RenderSetViewport(renderer, &viewport);
/* Draw a gray background */
SDL_SetRenderDrawColor(renderer, 0x80, 0x80, 0x80, 0xFF);
SDL_RenderClear(renderer);
/* Test inside points */
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xF, 0xFF);
SDL_RenderDrawPoint(renderer, viewport.h/2 + 10, viewport.w/2);
SDL_RenderDrawPoint(renderer, viewport.h/2 - 10, viewport.w/2);
SDL_RenderDrawPoint(renderer, viewport.h/2 , viewport.w/2 - 10);
SDL_RenderDrawPoint(renderer, viewport.h/2 , viewport.w/2 + 10);
/* Test horizontal and vertical lines */
SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
SDL_RenderDrawLine(renderer, 1, 0, viewport.w-2, 0);
SDL_RenderDrawLine(renderer, 1, viewport.h-1, viewport.w-2, viewport.h-1);
SDL_RenderDrawLine(renderer, 0, 1, 0, viewport.h-2);
SDL_RenderDrawLine(renderer, viewport.w-1, 1, viewport.w-1, viewport.h-2);
/* Test diagonal lines */
SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0xFF, 0xFF);
SDL_RenderDrawLine(renderer, 0, 0,
viewport.w-1, viewport.h-1);
SDL_RenderDrawLine(renderer, viewport.w-1, 0,
0, viewport.h-1);
/* Test outside points */
SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xF, 0xFF);
SDL_RenderDrawPoint(renderer, viewport.h/2 + viewport.h, viewport.w/2);
SDL_RenderDrawPoint(renderer, viewport.h/2 - viewport.h, viewport.w/2);
SDL_RenderDrawPoint(renderer, viewport.h/2 , viewport.w/2 - viewport.w);
SDL_RenderDrawPoint(renderer, viewport.h/2 , viewport.w/2 + viewport.w);
/* Add a box at the top */
rect.w = 8;
rect.h = 8;
rect.x = (viewport.w - rect.w) / 2;
rect.y = 0;
SDL_RenderFillRect(renderer, &rect);
}
SDL_RenderDrawPoint
#define SDL_RenderDrawPoint
Definition: SDL_dynapi_overrides.h:341
SDL_RenderSetViewport
#define SDL_RenderSetViewport
Definition: SDL_dynapi_overrides.h:330
rect
SDL_Rect rect
Definition: testrelative.c:27
SDL_RenderFillRect
#define SDL_RenderFillRect
Definition: SDL_dynapi_overrides.h:347
SDL_Rect::x
int x
Definition: SDL_rect.h:66
SDL_Rect::w
int w
Definition: SDL_rect.h:67
SDL_RenderClear
#define SDL_RenderClear
Definition: SDL_dynapi_overrides.h:340
SDL_Rect::h
int h
Definition: SDL_rect.h:67
SDL_SetRenderDrawColor
#define SDL_SetRenderDrawColor
Definition: SDL_dynapi_overrides.h:336
SDL_RenderDrawLine
#define SDL_RenderDrawLine
Definition: SDL_dynapi_overrides.h:343
SDL_Rect::y
int y
Definition: SDL_rect.h:66
SDL_Rect
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64

void loop | ( |  | ) |

Definition at line 92 of file testviewport.c.

References done, DrawOnViewport(), SDL_Rect::h, i, j, NULL, SDLTest_CommonState::num_windows, SDLTest_CommonState::renderers, SDL_GetTicks(), SDL_Log, SDL_PollEvent, SDL_RenderCopy, SDL_RenderPresent, SDL_SetRenderTarget, SDLTest_CommonEvent(), SDLTest_CommonState::targets, use_target, SDL_Rect::w, SDLTest_CommonState::windows, SDL_Rect::x, and SDL_Rect::y.

Referenced by main().

{
#ifdef __EMSCRIPTEN__
/* Avoid using delays */
if(SDL_GetTicks() - wait_start < 1000)
return;
wait_start = SDL_GetTicks();
#endif
SDL_Event event;
int i;
/* Check for events */
while (SDL_PollEvent(&event)) {
SDLTest_CommonEvent(state, &event, &done);
}
/* Move a viewport box in steps around the screen */
viewport.x = j * 100;
viewport.y = viewport.x;
viewport.w = 100 + j * 50;
viewport.h = 100 + j * 50;
j = (j + 1) % 4;
SDL_Log("Current Viewport x=%i y=%i w=%i h=%i", viewport.x, viewport.y, viewport.w, viewport.h);
for (i = 0; i < state->num_windows; ++i) {
if (state->windows[i] == NULL)
continue;
/* Draw using viewport */
DrawOnViewport(state->renderers[i], viewport);
/* Update the screen! */
if (use_target) {
SDL_SetRenderTarget(state->renderers[i], NULL);
SDL_RenderCopy(state->renderers[i], state->targets[i], NULL, NULL);
SDL_RenderPresent(state->renderers[i]);
SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
} else {
SDL_RenderPresent(state->renderers[i]);
}
}
#ifdef __EMSCRIPTEN__
if (done) {
emscripten_cancel_main_loop();
}
#endif
}
SDLTest_CommonState::targets
SDL_Texture ** targets
Definition: SDL_test_common.h:85
SDL_PollEvent
#define SDL_PollEvent
Definition: SDL_dynapi_overrides.h:128
SDL_SetRenderTarget
#define SDL_SetRenderTarget
Definition: SDL_dynapi_overrides.h:326
SDLTest_CommonState::windows
SDL_Window ** windows
Definition: SDL_test_common.h:78
SDLTest_CommonState::num_windows
int num_windows
Definition: SDL_test_common.h:77
SDL_RenderCopy
#define SDL_RenderCopy
Definition: SDL_dynapi_overrides.h:349
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:37
SDLTest_CommonEvent
void SDLTest_CommonEvent(SDLTest_CommonState *state, SDL_Event *event, int *done)
Common event handler for test windows.
Definition: SDL_test_common.c:1295
use_target
SDL_bool use_target
Definition: testviewport.c:30
SDL_GetTicks
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2649
SDL_Rect::x
int x
Definition: SDL_rect.h:66
SDL_Rect::w
int w
Definition: SDL_rect.h:67
SDLTest_CommonState::renderers
SDL_Renderer ** renderers
Definition: SDL_test_common.h:84
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:42
done
int done
Definition: testviewport.c:29
NULL
#define NULL
Definition: begin_code.h:143
j
int j
Definition: testviewport.c:29
SDL_Rect::h
int h
Definition: SDL_rect.h:67
viewport
SDL_Rect viewport
Definition: testviewport.c:28
SDL_Event
General event structure.
Definition: SDL_events.h:521
state
static SDLTest_CommonState * state
Definition: testviewport.c:26
SDL_Rect::y
int y
Definition: SDL_rect.h:66
SDL_RenderPresent
#define SDL_RenderPresent
Definition: SDL_dynapi_overrides.h:352
DrawOnViewport
void DrawOnViewport(SDL_Renderer *renderer, SDL_Rect viewport)
Definition: testviewport.c:44

int main | ( | int | argc,
|  | char * | argv[]
| ) |  |

Definition at line 141 of file testviewport.c.

References done, i, j, loop(), SDLTest_CommonState::num_windows, quit(), renderer, SDLTest_CommonState::renderers, SDL_CreateTexture, SDL_Delay, SDL_GetTicks(), SDL_GetWindowSize, SDL_INIT_VIDEO, SDL_Log, SDL_PIXELFORMAT_RGBA8888, SDL_RenderClear, SDL_SetRenderDrawColor, SDL_SetRenderTarget, SDL_strcasecmp, SDL_TEXTUREACCESS_TARGET, SDL_TRUE, SDLTest_CommonArg(), SDLTest_CommonCreateState(), SDLTest_CommonInit(), SDLTest_CommonUsage(), SDLTest_CommonState::targets, use_target, and SDLTest_CommonState::windows.

{
int i;
Uint32 then, now, frames;
/* Initialize test framework */
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO);
if (!state) {
return 1;
}
for (i = 1; i < argc;) {
int consumed;
consumed = SDLTest_CommonArg(state, i);
if (consumed == 0) {
consumed = -1;
if (SDL_strcasecmp(argv[i], "--target") == 0) {
use_target = SDL_TRUE;
consumed = 1;
}
}
if (consumed < 0) {
SDL_Log("Usage: %s %s [--target]\n",
argv[0], SDLTest_CommonUsage(state));
quit(1);
}
i += consumed;
}
if (!SDLTest_CommonInit(state)) {
quit(2);
}
if (use_target) {
int w, h;
for (i = 0; i < state->num_windows; ++i) {
SDL_GetWindowSize(state->windows[i], &w, &h);
state->targets[i] = SDL_CreateTexture(state->renderers[i], SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, w, h);
SDL_SetRenderTarget(state->renderers[i], state->targets[i]);
}
}
for (i = 0; i < state->num_windows; ++i) {
SDL_Renderer *renderer = state->renderers[i];
SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
SDL_RenderClear(renderer);
}
/* Main render loop */
frames = 0;
then = SDL_GetTicks();
done = 0;
j = 0;
#ifdef __EMSCRIPTEN__
wait_start = SDL_GetTicks();
emscripten_set_main_loop(loop, 0, 1);
#else
while (!done) {
++frames;
loop();
SDL_Delay(1000);
}
#endif
/* Print out some timing information */
now = SDL_GetTicks();
if (now > then) {
double fps = ((double) frames * 1000) / (now - then);
SDL_Log("%2.2f frames per second\n", fps);
}
quit(0);
return 0;
}
SDL_CreateTexture
#define SDL_CreateTexture
Definition: SDL_dynapi_overrides.h:312
SDLTest_CommonState::targets
SDL_Texture ** targets
Definition: SDL_test_common.h:85
SDL_SetRenderTarget
#define SDL_SetRenderTarget
Definition: SDL_dynapi_overrides.h:326
Uint32
uint32_t Uint32
An unsigned 32-bit integer type.
Definition: SDL_stdinc.h:157
SDLTest_CommonCreateState
SDLTest_CommonState * SDLTest_CommonCreateState(char **argv, Uint32 flags)
Parse command line parameters and create common state.
Definition: SDL_test_common.c:36
SDLTest_CommonArg
int SDLTest_CommonArg(SDLTest_CommonState *state, int index)
Process one common argument.
Definition: SDL_test_common.c:83
SDL_Renderer
Definition: SDL_sysrender.h:76
loop
void loop()
Definition: testviewport.c:92
SDL_strcasecmp
#define SDL_strcasecmp
Definition: SDL_dynapi_overrides.h:425
SDLTest_CommonInit
SDL_bool SDLTest_CommonInit(SDLTest_CommonState *state)
Open test window.
Definition: SDL_test_common.c:645
SDLTest_CommonState::windows
SDL_Window ** windows
Definition: SDL_test_common.h:78
SDLTest_CommonState::num_windows
int num_windows
Definition: SDL_test_common.h:77
SDL_PIXELFORMAT_RGBA8888
Definition: SDL_pixels.h:250
renderer
SDL_Renderer * renderer
Definition: testautomation_render.c:27
SDL_GetWindowSize
#define SDL_GetWindowSize
Definition: SDL_dynapi_overrides.h:531
SDL_Log
#define SDL_Log
Definition: SDL_dynapi_overrides.h:37
use_target
SDL_bool use_target
Definition: testviewport.c:30
SDL_GetTicks
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
SDL_TRUE
Definition: SDL_stdinc.h:129
SDLTest_CommonUsage
const char * SDLTest_CommonUsage(SDLTest_CommonState *state)
Returns common usage information.
Definition: SDL_test_common.c:451
SDL_Delay
#define SDL_Delay
Definition: SDL_dynapi_overrides.h:490
SDLTest_CommonState::renderers
SDL_Renderer ** renderers
Definition: SDL_test_common.h:84
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:42
done
int done
Definition: testviewport.c:29
SDL_RenderClear
#define SDL_RenderClear
Definition: SDL_dynapi_overrides.h:340
j
int j
Definition: testviewport.c:29
SDL_TEXTUREACCESS_TARGET
Definition: SDL_render.h:95
SDL_SetRenderDrawColor
#define SDL_SetRenderDrawColor
Definition: SDL_dynapi_overrides.h:336
w
GLubyte GLubyte GLubyte GLubyte w
Definition: SDL_opengl_glext.h:731
state
static SDLTest_CommonState * state
Definition: testviewport.c:26
h
GLfloat GLfloat GLfloat GLfloat h
Definition: SDL_opengl_glext.h:1946
SDL_INIT_VIDEO
#define SDL_INIT_VIDEO
Definition: SDL.h:77
quit
static void quit(int rc)
Definition: testviewport.c:37

static void quit | ( | int | rc | ) |

static

Definition at line 37 of file testviewport.c.

References SDLTest_CommonQuit().

Referenced by main().

{
SDLTest_CommonQuit(state);
exit(rc);
}
state
static SDLTest_CommonState * state
Definition: testviewport.c:26
SDLTest_CommonQuit
void SDLTest_CommonQuit(SDLTest_CommonState *state)
Close test window.
Definition: SDL_test_common.c:1560

## Variable Documentation

int done

Definition at line 29 of file testviewport.c.

Referenced by loop(), and main().

int j

Definition at line 29 of file testviewport.c.

Referenced by loop(), and main().

SDLTest_CommonState* state

static

Definition at line 26 of file testviewport.c.

SDL_bool use_target = SDL_FALSE

Definition at line 30 of file testviewport.c.

Referenced by loop(), and main().

SDL_Rect viewport

Definition at line 28 of file testviewport.c.

Referenced by Draw(), DrawComposite(), DrawLines(), DrawPoints(), DrawRects(), loop(), MoveSprites(), SDLTest_ScreenShot(), and UpdateLogicalSize().