Mercurial > sdl-ios-xcode
comparison Xcode-iPhoneOS/Demos/src/accelerometer.c @ 3690:e431b888ac6c
Fixed compilation on iPhone
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Thu, 21 Jan 2010 16:12:24 +0000 |
parents | 64ce267332c6 |
children | 78db79f5a4e2 |
comparison
equal
deleted
inserted
replaced
3689:af25b5586af7 | 3690:e431b888ac6c |
---|---|
23 static struct | 23 static struct |
24 { | 24 { |
25 float x, y; /* position of ship */ | 25 float x, y; /* position of ship */ |
26 float vx, vy; /* velocity of ship (in pixels per millesecond) */ | 26 float vx, vy; /* velocity of ship (in pixels per millesecond) */ |
27 SDL_Rect rect; /* (drawn) position and size of ship */ | 27 SDL_Rect rect; /* (drawn) position and size of ship */ |
28 } ship; | 28 } shipData; |
29 | 29 |
30 static SDL_Texture *ship = 0; /* texture for spaceship */ | 30 static SDL_Texture *ship = 0; /* texture for spaceship */ |
31 static SDL_Texture *space = 0; /* texture for space (background */ | 31 static SDL_Texture *space = 0; /* texture for space (background */ |
32 | 32 |
33 void | 33 void |
39 float ax = SDL_JoystickGetAxis(accelerometer, 0); | 39 float ax = SDL_JoystickGetAxis(accelerometer, 0); |
40 float ay = -SDL_JoystickGetAxis(accelerometer, 1); | 40 float ay = -SDL_JoystickGetAxis(accelerometer, 1); |
41 | 41 |
42 /* ship screen constraints */ | 42 /* ship screen constraints */ |
43 Uint32 minx = 0.0f; | 43 Uint32 minx = 0.0f; |
44 Uint32 maxx = SCREEN_WIDTH - ship.rect.w; | 44 Uint32 maxx = SCREEN_WIDTH - shipData.rect.w; |
45 Uint32 miny = 0.0f; | 45 Uint32 miny = 0.0f; |
46 Uint32 maxy = SCREEN_HEIGHT - ship.rect.h; | 46 Uint32 maxy = SCREEN_HEIGHT - shipData.rect.h; |
47 | 47 |
48 #define SINT16_MAX ((float)(0x7FFF)) | 48 #define SINT16_MAX ((float)(0x7FFF)) |
49 | 49 |
50 /* update velocity from accelerometer | 50 /* update velocity from accelerometer |
51 the factor SDL_IPHONE_MAX_G_FORCE / SINT16_MAX converts between | 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 | 52 SDL's units reported from the joytick, and units of g-force, as reported by the accelerometer |
53 */ | 53 */ |
54 ship.vx += | 54 shipData.vx += |
55 ax * SDL_IPHONE_MAX_GFORCE / SINT16_MAX * GRAVITY_CONSTANT * | 55 ax * SDL_IPHONE_MAX_GFORCE / SINT16_MAX * GRAVITY_CONSTANT * |
56 MILLESECONDS_PER_FRAME; | 56 MILLESECONDS_PER_FRAME; |
57 ship.vy += | 57 shipData.vy += |
58 ay * SDL_IPHONE_MAX_GFORCE / SINT16_MAX * GRAVITY_CONSTANT * | 58 ay * SDL_IPHONE_MAX_GFORCE / SINT16_MAX * GRAVITY_CONSTANT * |
59 MILLESECONDS_PER_FRAME; | 59 MILLESECONDS_PER_FRAME; |
60 | 60 |
61 float speed = sqrt(ship.vx * ship.vx + ship.vy * ship.vy); | 61 float speed = sqrt(shipData.vx * shipData.vx + shipData.vy * shipData.vy); |
62 | 62 |
63 if (speed > 0) { | 63 if (speed > 0) { |
64 /* compensate for friction */ | 64 /* compensate for friction */ |
65 float dirx = ship.vx / speed; /* normalized x velocity */ | 65 float dirx = shipData.vx / speed; /* normalized x velocity */ |
66 float diry = ship.vy / speed; /* normalized y velocity */ | 66 float diry = shipData.vy / speed; /* normalized y velocity */ |
67 | 67 |
68 /* update velocity due to friction */ | 68 /* update velocity due to friction */ |
69 if (speed - FRICTION * MILLESECONDS_PER_FRAME > 0) { | 69 if (speed - FRICTION * MILLESECONDS_PER_FRAME > 0) { |
70 /* apply friction */ | 70 /* apply friction */ |
71 ship.vx -= dirx * FRICTION * MILLESECONDS_PER_FRAME; | 71 shipData.vx -= dirx * FRICTION * MILLESECONDS_PER_FRAME; |
72 ship.vy -= diry * FRICTION * MILLESECONDS_PER_FRAME; | 72 shipData.vy -= diry * FRICTION * MILLESECONDS_PER_FRAME; |
73 } else { | 73 } else { |
74 /* applying friction would MORE than stop the ship, so just stop the ship */ | 74 /* applying friction would MORE than stop the ship, so just stop the ship */ |
75 ship.vx = 0.0f; | 75 shipData.vx = 0.0f; |
76 ship.vy = 0.0f; | 76 shipData.vy = 0.0f; |
77 } | 77 } |
78 } | 78 } |
79 | 79 |
80 /* update ship location */ | 80 /* update ship location */ |
81 ship.x += ship.vx * MILLESECONDS_PER_FRAME; | 81 shipData.x += shipData.vx * MILLESECONDS_PER_FRAME; |
82 ship.y += ship.vy * MILLESECONDS_PER_FRAME; | 82 shipData.y += shipData.vy * MILLESECONDS_PER_FRAME; |
83 | 83 |
84 if (ship.x > maxx) { | 84 if (shipData.x > maxx) { |
85 ship.x = maxx; | 85 shipData.x = maxx; |
86 ship.vx = -ship.vx * DAMPING; | 86 shipData.vx = -shipData.vx * DAMPING; |
87 } else if (ship.x < minx) { | 87 } else if (shipData.x < minx) { |
88 ship.x = minx; | 88 shipData.x = minx; |
89 ship.vx = -ship.vx * DAMPING; | 89 shipData.vx = -shipData.vx * DAMPING; |
90 } | 90 } |
91 if (ship.y > maxy) { | 91 if (shipData.y > maxy) { |
92 ship.y = maxy; | 92 shipData.y = maxy; |
93 ship.vy = -ship.vy * DAMPING; | 93 shipData.vy = -shipData.vy * DAMPING; |
94 } else if (ship.y < miny) { | 94 } else if (shipData.y < miny) { |
95 ship.y = miny; | 95 shipData.y = miny; |
96 ship.vy = -ship.vy * DAMPING; | 96 shipData.vy = -shipData.vy * DAMPING; |
97 } | 97 } |
98 | 98 |
99 /* draw the background */ | 99 /* draw the background */ |
100 SDL_RenderCopy(space, NULL, NULL); | 100 SDL_RenderCopy(space, NULL, NULL); |
101 | 101 |
102 /* draw the ship */ | 102 /* draw the ship */ |
103 ship.rect.x = ship.x; | 103 shipData.rect.x = shipData.x; |
104 ship.rect.y = ship.y; | 104 shipData.rect.y = shipData.y; |
105 | 105 |
106 SDL_RenderCopy(ship, NULL, &ship.rect); | 106 SDL_RenderCopy(ship, NULL, &shipData.rect); |
107 | 107 |
108 /* update screen */ | 108 /* update screen */ |
109 SDL_RenderPresent(); | 109 SDL_RenderPresent(); |
110 | 110 |
111 } | 111 } |
146 fatalError("could not create ship texture"); | 146 fatalError("could not create ship texture"); |
147 } | 147 } |
148 SDL_SetTextureBlendMode(ship, SDL_BLENDMODE_BLEND); | 148 SDL_SetTextureBlendMode(ship, SDL_BLENDMODE_BLEND); |
149 | 149 |
150 /* set the width and height of the ship from the surface dimensions */ | 150 /* set the width and height of the ship from the surface dimensions */ |
151 ship.rect.w = bmp_surface->w; | 151 shipData.rect.w = bmp_surface->w; |
152 ship.rect.h = bmp_surface->h; | 152 shipData.rect.h = bmp_surface->h; |
153 | 153 |
154 SDL_FreeSurface(bmp_surface_rgba); | 154 SDL_FreeSurface(bmp_surface_rgba); |
155 SDL_FreeSurface(bmp_surface); | 155 SDL_FreeSurface(bmp_surface); |
156 | 156 |
157 /* load the space background */ | 157 /* load the space background */ |
209 | 209 |
210 /* load graphics */ | 210 /* load graphics */ |
211 initializeTextures(); | 211 initializeTextures(); |
212 | 212 |
213 /* setup ship */ | 213 /* setup ship */ |
214 ship.x = (SCREEN_WIDTH - ship.rect.w) / 2; | 214 shipData.x = (SCREEN_WIDTH - shipData.rect.w) / 2; |
215 ship.y = (SCREEN_HEIGHT - ship.rect.h) / 2; | 215 shipData.y = (SCREEN_HEIGHT - shipData.rect.h) / 2; |
216 ship.vx = 0.0f; | 216 shipData.vx = 0.0f; |
217 ship.vy = 0.0f; | 217 shipData.vy = 0.0f; |
218 | 218 |
219 done = 0; | 219 done = 0; |
220 /* enter main loop */ | 220 /* enter main loop */ |
221 while (!done) { | 221 while (!done) { |
222 startFrame = SDL_GetTicks(); | 222 startFrame = SDL_GetTicks(); |