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

Go to the source code of this file.

Macros

#define WINDOW_WIDTH   640
 
#define WINDOW_HEIGHT   480
 
#define NUM_SPRITES   100
 
#define MAX_SPEED   1
 

Functions

static void quit (int rc)
 
int LoadSprite (char *file, SDL_Renderer *renderer)
 
void MoveSprites (SDL_Renderer *renderer, SDL_Texture *sprite)
 
void loop ()
 
int main (int argc, char *argv[])
 

Variables

static SDL_Texturesprite
 
static SDL_Rect positions [NUM_SPRITES]
 
static SDL_Rect velocities [NUM_SPRITES]
 
static int sprite_w
 
static int sprite_h
 
SDL_Rendererrenderer
 
int done
 

Macro Definition Documentation

◆ MAX_SPEED

#define MAX_SPEED   1

Definition at line 27 of file testspriteminimal.c.

◆ NUM_SPRITES

#define NUM_SPRITES   100

Definition at line 26 of file testspriteminimal.c.

◆ WINDOW_HEIGHT

#define WINDOW_HEIGHT   480

Definition at line 25 of file testspriteminimal.c.

◆ WINDOW_WIDTH

#define WINDOW_WIDTH   640

Definition at line 24 of file testspriteminimal.c.

Function Documentation

◆ LoadSprite()

int LoadSprite ( char *  file,
SDL_Renderer renderer 
)

Definition at line 45 of file testspriteminimal.c.

46{
47 SDL_Surface *temp;
48
49 /* Load the sprite image */
50 temp = SDL_LoadBMP(file);
51 if (temp == NULL) {
52 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't load %s: %s\n", file, SDL_GetError());
53 return (-1);
54 }
55 sprite_w = temp->w;
56 sprite_h = temp->h;
57
58 /* Set transparent pixel as the pixel at (0,0) */
59 if (temp->format->palette) {
60 SDL_SetColorKey(temp, SDL_TRUE, *(Uint8 *) temp->pixels);
61 } else {
62 switch (temp->format->BitsPerPixel) {
63 case 15:
65 (*(Uint16 *) temp->pixels) & 0x00007FFF);
66 break;
67 case 16:
68 SDL_SetColorKey(temp, SDL_TRUE, *(Uint16 *) temp->pixels);
69 break;
70 case 24:
72 (*(Uint32 *) temp->pixels) & 0x00FFFFFF);
73 break;
74 case 32:
75 SDL_SetColorKey(temp, SDL_TRUE, *(Uint32 *) temp->pixels);
76 break;
77 }
78 }
79
80 /* Create textures from the image */
82 if (!sprite) {
83 SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create texture: %s\n", SDL_GetError());
84 SDL_FreeSurface(temp);
85 return (-1);
86 }
87 SDL_FreeSurface(temp);
88
89 /* We're ready to roll. :) */
90 return (0);
91}
#define SDL_SetColorKey
#define SDL_GetError
#define SDL_CreateTextureFromSurface
#define SDL_LogError
#define SDL_FreeSurface
@ SDL_LOG_CATEGORY_APPLICATION
Definition: SDL_log.h:66
@ SDL_TRUE
Definition: SDL_stdinc.h:164
uint32_t Uint32
Definition: SDL_stdinc.h:203
uint16_t Uint16
Definition: SDL_stdinc.h:191
uint8_t Uint8
Definition: SDL_stdinc.h:179
#define SDL_LoadBMP(file)
Definition: SDL_surface.h:201
#define NULL
Definition: begin_code.h:167
Uint8 BitsPerPixel
Definition: SDL_pixels.h:319
SDL_Palette * palette
Definition: SDL_pixels.h:318
A collection of pixels used in software blitting.
Definition: SDL_surface.h:71
SDL_PixelFormat * format
Definition: SDL_surface.h:73
void * pixels
Definition: SDL_surface.h:76
static int sprite_w
SDL_Renderer * renderer
static SDL_Texture * sprite
static int sprite_h

References SDL_PixelFormat::BitsPerPixel, SDL_Surface::format, SDL_Surface::h, NULL, SDL_PixelFormat::palette, SDL_Surface::pixels, renderer, SDL_CreateTextureFromSurface, SDL_FreeSurface, SDL_GetError, SDL_LoadBMP, SDL_LOG_CATEGORY_APPLICATION, SDL_LogError, SDL_SetColorKey, SDL_TRUE, sprite, sprite_h, sprite_w, and SDL_Surface::w.

Referenced by main().

◆ loop()

void loop ( void  )

Definition at line 128 of file testspriteminimal.c.

129{
131
132 /* Check for events */
133 while (SDL_PollEvent(&event)) {
134 if (event.type == SDL_QUIT || event.type == SDL_KEYDOWN) {
135 done = 1;
136 }
137 }
139#ifdef __EMSCRIPTEN__
140 if (done) {
141 emscripten_cancel_main_loop();
142 }
143#endif
144}
#define SDL_PollEvent
@ SDL_QUIT
Definition: SDL_events.h:60
@ SDL_KEYDOWN
Definition: SDL_events.h:96
struct _cl_event * event
int done
void MoveSprites(SDL_Renderer *renderer, SDL_Texture *sprite)
General event structure.
Definition: SDL_events.h:558

References done, MoveSprites(), renderer, SDL_KEYDOWN, SDL_PollEvent, SDL_QUIT, and sprite.

Referenced by main().

◆ main()

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

Definition at line 147 of file testspriteminimal.c.

148{
150 int i;
151
152
153 /* Enable standard application logging */
155
157 quit(2);
158 }
159
160 if (LoadSprite("icon.bmp", renderer) < 0) {
161 quit(2);
162 }
163
164 /* Initialize the sprite positions */
165 srand(time(NULL));
166 for (i = 0; i < NUM_SPRITES; ++i) {
167 positions[i].x = rand() % (WINDOW_WIDTH - sprite_w);
168 positions[i].y = rand() % (WINDOW_HEIGHT - sprite_h);
171 velocities[i].x = 0;
172 velocities[i].y = 0;
173 while (!velocities[i].x && !velocities[i].y) {
174 velocities[i].x = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
175 velocities[i].y = (rand() % (MAX_SPEED * 2 + 1)) - MAX_SPEED;
176 }
177 }
178
179 /* Main render loop */
180 done = 0;
181
182#ifdef __EMSCRIPTEN__
183 emscripten_set_main_loop(loop, 0, 1);
184#else
185 while (!done) {
186 loop();
187 }
188#endif
189 quit(0);
190
191 return 0; /* to prevent compiler warning */
192}
#define SDL_LogSetPriority
#define SDL_CreateWindowAndRenderer
@ SDL_LOG_PRIORITY_INFO
Definition: SDL_log.h:106
GLint GLint GLint GLint GLint GLint y
Definition: SDL_opengl.h:1574
GLint GLint GLint GLint GLint x
Definition: SDL_opengl.h:1574
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
EGLSurface EGLnsecsANDROID time
Definition: eglext.h:518
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
int h
Definition: SDL_rect.h:80
int w
Definition: SDL_rect.h:80
int y
Definition: SDL_rect.h:79
int x
Definition: SDL_rect.h:79
The type used to identify a window.
Definition: SDL_sysvideo.h:74
static SDL_Rect velocities[NUM_SPRITES]
static void quit(int rc)
static SDL_Rect positions[NUM_SPRITES]
#define WINDOW_WIDTH
#define WINDOW_HEIGHT
int LoadSprite(char *file, SDL_Renderer *renderer)
#define NUM_SPRITES
#define MAX_SPEED
void loop()

References done, SDL_Rect::h, i, LoadSprite(), loop(), MAX_SPEED, NULL, NUM_SPRITES, positions, quit(), renderer, SDL_CreateWindowAndRenderer, SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, SDL_LogSetPriority, sprite_h, sprite_w, velocities, SDL_Rect::w, WINDOW_HEIGHT, WINDOW_WIDTH, SDL_Rect::x, and SDL_Rect::y.

◆ MoveSprites()

void MoveSprites ( SDL_Renderer renderer,
SDL_Texture sprite 
)

Definition at line 94 of file testspriteminimal.c.

95{
96 int i;
99 SDL_Rect *position, *velocity;
100
101 /* Draw a gray background */
102 SDL_SetRenderDrawColor(renderer, 0xA0, 0xA0, 0xA0, 0xFF);
104
105 /* Move the sprite, bounce at the wall, and draw */
106 for (i = 0; i < NUM_SPRITES; ++i) {
107 position = &positions[i];
108 velocity = &velocities[i];
109 position->x += velocity->x;
110 if ((position->x < 0) || (position->x >= (window_w - sprite_w))) {
111 velocity->x = -velocity->x;
112 position->x += velocity->x;
113 }
114 position->y += velocity->y;
115 if ((position->y < 0) || (position->y >= (window_h - sprite_h))) {
116 velocity->y = -velocity->y;
117 position->y += velocity->y;
118 }
119
120 /* Blit the sprite onto the screen */
121 SDL_RenderCopy(renderer, sprite, NULL, position);
122 }
123
124 /* Update the screen! */
126}
#define SDL_RenderPresent
#define SDL_SetRenderDrawColor
#define SDL_RenderClear
#define SDL_RenderCopy
A rectangle, with the origin at the upper left (integer).
Definition: SDL_rect.h:78
int window_w
Definition: testoverlay2.c:143
int window_h
Definition: testoverlay2.c:144

References i, NULL, NUM_SPRITES, positions, renderer, SDL_RenderClear, SDL_RenderCopy, SDL_RenderPresent, SDL_SetRenderDrawColor, sprite, sprite_h, sprite_w, velocities, window_h, WINDOW_HEIGHT, window_w, WINDOW_WIDTH, SDL_Rect::x, and SDL_Rect::y.

Referenced by loop().

◆ quit()

static void quit ( int  rc)
static

Definition at line 39 of file testspriteminimal.c.

40{
41 exit(rc);
42}

Referenced by main().

Variable Documentation

◆ done

int done

Definition at line 35 of file testspriteminimal.c.

Referenced by loop(), and main().

◆ positions

SDL_Rect positions[NUM_SPRITES]
static

Definition at line 30 of file testspriteminimal.c.

Referenced by main(), and MoveSprites().

◆ renderer

SDL_Renderer* renderer

Definition at line 34 of file testspriteminimal.c.

Referenced by LoadSprite(), loop(), main(), and MoveSprites().

◆ sprite

SDL_Texture* sprite
static

Definition at line 29 of file testspriteminimal.c.

Referenced by LoadSprite(), loop(), main(), and MoveSprites().

◆ sprite_h

int sprite_h
static

Definition at line 32 of file testspriteminimal.c.

Referenced by LoadSprite(), main(), and MoveSprites().

◆ sprite_w

int sprite_w
static

Definition at line 32 of file testspriteminimal.c.

Referenced by LoadSprite(), main(), and MoveSprites().

◆ velocities

SDL_Rect velocities[NUM_SPRITES]
static

Definition at line 31 of file testspriteminimal.c.

Referenced by main(), and MoveSprites().