annotate XCodeiPhoneOS/Demos/src/accelerometer.c @ 2385:0705e8ebe951 gsoc2008_iphone

Rectangles demo (SDL_RenderFill)
author Holmes Futrell <hfutrell@umail.ucsb.edu>
date Fri, 18 Jul 2008 20:52:18 +0000
parents 7fbcfc1574dc
children ecc18fbfdec3
rev   line source
2380
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
1 /*
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
2 * accelerometer.c
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
3 * written by Holmes Futrell
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
4 * use however you want
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
5 */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
6
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
7 #include "SDL.h"
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
8 #include "math.h"
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
9 #include "common.h"
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
10
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
11 #define MILLESECONDS_PER_FRAME 16 /* about 60 frames per second */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
12 #define DAMPING 0.5f; /* after bouncing off a wall, damping coefficient determines final speed */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
13 #define FRICTION 0.0008f /* coefficient of acceleration that opposes direction of motion */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
14 #define GRAVITY_CONSTANT 0.02f /* how sensitive the ship is to the accelerometer */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
15
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
16 static SDL_Joystick *accelerometer; /* used for controlling the ship */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
17
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
18 static struct {
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
19 float x, y; /* position of ship */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
20 float vx, vy; /* velocity of ship (in pixels per millesecond) */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
21 SDL_Rect rect; /* (drawn) position and size of ship */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
22 } ship;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
23
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
24 static SDL_TextureID shipID=0; /* texture for spaceship */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
25 static SDL_TextureID spaceID=0; /* texture for space (background */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
26
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
27 void render(void) {
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
28
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
29 /* get joystick (accelerometer) axis values and normalize them */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
30 float amax = (float)(0x7FFF); /* largest Sint16 number */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
31 float ax = SDL_JoystickGetAxis(accelerometer, 0) / amax;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
32 float ay = -SDL_JoystickGetAxis(accelerometer, 1) / amax;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
33
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
34 /* ship screen constraints */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
35 Uint32 minx = 0.0f;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
36 Uint32 maxx = SCREEN_WIDTH - ship.rect.w;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
37 Uint32 miny = 0.0f;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
38 Uint32 maxy = SCREEN_HEIGHT - ship.rect.h;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
39
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
40 /* update velocity from accelerometer */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
41 ship.vx += ax * GRAVITY_CONSTANT * MILLESECONDS_PER_FRAME;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
42 ship.vy += ay * GRAVITY_CONSTANT * MILLESECONDS_PER_FRAME;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
43
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
44 float speed = sqrt(ship.vx * ship.vx + ship.vy * ship.vy);
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
45
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
46 if (speed > 0) {
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
47 /* compensate for friction */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
48 float dirx = ship.vx / speed; /* normalized x velocity */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
49 float diry = ship.vy / speed; /* normalized y velocity */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
50
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
51 /* update velocity due to friction */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
52 if (speed - FRICTION * MILLESECONDS_PER_FRAME > 0) {
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
53 /* apply friction */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
54 ship.vx -= dirx * FRICTION * MILLESECONDS_PER_FRAME;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
55 ship.vy -= diry * FRICTION * MILLESECONDS_PER_FRAME;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
56 }
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
57 else {
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
58 /* applying friction would MORE than stop the ship, so just stop the ship */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
59 ship.vx = 0.0f;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
60 ship.vy = 0.0f;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
61 }
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
62 }
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
63
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
64 /* update ship location */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
65 ship.x += ship.vx * MILLESECONDS_PER_FRAME;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
66 ship.y += ship.vy * MILLESECONDS_PER_FRAME;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
67
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
68 if (ship.x > maxx) {
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
69 ship.x = maxx;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
70 ship.vx = -ship.vx * DAMPING;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
71 }
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
72 else if (ship.x < minx) {
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
73 ship.x = minx;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
74 ship.vx = -ship.vx * DAMPING;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
75 }
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
76 if (ship.y > maxy) {
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
77 ship.y = maxy;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
78 ship.vy = -ship.vy * DAMPING;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
79 }
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
80 else if (ship.y < miny) {
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
81 ship.y = miny;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
82 ship.vy = -ship.vy * DAMPING;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
83 }
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
84
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
85 /* draw the background */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
86 SDL_RenderCopy(spaceID, NULL, NULL);
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
87
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
88 /* draw the ship */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
89 ship.rect.x = ship.x;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
90 ship.rect.y = ship.y;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
91
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
92 SDL_RenderCopy(shipID, NULL, &ship.rect);
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
93
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
94 /* update screen */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
95 SDL_RenderPresent();
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
96
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
97 }
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
98
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
99 void initializeTextures() {
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
100
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
101 SDL_Surface *bmp_surface;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
102 SDL_Surface *bmp_surface_rgba;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
103 int format = SDL_PIXELFORMAT_ABGR8888; /* desired texture format */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
104 Uint32 Rmask, Gmask, Bmask, Amask; /* masks for desired format */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
105 int bpp; /* bits per pixel for desired format */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
106
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
107 /* load the ship */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
108 bmp_surface = SDL_LoadBMP("ship.bmp");
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
109 if (bmp_surface == NULL) {
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
110 fatalError("could not ship.bmp");
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
111 }
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
112 /* set blue to transparent on the ship */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
113 SDL_SetColorKey(bmp_surface, 1, SDL_MapRGB(bmp_surface->format, 0, 0, 255));
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
114 SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask, &Amask);
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
115 /*
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
116 create a new RGBA surface and blit the bmp to it
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
117 this is an extra step, but it seems to be necessary for the color key to work
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
118
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
119 does the fact that this is necessary indicate a bug in SDL?
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
120 */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
121 bmp_surface_rgba = SDL_CreateRGBSurface(0, bmp_surface->w, bmp_surface->h, bpp, Rmask, Gmask, Bmask, Amask);
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
122 SDL_BlitSurface(bmp_surface, NULL, bmp_surface_rgba, NULL);
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
123
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
124 /* create ship texture from surface */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
125 shipID = SDL_CreateTextureFromSurface(format, bmp_surface_rgba);
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
126 if (shipID == 0) {
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
127 fatalError("could not create ship texture");
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
128 }
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
129 SDL_SetTextureBlendMode(shipID, SDL_TEXTUREBLENDMODE_BLEND);
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
130
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
131 /* set the width and height of the ship from the surface dimensions */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
132 ship.rect.w = bmp_surface->w;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
133 ship.rect.h = bmp_surface->h;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
134
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
135 SDL_FreeSurface(bmp_surface_rgba);
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
136 SDL_FreeSurface(bmp_surface);
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
137
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
138 /* load the space background */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
139 bmp_surface = SDL_LoadBMP("space.bmp");
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
140 if (bmp_surface == NULL) {
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
141 fatalError("could not load space.bmp");
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
142 }
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
143 /* create space texture from surface */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
144 spaceID = SDL_CreateTextureFromSurface(format, bmp_surface);
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
145 if (spaceID == 0) {
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
146 fatalError("could not create space texture");
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
147 }
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
148 SDL_FreeSurface(bmp_surface);
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
149
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
150 }
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
151
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
152
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
153
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
154 int main(int argc, char *argv[]) {
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
155
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
156 SDL_WindowID windowID; /* ID of main window */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
157 Uint32 startFrame; /* time frame began to process */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
158 Uint32 endFrame; /* time frame ended processing */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
159 Uint32 delay; /* time to pause waiting to draw next frame */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
160 int done; /* should we clean up and exit? */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
161
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
162 /* initialize SDL */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
163 if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_JOYSTICK) < 0) {
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
164 fatalError("Could not initialize SDL");
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
165 }
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
166
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
167 /* create main window and renderer */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
168 windowID = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,\
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
169 SDL_WINDOW_OPENGL|SDL_WINDOW_SHOWN|SDL_WINDOW_BORDERLESS);
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
170 SDL_CreateRenderer(windowID, 0, 0);
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
171
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
172 /* print out some info about joysticks and try to open accelerometer for use */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
173 printf("There are %d joysticks available\n", SDL_NumJoysticks());
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
174 printf("Default joystick (index 0) is %s\n", SDL_JoystickName(0));
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
175 accelerometer = SDL_JoystickOpen(0);
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
176 if (accelerometer == NULL) {
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
177 fatalError("Could not open joystick (accelerometer)");
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
178 }
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
179 printf("joystick number of axis = %d\n", SDL_JoystickNumAxes(accelerometer));
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
180 printf("joystick number of hats = %d\n", SDL_JoystickNumHats(accelerometer));
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
181 printf("joystick number of balls = %d\n", SDL_JoystickNumBalls(accelerometer));
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
182 printf("joystick number of buttons = %d\n", SDL_JoystickNumButtons(accelerometer));
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
183
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
184 /* load graphics */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
185 initializeTextures();
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
186
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
187 /* setup ship */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
188 ship.x = (SCREEN_WIDTH - ship.rect.w) / 2;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
189 ship.y = (SCREEN_HEIGHT - ship.rect.h) / 2;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
190 ship.vx = 0.0f;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
191 ship.vy = 0.0f;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
192
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
193 done = 0;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
194 /* enter main loop */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
195 while(!done) {
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
196 startFrame = SDL_GetTicks();
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
197 SDL_Event event;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
198 while (SDL_PollEvent(&event)) {
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
199 if (event.type == SDL_QUIT) {
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
200 done = 1;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
201 }
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
202 }
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
203 render();
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
204 endFrame = SDL_GetTicks();
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
205
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
206 /* figure out how much time we have left, and then sleep */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
207 delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame);
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
208 if (delay < 0) {
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
209 delay = 0;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
210 } else if (delay > MILLESECONDS_PER_FRAME) {
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
211 delay = MILLESECONDS_PER_FRAME;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
212 }
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
213 SDL_Delay(delay);
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
214 }
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
215
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
216 /* delete textures */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
217 SDL_DestroyTexture(shipID);
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
218 SDL_DestroyTexture(spaceID);
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
219
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
220 /* shutdown SDL */
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
221 SDL_Quit();
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
222
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
223 return 0;
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
224
7fbcfc1574dc Demo of iPhone accelerometer as SDL joystick
Holmes Futrell <hfutrell@umail.ucsb.edu>
parents:
diff changeset
225 }