Join us on Discord!
You can help CodeWalrus stay online by donating here.

SOLVED: Updating the animation of a sprite given the refresh rate and time

Started by gameblabla, April 25, 2020, 01:47:52 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

gameblabla

Hello guys,
so let's assume we have a display refresh rate of 144hz and i want to display a sprite with 9 frames in 0.25 second.
Now if it was 1 second instead, it would take 16 frames to update the sprite's frame by one.
In this case, it would take only 4 frames before it updates the sprite's frame by one.
I also want it to also work for a display refresh rate of 60hz.
So in the case of 60hz, it would take 1.66666666667 frames instead to update it.

My sprite function looks like this :
void Put_sprite_top_left(uint_fast32_t a, double top_left_x, double top_left_y, int w, int h, int frame_start, int frame_end, int loop, double seconds)
{
 SDL_Rect position;
 SDL_Rect frame;
 
 position.x = (int) (current_internal_resolution_width * top_left_x);
 position.y = (int) (current_internal_resolution_height * top_left_y);
 position.w = w;
 position.h = h;
 
 if (!(sprites_current_frame_[a] >= (double)frame_end && loop == 0))
 {
 if (sprites_current_frame_[a] < frame_end)
 {
 sprites_current_frame_[a] += ((seconds)*(double)(frame_end-frame_start)) * (1.0/Get_Refresh_rate) ;
 }
 
 if ((sprites_current_frame_[a] >= (double)frame_end && loop == 1) || (sprites_current_frame_[a] < 0.0 && sprites_current_frame_[a] == 0.0))
 {
 sprites_current_frame_[a] = (double)frame_start;
 }
 }
 
 printf("sprites_current_frame_[a] %f\n", sprites_current_frame_[a]);
 
 frame.x = (int)(sprites_current_frame_[a])*(w);
 
 frame.y = 0;
 frame.w = w;
 frame.h = h;
 
 SDL_RenderCopy(renderer, texture_library_memory[a], &frame, &position);
}

Of course it is wrong...

See this line in particular
sprites_current_frame_[a] += ((seconds)*(double)(frame_end-frame_start)) * (1.0/Get_Refresh_rate) ;
How can i fix this ?

Thanks
  • Calculators owned: None (used to own an Nspire and TI-89)

obj04

Hello gameblabla,

the "seconds" variable is the time elapsed from the last frame update, I guess.
In this case, I wonder why you increase "sprites_current_frame_[a]" by "((seconds)*(double)(frame_end-frame_start)) * (1.0/Get_Refresh_rate)".
Maybe it helps to set the variable to this value instead of increasing it?

gameblabla

I actually found my solution to my issue though.
This is the fix for it :
sprites_current_frame_[a] += 1 / ( (Get_Refresh_rate * seconds) / (double)(frame_end-frame_start));
It shouldn't be set to this value because sprites_current_frame_[a] is a double then converted to an integer.
So in my case, i have :
Get_Refresh_rate = 144 (frames per seconds)
seconds = 0.25 (in seconds)
frame_end-frame_start = 9 (the number of images to display one by one)

The function's purpose is to display all 9 frames by 0.25 seconds given a framerate of 144 FPS.
This function is executed every 1/144. When i realized that, it clicked on me : )

I guess i should have explained it.

Solved
  • Calculators owned: None (used to own an Nspire and TI-89)

Powered by EzPortal