From a74f6af33938fede3ffd9b948a194a15f4d3ac88 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Wed, 7 Oct 2015 16:16:39 +1100 Subject: [PATCH] Use isometric blocks in hotbar. (Doesn't work for sprites, texture bleeding for Direct3D9 build) --- ClassicalSharp/2D/Drawing/GdiPlusDrawer2D.cs | 8 +- ClassicalSharp/2D/Drawing/IDrawer2D.cs | 2 + ClassicalSharp/2D/IsometricBlockDrawer.cs | 23 ++-- .../2D/Widgets/BlockHotbarWidget.cs | 122 ++++++++---------- ClassicalSharp/Entities/Player.cs | 2 +- ClassicalSharp/Map/Map.cs | 12 +- ClassicalSharp/Utils/FastColour.cs | 9 +- 7 files changed, 90 insertions(+), 88 deletions(-) diff --git a/ClassicalSharp/2D/Drawing/GdiPlusDrawer2D.cs b/ClassicalSharp/2D/Drawing/GdiPlusDrawer2D.cs index e90881819..56fbf89aa 100644 --- a/ClassicalSharp/2D/Drawing/GdiPlusDrawer2D.cs +++ b/ClassicalSharp/2D/Drawing/GdiPlusDrawer2D.cs @@ -61,8 +61,10 @@ namespace ClassicalSharp { } public override void DrawRectBounds( Color colour, float lineWidth, int x, int y, int width, int height ) { - using( Pen pen = new Pen( colour, lineWidth ) ) + using( Pen pen = new Pen( colour, lineWidth ) ) { + pen.Alignment = PenAlignment.Inset; g.DrawRectangle( pen, x, y, width, height ); + } } public override void DrawRoundedRect( Color colour, float radius, float x, float y, float width, float height ) { @@ -85,6 +87,10 @@ namespace ClassicalSharp { path.Dispose(); } + public override void Clear( Color colour ) { + g.Clear( colour ); + } + public override void Dispose() { g.Dispose(); g = null; diff --git a/ClassicalSharp/2D/Drawing/IDrawer2D.cs b/ClassicalSharp/2D/Drawing/IDrawer2D.cs index a41528a58..71c3af7b1 100644 --- a/ClassicalSharp/2D/Drawing/IDrawer2D.cs +++ b/ClassicalSharp/2D/Drawing/IDrawer2D.cs @@ -21,6 +21,8 @@ namespace ClassicalSharp { public abstract void DrawRoundedRect( Color colour, float radius, float x, float y, float width, float height ); + public abstract void Clear( Color colour ); + /// Disposes of any resources used by this class that are associated with the underlying bitmap. public abstract void Dispose(); diff --git a/ClassicalSharp/2D/IsometricBlockDrawer.cs b/ClassicalSharp/2D/IsometricBlockDrawer.cs index 4935c90f1..5b5c66d07 100644 --- a/ClassicalSharp/2D/IsometricBlockDrawer.cs +++ b/ClassicalSharp/2D/IsometricBlockDrawer.cs @@ -14,6 +14,12 @@ namespace ClassicalSharp { static int index; static float scale; + static FastColour colNormal, colXSide, colZSide, colYBottom; + static IsometricBlockDrawer() { + colNormal = FastColour.White; + FastColour.GetShaded( colNormal, ref colXSide, ref colZSide, ref colYBottom ); + } + public static void Draw( Game game, byte block, float size, float x, float y ) { info = game.BlockInfo; cache = game.ModelCache; @@ -26,7 +32,6 @@ namespace ClassicalSharp { pos.X = x; pos.Y = -y; pos.Z = 0; pos = Utils.RotateX( pos, (float)Utils.DegreesToRadians( -35.264f ) ); pos = Utils.RotateY( pos, (float)Utils.DegreesToRadians( 45f ) ); - Console.WriteLine( pos ); if( info.IsSprite[block] ) { DrawXFace( block, 0f, TileSide.Right ); @@ -48,6 +53,7 @@ namespace ClassicalSharp { graphics.LoadMatrix( ref m ); if( setFog ) graphics.Fog = false; + graphics.BeginVbBatch( VertexFormat.Pos3fTex2fCol4b ); } public static void RestoreState( IGraphicsApi graphics, bool setFog ) { @@ -56,12 +62,11 @@ namespace ClassicalSharp { graphics.Fog = true; } - static Vector3 pos = Vector3.Zero; - static FastColour col = FastColour.White; - + static Vector3 pos = Vector3.Zero; static void DrawYFace( byte block, float y, int side ) { int texId = info.GetTextureLoc( block, side ); TextureRectangle rec = atlas.GetTexRec( texId ); + FastColour col = colNormal; cache.vertices[index++] = new VertexPos3fTex2fCol4b( pos.X - scale, pos.Y + y, pos.Z - scale, rec.U2, rec.V2, col ); cache.vertices[index++] = new VertexPos3fTex2fCol4b( pos.X - scale, pos.Y + y, pos.Z + scale, rec.U1, rec.V2, col ); @@ -74,6 +79,7 @@ namespace ClassicalSharp { TextureRectangle rec = atlas.GetTexRec( texId ); if( blockHeight != 1 ) rec.V2 = rec.V1 + blockHeight * TerrainAtlas2D.invElementSize; + FastColour col = colZSide; cache.vertices[index++] = new VertexPos3fTex2fCol4b( pos.X - scale, pos.Y - scale * blockHeight, pos.Z + z, rec.U1, rec.V2, col ); cache.vertices[index++] = new VertexPos3fTex2fCol4b( pos.X - scale, pos.Y + scale * blockHeight, pos.Z + z, rec.U1, rec.V1, col ); @@ -86,11 +92,12 @@ namespace ClassicalSharp { TextureRectangle rec = atlas.GetTexRec( texId ); if( blockHeight != 1 ) rec.V2 = rec.V1 + blockHeight * TerrainAtlas2D.invElementSize; + FastColour col = colXSide; - cache.vertices[index++] = new VertexPos3fTex2fCol4b( pos.X + x, pos.Y - scale * blockHeight, pos.Z - scale, rec.U1, rec.V2, col ); - cache.vertices[index++] = new VertexPos3fTex2fCol4b( pos.X + x, pos.Y + scale * blockHeight, pos.Z - scale, rec.U1, rec.V1, col ); - cache.vertices[index++] = new VertexPos3fTex2fCol4b( pos.X + x, pos.Y + scale * blockHeight, pos.Z + scale, rec.U2, rec.V1, col ); - cache.vertices[index++] = new VertexPos3fTex2fCol4b( pos.X + x, pos.Y - scale * blockHeight, pos.Z + scale, rec.U2, rec.V2, col ); + cache.vertices[index++] = new VertexPos3fTex2fCol4b( pos.X + x, pos.Y - scale * blockHeight, pos.Z - scale, rec.U1, rec.V2, colNormal ); + cache.vertices[index++] = new VertexPos3fTex2fCol4b( pos.X + x, pos.Y + scale * blockHeight, pos.Z - scale, rec.U1, rec.V1, colNormal ); + cache.vertices[index++] = new VertexPos3fTex2fCol4b( pos.X + x, pos.Y + scale * blockHeight, pos.Z + scale, rec.U2, rec.V1, colNormal ); + cache.vertices[index++] = new VertexPos3fTex2fCol4b( pos.X + x, pos.Y - scale * blockHeight, pos.Z + scale, rec.U2, rec.V2, colNormal ); } } } \ No newline at end of file diff --git a/ClassicalSharp/2D/Widgets/BlockHotbarWidget.cs b/ClassicalSharp/2D/Widgets/BlockHotbarWidget.cs index 2abaffa11..d9bf47aac 100644 --- a/ClassicalSharp/2D/Widgets/BlockHotbarWidget.cs +++ b/ClassicalSharp/2D/Widgets/BlockHotbarWidget.cs @@ -1,9 +1,7 @@ -using System; -using System.Drawing; -using OpenTK; -using OpenTK.Input; -using ClassicalSharp.GraphicsAPI; -using ClassicalSharp.Renderers; +using System; +using System.Drawing; +using ClassicalSharp.Renderers; +using OpenTK.Input; namespace ClassicalSharp { @@ -12,12 +10,12 @@ namespace ClassicalSharp { public BlockHotbarWidget( Game game ) : base( game ) { HorizontalDocking = Docking.Centre; VerticalDocking = Docking.BottomOrRight; - game.Events.HeldBlockChanged += HeldBlockChanged; + hotbarCount = game.Inventory.Hotbar.Length; } - Texture[] barTextures = new Texture[9]; - Texture selectedBlock; - const int blockSize = 32; + int hotbarCount; + Texture selectedBlock, background; + const int blockSize = 40; public override bool HandlesKeyDown( Key key ) { if( key >= Key.Number1 && key <= Key.Number9 ) { @@ -27,77 +25,68 @@ namespace ClassicalSharp { return false; } + static FastColour backCol = new FastColour( 60, 60, 60, 160 ); + static FastColour outlineCol = new FastColour( 169, 143, 192 ); + static FastColour selCol = new FastColour( 213, 200, 223 ); public override void Init() { - int y = game.Height - blockSize; - + int width = blockSize * hotbarCount; + X = game.Width / 2 - width / 2; + Y = game.Height - blockSize; + Width = width; + Height = blockSize; + MakeBackgroundTexture( width ); + MakeSelectionTexture(); + } + + void MakeBackgroundTexture( int width ) { + Size size = new Size( width, blockSize ); + using( Bitmap bmp = IDrawer2D.CreatePow2Bitmap( size ) ) { + using( IDrawer2D drawer = game.Drawer2D ) { + drawer.SetBitmap( bmp ); + drawer.Clear( backCol ); + for( int xx = 0; xx < hotbarCount; xx++ ) { + drawer.DrawRectBounds( outlineCol, 3, xx * blockSize, 0, blockSize, blockSize ); + } + background = drawer.Make2DTexture( bmp, size, X, Y ); + } + } + } + + void MakeSelectionTexture() { Size size = new Size( blockSize, blockSize ); using( Bitmap bmp = IDrawer2D.CreatePow2Bitmap( size ) ) { using( IDrawer2D drawer = game.Drawer2D ) { drawer.SetBitmap( bmp ); - drawer.DrawRectBounds( Color.White, blockSize / 8, 0, 0, blockSize, blockSize ); - selectedBlock = drawer.Make2DTexture( bmp, size, 0, y ); + drawer.DrawRectBounds( selCol, 3, 0, 0, blockSize, blockSize ); + selectedBlock = drawer.Make2DTexture( bmp, size, 0, Y ); } } - - int x = game.Width / 2 - ( blockSize * barTextures.Length ) / 2; - X = x; - Y = y; - Width = blockSize * barTextures.Length; - Height = blockSize; - - for( int i = 0; i < barTextures.Length; i++ ) { - barTextures[i] = MakeTexture( x, y, game.Inventory.Hotbar[i] ); - x += blockSize; - } } - + public override void Render( double delta ) { graphicsApi.Texturing = true; + background.Render( graphicsApi ); // TODO: Maybe redesign this so we don't have to bind the whole atlas. Not cheap. graphicsApi.BindTexture( game.TerrainAtlas.TexId ); - int selectedX = 0; - for( int i = 0; i < barTextures.Length; i++ ) { - barTextures[i].RenderNoBind( graphicsApi ); - if( i == game.Inventory.HeldBlockIndex ) { - selectedX = barTextures[i].X1; - } - } - //bool setFog = game.EnvRenderer is StandardEnvRenderer; - //IsometricBlockDrawer.SetupState( graphicsApi, setFog ); - //IsometricBlockDrawer.Draw( game, (byte)Block.Brick, 200, 100 + 100 * (float)Math.Sin( game.accumulator ), 100 ); - //IsometricBlockDrawer.RestoreState( graphicsApi, setFog ); - - selectedBlock.X1 = selectedX; + bool setFog = game.EnvRenderer is StandardEnvRenderer; + IsometricBlockDrawer.SetupState( graphicsApi, setFog ); + for( int i = 0; i < hotbarCount; i++ ) { + int x = X + i * blockSize; + IsometricBlockDrawer.Draw( game, (byte)game.Inventory.Hotbar[i], 10, + x + blockSize / 2, game.Height - 12 ); + if( i == game.Inventory.HeldBlockIndex ) + selectedBlock.X1 = x; + } + + IsometricBlockDrawer.RestoreState( graphicsApi, setFog ); selectedBlock.Render( graphicsApi ); graphicsApi.Texturing = false; } public override void Dispose() { graphicsApi.DeleteTexture( ref selectedBlock ); - game.Events.HeldBlockChanged -= HeldBlockChanged; - } - - void HeldBlockChanged( object sender, EventArgs e ) { - int index = game.Inventory.HeldBlockIndex; - Block block = game.Inventory.HeldBlock; - int x = barTextures[index].X1; - barTextures[index] = MakeTexture( x, Y, block ); - } - - Texture MakeTexture( int x, int y, Block block ) { - int texId = game.BlockInfo.GetTextureLoc( (byte)block, TileSide.Left ); - TextureRectangle rec = game.TerrainAtlas.GetTexRec( texId ); - - int verSize = blockSize; - float height = game.BlockInfo.Height[(byte)block]; - int blockY = y; - if( height != 1 ) { - rec.V1 = rec.V1 + TerrainAtlas2D.invElementSize * height; - verSize = (int)( blockSize * height ); - blockY = y + blockSize - verSize; - } - return new Texture( -1, x, blockY, blockSize, verSize, rec ); + graphicsApi.DeleteTexture( ref background ); } public override void MoveTo( int newX, int newY ) { @@ -107,13 +96,8 @@ namespace ClassicalSharp { Y = newY; selectedBlock.X1 += deltaX; selectedBlock.Y1 += deltaY; - - for( int i = 0; i < barTextures.Length; i++ ) { - Texture tex = barTextures[i]; - tex.X1 += deltaX; - tex.Y1 += deltaY; - barTextures[i] = tex; - } + background.X1 += deltaX; + background.Y1 += deltaY; } } } \ No newline at end of file diff --git a/ClassicalSharp/Entities/Player.cs b/ClassicalSharp/Entities/Player.cs index c9e55ccb4..dfe81cb28 100644 --- a/ClassicalSharp/Entities/Player.cs +++ b/ClassicalSharp/Entities/Player.cs @@ -16,7 +16,7 @@ namespace ClassicalSharp { protected Game game; public string DisplayName, SkinName; - public SkinType SkinType; + public SkinType SkinType; public Player( Game game ) : base( game ) { this.game = game; diff --git a/ClassicalSharp/Map/Map.cs b/ClassicalSharp/Map/Map.cs index 37966e548..ead877ab3 100644 --- a/ClassicalSharp/Map/Map.cs +++ b/ClassicalSharp/Map/Map.cs @@ -91,12 +91,14 @@ namespace ClassicalSharp { public void SetSunlight( FastColour col ) { Set( col, ref Sunlight, EnvVar.SunlightColour ); - AdjustLight( Sunlight, ref SunlightXSide, ref SunlightZSide, ref SunlightYBottom ); + FastColour.GetShaded( Sunlight, ref SunlightXSide, + ref SunlightZSide, ref SunlightYBottom ); } public void SetShadowlight( FastColour col ) { Set( col, ref Shadowlight, EnvVar.ShadowlightColour ); - AdjustLight( Shadowlight, ref ShadowlightXSide, ref ShadowlightZSide, ref ShadowlightYBottom ); + FastColour.GetShaded( Shadowlight, ref ShadowlightXSide, + ref ShadowlightZSide, ref ShadowlightYBottom ); } public void SetWeather( Weather weather ) { @@ -154,12 +156,6 @@ namespace ClassicalSharp { } } - void AdjustLight( FastColour normal, ref FastColour xSide, ref FastColour zSide, ref FastColour yBottom ) { - xSide = FastColour.Scale( normal, 0.6f ); - zSide = FastColour.Scale( normal, 0.8f ); - yBottom = FastColour.Scale( normal, 0.5f ); - } - public void UseRawMap( byte[] map, int width, int height, int length ) { mapData = map; this.Width = width; diff --git a/ClassicalSharp/Utils/FastColour.cs b/ClassicalSharp/Utils/FastColour.cs index 1d1ae1e1c..d9aa194c9 100644 --- a/ClassicalSharp/Utils/FastColour.cs +++ b/ClassicalSharp/Utils/FastColour.cs @@ -57,7 +57,14 @@ namespace ClassicalSharp { result.G = (byte)( value.G * t ); result.B = (byte)( value.B * t ); return result; - } + } + + public static void GetShaded( FastColour normal, ref FastColour xSide, + ref FastColour zSide, ref FastColour yBottom ) { + xSide = FastColour.Scale( normal, 0.6f ); + zSide = FastColour.Scale( normal, 0.8f ); + yBottom = FastColour.Scale( normal, 0.5f ); + } public Color ToColor() { return Color.FromArgb( A, R, G, B );