comparison Xcode-iPhoneOS/Demos/src/accelerometer.c @ 3685:64ce267332c6

Switched from SDL_WindowID and SDL_TextureID to SDL_Window* and SDL_Texture* for code simplicity and improved performance.
author Sam Lantinga <slouken@libsdl.org>
date Thu, 21 Jan 2010 06:21:52 +0000
parents 20326ba2bda2
children e431b888ac6c
comparison
equal deleted inserted replaced
3684:cc564f08884f 3685:64ce267332c6
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 } ship;
29 29
30 static SDL_TextureID shipID = 0; /* texture for spaceship */ 30 static SDL_Texture *ship = 0; /* texture for spaceship */
31 static SDL_TextureID spaceID = 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(void)
35 { 35 {
36 36
95 ship.y = miny; 95 ship.y = miny;
96 ship.vy = -ship.vy * DAMPING; 96 ship.vy = -ship.vy * DAMPING;
97 } 97 }
98 98
99 /* draw the background */ 99 /* draw the background */
100 SDL_RenderCopy(spaceID, 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 ship.rect.x = ship.x;
104 ship.rect.y = ship.y; 104 ship.rect.y = ship.y;
105 105
106 SDL_RenderCopy(shipID, NULL, &ship.rect); 106 SDL_RenderCopy(ship, NULL, &ship.rect);
107 107
108 /* update screen */ 108 /* update screen */
109 SDL_RenderPresent(); 109 SDL_RenderPresent();
110 110
111 } 111 }
139 SDL_CreateRGBSurface(0, bmp_surface->w, bmp_surface->h, bpp, Rmask, 139 SDL_CreateRGBSurface(0, bmp_surface->w, bmp_surface->h, bpp, Rmask,
140 Gmask, Bmask, Amask); 140 Gmask, Bmask, Amask);
141 SDL_BlitSurface(bmp_surface, NULL, bmp_surface_rgba, NULL); 141 SDL_BlitSurface(bmp_surface, NULL, bmp_surface_rgba, NULL);
142 142
143 /* create ship texture from surface */ 143 /* create ship texture from surface */
144 shipID = SDL_CreateTextureFromSurface(format, bmp_surface_rgba); 144 ship = SDL_CreateTextureFromSurface(format, bmp_surface_rgba);
145 if (shipID == 0) { 145 if (ship == 0) {
146 fatalError("could not create ship texture"); 146 fatalError("could not create ship texture");
147 } 147 }
148 SDL_SetTextureBlendMode(shipID, 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 ship.rect.w = bmp_surface->w;
152 ship.rect.h = bmp_surface->h; 152 ship.rect.h = bmp_surface->h;
153 153
158 bmp_surface = SDL_LoadBMP("space.bmp"); 158 bmp_surface = SDL_LoadBMP("space.bmp");
159 if (bmp_surface == NULL) { 159 if (bmp_surface == NULL) {
160 fatalError("could not load space.bmp"); 160 fatalError("could not load space.bmp");
161 } 161 }
162 /* create space texture from surface */ 162 /* create space texture from surface */
163 spaceID = SDL_CreateTextureFromSurface(format, bmp_surface); 163 space = SDL_CreateTextureFromSurface(format, bmp_surface);
164 if (spaceID == 0) { 164 if (space == 0) {
165 fatalError("could not create space texture"); 165 fatalError("could not create space texture");
166 } 166 }
167 SDL_FreeSurface(bmp_surface); 167 SDL_FreeSurface(bmp_surface);
168 168
169 } 169 }
172 172
173 int 173 int
174 main(int argc, char *argv[]) 174 main(int argc, char *argv[])
175 { 175 {
176 176
177 SDL_WindowID windowID; /* ID of main window */ 177 SDL_Window *window; /* main window */
178 Uint32 startFrame; /* time frame began to process */ 178 Uint32 startFrame; /* time frame began to process */
179 Uint32 endFrame; /* time frame ended processing */ 179 Uint32 endFrame; /* time frame ended processing */
180 Uint32 delay; /* time to pause waiting to draw next frame */ 180 Uint32 delay; /* time to pause waiting to draw next frame */
181 int done; /* should we clean up and exit? */ 181 int done; /* should we clean up and exit? */
182 182
184 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) { 184 if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0) {
185 fatalError("Could not initialize SDL"); 185 fatalError("Could not initialize SDL");
186 } 186 }
187 187
188 /* create main window and renderer */ 188 /* create main window and renderer */
189 windowID = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT, 189 window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
190 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN | 190 SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN |
191 SDL_WINDOW_BORDERLESS); 191 SDL_WINDOW_BORDERLESS);
192 SDL_CreateRenderer(windowID, 0, 0); 192 SDL_CreateRenderer(window, 0, 0);
193 193
194 /* print out some info about joysticks and try to open accelerometer for use */ 194 /* print out some info about joysticks and try to open accelerometer for use */
195 printf("There are %d joysticks available\n", SDL_NumJoysticks()); 195 printf("There are %d joysticks available\n", SDL_NumJoysticks());
196 printf("Default joystick (index 0) is %s\n", SDL_JoystickName(0)); 196 printf("Default joystick (index 0) is %s\n", SDL_JoystickName(0));
197 accelerometer = SDL_JoystickOpen(0); 197 accelerometer = SDL_JoystickOpen(0);
238 } 238 }
239 SDL_Delay(delay); 239 SDL_Delay(delay);
240 } 240 }
241 241
242 /* delete textures */ 242 /* delete textures */
243 SDL_DestroyTexture(shipID); 243 SDL_DestroyTexture(ship);
244 SDL_DestroyTexture(spaceID); 244 SDL_DestroyTexture(space);
245 245
246 /* shutdown SDL */ 246 /* shutdown SDL */
247 SDL_Quit(); 247 SDL_Quit();
248 248
249 return 0; 249 return 0;