SDL Animation

I'm not really sure if this is the right place to post a question like this, although the question itself is really more of a maths thing and I couldn't find anywhere else on the web to ask, so here goes. I'm currently making my way through a book about game programming using the SDL library. It's going great, or it was until I came across this little bit of code:

1
2
3
4
void Game::update()
{
	g_sourceRect.x = 90 * int((SDL_GetTicks() / 100) % 6);
}


So basically this code is a basic method of animating a sprite. g_sourceRect is the rectangle that tells the code where the sprite is on the sprite sheet. eg.
1
2
3
4
g_sourceRect.y = 0;
g_sourceRect.x = 0;
g_sourceRect.w = 90;
g_sourceRect.h = 200;

The code moves g_sourceRect horizontally (g_sourceRect.x)along the sprite sheet to each individual frame of the animation. So if each sprite had a width of 90 it would do something like this: g_sourceRect.x += 90; Surely? Nope. Doesn't work. (That wasn't my question btw.)

SDL_GetTicks() is a function that returns the number of milliseconds since the library was initialised. Now after this is where i get confused. Apparently you then divide it by the amount of time you want between each frame in milliseconds (so 100 in this example). Umm... what? How? And then the modulo operator is to keep it in range of the amount of frames in the animation.

So basically after all that my question is... How on earth does this math work? I get what it's doing but not how.

Thanks in advance, guys!
Last edited on
Apparently you then divide it by the amount of time you want between each frame in milliseconds (so 100 in this example). Umm... what? How?


That's kind of how division works. You can plug in the units and use algebra to prove it:

SDL_GetTicks gives you milliseconds (m)
You want frame (f)
100 is our milliseconds per frame (m/f)

Therefore:
1
2
3
4
5
SDL_GetTicks / 100 = frame
m / (m/f) = f
m * (f/m)
mf/m = f
f = f




Or... you can plug some numbers in to see it visually:

SDL_GetTicks starts at 0. 0/100 is still 0, so at tick=0 we will display frame 0
All the way through tick=99.... 99/100 is still 0, so at tick=99, we will display frame 0
Once you hit tick 100... 100/100 is 1, so at tick=100 we will display frame 1

So it works out:


ticks   -- frame
_____________________
0  - 99     0
100-199     1
200-299     2
300-399     3
400-499     4
500-599     5
600-699     6
700-799     7


Effectively giving you 100 milliseconds for each frame of animation.


The % 6 is an easy to way to get that number to wrap. Remember that it is remainder after division... and the remainder of any division by 6 will always be less than 6


X   --  X % 6
_____________
0       0
1       1
2       2
3       3
4       4
5       5
6       0
7       1
8       2
9       3
10      4
11      5
12      0
13      1
...
Hmmm, I think I kind of understand. So / 100 is effectively saying each 100 tics (eg. 0-99) is a different frame?

So, for example, tick 354700 might be frame 4 making 354800 frame 5 once it's wrapped with modulo?

Thanks for your response.
Last edited on
So / 100 is effectively saying each 100 tics (eg. 0-99) is a different frame?


Yes.

So, for example, tick 354700 might be frame 4 making 354800 frame 5 once it's wrapped with modulo?


Yes... although those would actually be frames 1 and 2 ;P. But yeah that's the idea.
Ah. Thanks for your time mate. You've been a great help. :)
Registered users can post here. Sign in or register to post.