SDL: Clipping a Sprite Sheet from Left to Right

Posted by 0X1A on Game Development See other posts from Game Development or by 0X1A
Published on 2014-06-04T02:40:42Z Indexed on 2014/06/04 3:40 UTC
Read the original article Hit count: 230

Filed under:
|

I'm trying to get a sprite sheet clipped in the right order but I'm a bit stumped, every iteration I've tried has tended to be in the wrong order. This is my current implementation.

Frames = (TempSurface->h / ClipHeight) * (TempSurface->w / ClipWidth); SDL_Rect Clips[Frames]; for (i = 0; i < Frames; i++) { if (i != 0 && i % (TempSurface->h / ClipHeight) == 0) ColumnIndex++; Clips[i].x = ColumnIndex * ClipWidth; Clips[i].y = i % (TempSurface->h / ClipHeight) * ClipHeight; Clips[i].w = ClipWidth; Clips[i].h = ClipHeight;

Where TempSurface is the entire sheet loaded to a SDL_Surface and Clips[] is an array of SDL_Rects. What results from this is a sprite sheet set to SDL_Rects in the wrong order.

For example a sheet of dimensions 4x4 would load desirably as this: | 0 | 1 | 2 | 3 | | 4 | 5 | 6 | 7 | | 8 | 9 | 10| 11| | 12| 13| 14| 15|

But would be set as this order: | 0 | 4 | 8 | 12| | 1 | 5 | 9 | 13| | 2 | 6 | 10| 14| | 3 | 7 | 11| 15|

What should I be doing for these to be set correctly?

© Game Development or respective owner

Related posts about sprites

Related posts about sdl