Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Jun 28th, 2010  |  syntax: C++  |  size: 1.90 KB  |  views: 712  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // Wraps a rectangle around every sprite in a sprite sheet and stores it in an array of rectangles(Clip[])
  2.  
  3. void spriteSheet(string FileName, int SpriteWidth = 64,int SpriteHeight= 64, int SheetDimension = 4){
  4.  
  5.  
  6.     int Sprites = SheetDimension * SheetDimension;// Number of Sprites on sheet
  7.     SDL_Rect Clip[Sprites]; // Rectangles that will wrap around each sprite
  8.  
  9.     int SpriteXNum = 0;// The number sprite going from left to right
  10.     int SpriteYNum = 0;// The number sprite going from top to bottom
  11.     int YIncrement = 0;// Increment for each row.
  12.     for(int i = 0; i< Sprites; i++){// While i is less than number of sprites
  13.  
  14.         if(i = 0){// First sprite starts at 0,0
  15.             Clip[i].x = 0;
  16.             Clip[i].y = 0;
  17.             Clip[i].w = SpriteWidth;
  18.             Clip[i].h = SpriteHeight;
  19.         }
  20.         else{
  21.  
  22.             if(SpriteXNum < SheetDimension - 1 ){// If we have reached the end of the row, go back to the front of the next row
  23.                 SpriteXNum = 0;
  24.             }
  25.             if(YIncrement < SheetDimension - 1){
  26.                 SpriteYNum += 1;                         // Example of 4X4 Sheet
  27.             }                                            //   ________________
  28.             Clip[i].x = SpriteWidth * SpriteXNum;        //  | 0 | 1 | 2 | 3 |
  29.             Clip[i].y = SpriteHeight * SpriteYNum;       //  |===============|
  30.                                                          //  | 0 | 1 | 2 | 3 |
  31.                                                          //  |===============|
  32.             Clip[i].w = SpriteWidth;                     //  | 0 | 1 | 2 | 3 |
  33.             Clip[i].h = SpriteHeight;                    //  |===============|
  34.                                                          //  | 0 | 1 | 2 | 3 |
  35.         }                                                //  |---------------|
  36.         SpriteXNum++;
  37.         YIncrement++;
  38.     }
  39.  
  40. }