0
101 Sep 15, 2011 at 11:15

Hey everyone,
how do i externally limit my FPS in a DirectX9 Game? … The Game i mean gets horrible bugy with like more than 150fps.. I’ve already tried FPS_Limiter but i need to start that game with parameters and FPS_Limiter does not support that, i’ve also tried VSync but i get huge Input Laggs. So i started to google for some tutorials how to do this limiter on my own .. without success -.-. So i hope someone on this forum know how to do this or can give me an advice.

Greets SlnC

#### 12 Replies

0
103 Sep 15, 2011 at 11:25

You can do it with timeGetTime in your main loop, if you want to limit it to 60fps, thats 16 milliseconds per frame, just make a Sleep() operation that makes sure it waits the extra few milliseconds it needs to to make 60 fps.

0
101 Sep 15, 2011 at 11:47

the point is .. how do i do this without having acces to the source code of the game \^\^ i think i need to hook DirectX while the game is running or at the start of the game .. but how do i do this .. and most importantly .. what do i have to do next \^\^

0
103 Sep 15, 2011 at 11:59

Oh sorry I didnt read properly… maybe there is some kind of fps limiter, im not sure of the exact name of it, but back in the old 486 days to play an 86 game you needed to limit the fps in exactly the same way… woops, sorry man.

0
101 Sep 15, 2011 at 12:06

no problem :D

0
102 Sep 15, 2011 at 12:10

It is quite bad design to make your game logic depend on the FPS.

0
101 Sep 15, 2011 at 14:49

Like i metioned before, i have no access to the Source Code of the game … i didnt write this game\^\^

0
101 Sep 16, 2011 at 08:12

Basically you want to limit the FPS when playing any normal game, in most cases you just need to enable VSync as that “should” force the game to try and run at the same speed as your monitors refresh rate.

Failing that there are 3rd party programs that can do it, but as I’ve never used any of them I couldn’t comment on there effectiveness, your best bet is to google search FPS Limiter and read up on them.

0
101 Sep 16, 2011 at 12:33

@SlnC

Hey everyone,
……… I’ve already tried FPS_Limiter but i need to start that game with parameters and FPS_Limiter does not support that, i’ve also tried VSync but i get huge Input Laggs. …… Greets SlnC

\^\^

0
101 Sep 16, 2011 at 13:03

Not sure this will help but maybe you get some more ideas from my input.

• If this is an old dos game you could try dosbox.

• You could also use fraps as a FPS limiter, but then you will always record a video. Not sure if you could loop and always override the last 30 sec or so, as to not fill up your hard drive.

• ATI has some ATI Tray Tools, which might help (if you have an ATI card, NVidia might have similar options).

• And then there is RivaTuner which allows you to make some tweaks. http://www.guru3d.com/index.php?page=rivatuner

Also, generally you tend to get less input lags when your fps is slightly less than your monitors refresh rate. Maybe you could tweak VSync a bit to solve the problem.

0
101 Jan 27, 2012 at 17:26

So i hooked DirectX and tried to delay the Present() method by

//fps is loaded for an ini file( fps = 120 for example).
DWORD delay = 1000/fps;
Sleep(delay);


i was successful at not going over the 120 fps.
but my fps got jumpy and is between \~90 and \~110.
(without the “limitation” i get like 350-400 fps so its not due to my hardware)

the problem is simple with my calculation i limit the Present()-method to be
executed max 120 times per second but did not take into account that the method
itself needs time to process. so i started to meassure(with QueryPerformanceCounter() )
the time the mehthod would need to be called again

so my Present()-Method looks like this now:

//fps is loaded for an ini file( fps = 120 for example).
//g_CurentCount is first declared in the constructor
//(so naturally the first call is wrong but i dont realy care :) )
//and ignore the amount of casts :D
STDMETHOD(Present)(THIS_ CONST RECT* pSourceRect,CONST RECT* pDestRect,HWND hDestWindowOverride,CONST RGNDATA* pDirtyRegion)
{
QueryPerformanceCounter((LARGE_INTEGER*)&g_LastCount);
double dTimeDiff = (((double)(g_LastCount-g_CurentCount))/((double)g_Frequency)) * 1000;
double sleep = (1000/fps)-dTimeDiff;

if(sleep<=0){
sleep = 1;
}
Sleep((DWORD)sleep);

g_CurentCount = g_LastCount;

return m_device->Present(pSourceRect, pDestRect, hDestWindowOverride, pDirtyRegion);
}


so now i get still jumpy fps (160-180) and iam way ove the 120
so dTimeDiff is too big to get me to my 120fps.

so my question is, is that the “usual” approach to limit the fps via hook?
If yes, is there something like a formula for the delay-time?

PS.: Sorry for my english, not my native language :(

EDIT: Used this tutorial for hooking: http://www.gamedev.net/topic/359794-c-direct3d-hooking-sample/

0
101 Jan 27, 2012 at 20:56

Both ATI and nVidia drivers allow you to forcefully enable vsync in games. Your current approach obviously has a lot of educational value, but I think just doing it in the display settings is the fastest and most effective ;)

0
153 Jan 27, 2012 at 22:33

I thought I smelled a bit of necromancy ;)

SInC, in a nutshell that’s pretty much how it’s done, next to playing with VSync options of course. Keep in mind that sleeping is not an exact procedure. It only has millisecond accuracy at best, but in real cases it will delay a tiny bit longer than asked. You can check this article for details. If you aim for 60 FPS, or 16.66 MS per frame, then if your sleep timer is off by 4 milliseconds you can have up to 18 FPS difference. You also shouldn’t force a sleep of 1 millisecond if you don’t have to. In your code, if you’re lagging behind the frame rate you are further delaying it by adding a 1 millisecond delay. That’s like adding salt on a wound :P