ClassiCube/ClassicalSharp/2D/Screens/FpsScreen.cs

62 lines
1.5 KiB
C#
Raw Normal View History

2014-12-17 14:47:17 +11:00
using System;
using System.Drawing;
2014-12-17 14:47:17 +11:00
namespace ClassicalSharp {
public class FpsScreen : Screen {
readonly Font font;
StringBuffer text;
2014-12-17 14:47:17 +11:00
public FpsScreen( Game window ) : base( window ) {
font = new Font( "Arial", 13 );
text = new StringBuffer( 96 );
2014-12-17 14:47:17 +11:00
}
TextWidget fpsTextWidget;
public override void Render( double delta ) {
graphicsApi.Texturing = true;
2014-12-17 14:47:17 +11:00
UpdateFPS( delta );
fpsTextWidget.Render( delta );
graphicsApi.Texturing = false;
2014-12-17 14:47:17 +11:00
}
double accumulator, maxDelta;
int fpsCount;
unsafe void UpdateFPS( double delta ) {
2014-12-17 14:47:17 +11:00
fpsCount++;
maxDelta = Math.Max( maxDelta, delta );
accumulator += delta;
if( accumulator >= 1 ) {
fixed( char* ptr = text.value ) {
char* ptr2 = ptr;
text.Clear( ptr2 )
.Append( ref ptr2, "FPS: " ).AppendNum( ref ptr2, (int)( fpsCount / accumulator ) )
.Append( ref ptr2, " (min " ).AppendNum( ref ptr2, (int)( 1f / maxDelta ) )
.Append( ref ptr2, "), chunks/s: " ).AppendNum( ref ptr2, game.ChunkUpdates )
.Append( ref ptr2, ", vertices: " ).AppendNum( ref ptr2, game.Vertices );
}
string textString = text.GetString();
fpsTextWidget.SetText( textString );
2014-12-17 14:47:17 +11:00
maxDelta = 0;
accumulator = 0;
fpsCount = 0;
game.ChunkUpdates = 0;
2014-12-17 14:47:17 +11:00
}
}
public override void Init() {
fpsTextWidget = new TextWidget( game, font );
2014-12-17 14:47:17 +11:00
fpsTextWidget.Init();
fpsTextWidget.SetText( "FPS: no data yet" );
}
public override void Dispose() {
font.Dispose();
2014-12-17 14:47:17 +11:00
fpsTextWidget.Dispose();
}
}
}