Thread: Best performance.

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    161

    Best performance.

    after making a game like "slime volleyball" and pong. I want to make a game under linux so I know how to use images in linux. Before I start I would like to know how to do best frame rate. I want my game to be the same speed on all computers. I found one method of doing this is too have a delay of a certain amount of milliseconds. That is not too good because on every computer it will be the same framerate. If I do not give delay's then on fast computers the game will go fast and slow on slow computers.

    I played some games and they are programmed in a way that if the computer has slow framerate then it will move objects more pixels perframe and if the computer has a fast framerate then the objects will move at smaller pixels. What ends up at the end is that the games look like they both are playing at the same speed. How can I do something like this.

    Thanx in advance!

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    A simple way is to test at the end of a function how much time is elapsed:
    Code:
    float start=GetTickCount();
    //...
    while ((GetTickCount()-start)<33);
    That will cause a delay until 33 milliseconds (or whatever GetTickCount() measures time in) has passed.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Instead of frame-based movement, look up time-based movement. It adjusts how far objects move based on how much time has passed since the last frame was rendered.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Banned
    Join Date
    Jan 2003
    Posts
    1,708
    I programmed my game engine using a high resolution timer. The high resolution timer doesn't count time, it counts ticks. The system is always running at a certain number of 'ticks' per second. You count the number of ticks between each frame to get a 'timefraction', which is ticksbetweenframes/tickspersec.

    Then, once you have this timefraction, you have to setup speeds of objects in relation to seconds. Say you have a ball that moves at 10 feet per second. The amount it moves per frame is:
    10 * timefraction

    Code:
    void	FPS_t::Start()
    {
    	LARGE_INTEGER	a;
    	QueryPerformanceFrequency(&a);
    	tickspersec = a.QuadPart; //this cannot change while system is running
    	                          //can fark up on multiprocessor system due to bugs in bios
    							  //but shouldn't usually matter
    
    	QueryPerformanceCounter(&a); //initialize number of system ticks at last frame to this
    	lastframesticks = a.QuadPart;
    	frames	=	0; 
    	unsigned int time = timeGetTime();
    	LastTimeBufferSwapped = time;
    	CurrentTime = time;
    	LastTickTime = a.QuadPart; 
    	secondpassed = false;	
    }
    
    void	FPS_t::UpdateFPS()
    {
    	++frames;
    	CurrentTime = timeGetTime();
    	LARGE_INTEGER	a;
    	QueryPerformanceCounter(&a);  //current number of ticks
    	unsigned	int	b = a.QuadPart; //ticks for this frame
    	TimeFrac = (b - lastframesticks)  * (1/(float)tickspersec);
    	if(b - LastTickTime >= tickspersec) //if a second has passed then update FPS info
    	{
    			secondpassed	=	1; 
    			LastTickTime = b;
    			sprintf(display, "FPS: %d", (int)frames);
    			SetWindowText(hWnd, display);
    			frames = 0;
    	}
    	else
    	{
    			secondpassed	=	0;
    	}
    	lastframesticks = b;
    }
    second passed is just true/false
    TimeFrac is the time that has passed between frames in seconds. b is the current ticks, lastframeticks is the tickcount at the last frame, and the difference between the two, divided by the ticks per second (well, in this case multiplied by the reciprocal) gives the time fraction.

    It looks complicated, but I didn't like the way my timeGetTime() time based system worked. On the other hand, there is no absolute guarantee that all computers will be able to supply you with a high resolution timer (although 98% of the time they will).

    So, yeah, that's how I did that.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Performance and footprint of virtual function
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 01-31-2008, 07:34 PM
  2. File map performance
    By George2 in forum C++ Programming
    Replies: 8
    Last Post: 01-04-2008, 04:18 AM
  3. Observer Pattern and Performance questions
    By Scarvenger in forum C++ Programming
    Replies: 2
    Last Post: 09-21-2007, 11:12 PM
  4. inheritance and performance
    By kuhnmi in forum C++ Programming
    Replies: 5
    Last Post: 08-04-2004, 12:46 PM
  5. Performance Evaluation
    By rick barclay in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 09-16-2001, 10:16 AM