Mercurial > sdl-ios-xcode
comparison Xcode-iPhoneOS/Demos/src/accelerometer.c @ 5211:78db79f5a4e2
Updated the iPhone demos for the new API
author | Sam Lantinga <slouken@libsdl.org> |
---|---|
date | Sun, 06 Feb 2011 09:02:10 -0800 |
parents | e431b888ac6c |
children |
comparison
equal
deleted
inserted
replaced
5210:443a850284a1 | 5211:78db79f5a4e2 |
---|---|
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 |
34 render(void) | 34 render(SDL_Renderer *renderer) |
35 { | 35 { |
36 | 36 |
37 | 37 |
38 /* get joystick (accelerometer) axis values and normalize them */ | 38 /* get joystick (accelerometer) axis values and normalize them */ |
39 float ax = SDL_JoystickGetAxis(accelerometer, 0); | 39 float ax = SDL_JoystickGetAxis(accelerometer, 0); |
95 shipData.y = miny; | 95 shipData.y = miny; |
96 shipData.vy = -shipData.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(renderer, space, NULL, NULL); |
101 | 101 |
102 /* draw the ship */ | 102 /* draw the ship */ |
103 shipData.rect.x = shipData.x; | 103 shipData.rect.x = shipData.x; |
104 shipData.rect.y = shipData.y; | 104 shipData.rect.y = shipData.y; |
105 | 105 |
106 SDL_RenderCopy(ship, NULL, &shipData.rect); | 106 SDL_RenderCopy(renderer, ship, NULL, &shipData.rect); |
107 | 107 |
108 /* update screen */ | 108 /* update screen */ |
109 SDL_RenderPresent(); | 109 SDL_RenderPresent(renderer); |
110 | 110 |
111 } | 111 } |
112 | 112 |
113 void | 113 void |
114 initializeTextures() | 114 initializeTextures(SDL_Renderer *renderer) |
115 { | 115 { |
116 | 116 |
117 SDL_Surface *bmp_surface; | 117 SDL_Surface *bmp_surface; |
118 SDL_Surface *bmp_surface_rgba; | |
119 int format = SDL_PIXELFORMAT_ABGR8888; /* desired texture format */ | |
120 Uint32 Rmask, Gmask, Bmask, Amask; /* masks for desired format */ | |
121 int bpp; /* bits per pixel for desired format */ | |
122 | 118 |
123 /* load the ship */ | 119 /* load the ship */ |
124 bmp_surface = SDL_LoadBMP("ship.bmp"); | 120 bmp_surface = SDL_LoadBMP("ship.bmp"); |
125 if (bmp_surface == NULL) { | 121 if (bmp_surface == NULL) { |
126 fatalError("could not ship.bmp"); | 122 fatalError("could not ship.bmp"); |
127 } | 123 } |
128 /* set blue to transparent on the ship */ | 124 /* set blue to transparent on the ship */ |
129 SDL_SetColorKey(bmp_surface, 1, | 125 SDL_SetColorKey(bmp_surface, 1, |
130 SDL_MapRGB(bmp_surface->format, 0, 0, 255)); | 126 SDL_MapRGB(bmp_surface->format, 0, 0, 255)); |
131 SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask, &Amask); | |
132 /* | |
133 create a new RGBA surface and blit the bmp to it | |
134 this is an extra step, but it seems to be necessary for the color key to work | |
135 | |
136 does the fact that this is necessary indicate a bug in SDL? | |
137 */ | |
138 bmp_surface_rgba = | |
139 SDL_CreateRGBSurface(0, bmp_surface->w, bmp_surface->h, bpp, Rmask, | |
140 Gmask, Bmask, Amask); | |
141 SDL_BlitSurface(bmp_surface, NULL, bmp_surface_rgba, NULL); | |
142 | 127 |
143 /* create ship texture from surface */ | 128 /* create ship texture from surface */ |
144 ship = SDL_CreateTextureFromSurface(format, bmp_surface_rgba); | 129 ship = SDL_CreateTextureFromSurface(renderer, bmp_surface); |
145 if (ship == 0) { | 130 if (ship == 0) { |
146 fatalError("could not create ship texture"); | 131 fatalError("could not create ship texture"); |
147 } | 132 } |
148 SDL_SetTextureBlendMode(ship, SDL_BLENDMODE_BLEND); | 133 SDL_SetTextureBlendMode(ship, SDL_BLENDMODE_BLEND); |
149 | 134 |
150 /* set the width and height of the ship from the surface dimensions */ | 135 /* set the width and height of the ship from the surface dimensions */ |
151 shipData.rect.w = bmp_surface->w; | 136 shipData.rect.w = bmp_surface->w; |
152 shipData.rect.h = bmp_surface->h; | 137 shipData.rect.h = bmp_surface->h; |
153 | 138 |
154 SDL_FreeSurface(bmp_surface_rgba); | |
155 SDL_FreeSurface(bmp_surface); | 139 SDL_FreeSurface(bmp_surface); |
156 | 140 |
157 /* load the space background */ | 141 /* load the space background */ |
158 bmp_surface = SDL_LoadBMP("space.bmp"); | 142 bmp_surface = SDL_LoadBMP("space.bmp"); |
159 if (bmp_surface == NULL) { | 143 if (bmp_surface == NULL) { |
160 fatalError("could not load space.bmp"); | 144 fatalError("could not load space.bmp"); |
161 } | 145 } |
162 /* create space texture from surface */ | 146 /* create space texture from surface */ |
163 space = SDL_CreateTextureFromSurface(format, bmp_surface); | 147 space = SDL_CreateTextureFromSurface(renderer, bmp_surface); |
164 if (space == 0) { | 148 if (space == 0) { |
165 fatalError("could not create space texture"); | 149 fatalError("could not create space texture"); |
166 } | 150 } |
167 SDL_FreeSurface(bmp_surface); | 151 SDL_FreeSurface(bmp_surface); |
168 | 152 |
173 int | 157 int |
174 main(int argc, char *argv[]) | 158 main(int argc, char *argv[]) |
175 { | 159 { |
176 | 160 |
177 SDL_Window *window; /* main window */ | 161 SDL_Window *window; /* main window */ |
162 SDL_Renderer *renderer; | |
178 Uint32 startFrame; /* time frame began to process */ | 163 Uint32 startFrame; /* time frame began to process */ |
179 Uint32 endFrame; /* time frame ended processing */ | 164 Uint32 endFrame; /* time frame ended processing */ |
180 Uint32 delay; /* time to pause waiting to draw next frame */ | 165 Uint32 delay; /* time to pause waiting to draw next frame */ |
181 int done; /* should we clean up and exit? */ | 166 int done; /* should we clean up and exit? */ |
182 | 167 |
187 | 172 |
188 /* create main window and renderer */ | 173 /* create main window and renderer */ |
189 window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, | 174 window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, |
190 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | | 175 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | |
191 SDL_WINDOW_BORDERLESS); | 176 SDL_WINDOW_BORDERLESS); |
192 SDL_CreateRenderer(window, 0, 0); | 177 renderer = SDL_CreateRenderer(window, 0, 0); |
193 | 178 |
194 /* print out some info about joysticks and try to open accelerometer for use */ | 179 /* print out some info about joysticks and try to open accelerometer for use */ |
195 printf("There are %d joysticks available\n", SDL_NumJoysticks()); | 180 printf("There are %d joysticks available\n", SDL_NumJoysticks()); |
196 printf("Default joystick (index 0) is %s\n", SDL_JoystickName(0)); | 181 printf("Default joystick (index 0) is %s\n", SDL_JoystickName(0)); |
197 accelerometer = SDL_JoystickOpen(0); | 182 accelerometer = SDL_JoystickOpen(0); |
206 SDL_JoystickNumBalls(accelerometer)); | 191 SDL_JoystickNumBalls(accelerometer)); |
207 printf("joystick number of buttons = %d\n", | 192 printf("joystick number of buttons = %d\n", |
208 SDL_JoystickNumButtons(accelerometer)); | 193 SDL_JoystickNumButtons(accelerometer)); |
209 | 194 |
210 /* load graphics */ | 195 /* load graphics */ |
211 initializeTextures(); | 196 initializeTextures(renderer); |
212 | 197 |
213 /* setup ship */ | 198 /* setup ship */ |
214 shipData.x = (SCREEN_WIDTH - shipData.rect.w) / 2; | 199 shipData.x = (SCREEN_WIDTH - shipData.rect.w) / 2; |
215 shipData.y = (SCREEN_HEIGHT - shipData.rect.h) / 2; | 200 shipData.y = (SCREEN_HEIGHT - shipData.rect.h) / 2; |
216 shipData.vx = 0.0f; | 201 shipData.vx = 0.0f; |
224 while (SDL_PollEvent(&event)) { | 209 while (SDL_PollEvent(&event)) { |
225 if (event.type == SDL_QUIT) { | 210 if (event.type == SDL_QUIT) { |
226 done = 1; | 211 done = 1; |
227 } | 212 } |
228 } | 213 } |
229 render(); | 214 render(renderer); |
230 endFrame = SDL_GetTicks(); | 215 endFrame = SDL_GetTicks(); |
231 | 216 |
232 /* figure out how much time we have left, and then sleep */ | 217 /* figure out how much time we have left, and then sleep */ |
233 delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame); | 218 delay = MILLESECONDS_PER_FRAME - (endFrame - startFrame); |
234 if (delay < 0) { | 219 if (delay < 0) { |