Updated the iPhone demos for the new API
authorSam Lantinga <slouken@libsdl.org>
Sun, 06 Feb 2011 09:02:10 -0800
changeset 5208 78db79f5a4e2
parent 5207 443a850284a1
child 5209 115fff0641ee
Updated the iPhone demos for the new API
Xcode-iPhoneOS/Demos/DemosiPhoneOS.xcodeproj/project.pbxproj
Xcode-iPhoneOS/Demos/src/accelerometer.c
Xcode-iPhoneOS/Demos/src/fireworks.c
Xcode-iPhoneOS/Demos/src/happy.c
Xcode-iPhoneOS/Demos/src/mixer.c
Xcode-iPhoneOS/Demos/src/rectangles.c
Xcode-iPhoneOS/Demos/src/touch.c
--- a/Xcode-iPhoneOS/Demos/DemosiPhoneOS.xcodeproj/project.pbxproj	Sun Feb 06 08:57:29 2011 -0800
+++ b/Xcode-iPhoneOS/Demos/DemosiPhoneOS.xcodeproj/project.pbxproj	Sun Feb 06 09:02:10 2011 -0800
@@ -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 = "";
--- a/Xcode-iPhoneOS/Demos/src/accelerometer.c	Sun Feb 06 08:57:29 2011 -0800
+++ b/Xcode-iPhoneOS/Demos/src/accelerometer.c	Sun Feb 06 09:02:10 2011 -0800
@@ -31,7 +31,7 @@
 static SDL_Texture *space = 0;       /* texture for space (background */
 
 void
-render(void)
+render(SDL_Renderer *renderer)
 {
 
 
@@ -97,28 +97,24 @@
     }
 
     /* 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 @@
     /* 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 @@
     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 @@
         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 @@
 {
 
     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 @@
     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 @@
            SDL_JoystickNumButtons(accelerometer));
 
     /* load graphics */
-    initializeTextures();
+    initializeTextures(renderer);
 
     /* setup ship */
     shipData.x = (SCREEN_WIDTH - shipData.rect.w) / 2;
@@ -226,7 +211,7 @@
                 done = 1;
             }
         }
-        render();
+        render(renderer);
         endFrame = SDL_GetTicks();
 
         /* figure out how much time we have left, and then sleep */
--- a/Xcode-iPhoneOS/Demos/src/fireworks.c	Sun Feb 06 08:57:29 2011 -0800
+++ b/Xcode-iPhoneOS/Demos/src/fireworks.c	Sun Feb 06 09:02:10 2011 -0800
@@ -173,9 +173,6 @@
     /* draw our particles! */
     glDrawArrays(GL_POINTS, 0, num_active_particles);
 
-    /* update screen */
-    SDL_RenderPresent();
-
 }
 
 /*
@@ -437,6 +434,7 @@
         }
         stepParticles();
         drawParticles();
+		SDL_GL_SwapWindow(window);
         endFrame = SDL_GetTicks();
 
         /* figure out how much time we have left, and then sleep */
--- a/Xcode-iPhoneOS/Demos/src/happy.c	Sun Feb 06 08:57:29 2011 -0800
+++ b/Xcode-iPhoneOS/Demos/src/happy.c	Sun Feb 06 09:02:10 2011 -0800
@@ -36,7 +36,7 @@
 }
 
 void
-render(void)
+render(SDL_Renderer *renderer)
 {
 
     int i;
@@ -58,8 +58,8 @@
     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 @@
         }
         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 @@
 	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 @@
     /* 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 @@
 {
 
     SDL_Window *window;
+	SDL_Renderer *renderer;
     Uint32 startFrame;
     Uint32 endFrame;
     Uint32 delay;
@@ -153,9 +139,11 @@
                                 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 @@
                 done = 1;
             }
         }
-        render();
+        render(renderer);
         endFrame = SDL_GetTicks();
 
         /* figure out how much time we have left, and then sleep */
--- a/Xcode-iPhoneOS/Demos/src/mixer.c	Sun Feb 06 08:57:29 2011 -0800
+++ b/Xcode-iPhoneOS/Demos/src/mixer.c	Sun Feb 06 09:02:10 2011 -0800
@@ -33,7 +33,6 @@
 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 @@
 
 /* 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 @@
 
     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 @@
     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 @@
                 break;
             }
         }
-        render();               /* draw buttons */
+        render(renderer);               /* draw buttons */
         endFrame = SDL_GetTicks();
 
         /* figure out how much time we have left, and then sleep */
--- a/Xcode-iPhoneOS/Demos/src/rectangles.c	Sun Feb 06 08:57:29 2011 -0800
+++ b/Xcode-iPhoneOS/Demos/src/rectangles.c	Sun Feb 06 09:02:10 2011 -0800
@@ -9,7 +9,7 @@
 #include "common.h"
 
 void
-render(void)
+render(SDL_Renderer *renderer)
 {
 
     Uint8 r, g, b;
@@ -26,11 +26,11 @@
     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 @@
 {
 
     SDL_Window *window;
+	SDL_Renderer *renderer;
     int done;
     SDL_Event event;
 
@@ -57,13 +58,14 @@
     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 @@
                 done = 1;
             }
         }
-        render();
+        render(renderer);
         SDL_Delay(1);
     }
 
--- a/Xcode-iPhoneOS/Demos/src/touch.c	Sun Feb 06 08:57:29 2011 -0800
+++ b/Xcode-iPhoneOS/Demos/src/touch.c	Sun Feb 06 09:02:10 2011 -0800
@@ -18,7 +18,7 @@
 	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 @@
         x += dx_prime;
         y += dy_prime;
         /* draw brush blot */
-        SDL_RenderCopy(brush, NULL, &dstRect);
+        SDL_RenderCopy(renderer, brush, NULL, &dstRect);
     }
 }
 
@@ -51,7 +51,7 @@
 	loads the brush texture
 */
 void
-initializeTexture()
+initializeTexture(SDL_Renderer *renderer)
 {
     SDL_Surface *bmp_surface;
     bmp_surface = SDL_LoadBMP("stroke.bmp");
@@ -59,7 +59,7 @@
         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 @@
     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 @@
     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 @@
             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;
         }