From c8061b64fd71a119ab78273b227c479e252a1b48 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Sun, 8 Nov 2015 16:32:08 +1100 Subject: [PATCH] Cleanup SelectionManager, don't show all 64 spaces in ChatInputWidget. --- ClassicalSharp/2D/IsometricBlockDrawer.cs | 2 +- ClassicalSharp/2D/Screens/FpsScreen.cs | 2 +- .../2D/Widgets/Chat/TextInputWidget.cs | 20 +++-- .../Entities/Particles/ParticleManager.cs | 2 +- ClassicalSharp/Entities/Player.Rendering.cs | 2 +- ClassicalSharp/GraphicsAPI/Direct3D9Api.cs | 4 +- ClassicalSharp/GraphicsAPI/IGraphicsApi.cs | 12 +-- ClassicalSharp/GraphicsAPI/OpenGLApi.cs | 4 +- ClassicalSharp/Model/IModel.cs | 2 +- ClassicalSharp/Model/SheepModel.cs | 2 +- ClassicalSharp/Network/INetworkProcessor.cs | 21 ++++- ClassicalSharp/Rendering/PickingRenderer.cs | 2 +- ClassicalSharp/Rendering/WeatherRenderer.cs | 2 +- ClassicalSharp/Selections/SelectionBox.cs | 88 ++++++++----------- ClassicalSharp/Selections/SelectionManager.cs | 46 +++++++--- ClassicalSharp/Singleplayer/Server.cs | 1 + ClassicalSharp/TexturePack/ZipReader.cs | 7 +- ClassicalSharp/Utils/WrappableStringBuffer.cs | 6 +- 18 files changed, 126 insertions(+), 99 deletions(-) diff --git a/ClassicalSharp/2D/IsometricBlockDrawer.cs b/ClassicalSharp/2D/IsometricBlockDrawer.cs index 9776c903a..5a2fbd2ec 100644 --- a/ClassicalSharp/2D/IsometricBlockDrawer.cs +++ b/ClassicalSharp/2D/IsometricBlockDrawer.cs @@ -49,7 +49,7 @@ namespace ClassicalSharp { for( int i = 0; i < index; i++ ) TransformVertex( ref cache.vertices[i] ); - game.Graphics.DrawDynamicIndexedVb( DrawMode.Triangles, cache.vb, + game.Graphics.UpdateDynamicIndexedVb( DrawMode.Triangles, cache.vb, cache.vertices, index, index * 6 / 4 ); } diff --git a/ClassicalSharp/2D/Screens/FpsScreen.cs b/ClassicalSharp/2D/Screens/FpsScreen.cs index 51a04e175..4a7f645b3 100644 --- a/ClassicalSharp/2D/Screens/FpsScreen.cs +++ b/ClassicalSharp/2D/Screens/FpsScreen.cs @@ -105,7 +105,7 @@ namespace ClassicalSharp { AddChar( 14, ref index ); graphicsApi.BindTexture( posTexture.ID ); - graphicsApi.DrawDynamicIndexedVb( DrawMode.Triangles, + graphicsApi.UpdateDynamicIndexedVb( DrawMode.Triangles, game.ModelCache.vb, game.ModelCache.vertices, index, index * 6 / 4 ); } diff --git a/ClassicalSharp/2D/Widgets/Chat/TextInputWidget.cs b/ClassicalSharp/2D/Widgets/Chat/TextInputWidget.cs index c88e11e26..e32cca78e 100644 --- a/ClassicalSharp/2D/Widgets/Chat/TextInputWidget.cs +++ b/ClassicalSharp/2D/Widgets/Chat/TextInputWidget.cs @@ -20,7 +20,7 @@ namespace ClassicalSharp { } Texture chatInputTexture, caretTexture; - int caretPos = -1, typingLogPos = 0; + int caretPos = -1, typingLogPos = 0, defaultHeight; public int YOffset; internal WrappableStringBuffer chatInputText; readonly Font font, boldFont; @@ -46,6 +46,7 @@ namespace ClassicalSharp { game.Drawer2D.MakeBitmappedTextTexture( ref args, 0, 0 ) : game.Drawer2D.MakeTextTexture( ref args, 0, 0 ); chatInputText.WordWrap( ref parts, ref partLens, 64 ); + defaultHeight = game.Drawer2D.MeasureChatSize( ref args ).Height; maxWidth = 0; args = new DrawTextArgs( null, font, false ); @@ -85,7 +86,8 @@ namespace ClassicalSharp { Size trimmedSize = game.Drawer2D.MeasureChatSize( ref args ); caretTexture.X1 = 10 + trimmedSize.Width; - args.Text = new String( parts[indexY][indexX], 1 ); + string line = parts[indexY]; + args.Text = indexX < line.Length ? new String( line[indexX], 1 ) : " "; Size charSize = game.Drawer2D.MeasureChatSize( ref args ); caretTexture.Width = charSize.Width; @@ -101,7 +103,7 @@ namespace ClassicalSharp { totalHeight += sizes[i].Height; Size size = new Size( maxWidth, totalHeight ); - int realHeight = 0, y = 0; + int realHeight = 0; using( Bitmap bmp = IDrawer2D.CreatePow2Bitmap( size ) ) { using( IDrawer2D drawer = game.Drawer2D ) { drawer.SetBitmap( bmp ); @@ -115,15 +117,15 @@ namespace ClassicalSharp { drawer.DrawChatText( ref args, 0, realHeight ); realHeight += sizes[i].Height; } - y = game.Height - realHeight - YOffset; - chatInputTexture = drawer.Make2DTexture( bmp, size, 10, y ); + chatInputTexture = drawer.Make2DTexture( bmp, size, 10, 0 ); } } - caretTexture.Y1 += y; - Y = y; - Width = size.Width; - Height = realHeight; + Height = realHeight == 0 ? defaultHeight : realHeight; + Y = game.Height - Height - YOffset; + chatInputTexture.Y1 = Y; + caretTexture.Y1 += Y; + Width = size.Width; } public override void Dispose() { diff --git a/ClassicalSharp/Entities/Particles/ParticleManager.cs b/ClassicalSharp/Entities/Particles/ParticleManager.cs index 3914edc86..37db3f01b 100644 --- a/ClassicalSharp/Entities/Particles/ParticleManager.cs +++ b/ClassicalSharp/Entities/Particles/ParticleManager.cs @@ -37,7 +37,7 @@ namespace ClassicalSharp.Particles { count = Math.Min( count, 1000 ); graphics.SetBatchFormat( VertexFormat.Pos3fTex2fCol4b ); - graphics.DrawDynamicIndexedVb( DrawMode.Triangles, vb, vertices, count, count * 6 / 4 ); + graphics.UpdateDynamicIndexedVb( DrawMode.Triangles, vb, vertices, count, count * 6 / 4 ); graphics.AlphaTest = false; graphics.Texturing = false; } diff --git a/ClassicalSharp/Entities/Player.Rendering.cs b/ClassicalSharp/Entities/Player.Rendering.cs index 9cdbd08dc..296f9a537 100644 --- a/ClassicalSharp/Entities/Player.Rendering.cs +++ b/ClassicalSharp/Entities/Player.Rendering.cs @@ -57,7 +57,7 @@ namespace ClassicalSharp { api.texVerts[3] = new VertexPos3fTex2fCol4b( Utils.RotateY( x1, y2, 0, cosA, sinA ) + pos, u1, nameTex.V2, col ); api.SetBatchFormat( VertexFormat.Pos3fTex2fCol4b ); - api.DrawDynamicIndexedVb( DrawMode.Triangles, api.texVb, api.texVerts, 4, 6 ); + api.UpdateDynamicIndexedVb( DrawMode.Triangles, api.texVb, api.texVerts, 4, 6 ); api.Texturing = false; api.AlphaTest = false; } diff --git a/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs b/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs index 84a22c96b..8a3695c65 100644 --- a/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs +++ b/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs @@ -222,7 +222,7 @@ namespace ClassicalSharp.GraphicsAPI { return GetOrExpand( ref dynamicvBuffers, buffer, iBufferSize ); } - public override void DrawDynamicVb( DrawMode mode, int vb, T[] vertices, int count ) { + public override void UpdateDynamicVb( DrawMode mode, int vb, T[] vertices, int count ) { int size = count * batchStride; DataBuffer buffer = dynamicvBuffers[vb]; buffer.SetData( vertices, size, LockFlags.Discard ); @@ -231,7 +231,7 @@ namespace ClassicalSharp.GraphicsAPI { device.DrawPrimitives( modeMappings[(int)mode], 0, NumPrimitives( count, mode ) ); } - public override void DrawDynamicIndexedVb( DrawMode mode, int vb, T[] vertices, int vCount, int indicesCount ) { + public override void UpdateDynamicIndexedVb( DrawMode mode, int vb, T[] vertices, int vCount, int indicesCount ) { int size = vCount * batchStride; DataBuffer buffer = dynamicvBuffers[vb]; buffer.SetData( vertices, size, LockFlags.Discard ); diff --git a/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs b/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs index eff42cc90..9172d7b80 100644 --- a/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs +++ b/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs @@ -158,13 +158,13 @@ namespace ClassicalSharp.GraphicsAPI { /// draw calls will be in the given format. public abstract void SetBatchFormat( VertexFormat format ); - /// Draws the specified subset of the vertices in the current dynamic vertex buffer
+ /// Binds and draws the specified subset of the vertices in the current dynamic vertex buffer
/// This method also replaces the dynamic vertex buffer's data first with the given vertices before drawing.
- public abstract void DrawDynamicVb( DrawMode mode, int vb, T[] vertices, int count ) where T : struct; + public abstract void UpdateDynamicVb( DrawMode mode, int vb, T[] vertices, int count ) where T : struct; - /// Draws the specified subset of the vertices in the current dynamic vertex buffer
+ /// Binds and draws the specified subset of the vertices in the current dynamic vertex buffer
/// This method also replaces the dynamic vertex buffer's data first with the given vertices before drawing.
- public abstract void DrawDynamicIndexedVb( DrawMode mode, int vb, T[] vertices, + public abstract void UpdateDynamicIndexedVb( DrawMode mode, int vb, T[] vertices, int vCount, int indicesCount ) where T : struct; /// Draws the specified subset of the vertices in the current vertex buffer. @@ -252,7 +252,7 @@ namespace ClassicalSharp.GraphicsAPI { quadVerts[2] = new VertexPos3fCol4b( x + width, y + height, 0, col ); quadVerts[3] = new VertexPos3fCol4b( x, y + height, 0, col ); SetBatchFormat( VertexFormat.Pos3fCol4b ); - DrawDynamicIndexedVb( DrawMode.Triangles, quadVb, quadVerts, 4, 6 ); + UpdateDynamicIndexedVb( DrawMode.Triangles, quadVb, quadVerts, 4, 6 ); } internal VertexPos3fTex2fCol4b[] texVerts = new VertexPos3fTex2fCol4b[4]; @@ -270,7 +270,7 @@ namespace ClassicalSharp.GraphicsAPI { texVerts[2] = new VertexPos3fTex2fCol4b( x2, y2, 0, tex.U2, tex.V2, col ); texVerts[3] = new VertexPos3fTex2fCol4b( x1, y2, 0, tex.U1, tex.V2, col ); SetBatchFormat( VertexFormat.Pos3fTex2fCol4b ); - DrawDynamicIndexedVb( DrawMode.Triangles, texVb, texVerts, 4, 6 ); + UpdateDynamicIndexedVb( DrawMode.Triangles, texVb, texVerts, 4, 6 ); } public static void Make2DQuad( TextureRec xy, TextureRec uv, diff --git a/ClassicalSharp/GraphicsAPI/OpenGLApi.cs b/ClassicalSharp/GraphicsAPI/OpenGLApi.cs index ed38f3319..935038ab8 100644 --- a/ClassicalSharp/GraphicsAPI/OpenGLApi.cs +++ b/ClassicalSharp/GraphicsAPI/OpenGLApi.cs @@ -230,7 +230,7 @@ namespace ClassicalSharp.GraphicsAPI { } int batchStride; - public override void DrawDynamicVb( DrawMode mode, int id, T[] vertices, int count ) { + public override void UpdateDynamicVb( DrawMode mode, int id, T[] vertices, int count ) { GL.BindBuffer( BufferTarget.ArrayBuffer, id ); GL.BufferSubData( BufferTarget.ArrayBuffer, IntPtr.Zero, new IntPtr( count * batchStride ), vertices ); @@ -238,7 +238,7 @@ namespace ClassicalSharp.GraphicsAPI { GL.DrawArrays( modeMappings[(int)mode], 0, count ); } - public override void DrawDynamicIndexedVb( DrawMode mode, int id, T[] vertices, int vCount, int indicesCount ) { + public override void UpdateDynamicIndexedVb( DrawMode mode, int id, T[] vertices, int vCount, int indicesCount ) { GL.BindBuffer( BufferTarget.ArrayBuffer, id ); GL.BufferSubData( BufferTarget.ArrayBuffer, IntPtr.Zero, new IntPtr( vCount * batchStride ), vertices ); diff --git a/ClassicalSharp/Model/IModel.cs b/ClassicalSharp/Model/IModel.cs index 0eccc58f0..8a011f500 100644 --- a/ClassicalSharp/Model/IModel.cs +++ b/ClassicalSharp/Model/IModel.cs @@ -50,7 +50,7 @@ namespace ClassicalSharp.Model { graphics.SetBatchFormat( VertexFormat.Pos3fTex2fCol4b ); DrawPlayerModel( p ); - graphics.DrawDynamicIndexedVb( DrawMode.Triangles, cache.vb, cache.vertices, index, index * 6 / 4 ); + graphics.UpdateDynamicIndexedVb( DrawMode.Triangles, cache.vb, cache.vertices, index, index * 6 / 4 ); } protected abstract void DrawPlayerModel( Player p ); diff --git a/ClassicalSharp/Model/SheepModel.cs b/ClassicalSharp/Model/SheepModel.cs index a0b0971a2..a4ce82f5b 100644 --- a/ClassicalSharp/Model/SheepModel.cs +++ b/ClassicalSharp/Model/SheepModel.cs @@ -72,7 +72,7 @@ namespace ClassicalSharp.Model { DrawRotate( 0, 12/16f, 7/16f, p.rightLegXRot, 0, 0, LeftLegBack ); DrawRotate( 0, 12/16f, 7/16f, p.leftLegXRot, 0, 0, RightLegBack ); // Need to draw the two parts separately. - graphics.DrawDynamicIndexedVb( DrawMode.Triangles, cache.vb, cache.vertices, index, index * 6 / 4 ); + graphics.UpdateDynamicIndexedVb( DrawMode.Triangles, cache.vb, cache.vertices, index, index * 6 / 4 ); graphics.AlphaTest = true; index = 0; diff --git a/ClassicalSharp/Network/INetworkProcessor.cs b/ClassicalSharp/Network/INetworkProcessor.cs index e6f449b6e..4ab339b2b 100644 --- a/ClassicalSharp/Network/INetworkProcessor.cs +++ b/ClassicalSharp/Network/INetworkProcessor.cs @@ -10,24 +10,41 @@ namespace ClassicalSharp { public abstract bool IsSinglePlayer { get; } + /// Opens a connection to the given IP address and port, and prepares the initial state of the client. public abstract void Connect( IPAddress address, int port ); public abstract void SendChat( string text, bool partial ); + /// Informs the server of the client's current position and orientation. public abstract void SendPosition( Vector3 pos, float yaw, float pitch ); + /// Informs the server that the client placed or deleted a block at the given coordinates. public abstract void SendSetBlock( int x, int y, int z, bool place, byte block ); + /// Informs the server that using the given mouse button, + /// the client clicked on a particular block or entity. public abstract void SendPlayerClick( MouseButton button, bool buttonDown, byte targetId, PickedPos pos ); public abstract void Tick( double delta ); public abstract void Dispose(); - public string ServerName, ServerMotd; + public string ServerName; + public string ServerMotd; + + /// Whether the network processor is currently connected to a server. public bool Disconnected; - public bool UsingExtPlayerList, UsingPlayerClick; + + /// Whether the client should use extended player list management, with group names and ranks. + public bool UsingExtPlayerList; + + /// Whether the server supports handling PlayerClick packets from the client. + public bool UsingPlayerClick; + + /// Whether the server can handle partial message packets or not. public bool ServerSupportsPatialMessages; + + /// Whether the server supports receiving all code page 437 characters from this client. public bool ServerSupportsFullCP437; } } \ No newline at end of file diff --git a/ClassicalSharp/Rendering/PickingRenderer.cs b/ClassicalSharp/Rendering/PickingRenderer.cs index 4fe0edd21..a43f8cfa6 100644 --- a/ClassicalSharp/Rendering/PickingRenderer.cs +++ b/ClassicalSharp/Rendering/PickingRenderer.cs @@ -60,7 +60,7 @@ namespace ClassicalSharp.Renderers { ZQuad( p2.Z, p1.X, p2.Y, p2.X, p2.Y - size ); graphics.SetBatchFormat( VertexFormat.Pos3fCol4b ); - graphics.DrawDynamicIndexedVb( DrawMode.Triangles, vb, vertices, verticesCount, verticesCount * 6 / 4 ); + graphics.UpdateDynamicIndexedVb( DrawMode.Triangles, vb, vertices, verticesCount, verticesCount * 6 / 4 ); } public void Dispose() { diff --git a/ClassicalSharp/Rendering/WeatherRenderer.cs b/ClassicalSharp/Rendering/WeatherRenderer.cs index a2df53294..44fa0bd74 100644 --- a/ClassicalSharp/Rendering/WeatherRenderer.cs +++ b/ClassicalSharp/Rendering/WeatherRenderer.cs @@ -52,7 +52,7 @@ namespace ClassicalSharp { // fixes crashing on nVidia cards in OpenGL builds. if( index > 0 ) { graphics.SetBatchFormat( VertexFormat.Pos3fTex2fCol4b ); - graphics.DrawDynamicIndexedVb( DrawMode.Triangles, weatherVb, vertices, index, index * 6 / 4 ); + graphics.UpdateDynamicIndexedVb( DrawMode.Triangles, weatherVb, vertices, index, index * 6 / 4 ); } graphics.AlphaBlending = false; graphics.Texturing = false; diff --git a/ClassicalSharp/Selections/SelectionBox.cs b/ClassicalSharp/Selections/SelectionBox.cs index b9160f088..cec000fbe 100644 --- a/ClassicalSharp/Selections/SelectionBox.cs +++ b/ClassicalSharp/Selections/SelectionBox.cs @@ -7,88 +7,74 @@ namespace ClassicalSharp.Selections { public class SelectionBox { public short ID; - const int VerticesCount = 6 * 4, LineVerticesCount = 12 * 2, IndicesCount = 6 * 6; - public int Vb; - public IGraphicsApi Graphics; - public Vector3I Min, Max; + public FastColour Colour; - public Vector3I Dimensions { - get { return Max - Min + new Vector3I( 1 ); } - } - - public void Render( double delta ) { - Graphics.DepthWrite = false; - Graphics.BindVb( Vb ); - Graphics.DrawIndexedVb( DrawMode.Triangles, IndicesCount, 0 ); - Graphics.DepthWrite = true; - Graphics.DrawVb( DrawMode.Lines, VerticesCount, LineVerticesCount ); - } - - static VertexPos3fCol4b[] vertices = new VertexPos3fCol4b[VerticesCount + LineVerticesCount]; - public SelectionBox( Vector3I start, Vector3I end, FastColour col, IGraphicsApi graphics ) { - Graphics = graphics; + public SelectionBox( Vector3I start, Vector3I end, FastColour col ) { Min = Vector3I.Min( start, end ); - Max = Vector3I.Max( start, end ); - int index = 0; + Max = Vector3I.Max( start, end ); + Colour = col; + } + + public void Render( double delta, VertexPos3fCol4b[] vertices, VertexPos3fCol4b[] lineVertices, + ref int index, ref int lineIndex ) { Vector3 p1 = (Vector3)Min + new Vector3( 1/16f, 1/16f, 1/16f ); Vector3 p2 = (Vector3)Max - new Vector3( 1/16f, 1/16f, 1/16f ); + FastColour col = Colour; - YQuad( ref index, p1.X, p1.Z, p2.X, p2.Z, p1.Y, col ); // bottom - YQuad( ref index, p1.X, p1.Z, p2.X, p2.Z, p2.Y, col ); // top - XQuad( ref index, p1.X, p2.X, p1.Y, p2.Y, p1.Z, col ); // sides - XQuad( ref index, p1.X, p2.X, p1.Y, p2.Y, p2.Z, col ); - ZQuad( ref index, p1.Z, p2.Z, p1.Y, p2.Y, p1.X, col ); - ZQuad( ref index, p1.Z, p2.Z, p1.Y, p2.Y, p2.X, col ); + YQuad( vertices, ref index, p1.X, p1.Z, p2.X, p2.Z, p1.Y, col ); // bottom + YQuad( vertices, ref index, p1.X, p1.Z, p2.X, p2.Z, p2.Y, col ); // top + XQuad( vertices, ref index, p1.X, p1.Y, p2.X, p2.Y, p1.Z, col ); // sides + XQuad( vertices, ref index, p1.X, p1.Y, p2.X, p2.Y, p2.Z, col ); + ZQuad( vertices, ref index, p1.Z, p1.Y, p2.Z, p2.Y, p1.X, col ); + ZQuad( vertices, ref index, p1.Z, p1.Y, p2.Z, p2.Y, p2.X, col ); col = new FastColour( (byte)~col.R, (byte)~col.G, (byte)~col.B ); // bottom face - Line( ref index, p1.X, p1.Y, p1.Z, p2.X, p1.Y, p1.Z, col ); - Line( ref index, p2.X, p1.Y, p1.Z, p2.X, p1.Y, p2.Z, col ); - Line( ref index, p2.X, p1.Y, p2.Z, p1.X, p1.Y, p2.Z, col ); - Line( ref index, p1.X, p1.Y, p2.Z, p1.X, p1.Y, p1.Z, col ); + Line( lineVertices, ref lineIndex, p1.X, p1.Y, p1.Z, p2.X, p1.Y, p1.Z, col ); + Line( lineVertices, ref lineIndex, p2.X, p1.Y, p1.Z, p2.X, p1.Y, p2.Z, col ); + Line( lineVertices, ref lineIndex, p2.X, p1.Y, p2.Z, p1.X, p1.Y, p2.Z, col ); + Line( lineVertices, ref lineIndex, p1.X, p1.Y, p2.Z, p1.X, p1.Y, p1.Z, col ); // top face - Line( ref index, p1.X, p2.Y, p1.Z, p2.X, p2.Y, p1.Z, col ); - Line( ref index, p2.X, p2.Y, p1.Z, p2.X, p2.Y, p2.Z, col ); - Line( ref index, p2.X, p2.Y, p2.Z, p1.X, p2.Y, p2.Z, col ); - Line( ref index, p1.X, p2.Y, p2.Z, p1.X, p2.Y, p1.Z, col ); + Line( lineVertices, ref lineIndex, p1.X, p2.Y, p1.Z, p2.X, p2.Y, p1.Z, col ); + Line( lineVertices, ref lineIndex, p2.X, p2.Y, p1.Z, p2.X, p2.Y, p2.Z, col ); + Line( lineVertices, ref lineIndex, p2.X, p2.Y, p2.Z, p1.X, p2.Y, p2.Z, col ); + Line( lineVertices, ref lineIndex, p1.X, p2.Y, p2.Z, p1.X, p2.Y, p1.Z, col ); // side faces - Line( ref index, p1.X, p1.Y, p1.Z, p1.X, p2.Y, p1.Z, col ); - Line( ref index, p2.X, p1.Y, p1.Z, p2.X, p2.Y, p1.Z, col ); - Line( ref index, p2.X, p1.Y, p2.Z, p2.X, p2.Y, p2.Z, col ); - Line( ref index, p1.X, p1.Y, p2.Z, p1.X, p2.Y, p2.Z, col ); - - Vb = Graphics.CreateVb( vertices, VertexFormat.Pos3fCol4b, VerticesCount + LineVerticesCount ); + Line( lineVertices, ref lineIndex, p1.X, p1.Y, p1.Z, p1.X, p2.Y, p1.Z, col ); + Line( lineVertices, ref lineIndex, p2.X, p1.Y, p1.Z, p2.X, p2.Y, p1.Z, col ); + Line( lineVertices, ref lineIndex, p2.X, p1.Y, p2.Z, p2.X, p2.Y, p2.Z, col ); + Line( lineVertices, ref lineIndex, p1.X, p1.Y, p2.Z, p1.X, p2.Y, p2.Z, col ); } - void Line( ref int index, float x1, float y1, float z1, float x2, float y2, float z2, FastColour col ) { - vertices[index++] = new VertexPos3fCol4b( x1, y1, z1, col ); - vertices[index++] = new VertexPos3fCol4b( x2, y2, z2, col ); - } - - void ZQuad( ref int index, float z1, float z2, float y1, float y2, float x, FastColour col ) { + static void ZQuad( VertexPos3fCol4b[] vertices, ref int index, float z1, float y1, + float z2, float y2, float x, FastColour col ) { vertices[index++] = new VertexPos3fCol4b( x, y1, z1, col ); vertices[index++] = new VertexPos3fCol4b( x, y2, z1, col ); vertices[index++] = new VertexPos3fCol4b( x, y2, z2, col ); vertices[index++] = new VertexPos3fCol4b( x, y1, z2, col ); } - void XQuad( ref int index, float x1, float x2, float y1, float y2, float z, FastColour col ) { + static void XQuad( VertexPos3fCol4b[] vertices, ref int index, float x1, float y1, + float x2, float y2, float z, FastColour col ) { vertices[index++] = new VertexPos3fCol4b( x1, y1, z, col ); vertices[index++] = new VertexPos3fCol4b( x1, y2, z, col ); vertices[index++] = new VertexPos3fCol4b( x2, y2, z, col ); vertices[index++] = new VertexPos3fCol4b( x2, y1, z, col ); } - void YQuad( ref int index, float x1, float z1, float x2, float z2, float y, FastColour col ) { + static void YQuad( VertexPos3fCol4b[] vertices, ref int index, float x1, float z1, + float x2, float z2, float y, FastColour col ) { vertices[index++] = new VertexPos3fCol4b( x1, y, z1, col ); vertices[index++] = new VertexPos3fCol4b( x1, y, z2, col ); vertices[index++] = new VertexPos3fCol4b( x2, y, z2, col ); vertices[index++] = new VertexPos3fCol4b( x2, y, z1, col ); } - public void Dispose() { - Graphics.DeleteVb( Vb ); + static void Line( VertexPos3fCol4b[] vertices, ref int index, float x1, float y1, float z1, + float x2, float y2, float z2, FastColour col ) { + vertices[index++] = new VertexPos3fCol4b( x1, y1, z1, col ); + vertices[index++] = new VertexPos3fCol4b( x2, y2, z2, col ); } } } diff --git a/ClassicalSharp/Selections/SelectionManager.cs b/ClassicalSharp/Selections/SelectionManager.cs index 5a09a9575..c906074d2 100644 --- a/ClassicalSharp/Selections/SelectionManager.cs +++ b/ClassicalSharp/Selections/SelectionManager.cs @@ -9,6 +9,8 @@ namespace ClassicalSharp.Selections { protected Game game; public IGraphicsApi Graphics; + VertexPos3fCol4b[] vertices, lineVertices; + int vb, lineVb; public SelectionManager( Game window ) { game = window; @@ -18,7 +20,8 @@ namespace ClassicalSharp.Selections { List selections = new List( 256 ); public void AddSelection( byte id, Vector3I p1, Vector3I p2, FastColour col ) { - SelectionBox selection = new SelectionBox( p1, p2, col, Graphics ); + RemoveSelection( id ); + SelectionBox selection = new SelectionBox( p1, p2, col ); selection.ID = id; selections.Add( selection ); } @@ -27,42 +30,59 @@ namespace ClassicalSharp.Selections { for( int i = 0; i < selections.Count; i++ ) { SelectionBox sel = selections[i]; if( sel.ID == id ) { - sel.Dispose(); selections.RemoveAt( i ); break; } } } - - Vector3 pos; + SelectionBoxComparer comparer = new SelectionBoxComparer(); public void Render( double delta ) { Player player = game.LocalPlayer; - pos = player.Position; - if( selections.Count == 0 ) return; + if( selections.Count == 0 ) return; + // TODO: Proper selection box sorting. But this is very difficult because // we can have boxes within boxes, intersecting boxes, etc. Probably not worth it. - comparer.pos = pos; + comparer.pos = player.Position; selections.Sort( comparer ); + if( vertices == null ) + InitData(); // lazy init as most servers don't use this. - Graphics.SetBatchFormat( VertexFormat.Pos3fCol4b ); - Graphics.AlphaBlending = true; + int index = 0, lineIndex = 0; for( int i = 0; i < selections.Count; i++ ) { SelectionBox box = selections[i]; - box.Render( delta ); + box.Render( delta, vertices, lineVertices, ref index, ref lineIndex ); } + + Graphics.SetBatchFormat( VertexFormat.Pos3fCol4b ); + Graphics.UpdateDynamicVb( DrawMode.Lines, lineVb, lineVertices, selections.Count * LineVerticesCount ); + + Graphics.DepthWrite = false; + Graphics.AlphaBlending = true; + Graphics.UpdateDynamicIndexedVb( DrawMode.Triangles, vb, vertices, + selections.Count * VerticesCount, selections.Count * IndicesCount ); + Graphics.DepthWrite = true; Graphics.AlphaBlending = false; } public void Dispose() { OnNewMap( null, null ); game.Events.OnNewMap -= OnNewMap; + if( lineVb > 0 ) { + Graphics.DeleteDynamicVb( vb ); + Graphics.DeleteDynamicVb( lineVb ); + } + } + + const int VerticesCount = 6 * 4, LineVerticesCount = 12 * 2, IndicesCount = 6 * 6; + void InitData() { + vertices = new VertexPos3fCol4b[256 * VerticesCount]; + lineVertices = new VertexPos3fCol4b[256 * LineVerticesCount]; + vb = Graphics.CreateDynamicVb( VertexFormat.Pos3fCol4b, vertices.Length ); + lineVb = Graphics.CreateDynamicVb( VertexFormat.Pos3fCol4b, lineVertices.Length ); } void OnNewMap( object sender, EventArgs e ) { - foreach( SelectionBox sel in selections ) { - sel.Dispose(); - } selections.Clear(); } } diff --git a/ClassicalSharp/Singleplayer/Server.cs b/ClassicalSharp/Singleplayer/Server.cs index cb19f5201..abc7a830b 100644 --- a/ClassicalSharp/Singleplayer/Server.cs +++ b/ClassicalSharp/Singleplayer/Server.cs @@ -24,6 +24,7 @@ namespace ClassicalSharp.Singleplayer { game.Inventory.CanPlace[i] = true; game.Inventory.CanDelete[i] = true; } + game.Events.RaiseBlockPermissionsChanged(); NewMap(); MakeMap( 128, 128, 128 ); diff --git a/ClassicalSharp/TexturePack/ZipReader.cs b/ClassicalSharp/TexturePack/ZipReader.cs index cded4142e..2e5cec271 100644 --- a/ClassicalSharp/TexturePack/ZipReader.cs +++ b/ClassicalSharp/TexturePack/ZipReader.cs @@ -12,6 +12,7 @@ namespace ClassicalSharp.TexturePack { public string Filename; } + /// Extracts files from a stream that represents a .zip file. public sealed class ZipReader { public Action ProcessZipEntry; @@ -133,14 +134,14 @@ namespace ClassicalSharp.TexturePack { byte[] compressedData = reader.ReadBytes( compressedSize ); MemoryStream stream = new MemoryStream( compressedData ); int index = 0, read = 0; - DeflateStream deflater = new DeflateStream( stream, CompressionMode.Decompress ); + DeflateStream inflater = new DeflateStream( stream, CompressionMode.Decompress ); while( index < uncompressedSize && - ( read = deflater.Read( data, index, data.Length - index ) ) > 0 ) { + (read = inflater.Read( data, index, data.Length - index)) > 0 ) { index += read; } - deflater.Dispose(); + inflater.Dispose(); return data; } else { Utils.LogDebug( "Unsupported .zip entry compression method: " + compressionMethod ); diff --git a/ClassicalSharp/Utils/WrappableStringBuffer.cs b/ClassicalSharp/Utils/WrappableStringBuffer.cs index ee2bd2577..940ccd0ee 100644 --- a/ClassicalSharp/Utils/WrappableStringBuffer.cs +++ b/ClassicalSharp/Utils/WrappableStringBuffer.cs @@ -44,7 +44,7 @@ namespace ClassicalSharp { } // Output the used lines - OutputLines( ref lines, linesCount, lineSize ); + OutputLines( ref lines, linesCount, lineSize, lineLens ); value = realText; } @@ -58,13 +58,13 @@ namespace ClassicalSharp { value = wrap; } - void OutputLines( ref string[] lines, int linesCount, int lineSize ) { + void OutputLines( ref string[] lines, int linesCount, int lineSize, int[] lineLens ) { for( int i = 0; i < capacity; i++ ) { if( value[i] == '\0' ) value[i] = ' '; } for( int i = 0; i < Math.Max( 1, linesCount ); i++ ) { - lines[i] = new String( value, i * lineSize, lineSize ); + lines[i] = new String( value, i * lineSize, lineLens[i] ); } }