How to swap row/column major order?

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 9:39 UTC
Read the original article Hit count: 215

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 I need a sheet of dimensions 4x4 to look like this:

| 0 | 1 | 2 | 3 |
| 4 | 5 | 6 | 7 |
| 8 | 9 | 10| 11|
| 12| 13| 14| 15|

But instead I get this order:

| 0 | 4 | 8 | 12|
| 1 | 5 | 9 | 13|
| 2 | 6 | 10| 14|
| 3 | 7 | 11| 15|

Columns and rows order does not match. What should I do to fix the order?

© Game Development or respective owner

Related posts about c++

Related posts about spritesheet