FPS Counter problem
-
Hallo,
mein fps code: http://www.badworld-crew.com/nopaste/?6
ich habe einen fps counter für mein prog geschrieben, im prinzip ist es ja kein großes problem aber ich scheitere trotzdem.
Da kommen einfach falsche werte raus. Ich habe schon gegoogelt und boardsuche benutzen, aber alle fps counter sind imprinzip genauso wie meiner mit dem unterschied, dass sie funktionieren :).Ich finde einfach den fehler nicht. Vll findet ihr das problem
THX für die hilfe.
-
So wie ich das seh wird "static float fps" nicht mehr genullt.
-
Um noch etwas zu meckern ^^:
- vergiss das inline
- warum ist fps ein float? es wird immer wie ein int benutzt.
- statt dem ganzen static könnte man das alles auch als Klasse schreiben.
- wenn man schon mal dabei ist kann man auch die ausgabe von fps vom zählvorgang entkoppeln.
-
void CalculateFrameRate() { // Below we create a bunch of static variables because we want to keep the information // in these variables after the function quits. We could make these global but that would // be somewhat messy and superfluous. Note, that normally you don't want to display this to // the window title bar. This is because it's slow and doesn't work in full screen. // Try using the 3D/2D font's. You can check out the tutorials at www.gametutorials.com. static float framesPerSecond = 0.0f; // This will store our fps static float lastTime = 0.0f; // This will hold the time from the last frame static char strFrameRate[50] = {0}; // We will store the string here for the window title // Here we get the current tick count and multiply it by 0.001 to convert it from milliseconds to seconds. // GetTickCount() returns milliseconds (1000 ms = 1 second) so we want something more intuitive to work with. float currentTime = GetTickCount() * 0.001f; // Increase the frame counter ++framesPerSecond; // Now we want to subtract the current time by the last time that was stored. If it is greater than 1 // that means a second has passed and we need to display the new frame rate. Of course, the first time // will always be greater than 1 because lastTime = 0. The first second will NOT be true, but the remaining // ones will. The 1.0 represents 1 second. Let's say we got 12031 (12.031) from GetTickCount for the currentTime, // and the lastTime had 11230 (11.230). Well, 12.031 - 11.230 = 0.801, which is NOT a full second. So we try again // the next frame. Once the currentTime - lastTime comes out to be greater than a second (> 1), we calculate the // frames for this last second. if( currentTime - lastTime > 1.0f ) { // Here we set the lastTime to the currentTime. This will be used as the starting point for the next second. // This is because GetTickCount() counts up, so we need to create a delta that subtract the current time from. lastTime = currentTime; // Copy the frames per second into a string to display in the window title bar sprintf(strFrameRate, "Current Frames Per Second: %d", int(framesPerSecond)); // Set the window title bar to our string SetWindowText(g_hWnd, strFrameRate); // Reset the frames per second framesPerSecond = 0; } }(Nicht von mir)
-
danke für den beitrag. und codeposting.
Logisch gesehen sind beide codeteile equivalent, aber trotzdem funktioniert meiner nicht und deiner(von dir gepostet) schon.
Wo kann das problem oder der Grund liegen?
-
bin ich unsichtbar? ^^
fps wird nichtmehr genullt.
Dein Code müsste lauten:inline void FPS(HWND hwnd) { static float fps = 0.0f; static float prevTime = 0.0f; static char strFPS[200] = {0}; float currentTime = (GetTickCount() * 0.01f); if(currentTime-prevTime > 1.0f) { prevTime = currentTime; sprintf(strFPS,"FPS: %c ",(int)fps); SetWindowText(hwnd, strFPS); // Hier muss fps genullt werden, sonst steigt es ja immer weiter fps = 0; } fps+=1; }
-
und currentTime wird mit 0.001f multipliziert.