SDL  2.0
testrendercopyex.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 {
34 } DrawState;
35 
37 int done;
38 
39 /* Call this instead of exit(), so we can clean up SDL: atexit() is evil. */
40 static void
41 quit(int rc)
42 {
43  SDLTest_CommonQuit(state);
44  exit(rc);
45 }
46 
48 LoadTexture(SDL_Renderer *renderer, const char *file, SDL_bool transparent)
49 {
50  SDL_Surface *temp;
52 
53  /* Load the sprite image */
54  temp = SDL_LoadBMP(file);
55  if (temp == NULL) {
56  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
57  return NULL;
58  }
59 
60  /* Set transparent pixel as the pixel at (0,0) */
61  if (transparent) {
62  if (temp->format->palette) {
63  SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
64  } else {
65  switch (temp->format->BitsPerPixel) {
66  case 15:
68  (*(Uint16 *) temp->pixels) & 0x00007FFF);
69  break;
70  case 16:
71  SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
72  break;
73  case 24:
75  (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
76  break;
77  case 32:
78  SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
79  break;
80  }
81  }
82  }
83 
84  /* Create textures from the image */
85  texture = SDL_CreateTextureFromSurface(renderer, temp);
86  if (!texture) {
87  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
88  SDL_FreeSurface(temp);
89  return NULL;
90  }
91  SDL_FreeSurface(temp);
92 
93  /* We're ready to roll. :) */
94  return texture;
95 }
96 
97 void
99 {
102  SDL_Point *center=NULL;
103  SDL_Point origin = {0,0};
104 
105  SDL_RenderGetViewport(s->renderer, &viewport);
106 
108  SDL_SetRenderTarget(s->renderer, target);
109 
110  /* Draw the background */
112 
113  /* Scale and draw the sprite */
114  s->sprite_rect.w += s->scale_direction;
115  s->sprite_rect.h += s->scale_direction;
116  if (s->scale_direction > 0) {
117  center = &origin;
118  if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
119  s->scale_direction = -1;
120  }
121  } else {
122  if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
123  s->scale_direction = 1;
124  }
125  }
126  s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
127  s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
128 
130 
132  SDL_RenderCopy(s->renderer, target, NULL, NULL);
133  SDL_DestroyTexture(target);
134 
135  /* Update the screen! */
137  /* SDL_Delay(10); */
138 }
139 
140 void loop()
141 {
142  int i;
144 
145  /* Check for events */
146 
147  while (SDL_PollEvent(&event)) {
148  SDLTest_CommonEvent(state, &event, &done);
149  }
150  for (i = 0; i < state->num_windows; ++i) {
151  if (state->windows[i] == NULL)
152  continue;
153  Draw(&drawstates[i]);
154  }
155 #ifdef __EMSCRIPTEN__
156  if (done) {
157  emscripten_cancel_main_loop();
158  }
159 #endif
160 }
161 
162 int
163 main(int argc, char *argv[])
164 {
165  int i;
166  int frames;
167  Uint32 then, now;
168 
169  /* Enable standard application logging */
171 
172  /* Initialize test framework */
174  if (!state) {
175  return 1;
176  }
177  for (i = 1; i < argc;) {
178  int consumed;
179 
180  consumed = SDLTest_CommonArg(state, i);
181  if (consumed == 0) {
182  SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
183  return 1;
184  }
185  i += consumed;
186  }
187  if (!SDLTest_CommonInit(state)) {
188  quit(2);
189  }
190 
191  drawstates = SDL_stack_alloc(DrawState, state->num_windows);
192  for (i = 0; i < state->num_windows; ++i) {
193  DrawState *drawstate = &drawstates[i];
194 
195  drawstate->window = state->windows[i];
196  drawstate->renderer = state->renderers[i];
197  drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
198  drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
199  if (!drawstate->sprite || !drawstate->background) {
200  quit(2);
201  }
202  SDL_QueryTexture(drawstate->sprite, NULL, NULL,
203  &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
204  drawstate->scale_direction = 1;
205  }
206 
207  /* Main render loop */
208  frames = 0;
209  then = SDL_GetTicks();
210  done = 0;
211 
212 #ifdef __EMSCRIPTEN__
213  emscripten_set_main_loop(loop, 0, 1);
214 #else
215  while (!done) {
216  ++frames;
217  loop();
218  }
219 #endif
220  /* Print out some timing information */
221  now = SDL_GetTicks();
222  if (now > then) {
223  double fps = ((double) frames * 1000) / (now - then);
224  SDL_Log("%2.2f frames per second\n", fps);
225  }
226 
227  SDL_stack_free(drawstates);
228 
229  quit(0);
230  return 0;
231 }
232 
233 /* vi: set ts=4 sw=4 expandtab: */
#define SDL_CreateTexture
SDL_Renderer * renderer
static void quit(int rc)
#define SDL_PollEvent
#define SDL_GetError
SDL_Rect sprite_rect
SDL_Texture * LoadTexture(SDL_Renderer *renderer, const char *file, SDL_bool transparent)
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.
int SDLTest_CommonArg(SDLTest_CommonState *state, int index)
Process one common argument.
The structure that defines a point.
Definition: SDL_rect.h:48
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
void Draw(DrawState *s)
DrawState * drawstates
int done
uint32_t Uint32
Definition: SDL_stdinc.h:181
SDL_bool SDLTest_CommonInit(SDLTest_CommonState *state)
Open test window.
SDL_Window * window
static SDLTest_CommonState * state
SDL_Window ** windows
#define SDL_LogError
void loop()
#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
#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
SDL_RendererFlip
Flip constants for SDL_RenderCopyEx.
Definition: SDL_render.h:111
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
int main(int argc, char *argv[])
SDL_PixelFormat * format
Definition: SDL_surface.h:72
#define SDL_DestroyTexture
int h
Definition: SDL_rect.h:67
The type used to identify a window.
Definition: SDL_sysvideo.h:73
SDL_Rect viewport
Definition: testviewport.c:28
SDL_Texture * sprite
#define SDL_RenderCopyEx
uint16_t Uint16
Definition: SDL_stdinc.h:169
General event structure.
Definition: SDL_events.h:525
SDL_Palette * palette
Definition: SDL_pixels.h:318
SDL_Texture * background
#define SDL_stack_free(data)
Definition: SDL_stdinc.h:355
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