Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <iostream>
- #include <string>
- using namespace std;
- //internal variables
- long double lastMilliseconds = 0.0;
- long double deltatime = 0.0;
- unsigned int limit = 0;
- //function prototypes
- void SetDeltaTime();
- unsigned int FPSLimit(int targetFPS);
- DWORD CALLBACK Display(LPVOID parameter);
- DWORD CALLBACK CalcDelta(LPVOID parameter);
- DWORD CALLBACK MessageProc(LPVOID parameter);
- BOOL main(int argc, char* argv[]) {
- int fps = 0;
- if (argc > 1) { //if a number is passed, set the FPS limit to it
- fps = strtol(argv[1], 0, 0);
- }
- limit = FPSLimit(fps);
- CreateThread(0, 0, CalcDelta, 0, 0, 0); //create thread to calculate the delta time
- CreateThread(0, 0, Display, 0, 0, 0); //create thread to display the FPS
- MSG msg; //hold windows messages here
- while (msg.message != WM_QUIT) {
- while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) {
- TranslateMessage(&msg);
- DispatchMessage(&msg);
- }
- }
- return FALSE;
- }
- void SetDeltaTime() {
- SYSTEMTIME time;
- GetSystemTime(&time);
- long double milliseconds = (time.wSecond * 1000.0) + time.wMilliseconds;
- deltatime = (milliseconds - lastMilliseconds) * 0.001;
- lastMilliseconds = milliseconds;
- }
- unsigned int FPSLimit(int targetFPS) {
- return (unsigned int)(1.0 / (long double)targetFPS * 1000.0);
- }
- DWORD CALLBACK Display(LPVOID parameter) {
- while (true) {
- Sleep(1000);
- //unsigned long long was required because otherwise it just displayed infinite with the FPS uncapped
- cout << (unsigned long long int)(1.0 / deltatime) << endl;
- }
- return 1;
- }
- DWORD CALLBACK CalcDelta(LPVOID parameter) {
- while (true) {
- Sleep(limit);
- SetDeltaTime();
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment