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 */ |