Updated the iPhone demos for the new API
authorSam Lantinga <slouken@libsdl.org>
Sun, 06 Feb 2011 09:02:10 -0800
changeset 520878db79f5a4e2
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
     1.1 --- a/Xcode-iPhoneOS/Demos/DemosiPhoneOS.xcodeproj/project.pbxproj	Sun Feb 06 08:57:29 2011 -0800
     1.2 +++ b/Xcode-iPhoneOS/Demos/DemosiPhoneOS.xcodeproj/project.pbxproj	Sun Feb 06 09:02:10 2011 -0800
     1.3 @@ -748,7 +748,7 @@
     1.4  				GCC_WARN_ABOUT_RETURN_TYPE = YES;
     1.5  				GCC_WARN_UNUSED_VARIABLE = YES;
     1.6  				HEADER_SEARCH_PATHS = ../../include;
     1.7 -				LIBRARY_SEARCH_PATHS = "\"$(SRCROOT)/../SDLiPod/build/Release-iphoneos\"";
     1.8 +				LIBRARY_SEARCH_PATHS = "";
     1.9  				ONLY_ACTIVE_ARCH = YES;
    1.10  				OTHER_CFLAGS = "";
    1.11  				PREBINDING = NO;
    1.12 @@ -767,7 +767,7 @@
    1.13  				GCC_WARN_ABOUT_RETURN_TYPE = YES;
    1.14  				GCC_WARN_UNUSED_VARIABLE = YES;
    1.15  				HEADER_SEARCH_PATHS = ../../include;
    1.16 -				LIBRARY_SEARCH_PATHS = "\"$(SRCROOT)/../SDLiPod/build/Release-iphoneos\"";
    1.17 +				LIBRARY_SEARCH_PATHS = "";
    1.18  				OTHER_CFLAGS = "";
    1.19  				PREBINDING = NO;
    1.20  				PRELINK_LIBS = "";
     2.1 --- a/Xcode-iPhoneOS/Demos/src/accelerometer.c	Sun Feb 06 08:57:29 2011 -0800
     2.2 +++ b/Xcode-iPhoneOS/Demos/src/accelerometer.c	Sun Feb 06 09:02:10 2011 -0800
     2.3 @@ -31,7 +31,7 @@
     2.4  static SDL_Texture *space = 0;       /* texture for space (background */
     2.5  
     2.6  void
     2.7 -render(void)
     2.8 +render(SDL_Renderer *renderer)
     2.9  {
    2.10  
    2.11  
    2.12 @@ -97,28 +97,24 @@
    2.13      }
    2.14  
    2.15      /* draw the background */
    2.16 -    SDL_RenderCopy(space, NULL, NULL);
    2.17 +    SDL_RenderCopy(renderer, space, NULL, NULL);
    2.18  
    2.19      /* draw the ship */
    2.20      shipData.rect.x = shipData.x;
    2.21      shipData.rect.y = shipData.y;
    2.22  
    2.23 -    SDL_RenderCopy(ship, NULL, &shipData.rect);
    2.24 +    SDL_RenderCopy(renderer, ship, NULL, &shipData.rect);
    2.25  
    2.26      /* update screen */
    2.27 -    SDL_RenderPresent();
    2.28 +    SDL_RenderPresent(renderer);
    2.29  
    2.30  }
    2.31  
    2.32  void
    2.33 -initializeTextures()
    2.34 +initializeTextures(SDL_Renderer *renderer)
    2.35  {
    2.36  
    2.37      SDL_Surface *bmp_surface;
    2.38 -    SDL_Surface *bmp_surface_rgba;
    2.39 -    int format = SDL_PIXELFORMAT_ABGR8888;      /* desired texture format */
    2.40 -    Uint32 Rmask, Gmask, Bmask, Amask;  /* masks for desired format */
    2.41 -    int bpp;                    /* bits per pixel for desired format */
    2.42  
    2.43      /* load the ship */
    2.44      bmp_surface = SDL_LoadBMP("ship.bmp");
    2.45 @@ -128,20 +124,9 @@
    2.46      /* set blue to transparent on the ship */
    2.47      SDL_SetColorKey(bmp_surface, 1,
    2.48                      SDL_MapRGB(bmp_surface->format, 0, 0, 255));
    2.49 -    SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask, &Amask);
    2.50 -    /*
    2.51 -       create a new RGBA surface and blit the bmp to it
    2.52 -       this is an extra step, but it seems to be necessary for the color key to work
    2.53 -
    2.54 -       does the fact that this is necessary indicate a bug in SDL?
    2.55 -     */
    2.56 -    bmp_surface_rgba =
    2.57 -        SDL_CreateRGBSurface(0, bmp_surface->w, bmp_surface->h, bpp, Rmask,
    2.58 -                             Gmask, Bmask, Amask);
    2.59 -    SDL_BlitSurface(bmp_surface, NULL, bmp_surface_rgba, NULL);
    2.60  
    2.61      /* create ship texture from surface */
    2.62 -    ship = SDL_CreateTextureFromSurface(format, bmp_surface_rgba);
    2.63 +    ship = SDL_CreateTextureFromSurface(renderer, bmp_surface);
    2.64      if (ship == 0) {
    2.65          fatalError("could not create ship texture");
    2.66      }
    2.67 @@ -151,7 +136,6 @@
    2.68      shipData.rect.w = bmp_surface->w;
    2.69      shipData.rect.h = bmp_surface->h;
    2.70  
    2.71 -    SDL_FreeSurface(bmp_surface_rgba);
    2.72      SDL_FreeSurface(bmp_surface);
    2.73  
    2.74      /* load the space background */
    2.75 @@ -160,7 +144,7 @@
    2.76          fatalError("could not load space.bmp");
    2.77      }
    2.78      /* create space texture from surface */
    2.79 -    space = SDL_CreateTextureFromSurface(format, bmp_surface);
    2.80 +    space = SDL_CreateTextureFromSurface(renderer, bmp_surface);
    2.81      if (space == 0) {
    2.82          fatalError("could not create space texture");
    2.83      }
    2.84 @@ -175,6 +159,7 @@
    2.85  {
    2.86  
    2.87      SDL_Window *window;         /* main window */
    2.88 +	SDL_Renderer *renderer;
    2.89      Uint32 startFrame;          /* time frame began to process */
    2.90      Uint32 endFrame;            /* time frame ended processing */
    2.91      Uint32 delay;               /* time to pause waiting to draw next frame */
    2.92 @@ -189,7 +174,7 @@
    2.93      window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
    2.94                                  SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN |
    2.95                                  SDL_WINDOW_BORDERLESS);
    2.96 -    SDL_CreateRenderer(window, 0, 0);
    2.97 +    renderer = SDL_CreateRenderer(window, 0, 0);
    2.98  
    2.99      /* print out some info about joysticks and try to open accelerometer for use */
   2.100      printf("There are %d joysticks available\n", SDL_NumJoysticks());
   2.101 @@ -208,7 +193,7 @@
   2.102             SDL_JoystickNumButtons(accelerometer));
   2.103  
   2.104      /* load graphics */
   2.105 -    initializeTextures();
   2.106 +    initializeTextures(renderer);
   2.107  
   2.108      /* setup ship */
   2.109      shipData.x = (SCREEN_WIDTH - shipData.rect.w) / 2;
   2.110 @@ -226,7 +211,7 @@
   2.111                  done = 1;
   2.112              }
   2.113          }
   2.114 -        render();
   2.115 +        render(renderer);
   2.116          endFrame = SDL_GetTicks();
   2.117  
   2.118          /* figure out how much time we have left, and then sleep */
     3.1 --- a/Xcode-iPhoneOS/Demos/src/fireworks.c	Sun Feb 06 08:57:29 2011 -0800
     3.2 +++ b/Xcode-iPhoneOS/Demos/src/fireworks.c	Sun Feb 06 09:02:10 2011 -0800
     3.3 @@ -173,9 +173,6 @@
     3.4      /* draw our particles! */
     3.5      glDrawArrays(GL_POINTS, 0, num_active_particles);
     3.6  
     3.7 -    /* update screen */
     3.8 -    SDL_RenderPresent();
     3.9 -
    3.10  }
    3.11  
    3.12  /*
    3.13 @@ -437,6 +434,7 @@
    3.14          }
    3.15          stepParticles();
    3.16          drawParticles();
    3.17 +		SDL_GL_SwapWindow(window);
    3.18          endFrame = SDL_GetTicks();
    3.19  
    3.20          /* figure out how much time we have left, and then sleep */
     4.1 --- a/Xcode-iPhoneOS/Demos/src/happy.c	Sun Feb 06 08:57:29 2011 -0800
     4.2 +++ b/Xcode-iPhoneOS/Demos/src/happy.c	Sun Feb 06 09:02:10 2011 -0800
     4.3 @@ -36,7 +36,7 @@
     4.4  }
     4.5  
     4.6  void
     4.7 -render(void)
     4.8 +render(SDL_Renderer *renderer)
     4.9  {
    4.10  
    4.11      int i;
    4.12 @@ -58,8 +58,8 @@
    4.13      dstRect.h = HAPPY_FACE_SIZE;
    4.14  
    4.15      /* fill background in with black */
    4.16 -    SDL_SetRenderDrawColor(0, 0, 0, 255);
    4.17 -    SDL_RenderFill(NULL);
    4.18 +    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
    4.19 +    SDL_RenderClear(renderer);
    4.20  
    4.21      /*
    4.22         loop through all the happy faces:
    4.23 @@ -86,10 +86,10 @@
    4.24          }
    4.25          dstRect.x = faces[i].x;
    4.26          dstRect.y = faces[i].y;
    4.27 -        SDL_RenderCopy(texture, &srcRect, &dstRect);
    4.28 +        SDL_RenderCopy(renderer, texture, &srcRect, &dstRect);
    4.29      }
    4.30      /* update screen */
    4.31 -    SDL_RenderPresent();
    4.32 +    SDL_RenderPresent(renderer);
    4.33  
    4.34  }
    4.35  
    4.36 @@ -97,13 +97,9 @@
    4.37  	loads the happyface graphic into a texture
    4.38  */
    4.39  void
    4.40 -initializeTexture()
    4.41 +initializeTexture(SDL_Renderer *renderer)
    4.42  {
    4.43      SDL_Surface *bmp_surface;
    4.44 -    SDL_Surface *bmp_surface_rgba;
    4.45 -    int format = SDL_PIXELFORMAT_ABGR8888;      /* desired texture format */
    4.46 -    Uint32 Rmask, Gmask, Bmask, Amask;  /* masks for desired format */
    4.47 -    int bpp;                    /* bits per pixel for desired format */
    4.48      /* load the bmp */
    4.49      bmp_surface = SDL_LoadBMP("icon.bmp");
    4.50      if (bmp_surface == NULL) {
    4.51 @@ -112,26 +108,15 @@
    4.52      /* set white to transparent on the happyface */
    4.53      SDL_SetColorKey(bmp_surface, 1,
    4.54                      SDL_MapRGB(bmp_surface->format, 255, 255, 255));
    4.55 -    SDL_PixelFormatEnumToMasks(format, &bpp, &Rmask, &Gmask, &Bmask, &Amask);
    4.56 -    /*
    4.57 -       create a new RGBA surface and blit the bmp to it
    4.58 -       this is an extra step, but it seems to be necessary
    4.59 -       is this a bug?
    4.60 -     */
    4.61 -    bmp_surface_rgba =
    4.62 -        SDL_CreateRGBSurface(0, bmp_surface->w, bmp_surface->h, bpp, Rmask,
    4.63 -                             Gmask, Bmask, Amask);
    4.64 -    SDL_BlitSurface(bmp_surface, NULL, bmp_surface_rgba, NULL);
    4.65  
    4.66      /* convert RGBA surface to texture */
    4.67 -    texture = SDL_CreateTextureFromSurface(format, bmp_surface_rgba);
    4.68 +    texture = SDL_CreateTextureFromSurface(renderer, bmp_surface);
    4.69      if (texture == 0) {
    4.70          fatalError("could not create texture");
    4.71      }
    4.72      SDL_SetTextureBlendMode(texture, SDL_BLENDMODE_BLEND);
    4.73  
    4.74      /* free up allocated memory */
    4.75 -    SDL_FreeSurface(bmp_surface_rgba);
    4.76      SDL_FreeSurface(bmp_surface);
    4.77  }
    4.78  
    4.79 @@ -140,6 +125,7 @@
    4.80  {
    4.81  
    4.82      SDL_Window *window;
    4.83 +	SDL_Renderer *renderer;
    4.84      Uint32 startFrame;
    4.85      Uint32 endFrame;
    4.86      Uint32 delay;
    4.87 @@ -153,9 +139,11 @@
    4.88                                  SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN |
    4.89                                  SDL_WINDOW_BORDERLESS);
    4.90  
    4.91 -    SDL_CreateRenderer(window, -1, 0);
    4.92 +    //SDL_SetHint(SDL_HINT_RENDER_DRIVER, "opengles2");
    4.93 +	
    4.94 +    renderer = SDL_CreateRenderer(window, -1, 0);
    4.95  
    4.96 -    initializeTexture();
    4.97 +    initializeTexture(renderer);
    4.98      initializeHappyFaces();
    4.99  
   4.100      /* main loop */
   4.101 @@ -168,7 +156,7 @@
   4.102                  done = 1;
   4.103              }
   4.104          }
   4.105 -        render();
   4.106 +        render(renderer);
   4.107          endFrame = SDL_GetTicks();
   4.108  
   4.109          /* figure out how much time we have left, and then sleep */
     5.1 --- a/Xcode-iPhoneOS/Demos/src/mixer.c	Sun Feb 06 08:57:29 2011 -0800
     5.2 +++ b/Xcode-iPhoneOS/Demos/src/mixer.c	Sun Feb 06 09:02:10 2011 -0800
     5.3 @@ -33,7 +33,6 @@
     5.4  void handleMouseButtonDown(SDL_Event * event);
     5.5  void handleMouseButtonUp(SDL_Event * event);
     5.6  int playSound(struct sound *);
     5.7 -void render(void);
     5.8  void initializeButtons();
     5.9  void audioCallback(void *userdata, Uint8 * stream, int len);
    5.10  void loadSound(const char *file, struct sound *s);
    5.11 @@ -163,20 +162,20 @@
    5.12  
    5.13  /* draws buttons to screen */
    5.14  void
    5.15 -render(void)
    5.16 +render(SDL_Renderer *renderer)
    5.17  {
    5.18      int i;
    5.19 -    SDL_SetRenderDrawColor(50, 50, 50, 255);
    5.20 -    SDL_RenderFill(NULL);       /* draw background (gray) */
    5.21 +    SDL_SetRenderDrawColor(renderer, 50, 50, 50, 255);
    5.22 +    SDL_RenderClear(renderer);       /* draw background (gray) */
    5.23      /* draw the drum buttons */
    5.24      for (i = 0; i < NUM_DRUMS; i++) {
    5.25          SDL_Color color =
    5.26              buttons[i].isPressed ? buttons[i].downColor : buttons[i].upColor;
    5.27 -        SDL_SetRenderDrawColor(color.r, color.g, color.b, color.unused);
    5.28 -        SDL_RenderFill(&buttons[i].rect);
    5.29 +        SDL_SetRenderDrawColor(renderer, color.r, color.g, color.b, color.unused);
    5.30 +        SDL_RenderFillRect(renderer, &buttons[i].rect);
    5.31      }
    5.32      /* update the screen */
    5.33 -    SDL_RenderPresent();
    5.34 +    SDL_RenderPresent(renderer);
    5.35  }
    5.36  
    5.37  /*
    5.38 @@ -274,6 +273,7 @@
    5.39  
    5.40      int done;                   /* has user tried to quit ? */
    5.41      SDL_Window *window;         /* main window */
    5.42 +	SDL_Renderer *renderer;
    5.43      SDL_Event event;
    5.44      Uint32 startFrame;          /* holds when frame started processing */
    5.45      Uint32 endFrame;            /* holds when frame ended processing */
    5.46 @@ -285,7 +285,7 @@
    5.47      window =
    5.48          SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
    5.49                           SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS);
    5.50 -    SDL_CreateRenderer(window, 0, 0);
    5.51 +    renderer = SDL_CreateRenderer(window, 0, 0);
    5.52  
    5.53      /* initialize the mixer */
    5.54      SDL_memset(&mixer, 0, sizeof(mixer));
    5.55 @@ -328,7 +328,7 @@
    5.56                  break;
    5.57              }
    5.58          }
    5.59 -        render();               /* draw buttons */
    5.60 +        render(renderer);               /* draw buttons */
    5.61          endFrame = SDL_GetTicks();
    5.62  
    5.63          /* figure out how much time we have left, and then sleep */
     6.1 --- a/Xcode-iPhoneOS/Demos/src/rectangles.c	Sun Feb 06 08:57:29 2011 -0800
     6.2 +++ b/Xcode-iPhoneOS/Demos/src/rectangles.c	Sun Feb 06 09:02:10 2011 -0800
     6.3 @@ -9,7 +9,7 @@
     6.4  #include "common.h"
     6.5  
     6.6  void
     6.7 -render(void)
     6.8 +render(SDL_Renderer *renderer)
     6.9  {
    6.10  
    6.11      Uint8 r, g, b;
    6.12 @@ -26,11 +26,11 @@
    6.13      b = randomInt(50, 255);
    6.14  
    6.15      /*  Fill the rectangle in the color */
    6.16 -    SDL_SetRenderDrawColor(r, g, b, 255);
    6.17 -    SDL_RenderFill(&rect);
    6.18 +    SDL_SetRenderDrawColor(renderer, r, g, b, 255);
    6.19 +    SDL_RenderFillRect(renderer, &rect);
    6.20  
    6.21      /* update screen */
    6.22 -    SDL_RenderPresent();
    6.23 +    SDL_RenderPresent(renderer);
    6.24  
    6.25  }
    6.26  
    6.27 @@ -39,6 +39,7 @@
    6.28  {
    6.29  
    6.30      SDL_Window *window;
    6.31 +	SDL_Renderer *renderer;
    6.32      int done;
    6.33      SDL_Event event;
    6.34  
    6.35 @@ -57,13 +58,14 @@
    6.36      if (window == 0) {
    6.37          fatalError("Could not initialize Window");
    6.38      }
    6.39 -    if (SDL_CreateRenderer(window, -1, 0) != 0) {
    6.40 +    renderer = SDL_CreateRenderer(window, -1, 0);
    6.41 +	if (!renderer) {
    6.42          fatalError("Could not create renderer");
    6.43      }
    6.44  
    6.45      /* Fill screen with black */
    6.46 -    SDL_SetRenderDrawColor(0, 0, 0, 255);
    6.47 -    SDL_RenderFill(NULL);
    6.48 +    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
    6.49 +    SDL_RenderClear(renderer);
    6.50  
    6.51      /* Enter render loop, waiting for user to quit */
    6.52      done = 0;
    6.53 @@ -73,7 +75,7 @@
    6.54                  done = 1;
    6.55              }
    6.56          }
    6.57 -        render();
    6.58 +        render(renderer);
    6.59          SDL_Delay(1);
    6.60      }
    6.61  
     7.1 --- a/Xcode-iPhoneOS/Demos/src/touch.c	Sun Feb 06 08:57:29 2011 -0800
     7.2 +++ b/Xcode-iPhoneOS/Demos/src/touch.c	Sun Feb 06 09:02:10 2011 -0800
     7.3 @@ -18,7 +18,7 @@
     7.4  	this is accomplished by drawing several blots spaced PIXELS_PER_ITERATION apart
     7.5  */
     7.6  void
     7.7 -drawLine(float startx, float starty, float dx, float dy)
     7.8 +drawLine(SDL_Renderer *renderer, float startx, float starty, float dx, float dy)
     7.9  {
    7.10  
    7.11      float distance = sqrt(dx * dx + dy * dy);   /* length of line segment (pythagoras) */
    7.12 @@ -43,7 +43,7 @@
    7.13          x += dx_prime;
    7.14          y += dy_prime;
    7.15          /* draw brush blot */
    7.16 -        SDL_RenderCopy(brush, NULL, &dstRect);
    7.17 +        SDL_RenderCopy(renderer, brush, NULL, &dstRect);
    7.18      }
    7.19  }
    7.20  
    7.21 @@ -51,7 +51,7 @@
    7.22  	loads the brush texture
    7.23  */
    7.24  void
    7.25 -initializeTexture()
    7.26 +initializeTexture(SDL_Renderer *renderer)
    7.27  {
    7.28      SDL_Surface *bmp_surface;
    7.29      bmp_surface = SDL_LoadBMP("stroke.bmp");
    7.30 @@ -59,7 +59,7 @@
    7.31          fatalError("could not load stroke.bmp");
    7.32      }
    7.33      brush =
    7.34 -        SDL_CreateTextureFromSurface(SDL_PIXELFORMAT_ABGR8888, bmp_surface);
    7.35 +        SDL_CreateTextureFromSurface(renderer, bmp_surface);
    7.36      SDL_FreeSurface(bmp_surface);
    7.37      if (brush == 0) {
    7.38          fatalError("could not create brush texture");
    7.39 @@ -78,6 +78,7 @@
    7.40      Uint8 state;                /* mouse (touch) state */
    7.41      SDL_Event event;
    7.42      SDL_Window *window;         /* main window */
    7.43 +	SDL_Renderer *renderer;
    7.44      int done;                   /* does user want to quit? */
    7.45  
    7.46      /* initialize SDL */
    7.47 @@ -89,15 +90,15 @@
    7.48      window = SDL_CreateWindow(NULL, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT,
    7.49                                  SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN |
    7.50                                  SDL_WINDOW_BORDERLESS);
    7.51 -    SDL_CreateRenderer(window, 0, 0);
    7.52 +    renderer = SDL_CreateRenderer(window, 0, 0);
    7.53  
    7.54      /*load brush texture */
    7.55 -    initializeTexture();
    7.56 +    initializeTexture(renderer);
    7.57  
    7.58      /* fill canvass initially with all black */
    7.59 -    SDL_SetRenderDrawColor(0, 0, 0, 255);
    7.60 -    SDL_RenderFill(NULL);
    7.61 -    SDL_RenderPresent();
    7.62 +    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
    7.63 +    SDL_RenderClear();
    7.64 +    SDL_RenderPresent(renderer);
    7.65  
    7.66      done = 0;
    7.67      while (!done && SDL_WaitEvent(&event)) {
    7.68 @@ -109,8 +110,8 @@
    7.69              state = SDL_GetMouseState(&x, &y);  /* get its location */
    7.70              SDL_GetRelativeMouseState(&dx, &dy);        /* find how much the mouse moved */
    7.71              if (state & SDL_BUTTON_LMASK) {     /* is the mouse (touch) down? */
    7.72 -                drawLine(x - dx, y - dy, dx, dy);       /* draw line segment */
    7.73 -                SDL_RenderPresent();
    7.74 +                drawLine(renderer, x - dx, y - dy, dx, dy);       /* draw line segment */
    7.75 +                SDL_RenderPresent(renderer);
    7.76              }
    7.77              break;
    7.78          }