2014-12-17 14:47:17 +11:00
|
|
|
|
using System;
|
2015-05-09 18:37:20 +10:00
|
|
|
|
using System.Drawing;
|
2014-12-17 14:47:17 +11:00
|
|
|
|
|
|
|
|
|
namespace ClassicalSharp {
|
|
|
|
|
|
|
|
|
|
public class FpsScreen : Screen {
|
|
|
|
|
|
2015-05-09 18:37:20 +10:00
|
|
|
|
readonly Font font;
|
2015-09-02 19:44:24 +10:00
|
|
|
|
StringBuffer text;
|
2014-12-17 14:47:17 +11:00
|
|
|
|
public FpsScreen( Game window ) : base( window ) {
|
2015-05-09 18:37:20 +10:00
|
|
|
|
font = new Font( "Arial", 13 );
|
2015-09-02 19:44:24 +10:00
|
|
|
|
text = new StringBuffer( 96 );
|
2014-12-17 14:47:17 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TextWidget fpsTextWidget;
|
|
|
|
|
|
|
|
|
|
public override void Render( double delta ) {
|
2015-09-01 18:03:36 +10:00
|
|
|
|
graphicsApi.Texturing = true;
|
2014-12-17 14:47:17 +11:00
|
|
|
|
UpdateFPS( delta );
|
|
|
|
|
fpsTextWidget.Render( delta );
|
2015-09-01 18:03:36 +10:00
|
|
|
|
graphicsApi.Texturing = false;
|
2014-12-17 14:47:17 +11:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
double accumulator, maxDelta;
|
2015-05-10 19:53:01 +10:00
|
|
|
|
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 ) {
|
2015-05-10 19:53:01 +10:00
|
|
|
|
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 ) )
|
2015-09-01 18:03:36 +10:00
|
|
|
|
.Append( ref ptr2, "), chunks/s: " ).AppendNum( ref ptr2, game.ChunkUpdates )
|
|
|
|
|
.Append( ref ptr2, ", vertices: " ).AppendNum( ref ptr2, game.Vertices );
|
2015-05-10 19:53:01 +10:00
|
|
|
|
}
|
2015-05-21 16:22:48 +10:00
|
|
|
|
|
2015-09-05 07:36:51 +10:00
|
|
|
|
string textString = text.GetString();
|
2015-05-21 16:22:48 +10:00
|
|
|
|
fpsTextWidget.SetText( textString );
|
2014-12-17 14:47:17 +11:00
|
|
|
|
maxDelta = 0;
|
|
|
|
|
accumulator = 0;
|
|
|
|
|
fpsCount = 0;
|
2015-09-01 18:03:36 +10:00
|
|
|
|
game.ChunkUpdates = 0;
|
2014-12-17 14:47:17 +11:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public override void Init() {
|
2015-09-01 18:03:36 +10:00
|
|
|
|
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() {
|
2015-05-09 18:37:20 +10:00
|
|
|
|
font.Dispose();
|
2014-12-17 14:47:17 +11:00
|
|
|
|
fpsTextWidget.Dispose();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|