# DirectX/10.0/Direct3D/FPS, CPU usage and timers

This tutorial will introduce three new classes that will encapsulate the functionality of a frames per second counter, a cpu usage counter, and a high precision timer. The code in this tutorial is based on the code from the Font Engine tutorial.
The first new class is the FpsClass. The FpsClass will handle recording the frames per second that the application is running at. Knowing how many frames are rendered every second gives us a good metric for measuring our application's performance. This is one of the industry standard metrics that is used to determine acceptable graphics render speed. It is also useful when implementing new features to see how they impact the frame speed. If the new feature cuts the frame speed in half then you can immediately realize you have a major problem by using just this simple counter. Keep in mind that the current standard fps speed for computers is 60 fps.
Anything below 60 fps is considered to be performing poorly, and anything below 30 is very noticeable to the human eye. A general rule when coding is keep your fps maximized and if a properly implemented new feature makes a serious dent in that speed then it needs to be justified and at a minimum taken note of.
The second new class is the CpuClass. This will handle recording the cpu usage so that we can display the current percentage of cpu use to the screen. Knowing the cpu usage can be useful for debugging new changes to the code similar to how fps is used. It provides a simple and immediate metric for identifying recently implemented poor code or algorithms.
The final new class is the TimerClass. This is a high precision timer we can use for timing events and ensuring our application and its various components all synchronize to a common time frame.

## Framework

The frame work for this tutorial with the three new classes looks like the following:
We will start the tutorial by examining the three new classes.

## Fpsclass.h

The FpsClass is simply a counter with a timer associated with it. It counts how many frames occur in a one second period and constantly updates that count.

## Fpsclass.cpp

The Initialize function sets all the counters to zero and starts the timer.
The Frame function must be called each frame so that it can increment the frame count by 1. If it finds that one second has elapsed then it will store the frame count in the m_fps variable. It then resets the count and starts the timer again.
GetFps returns the frame per second speed for the last second that just passed. This function should be constantly queried so the latest fps can be displayed to the screen.

## Cpuclass.h

The CpuClass is used to determine the percentage of total cpu use that is occurring each second.
We use the pdh library to query the cpu usage.

## Cpuclass.cpp

The Initialize function will setup the handle for querying the cpu on its usage. The query setup here will combine the usage of all the cpus in the system and gives us back a total instead of each individual cpu's usage. If it can't get a query handle or poll the cpu usage for whatever reason it will set the m_canReadCpu flag to false and just keep the cpu usage at zero percent. Some cpus and operating systems privilege levels can cause this to fail. We also start the timer so we only sample the cpu usage once a second.
The Shutdown function releases the handle we used to query the cpu usage.
Just like the FpsClass we have to call the Frame function each frame. But to reduce the amount of querying we use a m_lastSampleTime variable to ensure we only sample once a second. So each second we ask the cpu for its usage and save that value in m_cpuUsage. More than this is not necessary.
The GetCpuPercentage function returns the value of the current cpu usage to any calling function. Once again if it couldn't read the cpu for whatever reason we just set the usage to zero.

## Timerclass.h

The TimerClass is a high precision timer that measures the exact time between frames of execution. Its primary use is for synchronizing objects that require a standard time frame for movement. In this tutorial we won't have a use for it but we will implement it in the code so you can see how to apply it to your projects. The most common usage of the TimerClass is to use the frame time to figure out what percentage of a second has passed in the current frame and then move the objects by that percentage.

## Timerclass.cpp

The Initialize function will first query the system to see if it supports high frequency timers. If it returns a frequency then we use that value to determine how many counter ticks will occur each millisecond. We can then use that value each frame to calculate the frame time. At the end of the Initialize function we query for the start time of this frame to start the timing.
The Frame function is called for every single loop of execution by the main program. This way we can calculate the difference of time between loops and determine the time it took to execute this frame. We query, calculate, and then store the time for this frame into m_frameTime so that it can be used by any calling object for synchronization. We then store the current time as the start of the next frame.
GetTime returns the most recent frame time that was calculated.

## Systemclass.h

Now that we have looked at the three new classes we can examine how they fit into the framework. For all three new classes they will all be located under SystemClass.
We include the three new classes here.
We create a new object for each of the three new classes.

## Systemclass.cpp

We will cover just the changes to this class since the font tutorial.
Initialize the three new objects to null in the class constructor.
Create and initialize the FpsClass.
Create and initialize the CpuClass.
Create and initialize the TimerClass.
Release the three new class objects here in the Shutdown function.
The final change is the Frame function. Each of the new classes needs to call its own Frame function for each frame of execution the application goes through. Once the Frame for each has been called we can now query for the updated data in each and send it into the GraphicsClass for use.

## Graphicsclass.h

We disable vsync for this tutorial so the application will run as fast as possible.

## Graphicsclass.cpp

I will cover just the functions that have changed since the previous font tutorial.
The Frame function now takes in the fps, cpu, and timer counts. The fps and cpu count are set in the TextClass so they can be rendered to the screen.

## Textclass.h

We now have two new functions for setting the fps count and the cpu usage.

## Textclass.cpp

I will cover just the functions that have changed since the previous font tutorial.
The SetFps function takes the fps integer value given to it and then converts it to a string. Once the fps count is in a string format it gets concatenated to another string so it has a prefix indicating that it is the fps speed. After that it is stored in the sentence structure for rendering. The SetFps function also sets the color of the fps string to green if above 60 fps, yellow if below 60 fps, and red if below 30 fps.
The SetCpu function is similar to the SetFps function. It takes the cpu value and converts it to a string which is then stored in the sentence structure and rendered.

## Summary

Now we can see the FPS and CPU usage while rendering our scenes. As well we now have precision timers which we use to ensure translation and rotation of objects is consistent regardless of the frame speed the application is running at.

## To Do Exercises

1. Recompile the code and ensure you can see the fps and cpu values. Press escape to quit.
2. Turn on the vsync in the graphicsclass.h file to see what refresh speed your video card/monitor locks the application to.