//////////////////////////////////////////////////////////////////////
//
// Project  : BRender Microsoft Windows Direct Draw Minimal Example
//            Copyright (c) 1996 Argonaut Technologies Ltd.
//            All rights reserved.
//
// Author   : Steve Williams, October 1996
//
// File     : TIMING.C
//
// Requires : BRender V1.2.3
//            Microsoft DirectX 3
//
// Comments : Utility code to work out frames per second
//
//////////////////////////////////////////////////////////////////////

#include "br_demo.h"

#define SAMPLE_FREQ   50

#ifdef WIN32

extern char BrDemo_Device_Name[MAX_DEVICES][20];

void BrDemo_DoTiming( WIN_BITS_PTR pBrApp )
{
    static LARGE_INTEGER Last_Time ;
    static BOOL bFirstTime = TRUE ;
    static LARGE_INTEGER Freq ;
    LARGE_INTEGER This_Time ;
    static int FrameCount = 0 ;
    static float Total = 0.0F ;
    static float frame_rate = 0.0F ;
    static float poly_rate = 0.0F ;

    FrameCount ++ ;

    if ( bFirstTime )
    {
      bFirstTime = FALSE ;
      QueryPerformanceFrequency( &Freq );
    }
    else
    {
       // Work out frames per sec.
      
       QueryPerformanceCounter( &This_Time );
       Total += (float) Freq.LowPart / (float) (This_Time.LowPart - Last_Time.LowPart );
    }
    
    Last_Time = This_Time ;

    if ( FrameCount > SAMPLE_FREQ )
    {
       char Message[200];

      frame_rate = Total / (float) SAMPLE_FREQ ;
      Total = 0.0F ;
      FrameCount = 0 ;
     
      BrSprintf( Message, "%s       Frame rate : %0.2f", pBrApp->name, frame_rate );
      
	  // If we're fullscreen, don't write to the title bar 'cause it
	  // flickers on the screen. If we're using a 3Dfx, we can do it
	  // though because we can use a second monitor.
	  
	  if ( !pBrApp->bFullScreen || !stricmp( "3DFX_WIN", BrDemo_Device_Name[pBrApp->Current_Device] ) )
	     SetWindowText( pBrApp->hWnd, Message );
   }
}


#else 

void Do_Timing( br_pixelmap *pPix )
{
    br_uint_32 this_time ;      
    static br_uint_32 total = 0 ;
    static br_uint_32 last_time ;
    static int FrameCount = 0 ;
    static float frame_rate ;
    static br_boolean bFirstTime = BR_TRUE ;  
    static br_colour colour ;
  
    this_time = DOSClockRead();

 	FrameCount ++ ;
	
    if ( !bFirstTime )
    {
       total += this_time - last_time ;
    }
    else 
 	 {
       if ( pPix->type == BR_PMT_INDEX_8 )
          colour = 135 ;
       else 
          colour = 0xFFFF ;

       bFirstTime = BR_FALSE ;
    }

    last_time = this_time ;

    if ( FrameCount == SAMPLE_FREQ )
    {
       float total_time ;

	    total_time = (float) total / (float) BR_DOS_CLOCK_RATE ;
       frame_rate = (float) SAMPLE_FREQ / total_time ;
	    total = 0 ;
	    FrameCount = 0 ;
    }

    BrPixelmapTextF( pPix, 10 - pPix->origin_x, 10 - pPix->origin_y,
                     colour, BrFontFixed3x5, "FPS : %4.1f", frame_rate );
}



#endif


