Overlay/Get FPS of window

11,589

FRAPS and similar programs work by hooking the API calls used to display the rendered frame (for example IDirect3DDevice9::Present method or OpenGL SwapBuffers() )

Essentially when a game is about to display a new frame, the program execution is transferred to the hook installed by FRAPS. That code can modify the frame any way it wants, drawing the FPS counter over it, changing colors or taking a screenshot for video recording. When that’s finished FRAPS calls the original API to display the frame.

Different graphics APIs need different hooks, so FRAPS must have separate implementations for OpenGL and Direct3D 8 / 9 / 10 etc. It also means when new technologies (like Direct2D) are released FRAPS needs updates to handle these.

The actual process of installing and removing hooks is rather convoluted; you can find more information in this StackOverflow question:

Hooking DirectX EndScene from an injected DLL

or here: Case study: Fraps

Counting FPS is simple; programs just need the time elapsed between frames. For example: frame time = 20 ms; FPS = 1000 ms / 20 ms = 50;

If frame times vary a lot, the FPS value will wildly fluctuate. A better method would be calculating an average of the last 10 frames or counting the number of frames drawn over the last second. Although numbers in FRAPS seem to change too fast.

Share:
11,589

Related videos on Youtube

agz
Author by

agz

Updated on September 18, 2022

Comments

  • agz
    agz over 1 year

    I'm not sure if this is more proper in stackexchange, but how do programs such as steam (the game overlay) and FRAPS overlay data over a window? I've noticed that the overlay/fraps can only work on windows that are driven by opengl/directx, or is the overlay just a feature int he windows api? Also, how does fraps get the fps of the window?

  • agz
    agz over 11 years
    Would this also be how steam overlay works?
  • user2742003
    user2742003 over 11 years
    Yes, generally every software which displays an overlay on fullscreen OpenGL or D3D applications works the same way