C++ and SDL

Started by
23 comments, last by Smacker_626 21 years, 7 months ago
this is a really quick very messy, not fully tested, class thingy that that i made real quick that makes an sdl program only use the CPU it needs to reach a specified number of frames per second.

just call Start() before the main loop, SetLimit(int) sometime after that but still before the main loop, and then call
RunFrame() at the end of every frame drawing loop...

btw, this is very messy code...



  class FpsCalculator{public:	void Start()	{		lasttick=SDL_GetTicks();		fps=0.0f;		frames=0;		fpslimit=30;		tickdelta=1.0f;		currentticks=lasttick;		oldtick=lasttick;	};	void RunFrame()	{		float milliPerFrame;		float aimMilliPerFrame;		float shiftMilliPerFrame;		currentticks=SDL_GetTicks();		frames++;		if (oldtick+1000<currentticks)		{			frames=0;						oldtick=currentticks;		}		milliPerFrame=currentticks-lasttick;		tickdelta=(tickdelta+1.0f/((((float)fpslimit)/1000.0f)*milliPerFrame))/2.0f;		fps=tickdelta*(float)fpslimit;		aimMilliPerFrame=1000.0f/(float)fpslimit;		shiftMilliPerFrame=aimMilliPerFrame*tickdelta-aimMilliPerFrame;		if (shiftMilliPerFrame>0.0f)		{			SDL_Delay((Uint32)shiftMilliPerFrame);		}		lasttick=currentticks;	};	void Setlimit(int limit)	{		fpslimit=limit;	};	float Getfps()	{		tickdelta=1.0f;		return fps;	};	int GetFrames()	{		return frames;	};	Uint32 currentticks;	Uint32 lasttick;	Uint32 oldtick;	float tickdelta;	float fps;	int frames;	int fpslimit;};  
Advertisement
yeah i knew about that warpexplorer but i made it so you could hold the button and keep the image moving (its the if statement at the end of the for loop) but i needed a second variable to find out the second key pressed and its in the new file on my server (incase anyone wants to see or if a newbie has trouble maybe...) thanks anyway.

And about your last post with the Fpscalculator class thanks and it looks cool but umm *scratchs head* could you maybe explain umm what it does... plz (im gonna go test it after i post the other version on my server)

Thanks to everyone for the help to

link to the all working (but cpu intensive code) Get it here

EDIT---Sorry i just got it up---

--------------------signature--------------------

Looking for usefull, interesting resources?

WELL DONT LOOK HERE>>http://www.dragonruins.com/

but its still fun to have a look around

[edited by - Smacker_626 on August 28, 2002 4:03:53 AM]
Ok i tested your FPSCalc and it works great its whent from 99% to 37% to 47% thanks alot!

And i just updated the file on my server for whoever once again (thats the last time)

But warp could you still explain how it works?

--------------------signature--------------------

Looking for usefull, interesting resources?

WELL DONT LOOK HERE>>http://www.dragonruins.com/

but its still fun to have a look around
umm how it works....oh gosh, Its too messy, I''ll rewrite after I get back from class.

basically how it works, is it finds the number of milliseconds it took to complete the last frame, then it calculates how many milliseconds perframe it needs in order to get the right number of frames per second, then it takes the difference of those two numbers, and if it is greater than 0 it waits that many before continueing....(ignore the tickdelta stuff, is useless and ugly )

this method doesnt really work because it drops out any fractional milliseconds in the delay. This makes you get a higher fps than what you set as the limit. ill write a better one after i get back...

ok, this is a much better way to do it...


  Uint32 timecallback(Uint32 interval, void* param){       //Draw Frame};int main( int argc, char* argv[] ){	SDL_Event	event;	bool		done=false;	SDL_TimerID     id;        int             fps=30;	id=SDL_AddTimer(1000/fps,timecallback, &scenedata);        //1000/fps is the number of milliseconds per frame        // this will have to be adjusted to account for timer        // latency if you want to have an exact fps	while (!done)	{                //Main program loop		SDL_WaitEvent(&event);                 //basically all we have to do is wait for events                // and then process them	}	SDL_RemoveTimer(id);	SDL_Quit();  return 0;}  

This topic is closed to new replies.

Advertisement