对于我的 C - SDL 视频游戏,我需要使用自己制作的精灵表。图片尺寸是100x100,我要拍的每张照片都是25x25
我在某个地方找到了一个不错的功能,可以为我做到这一点:
// Wraps a rectangle around every sprite in a sprite sheet and stores it in an array of rectangles(Clip[])
void spriteSheet(){
int SpriteWidth = 25; int SpriteHeight= 25; int SheetDimension = 4;
int LeSprites = SheetDimension * SheetDimension;// Number of Sprites on sheet
SDL_Rect Clip[LeSprites]; // Rectangles that will wrap around each sprite
int SpriteXNum = 0;// The number sprite going from left to right
int SpriteYNum = 0;// The number sprite going from top to bottom
int YIncrement = 0;// Increment for each row.
int i = 0;
for(i = 0; i< LeSprites; i++){// While i is less than number of sprites
if(i = 0){// First sprite starts at 0,0
Clip[i].x = 0;
Clip[i].y = 0;
Clip[i].w = SpriteWidth;
Clip[i].h = SpriteHeight;
}
else{
if(SpriteXNum < SheetDimension - 1 ){// If we have reached the end of the row, go back to the front of the next row
SpriteXNum = 0;
}
if(YIncrement < SheetDimension - 1){
SpriteYNum += 1; // Example of 4X4 Sheet
} // ________________
Clip[i].x = SpriteWidth * SpriteXNum; // | 0 | 1 | 2 | 3 |
Clip[i].y = SpriteHeight * SpriteYNum; // |===============|
// | 0 | 1 | 2 | 3 |
// |===============|
Clip[i].w = SpriteWidth; // | 0 | 1 | 2 | 3 |
Clip[i].h = SpriteHeight; // |===============|
// | 0 | 1 | 2 | 3 |
} // |---------------|
SpriteXNum++;
YIncrement++;
}
}
但现在我不知道如何在其上加载我的(png)图片以应用此功能。