🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Problem: Too fast FPS in demo's from tutorials

Started by
14 comments, last by Joni-Matti 18 years, 2 months ago
It was hard to fiind a subject to describe my problem properly. Anyways, my problem is that when I run the examples you get in the tutorials they tend to run extremely fast, that is so fast that it's beyond 999fps. Is there anyway to solve this? I'm now on lesson 11 and the waves on the texture are so fast that it's damn ugly to look at.
Advertisement
mesure the time it took to render the last frame (lets call it delta), then sleep for 1 / FRAME_RATE - delta.

Note that you need both sleep and time quering functions with good precisission for this to work (getimeofday, usleep on Unix systems).

alternatively you can calculate when the next frame should be drawn and add an idle loop at the end of you rendering code that does nothing as long as the current time is less than what you calculated for the next frame.
Thx... I solved the problem, I made a simple function using the time.h header.
It doesn't contain any calculation of current framerate, all it does is run a loop for a given number of milliseconds, the given number is of your choosing. It works fine in theese simple turtorials at least. You just guess your way to a fitting number, to obtain the framerate you want.

void SlowdownFPS(int fps){		clock_t endwait;	int waitMs = 1000/fps;	endwait = clock() + utvid;	while (clock() < endwait) {}}


Oh, by FRAME_RATE I meant the limit you want to set for the framerate. For more complicated sceens (or if you target a wide array of hardware) it makes sence to take the time that the last frame took to render into account. Calculating the delta ist equivalent to 2 cals to clock and a substraction. So it isn't that much of an overhead.

busy wait is considered ok for games, but I still prefere sleep. Its nice if some long running process that runs in the backround still gets the uneeded processor time.
Quote: Original post by Mapster87
Thx... I solved the problem, I made a simple function using the time.h header.
It doesn't contain any calculation of current framerate, all it does is run a loop for a given number of milliseconds, the given number is of your choosing. It works fine in theese simple turtorials at least. You just guess your way to a fitting number, to obtain the framerate you want.

void SlowdownFPS(int fps){		clock_t endwait;	int waitMs = 1000/fps;	endwait = clock() + utvid;	while (clock() < endwait) {}}


For demo's/tutorials your solution is good enough. For games this would be a waste, as you are just throwing your cpu-cycles away. Most games use time-based animations, try searching for it on the forums.
Just force vsync on your graphics drivers.

If you need help doing this, do you have ATI or NVIDIA?
I don't have much experience in programming and I ran into a problem with measuring the time it took to render the last frame. I don't know how to convert the clock_t form to an int.

Anyways, I know it isn't good enough for a game, but at the moment I don't know a percent of what I need to make a game, though that's my goal.

edit:
Ahh.. that was another solution. worked fine that too Boder.

[Edited by - mapster on April 18, 2006 1:17:27 PM]
VSync will be better for you, the method you were using works fine, but won't behave well on different hardware (or even on your own hardware if other tasks are taking up system resources as well).

- Jason Astle-Adams

This sleep function you speak of nefthy, where do I find information about it?
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dllproc/base/sleep.asp

This topic is closed to new replies.

Advertisement