SDL  2.0
testscale.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "SDL_test_common.h"
+ Include dependency graph for testscale.c:

Go to the source code of this file.

Data Structures

struct  DrawState
 

Macros

#define WINDOW_WIDTH   640
 
#define WINDOW_HEIGHT   480
 

Functions

static void quit (int rc)
 
SDL_TextureLoadTexture (SDL_Renderer *renderer, char *file, SDL_bool transparent)
 
void Draw (DrawState *s)
 
void loop ()
 
int main (int argc, char *argv[])
 

Variables

static SDLTest_CommonStatestate
 
DrawStatedrawstates
 
int done
 

Macro Definition Documentation

◆ WINDOW_HEIGHT

#define WINDOW_HEIGHT   480

Definition at line 25 of file testscale.c.

◆ WINDOW_WIDTH

#define WINDOW_WIDTH   640

Definition at line 24 of file testscale.c.

Function Documentation

◆ Draw()

void Draw ( DrawState s)

Definition at line 100 of file testscale.c.

References DrawState::background, SDL_Rect::h, NULL, DrawState::renderer, DrawState::scale_direction, SDL_RenderCopy, SDL_RenderGetViewport, SDL_RenderPresent, DrawState::sprite, DrawState::sprite_rect, viewport, SDL_Rect::w, SDL_Rect::x, and SDL_Rect::y.

Referenced by loop().

101 {
103 
104  SDL_RenderGetViewport(s->renderer, &viewport);
105 
106  /* Draw the background */
108 
109  /* Scale and draw the sprite */
110  s->sprite_rect.w += s->scale_direction;
111  s->sprite_rect.h += s->scale_direction;
112  if (s->scale_direction > 0) {
113  if (s->sprite_rect.w >= viewport.w || s->sprite_rect.h >= viewport.h) {
114  s->scale_direction = -1;
115  }
116  } else {
117  if (s->sprite_rect.w <= 1 || s->sprite_rect.h <= 1) {
118  s->scale_direction = 1;
119  }
120  }
121  s->sprite_rect.x = (viewport.w - s->sprite_rect.w) / 2;
122  s->sprite_rect.y = (viewport.h - s->sprite_rect.h) / 2;
123 
125 
126  /* Update the screen! */
128 }
SDL_Renderer * renderer
SDL_Rect sprite_rect
#define SDL_RenderCopy
#define SDL_RenderGetViewport
int x
Definition: SDL_rect.h:66
int w
Definition: SDL_rect.h:67
#define NULL
Definition: begin_code.h:164
int h
Definition: SDL_rect.h:67
SDL_Rect viewport
Definition: testviewport.c:28
SDL_Texture * sprite
SDL_Texture * background
int y
Definition: SDL_rect.h:66
A rectangle, with the origin at the upper left.
Definition: SDL_rect.h:64
#define SDL_RenderPresent

◆ LoadTexture()

SDL_Texture* LoadTexture ( SDL_Renderer renderer,
char *  file,
SDL_bool  transparent 
)

Definition at line 50 of file testscale.c.

References SDL_PixelFormat::BitsPerPixel, SDL_Surface::format, NULL, SDL_PixelFormat::palette, SDL_Surface::pixels, SDL_CreateTextureFromSurface, SDL_FreeSurface, SDL_GetError, SDL_LoadBMP, SDL_LOG_CATEGORY_APPLICATION, SDL_LogError, SDL_SetColorKey, and SDL_TRUE.

Referenced by main().

51 {
52  SDL_Surface *temp;
54 
55  /* Load the sprite image */
56  temp = SDL_LoadBMP(file);
57  if (temp == NULL) {
58  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s", file, SDL_GetError());
59  return NULL;
60  }
61 
62  /* Set transparent pixel as the pixel at (0,0) */
63  if (transparent) {
64  if (temp->format->palette) {
65  SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
66  } else {
67  switch (temp->format->BitsPerPixel) {
68  case 15:
70  (*(Uint16 *) temp->pixels) & 0x00007FFF);
71  break;
72  case 16:
73  SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
74  break;
75  case 24:
77  (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
78  break;
79  case 32:
80  SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
81  break;
82  }
83  }
84  }
85 
86  /* Create textures from the image */
87  texture = SDL_CreateTextureFromSurface(renderer, temp);
88  if (!texture) {
89  SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
90  SDL_FreeSurface(temp);
91  return NULL;
92  }
93  SDL_FreeSurface(temp);
94 
95  /* We're ready to roll. :) */
96  return texture;
97 }
#define SDL_GetError
#define SDL_LoadBMP(file)
Definition: SDL_surface.h:200
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
uint32_t Uint32
Definition: SDL_stdinc.h:181
#define SDL_LogError
GLenum GLenum GLuint texture
#define SDL_CreateTextureFromSurface
void * pixels
Definition: SDL_surface.h:75
#define SDL_FreeSurface
uint8_t Uint8
Definition: SDL_stdinc.h:157
Uint8 BitsPerPixel
Definition: SDL_pixels.h:319
#define SDL_SetColorKey
#define NULL
Definition: begin_code.h:164
SDL_PixelFormat * format
Definition: SDL_surface.h:72
uint16_t Uint16
Definition: SDL_stdinc.h:169
SDL_Palette * palette
Definition: SDL_pixels.h:318

◆ loop()

void loop ( )

Definition at line 131 of file testscale.c.

References done, Draw(), i, NULL, SDLTest_CommonState::num_windows, SDL_PollEvent, SDLTest_CommonEvent(), and SDLTest_CommonState::windows.

Referenced by main().

132 {
133  int i;
135 
136  /* Check for events */
137  while (SDL_PollEvent(&event)) {
138  SDLTest_CommonEvent(state, &event, &done);
139  }
140  for (i = 0; i < state->num_windows; ++i) {
141  if (state->windows[i] == NULL)
142  continue;
143  Draw(&drawstates[i]);
144  }
145 #ifdef __EMSCRIPTEN__
146  if (done) {
147  emscripten_cancel_main_loop();
148  }
149 #endif
150 }
#define SDL_PollEvent
static SDLTest_CommonState * state
Definition: testscale.c:27
SDL_Window ** windows
DrawState * drawstates
Definition: testscale.c:38
void SDLTest_CommonEvent(SDLTest_CommonState *state, SDL_Event *event, int *done)
Common event handler for test windows.
struct _cl_event * event
void Draw(DrawState *s)
Definition: testscale.c:100
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
#define NULL
Definition: begin_code.h:164
int done
Definition: testscale.c:39
General event structure.
Definition: SDL_events.h:525

◆ main()

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

Definition at line 153 of file testscale.c.

References DrawState::background, done, SDL_Rect::h, i, LoadTexture(), loop(), NULL, SDLTest_CommonState::num_windows, quit(), DrawState::renderer, SDLTest_CommonState::renderers, DrawState::scale_direction, SDL_FALSE, SDL_GetTicks(), SDL_INIT_VIDEO, SDL_Log, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogSetPriority, SDL_QueryTexture, SDL_stack_alloc, SDL_stack_free, SDL_TRUE, SDLTest_CommonArg(), SDLTest_CommonCreateState(), SDLTest_CommonInit(), SDLTest_CommonUsage(), DrawState::sprite, DrawState::sprite_rect, SDL_Rect::w, DrawState::window, and SDLTest_CommonState::windows.

154 {
155  int i;
156  int frames;
157  Uint32 then, now;
158 
159  /* Enable standard application logging */
161 
162  /* Initialize test framework */
164  if (!state) {
165  return 1;
166  }
167  for (i = 1; i < argc;) {
168  int consumed;
169 
170  consumed = SDLTest_CommonArg(state, i);
171  if (consumed == 0) {
172  SDL_Log("Usage: %s %s\n", argv[0], SDLTest_CommonUsage(state));
173  return 1;
174  }
175  i += consumed;
176  }
177  if (!SDLTest_CommonInit(state)) {
178  quit(2);
179  }
180 
182  for (i = 0; i < state->num_windows; ++i) {
183  DrawState *drawstate = &drawstates[i];
184 
185  drawstate->window = state->windows[i];
186  drawstate->renderer = state->renderers[i];
187  drawstate->sprite = LoadTexture(drawstate->renderer, "icon.bmp", SDL_TRUE);
188  drawstate->background = LoadTexture(drawstate->renderer, "sample.bmp", SDL_FALSE);
189  if (!drawstate->sprite || !drawstate->background) {
190  quit(2);
191  }
192  SDL_QueryTexture(drawstate->sprite, NULL, NULL,
193  &drawstate->sprite_rect.w, &drawstate->sprite_rect.h);
194  drawstate->scale_direction = 1;
195  }
196 
197  /* Main render loop */
198  frames = 0;
199  then = SDL_GetTicks();
200  done = 0;
201 
202 #ifdef __EMSCRIPTEN__
203  emscripten_set_main_loop(loop, 0, 1);
204 #else
205  while (!done) {
206  ++frames;
207  loop();
208  }
209 #endif
210 
211  /* Print out some timing information */
212  now = SDL_GetTicks();
213  if (now > then) {
214  double fps = ((double) frames * 1000) / (now - then);
215  SDL_Log("%2.2f frames per second\n", fps);
216  }
217 
219 
220  quit(0);
221  return 0;
222 }
SDL_Renderer * renderer
SDL_Rect sprite_rect
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.
static SDLTest_CommonState * state
Definition: testscale.c:27
uint32_t Uint32
Definition: SDL_stdinc.h:181
SDL_bool SDLTest_CommonInit(SDLTest_CommonState *state)
Open test window.
SDL_Window * window
SDL_Texture * LoadTexture(SDL_Renderer *renderer, char *file, SDL_bool transparent)
Definition: testscale.c:50
SDL_Window ** windows
#define SDL_Log
DrawState * drawstates
Definition: testscale.c:38
Uint32 SDL_GetTicks(void)
Get the number of milliseconds since the SDL library initialization.
#define SDL_stack_alloc(type, count)
Definition: SDL_stdinc.h:354
#define SDL_QueryTexture
const char * SDLTest_CommonUsage(SDLTest_CommonState *state)
Returns common usage information.
void loop()
Definition: testscale.c:131
int w
Definition: SDL_rect.h:67
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
#define SDL_LogSetPriority
#define NULL
Definition: begin_code.h:164
static void quit(int rc)
Definition: testscale.c:43
int h
Definition: SDL_rect.h:67
SDL_Texture * sprite
int done
Definition: testscale.c:39
SDL_Texture * background
#define SDL_stack_free(data)
Definition: SDL_stdinc.h:355
#define SDL_INIT_VIDEO
Definition: SDL.h:78

◆ quit()

static void quit ( int  rc)
static

Definition at line 43 of file testscale.c.

References SDLTest_CommonQuit().

Referenced by main().

44 {
46  exit(rc);
47 }
static SDLTest_CommonState * state
Definition: testscale.c:27
void SDLTest_CommonQuit(SDLTest_CommonState *state)
Close test window.

Variable Documentation

◆ done

int done

Definition at line 39 of file testscale.c.

Referenced by loop(), and main().

◆ drawstates

DrawState* drawstates

Definition at line 38 of file testscale.c.

◆ state

SDLTest_CommonState* state
static

Definition at line 27 of file testscale.c.