Updated the iPhone demos for the new API

This commit is contained in:
Sam Lantinga 2011-02-06 09:02:10 -08:00
parent a83bca04d0
commit ccdb593a0b
7 changed files with 58 additions and 84 deletions

View file

@ -31,7 +31,7 @@ static SDL_Texture *ship = 0; /* texture for spaceship */
static SDL_Texture *space = 0; /* texture for space (background */
void
render(void)
render(SDL_Renderer *renderer)
{
@ -97,28 +97,24 @@ render(void)
}
/* draw the background */
SDL_RenderCopy(space, NULL, NULL);
SDL_RenderCopy(renderer, space, NULL, NULL);
/* draw the ship */
shipData.rect.x = shipData.x;
shipData.rect.y = shipData.y;
SDL_RenderCopy(ship, NULL, &shipData.rect);
SDL_RenderCopy(renderer, ship, NULL, &shipData.rect);
/* update screen */
SDL_RenderPresent();
SDL_RenderPresent(renderer);
}
void
initializeTextures()
initializeTextures(SDL_Renderer *renderer)
{
SDL_Surface *bmp_surface;
SDL_Surface *bmp_surface_rgba;
int format = SDL_PIXELFORMAT_ABGR8888; /* desired texture format */
Uint32 Rmask, Gmask, Bmask, Amask; /* masks for desired format */
int bpp; /* bits per pixel for desired format */
/* load the ship */
bmp_surface = SDL_LoadBMP("ship.bmp");
@ -128,20 +124,9 @@ initializeTextures()
/* set blue to transparent on the ship */
SDL_SetColorKey(bmp_surface, 1,
SDL_MapRGB(bmp_surface->format, 0, 0, 255));
SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask, &Amask);
/*
create a new RGBA surface and blit the bmp to it
this is an extra step, but it seems to be necessary for the color key to work
does the fact that this is necessary indicate a bug in SDL?
*/
bmp_surface_rgba =
SDL_CreateRGBSurface(0, bmp_surface->w, bmp_surface->h, bpp, Rmask,
Gmask, Bmask, Amask);
SDL_BlitSurface(bmp_surface, NULL, bmp_surface_rgba, NULL);
/* create ship texture from surface */
ship = SDL_CreateTextureFromSurface(format, bmp_surface_rgba);
ship = SDL_CreateTextureFromSurface(renderer, bmp_surface);
if (ship == 0) {
fatalError("could not create ship texture");
}
@ -151,7 +136,6 @@ initializeTextures()
shipData.rect.w = bmp_surface->w;
shipData.rect.h = bmp_surface->h;
SDL_FreeSurface(bmp_surface_rgba);
SDL_FreeSurface(bmp_surface);
/* load the space background */
@ -160,7 +144,7 @@ initializeTextures()
fatalError("could not load space.bmp");
}
/* create space texture from surface */
space = SDL_CreateTextureFromSurface(format, bmp_surface);
space = SDL_CreateTextureFromSurface(renderer, bmp_surface);
if (space == 0) {
fatalError("could not create space texture");
}
@ -175,6 +159,7 @@ main(int argc, char *argv[])
{
SDL_Window *window; /* main window */
SDL_Renderer *renderer;
Uint32 startFrame; /* time frame began to process */
Uint32 endFrame; /* time frame ended processing */
Uint32 delay; /* time to pause waiting to draw next frame */
@ -189,7 +174,7 @@ main(int argc, char *argv[])
window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN |
SDL_WINDOW_BORDERLESS);
SDL_CreateRenderer(window, 0, 0);
renderer = SDL_CreateRenderer(window, 0, 0);
/* print out some info about joysticks and try to open accelerometer for use */
printf("There are %d joysticks available\n", SDL_NumJoysticks());
@ -208,7 +193,7 @@ main(int argc, char *argv[])
SDL_JoystickNumButtons(accelerometer));
/* load graphics */
initializeTextures();
initializeTextures(renderer);
/* setup ship */
shipData.x = (SCREEN_WIDTH - shipData.rect.w) / 2;
@ -226,7 +211,7 @@ main(int argc, char *argv[])
done = 1;
}
}
render();
render(renderer);
endFrame = SDL_GetTicks();
/* figure out how much time we have left, and then sleep */