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

200 lines
5.9 KiB
C#
Raw Normal View History

2014-12-17 14:47:17 +11:00
using System;
using System.Drawing;
using ClassicalSharp.GraphicsAPI;
2014-12-17 14:47:17 +11:00
namespace ClassicalSharp {
public class FpsScreen : Screen {
Font font, posFont;
StringBuffer text;
public FpsScreen( Game game ) : base( game ) {
text = new StringBuffer( 96 );
2014-12-17 14:47:17 +11:00
}
TextWidget fpsTextWidget, hackStatesWidget;
Texture posTexture;
2015-10-03 09:36:29 +10:00
public override void Render( double delta ) {
2014-12-17 14:47:17 +11:00
UpdateFPS( delta );
if( game.HideGui || !game.ShowFPS ) return;
2015-09-11 19:39:48 +10:00
graphicsApi.Texturing = true;
2014-12-17 14:47:17 +11:00
fpsTextWidget.Render( delta );
if( game.activeScreen is NormalScreen ) {
UpdateHackState( false );
DrawPosition();
hackStatesWidget.Render( delta );
}
graphicsApi.Texturing = false;
2014-12-17 14:47:17 +11:00
}
double accumulator, maxDelta;
2015-10-03 09:36:29 +10:00
int fpsCount;
void UpdateFPS( double delta ) {
2014-12-17 14:47:17 +11:00
fpsCount++;
maxDelta = Math.Max( maxDelta, delta );
accumulator += delta;
2015-10-03 09:36:29 +10:00
if( accumulator >= 1 ) {
int index = 0;
text.Clear()
.Append( ref index, "FPS: " ).AppendNum( ref index, (int)(fpsCount / accumulator) )
.Append( ref index, " (min " ).AppendNum( ref index, (int)(1f / maxDelta) )
.Append( ref index, "), chunks/s: " ).AppendNum( ref index, game.ChunkUpdates )
.Append( ref index, ", vertices: " ).AppendNum( ref index, 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() {
font = new Font( "Arial", 13 );
posFont = new Font( "Arial", 12, FontStyle.Italic );
game.Events.ChatFontChanged += ChatFontChanged;
fpsTextWidget = new ChatTextWidget( game, font );
fpsTextWidget.XOffset = 2;
fpsTextWidget.YOffset = 2;
2014-12-17 14:47:17 +11:00
fpsTextWidget.Init();
fpsTextWidget.SetText( "FPS: no data yet" );
MakePosTextWidget();
hackStatesWidget = new ChatTextWidget( game, posFont );
hackStatesWidget.XOffset = 2;
hackStatesWidget.YOffset = fpsTextWidget.Height + posHeight + 2;
hackStatesWidget.Init();
UpdateHackState( true );
2014-12-17 14:47:17 +11:00
}
public override void Dispose() {
font.Dispose();
posFont.Dispose();
2014-12-17 14:47:17 +11:00
fpsTextWidget.Dispose();
graphicsApi.DeleteTexture( ref posTexture );
game.Events.ChatFontChanged -= ChatFontChanged;
}
void ChatFontChanged( object sender, EventArgs e ) {
Dispose();
Init();
}
public override void OnResize( int oldWidth, int oldHeight, int width, int height ) {
}
void DrawPosition() {
int index = 0;
TextureRec xy = new TextureRec( 2, posTexture.Y1, baseWidth, posTexture.Height );
TextureRec uv = new TextureRec( 0, 0, posTexture.U2, posTexture.V2 );
IGraphicsApi.Make2DQuad( xy, uv, game.ModelCache.vertices, ref index );
Vector3I pos = Vector3I.Floor( game.LocalPlayer.Position );
curX = baseWidth + 2;
AddChar( 13, ref index );
AddInt( pos.X, ref index, true );
AddInt( pos.Y, ref index, true );
AddInt( pos.Z, ref index, false );
AddChar( 14, ref index );
graphicsApi.BindTexture( posTexture.ID );
graphicsApi.DrawDynamicIndexedVb( DrawMode.Triangles,
game.ModelCache.vb, game.ModelCache.vertices, index, index * 6 / 4 );
}
bool speeding, noclip, fly;
void UpdateHackState( bool force ) {
LocalPlayer p = game.LocalPlayer;
if( force || p.speeding != speeding || p.noClip != noclip || p.flying != fly ) {
speeding = p.speeding; noclip = p.noClip; fly = p.flying;
int index = 0;
text.Clear();
if( fly ) text.Append( ref index, "Fly ON " );
if( speeding ) text.Append( ref index, "Speed ON " );
if( noclip ) text.Append( ref index, "Noclip ON " );
hackStatesWidget.SetText( text.GetString() );
}
}
const string possibleChars = "0123456789-, ()";
int[] widths = new int[possibleChars.Length];
int baseWidth, curX, posHeight;
float texWidth;
void MakePosTextWidget() {
DrawTextArgs args = new DrawTextArgs( "", posFont, true );
for( int i = 0; i < possibleChars.Length; i++ ) {
args.Text = new String( possibleChars[i], 1 );
widths[i] = game.Drawer2D.MeasureChatSize( game.UseArial, ref args ).Width;
}
using( IDrawer2D drawer = game.Drawer2D ) {
args.Text = "Feet pos: ";
Size size = game.Drawer2D.MeasureChatSize( game.UseArial, ref args );
baseWidth = size.Width;
size.Width += 16 * possibleChars.Length;
posHeight = size.Height;
using( Bitmap bmp = IDrawer2D.CreatePow2Bitmap( size ) ) {
drawer.SetBitmap( bmp );
drawer.DrawChatText( game.UseArial, ref args, 0, 0 );
for( int i = 0; i < possibleChars.Length; i++ ) {
args.Text = new String( possibleChars[i], 1 );
drawer.DrawChatText( game.UseArial, ref args, baseWidth + 16 * i, 0 );
}
int y = fpsTextWidget.Height + 2;
posTexture = drawer.Make2DTexture( bmp, size, 0, y );
posTexture.U2 = (float)baseWidth / bmp.Width;
posTexture.Width = baseWidth;
texWidth = bmp.Width;
}
}
}
void AddChar( int charIndex, ref int index ) {
int width = widths[charIndex];
TextureRec xy = new TextureRec( curX, posTexture.Y1, width, posTexture.Height );
TextureRec uv = new TextureRec( (baseWidth + charIndex * 16) / texWidth, 0, width / texWidth, posTexture.V2 );
curX += width;
IGraphicsApi.Make2DQuad( xy, uv, game.ModelCache.vertices, ref index );
}
void AddInt( int value, ref int index, bool more ) {
if( value < 0 )
AddChar( 10, ref index );
int count = 0;
value = Reverse( Math.Abs( value ), out count );
for( int i = 0; i < count; i++ ) {
AddChar( value % 10, ref index ); value /= 10;
}
if( more )
AddChar( 11, ref index );
}
static int Reverse( int value, out int count ) {
int orig = value, reversed = 0;
count = 1; value /= 10;
while( value > 0 ) {
count++; value /= 10;
}
for( int i = 0; i < count; i++ ) {
reversed *= 10;
reversed += orig % 10;
orig /= 10;
}
return reversed;
2014-12-17 14:47:17 +11:00
}
}
}