Thread: direct x, getting fps

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    731

    direct x, getting fps

    I have my render set up like this...

    Code:
    //-----------------------------------------------------------------------------
    // Name: WinMain()
    // Desc: The application's entry point
    //-----------------------------------------------------------------------------
    INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
    {
        // Register the window class
        WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
                          GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                          "D3D", NULL };
        RegisterClassEx( &wc );
    
        // Create the application's window
        HWND hWnd = CreateWindow( "D3D", "D3D",
                                  WS_OVERLAPPEDWINDOW, 150, 150, 500, 500,
                                  GetDesktopWindow(), NULL, wc.hInstance, NULL );
    
        // Initialize Direct3D
        if( SUCCEEDED( InitD3D( hWnd ) ) )
        {
            //Program Functions
            ProgramStart();
            
            // Show the window
            ShowWindow( hWnd, SW_SHOWDEFAULT );
            UpdateWindow( hWnd );
            
            // Enter the message loop
            MSG msg;
            ZeroMemory( &msg, sizeof(msg) );
            while( msg.message!=WM_QUIT )
            {
                if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
                {
                    TranslateMessage( &msg );
                    DispatchMessage( &msg );
                }
                else
                {
                    ProgramStep();  //Other stuff to happen before rendering
                    Render();  //Actaully rendering of d3d objects
                }
            }
        }
    
        UnregisterClass( "D3D", wc.hInstance );
        return 0;
    }
    The stuff I need looked at is near the end of the code.

    But I want to know if that is the correct place to put it and how I should put it there. It seems to be a bit laggy, and I don't know how to get the fps.

    Any help of any sort is helpfull to me!

    Thanks

  2. #2
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    seems like an ok spot to put everything. Getting your FPS can be pretty simple:
    1) count the number of frames drawn so far
    2) once you hit 1 second of elapsed time, display that frame count, and reset to 0

    That's the quick and dirty, and will suffice unless you want to do some pretty deep algo testing.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    But that is my problem, how do I count to 1 second without stoping everything else?

    And I am glad to see I am on track so far.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Here is some pseudo-code to point you in the right direction:

    Code:
    DWORD time, lastTime, frames = 0;
    
    // before main loop
    // set time to current time
    // set lastTime to current time
    
    // Enter your main program loop
    
    // inside your rendering loop
    // set time to current time
    // increment frames
    // if (time - lastTime) > 1 second
    //   lastTime = time
    //   fps = frames
    //   frames = 0
    Look up QueryPerformanceCounter / QueryPerformanceFrequency for good timing functions.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    Maybe I forgoten alot of c++ but QueryPerformanceCounter gives a LARGE_INTEGER a value. But when I try to subtract a LARGE_INTEGER it gives an error saying I'm trying to do a int type thing.

    Not to good of a explainanation, but how do I use math operations on the LARGE_INTEGERS?

  6. #6
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Here is one way to do it:

    Code:
    bool Display(float timeDelta)
    {
      
      if (Device)
      {
        //Clear back buffer to BLACK R:0 G:0 B:0 or 0x00000000
        Device->Clear(0,0,D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,d3d::BLACK,1.0f,0);
       
    	//Start scene
        
        Device->BeginScene();
        
        //Compute FPS
          ElapsedTime+=timeDelta;
          FrameCount++;
        
          if (ElapsedTime>=1.0f)
          {
            FPS=(float)FrameCount/ElapsedTime;
            ElapsedTime=0.0f;
            FrameCount=0;
          } 
          ....
          ....
    }

  7. #7
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    ok I get how to do, but it is the matter of doing it now.

    I will jiggle around with it and see what happens.

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Quote Originally Posted by Rune Hunter
    Not to good of a explainanation, but how do I use math operations on the LARGE_INTEGERS?
    You perform the operations on the QuadPart member of the LARGE_INTEGER structure if your compiler has built in support for 64-bit integers. Otherwise you'll have to operate on the LowPart and HighPart members which are the lower and upper 32 bits respectively.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  9. #9
    Registered User
    Join Date
    Aug 2003
    Posts
    1,218
    When using QueryPerformanceCounter and QueryPerformanceFrequency and you are using MSVC you can (read should) use __int64 instead of LARGE_INTEGER. Just perform a cast to LARGE_INTEGER when sending it to the functions.
    STL Util a small headers-only library with various utility functions. Mainly for fun but feedback is welcome.

  10. #10
    Registered User
    Join Date
    Aug 2004
    Posts
    731
    I'm using dev-c++. I will try using a type cast to int64 anyways.

    EDIT: The type cast seems to have worked (no error) but now I need to know how to change a number into a LPSTR. It isn't alowing me to go like

    (LPSTR)number

    were number is the __int64 variable. Or if it is alowing me to do that (I get no error if I do) the variable is NULL, wich it shouldn't be cause I have it initialized as 0 and it is used.
    Last edited by Rune Hunter; 07-31-2005 at 09:41 AM.

  11. #11
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    A simple sprintf will do the trick or you can use any of the string utility classes or MFC string classes to convert from a numeric data type to a string type. Windows also has wsprintf and other string functions that will also do the trick.

    My advice is to link in the MFC string class via the DLL option - which does not load all of MFC - just the portion that has the string class. The MFC string class is quite nice and very very easy to use.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. SwapBuffers & fps
    By hannibar in forum Windows Programming
    Replies: 0
    Last Post: 03-13-2006, 05:19 AM
  2. Game update...
    By jdinger in forum Game Programming
    Replies: 14
    Last Post: 11-08-2002, 07:10 AM
  3. FPS Counter Prob in OpenGL
    By brandonp in forum Game Programming
    Replies: 1
    Last Post: 07-16-2002, 02:49 PM
  4. SkyLock graphics demo (scrolling, etc.)
    By jdinger in forum Game Programming
    Replies: 9
    Last Post: 06-30-2002, 08:18 PM
  5. dramatic decrease in fps
    By DavidP in forum Game Programming
    Replies: 4
    Last Post: 06-27-2002, 09:05 AM