From bc43320f6f6cc86ed428bec56ed9cb1fcb98c94d Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sun, 6 Feb 2011 09:02:10 -0800 Subject: [PATCH] Updated the iPhone demos for the new API --- .../DemosiPhoneOS.xcodeproj/project.pbxproj | 4 +- Xcode-iPhoneOS/Demos/src/accelerometer.c | 37 ++++++------------ Xcode-iPhoneOS/Demos/src/fireworks.c | 4 +- Xcode-iPhoneOS/Demos/src/happy.c | 38 +++++++------------ Xcode-iPhoneOS/Demos/src/mixer.c | 18 ++++----- Xcode-iPhoneOS/Demos/src/rectangles.c | 18 +++++---- Xcode-iPhoneOS/Demos/src/touch.c | 23 +++++------ 7 files changed, 58 insertions(+), 84 deletions(-) diff --git a/Xcode-iPhoneOS/Demos/DemosiPhoneOS.xcodeproj/project.pbxproj b/Xcode-iPhoneOS/Demos/DemosiPhoneOS.xcodeproj/project.pbxproj index b4f15f2b4..f25b71e24 100755 --- a/Xcode-iPhoneOS/Demos/DemosiPhoneOS.xcodeproj/project.pbxproj +++ b/Xcode-iPhoneOS/Demos/DemosiPhoneOS.xcodeproj/project.pbxproj @@ -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; @@ -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 = ""; diff --git a/Xcode-iPhoneOS/Demos/src/accelerometer.c b/Xcode-iPhoneOS/Demos/src/accelerometer.c index 91cff0481..339febc11 100644 --- a/Xcode-iPhoneOS/Demos/src/accelerometer.c +++ b/Xcode-iPhoneOS/Demos/src/accelerometer.c @@ -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 */ diff --git a/Xcode-iPhoneOS/Demos/src/fireworks.c b/Xcode-iPhoneOS/Demos/src/fireworks.c index ba1949524..1e36e4f46 100644 --- a/Xcode-iPhoneOS/Demos/src/fireworks.c +++ b/Xcode-iPhoneOS/Demos/src/fireworks.c @@ -173,9 +173,6 @@ drawParticles() /* draw our particles! */ glDrawArrays(GL_POINTS, 0, num_active_particles); - /* update screen */ - SDL_RenderPresent(); - } /* @@ -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 */ diff --git a/Xcode-iPhoneOS/Demos/src/happy.c b/Xcode-iPhoneOS/Demos/src/happy.c index 487b4c3c5..564c2b3eb 100644 --- a/Xcode-iPhoneOS/Demos/src/happy.c +++ b/Xcode-iPhoneOS/Demos/src/happy.c @@ -36,7 +36,7 @@ initializeHappyFaces() } void -render(void) +render(SDL_Renderer *renderer) { int i; @@ -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: @@ -86,10 +86,10 @@ 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); } @@ -97,13 +97,9 @@ render(void) 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) { @@ -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); } @@ -140,6 +125,7 @@ main(int argc, char *argv[]) { SDL_Window *window; + SDL_Renderer *renderer; Uint32 startFrame; Uint32 endFrame; Uint32 delay; @@ -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 */ @@ -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 */ diff --git a/Xcode-iPhoneOS/Demos/src/mixer.c b/Xcode-iPhoneOS/Demos/src/mixer.c index f5808d62e..893361f9a 100644 --- a/Xcode-iPhoneOS/Demos/src/mixer.c +++ b/Xcode-iPhoneOS/Demos/src/mixer.c @@ -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); @@ -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); } /* @@ -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 */ @@ -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)); @@ -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 */ diff --git a/Xcode-iPhoneOS/Demos/src/rectangles.c b/Xcode-iPhoneOS/Demos/src/rectangles.c index e67d8c8aa..4dcb32035 100644 --- a/Xcode-iPhoneOS/Demos/src/rectangles.c +++ b/Xcode-iPhoneOS/Demos/src/rectangles.c @@ -9,7 +9,7 @@ #include "common.h" void -render(void) +render(SDL_Renderer *renderer) { Uint8 r, g, b; @@ -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); } @@ -39,6 +39,7 @@ main(int argc, char *argv[]) { SDL_Window *window; + SDL_Renderer *renderer; int done; SDL_Event event; @@ -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; @@ -73,7 +75,7 @@ main(int argc, char *argv[]) done = 1; } } - render(); + render(renderer); SDL_Delay(1); } diff --git a/Xcode-iPhoneOS/Demos/src/touch.c b/Xcode-iPhoneOS/Demos/src/touch.c index bdaf3b754..a8106cd1a 100644 --- a/Xcode-iPhoneOS/Demos/src/touch.c +++ b/Xcode-iPhoneOS/Demos/src/touch.c @@ -18,7 +18,7 @@ static SDL_Texture *brush = 0; /* texture for the brush */ this is accomplished by drawing several blots spaced PIXELS_PER_ITERATION apart */ void -drawLine(float startx, float starty, float dx, float dy) +drawLine(SDL_Renderer *renderer, float startx, float starty, float dx, float dy) { float distance = sqrt(dx * dx + dy * dy); /* length of line segment (pythagoras) */ @@ -43,7 +43,7 @@ drawLine(float startx, float starty, float dx, float dy) x += dx_prime; y += dy_prime; /* draw brush blot */ - SDL_RenderCopy(brush, NULL, &dstRect); + SDL_RenderCopy(renderer, brush, NULL, &dstRect); } } @@ -51,7 +51,7 @@ drawLine(float startx, float starty, float dx, float dy) loads the brush texture */ void -initializeTexture() +initializeTexture(SDL_Renderer *renderer) { SDL_Surface *bmp_surface; bmp_surface = SDL_LoadBMP("stroke.bmp"); @@ -59,7 +59,7 @@ initializeTexture() fatalError("could not load stroke.bmp"); } brush = - SDL_CreateTextureFromSurface(SDL_PIXELFORMAT_ABGR8888, bmp_surface); + SDL_CreateTextureFromSurface(renderer, bmp_surface); SDL_FreeSurface(bmp_surface); if (brush == 0) { fatalError("could not create brush texture"); @@ -78,6 +78,7 @@ main(int argc, char *argv[]) Uint8 state; /* mouse (touch) state */ SDL_Event event; SDL_Window *window; /* main window */ + SDL_Renderer *renderer; int done; /* does user want to quit? */ /* initialize SDL */ @@ -89,15 +90,15 @@ 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); /*load brush texture */ - initializeTexture(); + initializeTexture(renderer); /* fill canvass initially with all black */ - SDL_SetRenderDrawColor(0, 0, 0, 255); - SDL_RenderFill(NULL); - SDL_RenderPresent(); + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + SDL_RenderClear(); + SDL_RenderPresent(renderer); done = 0; while (!done && SDL_WaitEvent(&event)) { @@ -109,8 +110,8 @@ main(int argc, char *argv[]) state = SDL_GetMouseState(&x, &y); /* get its location */ SDL_GetRelativeMouseState(&dx, &dy); /* find how much the mouse moved */ if (state & SDL_BUTTON_LMASK) { /* is the mouse (touch) down? */ - drawLine(x - dx, y - dy, dx, dy); /* draw line segment */ - SDL_RenderPresent(); + drawLine(renderer, x - dx, y - dy, dx, dy); /* draw line segment */ + SDL_RenderPresent(renderer); } break; }