Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Commit

Permalink
Updated the iPhone demos for the new API
Browse files Browse the repository at this point in the history
  • Loading branch information
slouken committed Feb 6, 2011
1 parent 2338a7b commit bc43320
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 84 deletions.
4 changes: 2 additions & 2 deletions Xcode-iPhoneOS/Demos/DemosiPhoneOS.xcodeproj/project.pbxproj
Expand Up @@ -748,7 +748,7 @@
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = ../../include;
LIBRARY_SEARCH_PATHS = "\"$(SRCROOT)/../SDLiPod/build/Release-iphoneos\"";
LIBRARY_SEARCH_PATHS = "";
ONLY_ACTIVE_ARCH = YES;
OTHER_CFLAGS = "";
PREBINDING = NO;
Expand All @@ -767,7 +767,7 @@
GCC_WARN_ABOUT_RETURN_TYPE = YES;
GCC_WARN_UNUSED_VARIABLE = YES;
HEADER_SEARCH_PATHS = ../../include;
LIBRARY_SEARCH_PATHS = "\"$(SRCROOT)/../SDLiPod/build/Release-iphoneos\"";
LIBRARY_SEARCH_PATHS = "";
OTHER_CFLAGS = "";
PREBINDING = NO;
PRELINK_LIBS = "";
Expand Down
37 changes: 11 additions & 26 deletions Xcode-iPhoneOS/Demos/src/accelerometer.c
Expand Up @@ -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)
{


Expand Down Expand Up @@ -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");
Expand All @@ -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");
}
Expand All @@ -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 */
Expand All @@ -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");
}
Expand All @@ -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 */
Expand All @@ -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());
Expand All @@ -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;
Expand All @@ -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 */
Expand Down
4 changes: 1 addition & 3 deletions Xcode-iPhoneOS/Demos/src/fireworks.c
Expand Up @@ -173,9 +173,6 @@ drawParticles()
/* draw our particles! */
glDrawArrays(GL_POINTS, 0, num_active_particles);

/* update screen */
SDL_RenderPresent();

}

/*
Expand Down Expand Up @@ -437,6 +434,7 @@ main(int argc, char *argv[])
}
stepParticles();
drawParticles();
SDL_GL_SwapWindow(window);
endFrame = SDL_GetTicks();

/* figure out how much time we have left, and then sleep */
Expand Down
38 changes: 13 additions & 25 deletions Xcode-iPhoneOS/Demos/src/happy.c
Expand Up @@ -36,7 +36,7 @@ initializeHappyFaces()
}

void
render(void)
render(SDL_Renderer *renderer)
{

int i;
Expand All @@ -58,8 +58,8 @@ render(void)
dstRect.h = HAPPY_FACE_SIZE;

/* fill background in with black */
SDL_SetRenderDrawColor(0, 0, 0, 255);
SDL_RenderFill(NULL);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);

/*
loop through all the happy faces:
Expand All @@ -86,24 +86,20 @@ render(void)
}
dstRect.x = faces[i].x;
dstRect.y = faces[i].y;
SDL_RenderCopy(texture, &srcRect, &dstRect);
SDL_RenderCopy(renderer, texture, &srcRect, &dstRect);
}
/* update screen */
SDL_RenderPresent();
SDL_RenderPresent(renderer);

}

/*
loads the happyface graphic into a texture
*/
void
initializeTexture()
initializeTexture(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 bmp */
bmp_surface = SDL_LoadBMP("icon.bmp");
if (bmp_surface == NULL) {
Expand All @@ -112,26 +108,15 @@ initializeTexture()
/* set white to transparent on the happyface */
SDL_SetColorKey(bmp_surface, 1,
SDL_MapRGB(bmp_surface->format, 255, 255, 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
is this a bug?
*/
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);

/* convert RGBA surface to texture */
texture = SDL_CreateTextureFromSurface(format, bmp_surface_rgba);
texture = SDL_CreateTextureFromSurface(renderer, bmp_surface);
if (texture == 0) {
fatalError("could not create texture");
}
SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);

/* free up allocated memory */
SDL_FreeSurface(bmp_surface_rgba);
SDL_FreeSurface(bmp_surface);
}

Expand All @@ -140,6 +125,7 @@ main(int argc, char *argv[])
{

SDL_Window *window;
SDL_Renderer *renderer;
Uint32 startFrame;
Uint32 endFrame;
Uint32 delay;
Expand All @@ -153,9 +139,11 @@ main(int argc, char *argv[])
SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN |
SDL_WINDOW_BORDERLESS);

SDL_CreateRenderer(window, -1, 0);
//SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengles2");

renderer = SDL_CreateRenderer(window, -1, 0);

initializeTexture();
initializeTexture(renderer);
initializeHappyFaces();

/* main loop */
Expand All @@ -168,7 +156,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 */
Expand Down
18 changes: 9 additions & 9 deletions Xcode-iPhoneOS/Demos/src/mixer.c
Expand Up @@ -33,7 +33,6 @@ static struct sound drums[NUM_DRUMS];
void handleMouseButtonDown(SDL_Event * event);
void handleMouseButtonUp(SDL_Event * event);
int playSound(struct sound *);
void render(void);
void initializeButtons();
void audioCallback(void *userdata, Uint8 * stream, int len);
void loadSound(const char *file, struct sound *s);
Expand Down Expand Up @@ -163,20 +162,20 @@ handleMouseButtonUp(SDL_Event * event)

/* draws buttons to screen */
void
render(void)
render(SDL_Renderer *renderer)
{
int i;
SDL_SetRenderDrawColor(50, 50, 50, 255);
SDL_RenderFill(NULL); /* draw background (gray) */
SDL_SetRenderDrawColor(renderer, 50, 50, 50, 255);
SDL_RenderClear(renderer); /* draw background (gray) */
/* draw the drum buttons */
for (i = 0; i < NUM_DRUMS; i++) {
SDL_Color color =
buttons[i].isPressed ? buttons[i].downColor : buttons[i].upColor;
SDL_SetRenderDrawColor(color.r, color.g, color.b, color.unused);
SDL_RenderFill(&buttons[i].rect);
SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.unused);
SDL_RenderFillRect(renderer, &buttons[i].rect);
}
/* update the screen */
SDL_RenderPresent();
SDL_RenderPresent(renderer);
}

/*
Expand Down Expand Up @@ -274,6 +273,7 @@ main(int argc, char *argv[])

int done; /* has user tried to quit ? */
SDL_Window *window; /* main window */
SDL_Renderer *renderer;
SDL_Event event;
Uint32 startFrame; /* holds when frame started processing */
Uint32 endFrame; /* holds when frame ended processing */
Expand All @@ -285,7 +285,7 @@ main(int argc, char *argv[])
window =
SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS);
SDL_CreateRenderer(window, 0, 0);
renderer = SDL_CreateRenderer(window, 0, 0);

/* initialize the mixer */
SDL_memset(&mixer, 0, sizeof(mixer));
Expand Down Expand Up @@ -328,7 +328,7 @@ main(int argc, char *argv[])
break;
}
}
render(); /* draw buttons */
render(renderer); /* draw buttons */
endFrame = SDL_GetTicks();

/* figure out how much time we have left, and then sleep */
Expand Down
18 changes: 10 additions & 8 deletions Xcode-iPhoneOS/Demos/src/rectangles.c
Expand Up @@ -9,7 +9,7 @@
#include "common.h"

void
render(void)
render(SDL_Renderer *renderer)
{

Uint8 r, g, b;
Expand All @@ -26,11 +26,11 @@ render(void)
b = randomInt(50, 255);

/* Fill the rectangle in the color */
SDL_SetRenderDrawColor(r, g, b, 255);
SDL_RenderFill(&rect);
SDL_SetRenderDrawColor(renderer, r, g, b, 255);
SDL_RenderFillRect(renderer, &rect);

/* update screen */
SDL_RenderPresent();
SDL_RenderPresent(renderer);

}

Expand All @@ -39,6 +39,7 @@ main(int argc, char *argv[])
{

SDL_Window *window;
SDL_Renderer *renderer;
int done;
SDL_Event event;

Expand All @@ -57,13 +58,14 @@ main(int argc, char *argv[])
if (window == 0) {
fatalError("Could not initialize Window");
}
if (SDL_CreateRenderer(window, -1, 0) != 0) {
renderer = SDL_CreateRenderer(window, -1, 0);
if (!renderer) {
fatalError("Could not create renderer");
}

/* Fill screen with black */
SDL_SetRenderDrawColor(0, 0, 0, 255);
SDL_RenderFill(NULL);
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);

/* Enter render loop, waiting for user to quit */
done = 0;
Expand All @@ -73,7 +75,7 @@ main(int argc, char *argv[])
done = 1;
}
}
render();
render(renderer);
SDL_Delay(1);
}

Expand Down

0 comments on commit bc43320

Please sign in to comment.