testviewport.c
Go to the documentation of this file.
/*
Copyright (C) 1997-2016 Sam Lantinga <slouken@libsdl.org>
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely.
*/
/* Simple program: Check viewports */
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#ifdef __EMSCRIPTEN__
#include <emscripten/emscripten.h>
#endif
#include "SDL_test.h"
#include "SDL_test_common.h"
static SDLTest_CommonState *state;
SDL_Rect viewport;
int done, j;
SDL_bool use_target = SDL_FALSE;
#ifdef __EMSCRIPTEN__
Uint32 wait_start;
#endif
/* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
static void
quit(int rc)
{
SDLTest_CommonQuit(state);
exit(rc);
}
void
DrawOnViewport(SDL_Renderer * renderer, SDL_Rect viewport)
{
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);
}
void
loop()
{
#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
}
int
main(int argc, char *argv[])
{
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;
}
/* vi: set ts=4 sw=4 expandtab: */
SDL_CreateTexture
#define SDL_CreateTexture
Definition: SDL_dynapi_overrides.h:312
SDL_test.h
SDL_RenderDrawPoint
#define SDL_RenderDrawPoint
Definition: SDL_dynapi_overrides.h:341
SDL_RenderSetViewport
#define SDL_RenderSetViewport
Definition: SDL_dynapi_overrides.h:330
SDLTest_CommonState::targets
SDL_Texture ** targets
Definition: SDL_test_common.h:85
SDL_PollEvent
#define SDL_PollEvent
Definition: SDL_dynapi_overrides.h:128
main
int main(int argc, char *argv[])
Definition: testviewport.c:141
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
rect
SDL_Rect rect
Definition: testrelative.c:27
SDL_RenderFillRect
#define SDL_RenderFillRect
Definition: SDL_dynapi_overrides.h:347
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
SDL_test_common.h
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
SDL_bool
SDL_bool
Definition: SDL_stdinc.h:126
SDLTest_CommonState::num_windows
int num_windows
Definition: SDL_test_common.h:77
SDL_PIXELFORMAT_RGBA8888
Definition: SDL_pixels.h:250
SDL_RenderCopy
#define SDL_RenderCopy
Definition: SDL_dynapi_overrides.h:349
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
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_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_Rect::w
int w
Definition: SDL_rect.h:67
SDLTest_CommonState
Definition: SDL_test_common.h:51
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
NULL
#define NULL
Definition: begin_code.h:143
SDL_RenderClear
#define SDL_RenderClear
Definition: SDL_dynapi_overrides.h:340
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_FALSE
Definition: SDL_stdinc.h:128
SDL_TEXTUREACCESS_TARGET
Definition: SDL_render.h:95
SDL_Event
General event structure.
Definition: SDL_events.h:521
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
SDL_RenderDrawLine
#define SDL_RenderDrawLine
Definition: SDL_dynapi_overrides.h:343
SDL_Rect::y
int y
Definition: SDL_rect.h:66
SDLTest_CommonQuit
void SDLTest_CommonQuit(SDLTest_CommonState *state)
Close test window.
Definition: SDL_test_common.c:1560
h
GLfloat GLfloat GLfloat GLfloat h
Definition: SDL_opengl_glext.h:1946
SDL_INIT_VIDEO
#define SDL_INIT_VIDEO
Definition: SDL.h:77
SDL_Rect
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
quit
static void quit(int rc)
Definition: testviewport.c:37
SDL_RenderPresent
#define SDL_RenderPresent
Definition: SDL_dynapi_overrides.h:352
DrawOnViewport
void DrawOnViewport(SDL_Renderer *renderer, SDL_Rect viewport)
Definition: testviewport.c:44