Regulating FPS in sdl

Why doesnt it work when it's less than .25 frames?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  #include "SDL.h"
#include <iostream>
#include "SDL_ttf.h"
#include "SDL_mixer.h"
#include"SDL_image.h"
#include<sstream>
using namespace std;
void SetCordinates(SDL_Rect *clip)
{
	for(int i=0; i<10 ; i++)
	{
		clip[i].x=i*50;
		clip[i].y=0;
		clip[i].w=50;
		clip[i].h=55;
	}
}
int main(int argc, char** argv)
{
     SDL_Init(SDL_INIT_EVERYTHING);
	 SDL_Surface *screen , * hero , * object,*animation;
	 screen=SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
	 animation=SDL_DisplayFormat(SDL_LoadBMP("hero.bmp"));
	 
	 SDL_Rect Hrect[10];
	 SDL_Rect Hposition;
	 Hposition.x=70;
	 Hposition.y=80;
	 SetCordinates(Hrect);
	float frames=0;
	
	 bool running =true;
	
	 const int FPS=32;
	 Uint32 start;
	 Uint32 color=SDL_MapRGB(screen->format,0xff,0xff,0xff);
	 SDL_SetColorKey(animation,SDL_SRCCOLORKEY,SDL_MapRGB(screen->format,0x00,0xff,0xff));
	 while(running)
	 {
		 SDL_Event event ;
		 while(SDL_PollEvent(&event))
		 {
			 start=SDL_GetTicks();
			 switch(event.type)
			 {
			 case SDL_QUIT:
				 running =false;
				 break;
			 }
		 }
		 SDL_FillRect(screen,NULL, color);// SDL_FillRect(screen, &screen->clip_rect, color);
		 if(1000/FPS>SDL_GetTicks()-start)
		 {
			 SDL_Delay(1000/FPS-(SDL_GetTicks()-start));
		 }
		 frames=frames+.25;
		 if(frames==10)
		 frames=0;
		 SDL_BlitSurface(animation,&Hrect [static_cast<int>(frames)],screen,&Hposition);
		 SDL_Flip(screen);
		 
	 }
	 SDL_FreeSurface(animation);
	 SDL_Quit();
	 return 0;
}

The animation works fine when it's more than .25 to make the animation slow i could
use SDL_Delay(); but that puts limitation or other things , i have been trying this
for a while and can not find why..........
The link to the bmp file is http://www.2shared.com/photo/feoA1vHC/hero.html
Floating point numbers can have rounding errors so comparing using the == operator is often not reliable. In this case I think it is better to check if frames is equal to or larger than 10.
Yes that was careless of me lol , it was easy to solve but i didn't know floating numbers have rounding errors though
Thank you Peter :)
Topic archived. No new replies allowed.