Not that I'm an expert but here's two cents :-
I think you need to think of it in stages instead of one function that does everything.
something like this :
draw_animation_frame(int frame_number, etc...)
Now the way I do the update of the frame is tied into the timing loop.
Here's my code : as basic as I can make it - that's how I code but it's C.
while( old_time < al_get_timer_count(timer))
if(hero_frame_counter <= 0)
hero_frame_counter = HERO_TICKS_PER_FRAMEUPDATE;
As you can see every time the loop runs the hero_frame_counter gets one knocked off it. That means for each tick of the timer. When it reaches 0 it's time to move to the next frame.
When this loop stops, then the draw function gets called and the right frame gets called/** edit : drawn **/. Hopefully this would be a subsequent frame - ie frame 4 after frame 3 but if the previous draw took a long time then the logic loop (above) will get called more times in order to catch up with the actual time.
As far as I know this the way to do it.
HERO_TICKS_PER_FRAMEUPDATE is 30 which means that the loop must be called 30 times for hero_frame_counter to get to 0. When it does, hero_frame_counter is set back to 30.
My timer does 240 ticks per second so assuming the draw function isn't slowing things down, the frame changes 8 times per second ie at 30 ticks, 60 ticks, 90 ticks etc....
It's a bit of headache to get but using a proper timing loop will mean your game will look the same on different processors which run at different speeds.
Please ask any questions you need to.
/** ED : but I'm now off to bed..