Thread: Need help fixing my game timer FPS function

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    319

    Need help fixing my game timer FPS function

    This is my code, the result says 0.0f for fps and i am not sure why
    BTW i know about
    QuerryPerformaceCounter
    GetTickCount
    i just wanted to try to calculate the FPS with this function

    Code:
    MSG msg;
    
    	static float lastTime = (float)timeGetTime();
    
        while(msg.message != WM_QUIT)
        {
            if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
    		else
    		{
            //if(msg.message == WM_QUIT)
            //    break;
    
            
    		float currTime  = (float)timeGetTime();
            float timeDelta = (currTime - lastTime)*0.001f;
            
    		render_frame(timeDelta);
           
    		lastTime = currTime;
    
            if(KEY_DOWN(VK_ESCAPE))
                PostMessage(hWnd, WM_DESTROY, 0, 0);
    		}
        }
    Code:
    void CalcFPS(float timeDelta)
    {
      
      float FrameCount;
      float FPS;
      static float TimeElapsed;
      char outBuffer[64];
    
      FrameCount++;
    
      
      
      TimeElapsed += timeDelta;
    
      if (TimeElapsed>=1.0f)
      {
        FPS=(float)FrameCount/TimeElapsed;
        TimeElapsed=0.0f;
        FrameCount=0;
      }
    
      SetRect(&TextBox, 0, 80, 640, 480);
      sprintf(outBuffer,"FPS = %f",FPS);
      dxfont->DrawTextA(NULL,
                          outBuffer,
                          strlen(outBuffer),
                          &TextBox,
                          NULL,//DT_CENTER | DT_VCENTER,
                          D3DCOLOR_ARGB(255, 255,0, 0));
    
    }
    Last edited by Anddos; 04-01-2012 at 10:52 AM.

  2. #2
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    That counter will only change every second. Instant frame rate is 1.0f / frameDelta. I do not recommend you use timeGetTime(). It has a default period of about 5ms and if you do not call timeBeginPeriod() timeEndPeriod() you will be way off in your calculations.

    Code:
    LARGE_INTEGER perfFreq;
    LARGE_INTEGER curTime;
    LARGE_INTERGER prevTime;
    
    QueryPerformanceCounter(&perfFreq);
    float freqRatio = 1.0f / perfFreq.QuadPart;
    QueryPerformanceCounter(&prevTime);
    
    while (gameActive)
    {
       QueryPerformanceCounter(&curTime);
    
       while (PeekMessage(&msg,0,0,0,PM_REMOVE))
       {
           TranslateMessage(&msg);
           DispatchMessage(&msg);
       }
    
       float frameDelta = (curTime.QuadPart - prevTime.QuadPart) * freqRatio;
    
       float FPS = 1.0f / frameDelta;
       Engine.Update(frameDelta);
       Engine.Render();
    
       prevTime = curTime
    }

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    how do i do this so it outputs correctly
    sprintf(outBuffer,"FPS = %f",Delta);

    am i suppose todo maths stuff with FPS or frameDelta now
    like vTurn += vTurn * Speed * frameDelta or FPS?
    Last edited by Anddos; 04-01-2012 at 11:45 AM.

  4. #4
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    frameDelta is used for calculations since it represent the time between frame X and X + 1. FPS is a measure of how many frames you draw in one frame and is primarily used for statistics and performance measurement...although there are far more measurements that are more helpful. FPS is the result and is only one side of the story.

    If you do not know how to use sprintf I would recommend sidelining the game for a bit and concentrate on learning the language.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. win32 timer function
    By Anuradh_a in forum Windows Programming
    Replies: 2
    Last Post: 03-04-2008, 11:51 AM
  2. Can a DLL use timer function?
    By yunicholas in forum C Programming
    Replies: 5
    Last Post: 12-08-2005, 07:45 PM
  3. Timer for C++ console reflex game
    By Mariano L Gappa in forum C++ Programming
    Replies: 10
    Last Post: 11-27-2005, 11:10 PM
  4. Replies: 4
    Last Post: 08-13-2003, 07:25 PM
  5. is there a timer function?
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 12-14-2001, 07:51 PM