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?
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:
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.