SDL  2.0
testrendertarget.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: Move N sprites around on the screen as fast as possible */
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_common.h"
23 
24 
26 
27 typedef struct {
32  SDL_Rect sprite_rect;
33  int scale_direction;
34 } DrawState;
35 
37 int done;
39 
40 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
41 static void
42 quit(int rc)
43 {
44  SDLTest_CommonQuit(state);
45  exit(rc);
46 }
47 
49 LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
50 {
51  SDL_Surface *temp;
53 
54  /* Load the sprite image */
55  temp = SDL_LoadBMP(file);
56  if (temp == NULL) {
57  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
58  return NULL;
59  }
60 
61  /* Set transparent pixel as the pixel at (0,0) */
62  if (transparent) {
63  if (temp->format->palette) {
64  SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
65  } else {
66  switch (temp->format->BitsPerPixel) {
67  case 15:
69  (*(Uint16 *) temp->pixels) & 0x00007FFF);
70  break;
71  case 16:
72  SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
73  break;
74  case 24:
76  (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
77  break;
78  case 32:
79  SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
80  break;
81  }
82  }
83  }
84 
85  /* Create textures from the image */
86  texture = SDL_CreateTextureFromSurface(renderer, temp);
87  if (!texture) {
88  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
89  SDL_FreeSurface(temp);
90  return NULL;
91  }
92  SDL_FreeSurface(temp);
93 
94  /* We're ready to roll. :) */
95  return texture;
96 }
97 
100 {
101  SDL_Rect viewport, R;
103 
104  static SDL_bool blend_tested = SDL_FALSE;
105  if (!blend_tested) {
106  SDL_Texture *A, *B;
107  Uint32 P;
108 
111 
114 
116  SDL_SetRenderDrawColor(s->renderer, 0x00, 0x00, 0x00, 0x80);
118 
120  SDL_SetRenderDrawColor(s->renderer, 0x00, 0x00, 0x00, 0x00);
122  SDL_RenderCopy(s->renderer, A, NULL, NULL);
124 
125  SDL_Log("Blended pixel: 0x%8.8X\n", P);
126 
129  blend_tested = SDL_TRUE;
130  }
131 
132  SDL_RenderGetViewport(s->renderer, &viewport);
133 
136  SDL_SetRenderTarget(s->renderer, target);
137 
138  /* Draw the background.
139  This is solid black so when the sprite is copied to it, any per-pixel alpha will be blended through.
140  */
141  SDL_SetRenderDrawColor(s->renderer, 0x00, 0x00, 0x00, 0x00);
143 
144  /* Scale and draw the sprite */
145  s->sprite_rect.w += s->scale_direction;
146  s->sprite_rect.h += s->scale_direction;
147  if (s->scale_direction > 0) {
148  if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
149  s->scale_direction = -1;
150  }
151  } else {
152  if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
153  s->scale_direction = 1;
154  }
155  }
156  s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
157  s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
158 
160 
163 
165  SDL_SetRenderDrawColor(s->renderer, 0xff, 0x00, 0x00, 0x80);
166  R.x = 0;
167  R.y = 0;
168  R.w = 100;
169  R.h = 100;
172 
173  SDL_RenderCopy(s->renderer, target, NULL, NULL);
174  SDL_DestroyTexture(target);
175 
176  /* Update the screen! */
178  return SDL_TRUE;
179 }
180 
181 SDL_bool
183 {
186 
187  SDL_RenderGetViewport(s->renderer, &viewport);
188 
190  if (!target) {
191  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create render target texture: %s\n", SDL_GetError());
192  return SDL_FALSE;
193  }
194  SDL_SetRenderTarget(s->renderer, target);
195 
196  /* Draw the background */
198 
199  /* Scale and draw the sprite */
200  s->sprite_rect.w += s->scale_direction;
201  s->sprite_rect.h += s->scale_direction;
202  if (s->scale_direction > 0) {
203  if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
204  s->scale_direction = -1;
205  }
206  } else {
207  if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
208  s->scale_direction = 1;
209  }
210  }
211  s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
212  s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
213 
215 
217  SDL_RenderCopy(s->renderer, target, NULL, NULL);
218  SDL_DestroyTexture(target);
219 
220  /* Update the screen! */
222  return SDL_TRUE;
223 }
224 
225 void
227 {
228  int i;
230 
231  /* Check for events */
232  while (SDL_PollEvent(&event)) {
233  SDLTest_CommonEvent(state, &event, &done);
234  }
235  for (i = 0; i < state->num_windows; ++i) {
236  if (state->windows[i] == NULL)
237  continue;
238  if (test_composite) {
239  if (!DrawComposite(&drawstates[i])) done = 1;
240  } else {
241  if (!Draw(&drawstates[i])) done = 1;
242  }
243  }
244 #ifdef __EMSCRIPTEN__
245  if (done) {
246  emscripten_cancel_main_loop();
247  }
248 #endif
249 }
250 
251 int
252 main(int argc, char *argv[])
253 {
254  int i;
255  int frames;
256  Uint32 then, now;
257 
258  /* Enable standard application logging */
260 
261  /* Initialize test framework */
263  if (!state) {
264  return 1;
265  }
266  for (i = 1; i < argc;) {
267  int consumed;
268 
269  consumed = SDLTest_CommonArg(state, i);
270  if (consumed == 0) {
271  consumed = -1;
272  if (SDL_strcasecmp(argv[i], "--composite") == 0) {
274  consumed = 1;
275  }
276  }
277  if (consumed < 0) {
278  SDL_Log("Usage: %s %s [--composite]\n",
279  argv[0], SDLTest_CommonUsage(state));
280  quit(1);
281  }
282  i += consumed;
283  }
284  if (!SDLTest_CommonInit(state)) {
285  quit(2);
286  }
287 
288  drawstates = SDL_stack_alloc(DrawState, state->num_windows);
289  for (i = 0; i < state->num_windows; ++i) {
290  DrawState *drawstate = &drawstates[i];
291 
292  drawstate->window = state->windows[i];
293  drawstate->renderer = state->renderers[i];
294  if (test_composite) {
295  drawstate->sprite = LoadTexture(drawstate->renderer, "icon-alpha.bmp", SDL_TRUE);
296  } else {
297  drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
298  }
299  drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
300  if (!drawstate->sprite || !drawstate->background) {
301  quit(2);
302  }
303  SDL_QueryTexture(drawstate->sprite, NULL, NULL,
304  &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
305  drawstate->scale_direction = 1;
306  }
307 
308  /* Main render loop */
309  frames = 0;
310  then = SDL_GetTicks();
311  done = 0;
312 
313 #ifdef __EMSCRIPTEN__
314  emscripten_set_main_loop(loop, 0, 1);
315 #else
316  while (!done) {
317  ++frames;
318  loop();
319  }
320 #endif
321 
322  /* Print out some timing information */
323  now = SDL_GetTicks();
324  if (now > then) {
325  double fps = ((double) frames * 1000) / (now - then);
326  SDL_Log("%2.2f frames per second\n", fps);
327  }
328 
329  SDL_stack_free(drawstates);
330 
331  quit(0);
332  return 0;
333 }
334 
335 /* vi: set ts=4 sw=4 expandtab: */
#define SDL_CreateTexture
#define SDL_RenderReadPixels
SDL_Renderer * renderer
#define SDL_PollEvent
#define SDL_GetError
SDL_Rect sprite_rect
GLdouble s
Definition: SDL_opengl.h:2063
#define SDL_SetRenderTarget
#define SDL_LoadBMP(file)
Definition: SDL_surface.h:200
SDLTest_CommonState * SDLTest_CommonCreateState(char **argv, Uint32 flags)
Parse command line parameters and create common state.
#define SDL_RenderFillRect
int SDLTest_CommonArg(SDLTest_CommonState *state, int index)
Process one common argument.
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
#define SDL_SetTextureBlendMode
#define SDL_SetRenderDrawBlendMode
static void quit(int rc)
int main(int argc, char *argv[])
SDL_Texture * background
uint32_t Uint32
Definition: SDL_stdinc.h:181
#define SDL_strcasecmp
SDL_bool SDLTest_CommonInit(SDLTest_CommonState *state)
Open test window.
SDL_bool DrawComposite(DrawState *s)
SDL_Window * window
SDL_bool test_composite
SDL_Window ** windows
#define SDL_LogError
#define SDL_RenderCopy
#define SDL_Log
GLenum GLenum GLuint texture
#define SDL_CreateTextureFromSurface
void SDLTest_CommonEvent(SDLTest_CommonState *state, SDL_Event *event, int *done)
Common event handler for test windows.
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
void * pixels
Definition: SDL_surface.h:75
#define SDL_FreeSurface
static SDL_Renderer * renderer
uint8_t Uint8
Definition: SDL_stdinc.h:157
#define SDL_stack_alloc(type, count)
Definition: SDL_stdinc.h:354
struct _cl_event * event
Uint8 BitsPerPixel
Definition: SDL_pixels.h:319
#define SDL_QueryTexture
void loop()
#define SDL_RenderGetViewport
#define SDL_SetColorKey
int x
Definition: SDL_rect.h:66
const char * SDLTest_CommonUsage(SDLTest_CommonState *state)
Returns common usage information.
int w
Definition: SDL_rect.h:67
static SDLTest_CommonState * state
SDL_Renderer ** renderers
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
GLenum target
#define SDL_LogSetPriority
#define NULL
Definition: begin_code.h:164
SDL_bool
Definition: SDL_stdinc.h:139
SDL_PixelFormat * format
Definition: SDL_surface.h:72
DrawState * drawstates
SDL_Texture * LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
#define SDL_DestroyTexture
int h
Definition: SDL_rect.h:67
static SDL_Texture * sprite
The type used to identify a window.
Definition: SDL_sysvideo.h:73
SDL_Rect viewport
Definition: testviewport.c:28
SDL_Texture * sprite
uint16_t Uint16
Definition: SDL_stdinc.h:169
General event structure.
Definition: SDL_events.h:525
#define SDL_SetRenderDrawColor
SDL_Palette * palette
Definition: SDL_pixels.h:318
SDL_Texture * background
#define SDL_stack_free(data)
Definition: SDL_stdinc.h:355
SDL_bool Draw(DrawState *s)
int done
int y
Definition: SDL_rect.h:66
void SDLTest_CommonQuit(SDLTest_CommonState *state)
Close test window.
#define SDL_INIT_VIDEO
Definition: SDL.h:78
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
#define SDL_RenderPresent