SDL  2.0
testviewport.c
Go to the documentation of this file.
1 /*
2  Copyright (C) 1997-2020 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 static int done, j;
31 #ifdef __EMSCRIPTEN__
32 static Uint32 wait_start;
33 #endif
35 static int sprite_w, sprite_h;
36 
37 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
38 static void
39 quit(int rc)
40 {
42  exit(rc);
43 }
44 
45 int
47 {
48  SDL_Surface *temp;
49 
50  /* Load the sprite image */
51  temp = SDL_LoadBMP(file);
52  if (temp == NULL) {
53  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", file, SDL_GetError());
54  return (-1);
55  }
56  sprite_w = temp->w;
57  sprite_h = temp->h;
58 
59  /* Set transparent pixel as the pixel at (0,0) */
60  if (temp->format->palette) {
61  SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
62  } else {
63  switch (temp->format->BitsPerPixel) {
64  case 15:
66  (*(Uint16 *) temp->pixels) & 0x00007FFF);
67  break;
68  case 16:
69  SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
70  break;
71  case 24:
73  (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
74  break;
75  case 32:
76  SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
77  break;
78  }
79  }
80 
81  /* Create textures from the image */
83  if (!sprite) {
84  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
85  SDL_FreeSurface(temp);
86  return (-1);
87  }
88  SDL_FreeSurface(temp);
89 
90  /* We're ready to roll. :) */
91  return (0);
92 }
93 
94 void
96 {
97  SDL_Rect rect;
98 
99  /* Set the viewport */
101 
102  /* Draw a gray background */
103  SDL_SetRenderDrawColor(renderer, 0x80, 0x80, 0x80, 0xFF);
105 
106  /* Test inside points */
107  SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0x00, 0xFF);
112 
113  /* Test horizontal and vertical lines */
114  SDL_SetRenderDrawColor(renderer, 0x00, 0xFF, 0x00, 0xFF);
115  SDL_RenderDrawLine(renderer, 1, 0, viewport.w-2, 0);
117  SDL_RenderDrawLine(renderer, 0, 1, 0, viewport.h-2);
119 
120  /* Test diagonal lines */
121  SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0xFF, 0xFF);
124 
125  /* Test outside points */
126  SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0x00, 0xFF);
131 
132  /* Add a box at the top */
133  rect.w = 8;
134  rect.h = 8;
135  rect.x = (viewport.w - rect.w) / 2;
136  rect.y = 0;
138 
139  /* Add a clip rect and fill it with the sprite */
141  rect.x = (viewport.w - rect.w) / 2;
142  rect.y = (viewport.h - rect.h) / 2;
146 }
147 
148 void
150 {
151 #ifdef __EMSCRIPTEN__
152  /* Avoid using delays */
153  if(SDL_GetTicks() - wait_start < 1000)
154  return;
155  wait_start = SDL_GetTicks();
156 #endif
158  int i;
159  /* Check for events */
160  while (SDL_PollEvent(&event)) {
162  }
163 
164  /* Move a viewport box in steps around the screen */
165  viewport.x = j * 100;
166  viewport.y = viewport.x;
167  viewport.w = 100 + j * 50;
168  viewport.h = 100 + j * 50;
169  j = (j + 1) % 4;
170  SDL_Log("Current Viewport x=%i y=%i w=%i h=%i", viewport.x, viewport.y, viewport.w, viewport.h);
171 
172  for (i = 0; i < state->num_windows; ++i) {
173  if (state->windows[i] == NULL)
174  continue;
175 
176  /* Draw using viewport */
178 
179  /* Update the screen! */
180  if (use_target) {
185  } else {
187  }
188  }
189 
190 #ifdef __EMSCRIPTEN__
191  if (done) {
192  emscripten_cancel_main_loop();
193  }
194 #endif
195 }
196 
197 int
198 main(int argc, char *argv[])
199 {
200  int i;
201  Uint32 then, now, frames;
202 
203  /* Initialize test framework */
205  if (!state) {
206  return 1;
207  }
208 
209 
210  for (i = 1; i < argc;) {
211  int consumed;
212 
213  consumed = SDLTest_CommonArg(state, i);
214  if (consumed == 0) {
215  consumed = -1;
216  if (SDL_strcasecmp(argv[i], "--target") == 0) {
218  consumed = 1;
219  }
220  }
221  if (consumed < 0) {
222  static const char *options[] = { "[--target]", NULL };
223  SDLTest_CommonLogUsage(state, argv[0], options);
224  quit(1);
225  }
226  i += consumed;
227  }
228  if (!SDLTest_CommonInit(state)) {
229  quit(2);
230  }
231 
232  if (LoadSprite("icon.bmp", state->renderers[0]) < 0) {
233  quit(2);
234  }
235 
236  if (use_target) {
237  int w, h;
238 
239  for (i = 0; i < state->num_windows; ++i) {
243  }
244  }
245 
246  for (i = 0; i < state->num_windows; ++i) {
248  SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
250  }
251 
252  /* Main render loop */
253  frames = 0;
254  then = SDL_GetTicks();
255  done = 0;
256  j = 0;
257 
258 #ifdef __EMSCRIPTEN__
259  wait_start = SDL_GetTicks();
260  emscripten_set_main_loop(loop, 0, 1);
261 #else
262  while (!done) {
263  ++frames;
264  loop();
265  SDL_Delay(1000);
266  }
267 #endif
268 
269  /* Print out some timing information */
270  now = SDL_GetTicks();
271  if (now > then) {
272  double fps = ((double) frames * 1000) / (now - then);
273  SDL_Log("%2.2f frames per second\n", fps);
274  }
275  quit(0);
276  return 0;
277 }
278 
279 /* vi: set ts=4 sw=4 expandtab: */
SDLTest_CommonState::windows
SDL_Window ** windows
Definition: SDL_test_common.h:78
SDL_GetError
#define SDL_GetError
Definition: SDL_dynapi_overrides.h:113
SDL_RenderPresent
#define SDL_RenderPresent
Definition: SDL_dynapi_overrides.h:346
Uint32
uint32_t Uint32
Definition: SDL_stdinc.h:203
SDL_SetRenderTarget
#define SDL_SetRenderTarget
Definition: SDL_dynapi_overrides.h:320
SDLTest_CommonState::targets
SDL_Texture ** targets
Definition: SDL_test_common.h:85
SDL_PixelFormat::BitsPerPixel
Uint8 BitsPerPixel
Definition: SDL_pixels.h:322
SDL_Surface
A collection of pixels used in software blitting.
Definition: SDL_surface.h:70
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:167
SDL_Surface::pixels
void * pixels
Definition: SDL_surface.h:76
SDLTest_CommonState
Definition: SDL_test_common.h:51
SDL_Surface::w
int w
Definition: SDL_surface.h:74
SDL_RenderFillRect
#define SDL_RenderFillRect
Definition: SDL_dynapi_overrides.h:341
viewport
static SDL_Rect viewport
Definition: testviewport.c:28
loop
void loop()
Definition: testviewport.c:149
h
GLfloat GLfloat GLfloat GLfloat h
Definition: SDL_opengl_glext.h:1949
SDL_Rect::x
int x
Definition: SDL_rect.h:79
SDLTest_CommonState::renderers
SDL_Renderer ** renderers
Definition: SDL_test_common.h:84
SDL_LogError
#define SDL_LogError
Definition: SDL_dynapi_overrides.h:36
LoadSprite
int LoadSprite(char *file, SDL_Renderer *renderer)
Definition: testviewport.c:46
SDL_Rect::w
int w
Definition: SDL_rect.h:80
SDLTest_CommonCreateState
SDLTest_CommonState * SDLTest_CommonCreateState(char **argv, Uint32 flags)
Parse command line parameters and create common state.
Definition: SDL_test_common.c:59
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:1910
SDL_GetWindowSize
#define SDL_GetWindowSize
Definition: SDL_dynapi_overrides.h:527
sprite_w
static int sprite_w
Definition: testviewport.c:35
SDL_CreateTextureFromSurface
#define SDL_CreateTextureFromSurface
Definition: SDL_dynapi_overrides.h:307
SDLTest_CommonState::num_windows
int num_windows
Definition: SDL_test_common.h:77
done
static int done
Definition: testviewport.c:29
event
struct _cl_event * event
Definition: SDL_opengl_glext.h:2652
SDL_Renderer
Definition: SDL_sysrender.h:109
SDL_FALSE
@ SDL_FALSE
Definition: SDL_stdinc.h:163
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:79
SDL_Rect::h
int h
Definition: SDL_rect.h:80
SDL_LOG_CATEGORY_APPLICATION
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
SDL_QueryTexture
#define SDL_QueryTexture
Definition: SDL_dynapi_overrides.h:308
SDLTest_CommonArg
int SDLTest_CommonArg(SDLTest_CommonState *state, int index)
Process one common argument.
Definition: SDL_test_common.c:117
rect
SDL_Rect rect
Definition: testrelative.c:27
SDL_FreeSurface
#define SDL_FreeSurface
Definition: SDL_dynapi_overrides.h:446
SDL_PixelFormat::palette
SDL_Palette * palette
Definition: SDL_pixels.h:321
Uint16
uint16_t Uint16
Definition: SDL_stdinc.h:191
SDL_SetColorKey
#define SDL_SetColorKey
Definition: SDL_dynapi_overrides.h:453
SDL_GetTicks
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
SDLTest_CommonLogUsage
void SDLTest_CommonLogUsage(SDLTest_CommonState *state, const char *argv0, const char **options)
Logs command line usage info.
Definition: SDL_test_common.c:498
j
static int j
Definition: testviewport.c:29
use_target
static SDL_bool use_target
Definition: testviewport.c:30
SDL_Delay
#define SDL_Delay
Definition: SDL_dynapi_overrides.h:486
SDL_LoadBMP
#define SDL_LoadBMP(file)
Definition: SDL_surface.h:201
SDL_Surface::h
int h
Definition: SDL_surface.h:74
SDLTest_CommonInit
SDL_bool SDLTest_CommonInit(SDLTest_CommonState *state)
Open test window.
Definition: SDL_test_common.c:835
SDL_INIT_VIDEO
#define SDL_INIT_VIDEO
Definition: SDL.h:80
main
int main(int argc, char *argv[])
Definition: testviewport.c:198
renderer
static SDL_Renderer * renderer
Definition: testaudiocapture.c:21
frames
static Uint32 frames
Definition: testsprite2.c:40
SDL_TRUE
@ SDL_TRUE
Definition: SDL_stdinc.h:164
SDL_PIXELFORMAT_RGBA8888
@ SDL_PIXELFORMAT_RGBA8888
Definition: SDL_pixels.h:254
SDL_Rect
A rectangle, with the origin at the upper left (integer).
Definition: SDL_rect.h:77
SDL_Texture
Definition: SDL_sysrender.h:36
SDL_TEXTUREACCESS_TARGET
@ SDL_TEXTUREACCESS_TARGET
Definition: SDL_render.h:105
SDL_RenderClear
#define SDL_RenderClear
Definition: SDL_dynapi_overrides.h:334
sprite
static SDL_Texture * sprite
Definition: testviewport.c:34
SDL_SetRenderDrawColor
#define SDL_SetRenderDrawColor
Definition: SDL_dynapi_overrides.h:330
DrawOnViewport
void DrawOnViewport(SDL_Renderer *renderer, SDL_Rect viewport)
Definition: testviewport.c:95
SDL_Event
General event structure.
Definition: SDL_events.h:558
SDLTest_CommonEvent
void SDLTest_CommonEvent(SDLTest_CommonState *state, SDL_Event *event, int *done)
Common event handler for test windows.
Definition: SDL_test_common.c:1579
SDL_RenderDrawLine
#define SDL_RenderDrawLine
Definition: SDL_dynapi_overrides.h:337
SDL_RenderSetClipRect
#define SDL_RenderSetClipRect
Definition: SDL_dynapi_overrides.h:326
sprite_h
static int sprite_h
Definition: testviewport.c:35
state
static SDLTest_CommonState * state
Definition: testviewport.c:26
quit
static void quit(int rc)
Definition: testviewport.c:39
SDL_Surface::format
SDL_PixelFormat * format
Definition: SDL_surface.h:73
SDL_CreateTexture
#define SDL_CreateTexture
Definition: SDL_dynapi_overrides.h:306
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_bool
SDL_bool
Definition: SDL_stdinc.h:161
w
GLubyte GLubyte GLubyte GLubyte w
Definition: SDL_opengl_glext.h:734
Uint8
uint8_t Uint8
Definition: SDL_stdinc.h:179