Sign in to follow this  
EmrldDrgn

OpenGL 60FPS?

Recommended Posts

Does OpenGL provide a timer or something which I can use to enforce a 60 fps speed for my game? At the moment it seems like it would run slower on slow computers and faster on fast ones.

Share this post


Link to post
Share on other sites
I'm using DevIL, Windows Platform SDK, and OpenGL currently. I also have OpenAL, but I'm not using it on my project currently. The only one of those which might have a timer is the WPSDK, but I can't find any documentation on it (although I haven't really looked). I'm currently developing on Windows, and don't actually plan to release this project (just a learning thing). I have a little experience with DirectX and I know this is possible, but I don't know how to do it.

On a semi-related matter, can someone point me to some windows sdk documentation? I could really use it.

Share this post


Link to post
Share on other sites
What you need to do is to temper the speed of motion by the FPS speed. That is, for all motion figure out how much it should be per second, then multiply that by 1 over FPS. Then take the resulting value as the motion to use.

// EXAMPLE Motion right on X, 2 units per second.
motionthisframe = 2 * (1/fps);

Share this post


Link to post
Share on other sites
That sounds like it would work, but how do I know what the current FPS is on the platform running the game? Obviously on a tricked - out gaming PC, the FPS will be higher than on someone's grandmother's old Pentium II. How do I get the exact FPS on the current computer?

Share this post


Link to post
Share on other sites
the second easiest (and most coarse) method is to:

a. Keep a counter that holds how many frames have been rendered.
b. For each second that passes, that number is your FPS. Copy it to somewhere useful. Render that useful number. Clear the original and the cycle starts over.

Obviously this doesn't take into account the fact that almost never will an exact second pass. Hence, the coarseness.

the first easiest method is to just use the reciprocal of the amount of time elapsed during a single frame.

But the average FPS is calculated in a similar manor to the following pseudo-code:


int frames_rendered = 0
float frames_per_second = 0
// put the current elapsed time into T1 to prevent possible whacked out first FPS record
float T1 = 0

while (game_looping)
{
do_game_interval()

frames_rendered = frames_rendered + 1

float T2 = time_get_seconds()

if (T2 - T1 > 1.0)
{
frames_per_second = frames_rendered / (T2 - T1)
T1 = T2
frames_rendered = 0
}
}






... god I hope that's right

I suggest using the reciprocal and the average methods at the same time to display to the user (or you).


Now, to use this with time-based movement, you're going to pretty much have to use the reciprocal method if you're not using fixed time step physics (which is recommended, but a tad harder).

So, get the time elapsed at the beginning of the frame (T1) and once again at the end (T2). This becomes your time step for the next frame. If this is in seconds, then all you do is multiply every velocity by the difference of T2 and T1.

Granted, it doesn't *have* to be in seconds per se, but it's the easiest to mentally associate with I find.

Share this post


Link to post
Share on other sites
That helps a lot, but I'm still confused. time_get_seconds() isn't the actual method to get the seconds, is it? Because if it is, I'm missing some library or something - it isn't working. Is the problem that I don't have some library included in my program, or was that just a pseudocode example? If so, what is the actual function to get the seconds? TIA.

Share this post


Link to post
Share on other sites
int timeGetTime()

is fine... but win32

From MSDN :

Requirements

Windows NT/2000/XP: Included in Windows NT 3.1 and later.
Windows 95/98/Me: Included in Windows 95 and later.
Header: Declared in Mmsystem.h; include Windows.h.
Library: Use Winmm.lib.

Share this post


Link to post
Share on other sites
I went through MSDN and found what I wanted. Here is my fps code. It seems to work just fine. Thanks very much for all your help.

[SOURCE] 		SYSTEMTIME timer;
GetSystemTime(&timer);
float T1;
float T2 = timer.wSecond;
int FramesElapsed = 0;
float fps;

while(!done) // Loop That Runs While done=FALSE
{
FramesElapsed++;
GetSystemTime(&timer);
T1 = timer.wSecond;

if (T1 >= T2)
{
fps = FramesElapsed;
FramesElapsed = 0;
GetSystemTime(&timer);
T2 = timer.wSecond;
T2++;
T1 = 0.0f;
}
[/SOURCE]

Share this post


Link to post
Share on other sites
To keep const(units/sec) the speed that your objects are moving lets assume that you calculated FPS frames per second

double v = 0;
double GTC=0;
if(FPS>20)
{
v=(double)6*(double)((GetTickCount()-GTC))/1000);//6un/sec
i+=v;
GTC=GetTickCount();
}
else
{
i+=6;
GTC=GetTickCount();
}
the if is for the start of the game that frame rate is minimum.

DWORD GetTickCount(void);

The GetTickCount function retrieves the number of milliseconds that have elapsed since the system was started(so the division by 1000). It is limited to the resolution of the system timer.

Declared in Winbase.h; include Windows.h.
Link to Kernel32.lib.
Requires Kernel32.dll.

Share this post


Link to post
Share on other sites
GetTickCount() is good to use, but watch out that you don't rely on it incrementing. It wraps after 49.7 days, which means if you happen to be reading it at that time, it'll return 4,294,967,294, 4,294,967,295, 0, 1... Many programs fail because of timer wrapping like this.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this