SDL  2.0
accelerometer.c File Reference
#include "SDL.h"
#include <math.h>
#include "common.h"
+ Include dependency graph for accelerometer.c:

Go to the source code of this file.

Macros

#define DAMPING   0.5f; /* after bouncing off a wall, damping coefficient determines final speed */
 
#define FRICTION   0.0008f /* coefficient of acceleration that opposes direction of motion */
 
#define GRAVITY_CONSTANT   0.004f /* how sensitive the ship is to the accelerometer */
 
#define SDL_IPHONE_MAX_GFORCE   5.0f
 
#define SINT16_MAX   ((float)(0x7FFF))
 

Functions

void render (SDL_Renderer *renderer, int w, int h, double deltaTime)
 
void initializeTextures (SDL_Renderer *renderer)
 
int main (int argc, char *argv[])
 

Variables

static SDL_Joystick * accelerometer
 
struct {
   float   x
 
   float   y
 
   float   vx
 
   float   vy
 
   SDL_Rect   rect
 
shipData
 
static SDL_Textureship = 0
 
static SDL_Texturespace = 0
 

Macro Definition Documentation

◆ DAMPING

#define DAMPING   0.5f; /* after bouncing off a wall, damping coefficient determines final speed */

Definition at line 11 of file accelerometer.c.

Referenced by render().

◆ FRICTION

#define FRICTION   0.0008f /* coefficient of acceleration that opposes direction of motion */

Definition at line 12 of file accelerometer.c.

Referenced by render().

◆ GRAVITY_CONSTANT

#define GRAVITY_CONSTANT   0.004f /* how sensitive the ship is to the accelerometer */

Definition at line 13 of file accelerometer.c.

Referenced by render().

◆ SDL_IPHONE_MAX_GFORCE

#define SDL_IPHONE_MAX_GFORCE   5.0f

Definition at line 17 of file accelerometer.c.

Referenced by render().

◆ SINT16_MAX

#define SINT16_MAX   ((float)(0x7FFF))

Referenced by render().

Function Documentation

◆ initializeTextures()

void initializeTextures ( SDL_Renderer renderer)

Definition at line 114 of file accelerometer.c.

References fatalError(), SDL_Surface::format, SDL_Surface::h, NULL, SDL_BLENDMODE_BLEND, SDL_CreateTextureFromSurface, SDL_FreeSurface, SDL_LoadBMP, SDL_MapRGB, SDL_SetColorKey, SDL_SetTextureBlendMode, shipData, and SDL_Surface::w.

Referenced by main().

115 {
116 
117  SDL_Surface *bmp_surface;
118 
119  /* load the ship */
120  bmp_surface = SDL_LoadBMP("ship.bmp");
121  if (bmp_surface == NULL) {
122  fatalError("could not ship.bmp");
123  }
124  /* set blue to transparent on the ship */
125  SDL_SetColorKey(bmp_surface, 1,
126  SDL_MapRGB(bmp_surface->format, 0, 0, 255));
127 
128  /* create ship texture from surface */
129  ship = SDL_CreateTextureFromSurface(renderer, bmp_surface);
130  if (ship == 0) {
131  fatalError("could not create ship texture");
132  }
134 
135  /* set the width and height of the ship from the surface dimensions */
136  shipData.rect.w = bmp_surface->w;
137  shipData.rect.h = bmp_surface->h;
138 
139  SDL_FreeSurface(bmp_surface);
140 
141  /* load the space background */
142  bmp_surface = SDL_LoadBMP("space.bmp");
143  if (bmp_surface == NULL) {
144  fatalError("could not load space.bmp");
145  }
146  /* create space texture from surface */
147  space = SDL_CreateTextureFromSurface(renderer, bmp_surface);
148  if (space == 0) {
149  fatalError("could not create space texture");
150  }
151  SDL_FreeSurface(bmp_surface);
152 
153 }
#define SDL_LoadBMP(file)
Definition: SDL_surface.h:200
void fatalError(const char *string)
Definition: common.c:32
A collection of pixels used in software blitting.
Definition: SDL_surface.h:69
#define SDL_SetTextureBlendMode
static struct @61 shipData
#define SDL_CreateTextureFromSurface
#define SDL_FreeSurface
static SDL_Texture * space
Definition: accelerometer.c:30
#define SDL_SetColorKey
#define NULL
Definition: begin_code.h:164
SDL_PixelFormat * format
Definition: SDL_surface.h:72
static SDL_Texture * ship
Definition: accelerometer.c:29
#define SDL_MapRGB

◆ main()

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

Definition at line 158 of file accelerometer.c.

References accelerometer, done, fatalError(), initializeTextures(), NULL, render(), renderer, SDL_CreateRenderer, SDL_CreateWindow, SDL_Delay, SDL_DestroyTexture, SDL_GetWindowSize, SDL_Init, SDL_INIT_JOYSTICK, SDL_INIT_VIDEO, SDL_JoystickName, SDL_JoystickNumAxes, SDL_JoystickNumBalls, SDL_JoystickNumButtons, SDL_JoystickNumHats, SDL_JoystickOpen, SDL_NumJoysticks, SDL_PollEvent, SDL_Quit, SDL_QUIT, SDL_RenderSetLogicalSize, SDL_WINDOW_ALLOW_HIGHDPI, SDL_WINDOW_FULLSCREEN, shipData, SDL_Event::type, and updateDeltaTime().

159 {
160 
161  SDL_Window *window; /* main window */
163  int done; /* should we clean up and exit? */
164  int w, h;
165 
166  /* initialize SDL */
168  fatalError("Could not initialize SDL");
169  }
170 
171  /* create main window and renderer */
173  renderer = SDL_CreateRenderer(window, 0, 0);
174 
175  SDL_GetWindowSize(window, &w, &h);
176  SDL_RenderSetLogicalSize(renderer, w, h);
177 
178  /* print out some info about joysticks and try to open accelerometer for use */
179  printf("There are %d joysticks available\n", SDL_NumJoysticks());
180  printf("Default joystick (index 0) is %s\n", SDL_JoystickName(0));
182  if (accelerometer == NULL) {
183  fatalError("Could not open joystick (accelerometer)");
184  }
185  printf("joystick number of axis = %d\n",
187  printf("joystick number of hats = %d\n",
189  printf("joystick number of balls = %d\n",
191  printf("joystick number of buttons = %d\n",
193 
194  /* load graphics */
195  initializeTextures(renderer);
196 
197  /* setup ship */
198  shipData.x = (w - shipData.rect.w) / 2;
199  shipData.y = (h - shipData.rect.h) / 2;
200  shipData.vx = 0.0f;
201  shipData.vy = 0.0f;
202 
203  done = 0;
204  /* enter main loop */
205  while (!done) {
206  double deltaTime = updateDeltaTime();
208  while (SDL_PollEvent(&event)) {
209  if (event.type == SDL_QUIT) {
210  done = 1;
211  }
212  }
213  render(renderer, w, h, deltaTime);
214  SDL_Delay(1);
215  }
216 
217  /* delete textures */
220 
221  /* shutdown SDL */
222  SDL_Quit();
223 
224  return 0;
225 
226 }
#define SDL_PollEvent
double updateDeltaTime(void)
Definition: common.c:42
#define SDL_INIT_JOYSTICK
Definition: SDL.h:80
void fatalError(const char *string)
Definition: common.c:32
GLfloat GLfloat GLfloat GLfloat h
#define SDL_JoystickOpen
#define SDL_NumJoysticks
static SDL_Joystick * accelerometer
Definition: accelerometer.c:20
#define SDL_JoystickNumButtons
#define SDL_CreateWindow
void render(SDL_Renderer *renderer, int w, int h, double deltaTime)
Definition: accelerometer.c:33
static struct @61 shipData
#define SDL_JoystickName
void initializeTextures(SDL_Renderer *renderer)
#define SDL_JoystickNumAxes
#define SDL_GetWindowSize
static SDL_Renderer * renderer
struct _cl_event * event
#define SDL_RenderSetLogicalSize
#define SDL_Quit
static SDL_Texture * space
Definition: accelerometer.c:30
int done
Definition: checkkeys.c:28
GLubyte GLubyte GLubyte GLubyte w
#define SDL_Delay
#define NULL
Definition: begin_code.h:164
#define SDL_JoystickNumHats
EGLSurface EGLNativeWindowType * window
Definition: eglext.h:1025
#define SDL_DestroyTexture
The type used to identify a window.
Definition: SDL_sysvideo.h:73
static SDL_Texture * ship
Definition: accelerometer.c:29
#define SDL_JoystickNumBalls
#define SDL_Init
General event structure.
Definition: SDL_events.h:557
#define SDL_INIT_VIDEO
Definition: SDL.h:79
#define SDL_CreateRenderer
Uint32 type
Definition: SDL_events.h:559

◆ render()

void render ( SDL_Renderer renderer,
int  w,
int  h,
double  deltaTime 
)

Definition at line 33 of file accelerometer.c.

References accelerometer, DAMPING, FRICTION, GRAVITY_CONSTANT, NULL, SDL_IPHONE_MAX_GFORCE, SDL_JoystickGetAxis, SDL_RenderCopy, SDL_RenderPresent, shipData, and SINT16_MAX.

Referenced by main().

34 {
35  double deltaMilliseconds = deltaTime * 1000;
36  float speed;
37 
38  /* get joystick (accelerometer) axis values and normalize them */
39  float ax = SDL_JoystickGetAxis(accelerometer, 0);
40  float ay = SDL_JoystickGetAxis(accelerometer, 1);
41 
42  /* ship screen constraints */
43  Uint32 minx = 0.0f;
44  Uint32 maxx = w - shipData.rect.w;
45  Uint32 miny = 0.0f;
46  Uint32 maxy = h - shipData.rect.h;
47 
48 #define SINT16_MAX ((float)(0x7FFF))
49 
50  /* update velocity from accelerometer
51  the factor SDL_IPHONE_MAX_G_FORCE / SINT16_MAX converts between
52  SDL's units reported from the joytick, and units of g-force, as reported by the accelerometer
53  */
54  shipData.vx +=
56  deltaMilliseconds;
57  shipData.vy +=
59  deltaMilliseconds;
60 
61  speed = sqrt(shipData.vx * shipData.vx + shipData.vy * shipData.vy);
62 
63  if (speed > 0) {
64  /* compensate for friction */
65  float dirx = shipData.vx / speed; /* normalized x velocity */
66  float diry = shipData.vy / speed; /* normalized y velocity */
67 
68  /* update velocity due to friction */
69  if (speed - FRICTION * deltaMilliseconds > 0) {
70  /* apply friction */
71  shipData.vx -= dirx * FRICTION * deltaMilliseconds;
72  shipData.vy -= diry * FRICTION * deltaMilliseconds;
73  } else {
74  /* applying friction would MORE than stop the ship, so just stop the ship */
75  shipData.vx = 0.0f;
76  shipData.vy = 0.0f;
77  }
78  }
79 
80  /* update ship location */
81  shipData.x += shipData.vx * deltaMilliseconds;
82  shipData.y += shipData.vy * deltaMilliseconds;
83 
84  if (shipData.x > maxx) {
85  shipData.x = maxx;
86  shipData.vx = -shipData.vx * DAMPING;
87  } else if (shipData.x < minx) {
88  shipData.x = minx;
89  shipData.vx = -shipData.vx * DAMPING;
90  }
91  if (shipData.y > maxy) {
92  shipData.y = maxy;
93  shipData.vy = -shipData.vy * DAMPING;
94  } else if (shipData.y < miny) {
95  shipData.y = miny;
96  shipData.vy = -shipData.vy * DAMPING;
97  }
98 
99  /* draw the background */
100  SDL_RenderCopy(renderer, space, NULL, NULL);
101 
102  /* draw the ship */
103  shipData.rect.x = shipData.x;
104  shipData.rect.y = shipData.y;
105 
106  SDL_RenderCopy(renderer, ship, NULL, &shipData.rect);
107 
108  /* update screen */
109  SDL_RenderPresent(renderer);
110 
111 }
#define GRAVITY_CONSTANT
Definition: accelerometer.c:13
GLfloat GLfloat GLfloat GLfloat h
#define FRICTION
Definition: accelerometer.c:12
static SDL_Joystick * accelerometer
Definition: accelerometer.c:20
static struct @61 shipData
#define SDL_RenderCopy
#define SDL_IPHONE_MAX_GFORCE
Definition: accelerometer.c:17
static SDL_Texture * space
Definition: accelerometer.c:30
GLubyte GLubyte GLubyte GLubyte w
#define DAMPING
Definition: accelerometer.c:11
#define NULL
Definition: begin_code.h:164
#define SINT16_MAX
#define SDL_JoystickGetAxis
static SDL_Texture * ship
Definition: accelerometer.c:29
uint32_t Uint32
Definition: SDL_stdinc.h:203
#define SDL_RenderPresent

Variable Documentation

◆ accelerometer

SDL_Joystick* accelerometer
static

Definition at line 20 of file accelerometer.c.

Referenced by IOS_AddJoystickDevice(), main(), and render().

◆ rect

SDL_Rect rect

Definition at line 26 of file accelerometer.c.

◆ ship

SDL_Texture* ship = 0
static

Definition at line 29 of file accelerometer.c.

◆ shipData

struct { ... } shipData

Referenced by initializeTextures(), main(), and render().

◆ space

SDL_Texture* space = 0
static

Definition at line 30 of file accelerometer.c.

◆ vx

float vx

Definition at line 25 of file accelerometer.c.

◆ vy

float vy

Definition at line 25 of file accelerometer.c.

◆ x

float x

Definition at line 24 of file accelerometer.c.

◆ y

float y

Definition at line 24 of file accelerometer.c.