diff --git a/ClassicalSharp/2D/IsometricBlockDrawer.cs b/ClassicalSharp/2D/IsometricBlockDrawer.cs index 28a7bf1f7..0dd768eff 100644 --- a/ClassicalSharp/2D/IsometricBlockDrawer.cs +++ b/ClassicalSharp/2D/IsometricBlockDrawer.cs @@ -1,7 +1,7 @@ using System; using ClassicalSharp.GraphicsAPI; -using OpenTK; using ClassicalSharp.Model; +using OpenTK; namespace ClassicalSharp { @@ -15,9 +15,15 @@ namespace ClassicalSharp { static float scale; static FastColour colNormal, colXSide, colZSide, colYBottom; + static float cosX, sinX, cosY, sinY; static IsometricBlockDrawer() { colNormal = FastColour.White; FastColour.GetShaded( colNormal, ref colXSide, ref colZSide, ref colYBottom ); + + cosX = (float)Math.Cos( 26.565f * Utils.Deg2Rad ); + sinX = (float)Math.Sin( 26.565f * Utils.Deg2Rad ); + cosY = (float)Math.Cos( -45f * Utils.Deg2Rad ); + sinY = (float)Math.Sin( -45f * Utils.Deg2Rad ); } public static void Draw( Game game, byte block, float size, float x, float y ) { @@ -28,9 +34,9 @@ namespace ClassicalSharp { index = 0; scale = size; - // screen to isometric coords - pos.X = x; pos.Y = y; pos.Z = 0; - pos = Utils.RotateY( Utils.RotateX( pos, -angleX ), -angleY ); + // screen to isometric coords (cos(-x) = cos(x), sin(-x) = -sin(x)) + pos.X = x; pos.Y = y; pos.Z = 0; + pos = Utils.RotateY( Utils.RotateX( pos, cosX, -sinX ), cosY, -sinY ); if( info.IsSprite[block] ) { DrawXFace( block, 0f, TileSide.Right ); @@ -47,8 +53,6 @@ namespace ClassicalSharp { cache.vertices, index, index * 6 / 4 ); } - static float angleY = (float)Utils.DegreesToRadians( -45f ); - static float angleX = (float)Utils.DegreesToRadians( 26.565f ); static void TransformVertex( ref VertexPos3fTex2fCol4b vertex ) { Vector3 pos = new Vector3( vertex.X, vertex.Y, vertex.Z ); #if USE_DX @@ -56,18 +60,9 @@ namespace ClassicalSharp { pos.X -= 0.5f; pos.Y -= 0.5f; #endif - pos = Utils.RotateX( Utils.RotateY( pos, angleY ), angleX ); + pos = Utils.RotateX( Utils.RotateY( pos, cosY, sinY ), cosX, sinX ); vertex.X = pos.X; vertex.Y = pos.Y; vertex.Z = pos.Z; } - - public static void SetupState( IGraphicsApi graphics, bool setFog ) { - if( setFog ) graphics.Fog = false; - graphics.BeginVbBatch( VertexFormat.Pos3fTex2fCol4b ); - } - - public static void RestoreState( IGraphicsApi graphics, bool setFog ) { - if( setFog ) graphics.Fog = true; - } static Vector3 pos = Vector3.Zero; static void DrawYFace( byte block, float y, int side ) { diff --git a/ClassicalSharp/2D/Screens/BlockSelectScreen.cs b/ClassicalSharp/2D/Screens/BlockSelectScreen.cs index e2a0141b8..1383ffa4b 100644 --- a/ClassicalSharp/2D/Screens/BlockSelectScreen.cs +++ b/ClassicalSharp/2D/Screens/BlockSelectScreen.cs @@ -1,6 +1,6 @@ using System; using System.Drawing; -using ClassicalSharp.Renderers; +using ClassicalSharp.GraphicsAPI; using OpenTK.Input; namespace ClassicalSharp { @@ -26,10 +26,9 @@ namespace ClassicalSharp { graphicsApi.Draw2DQuad( startX - 5, startY - 30 - 5, blocksPerRow * blockSize + 10, rows * blockSize + 30 + 10, backCol ); graphicsApi.Texturing = true; - graphicsApi.BindTexture( game.TerrainAtlas.TexId ); + graphicsApi.BindTexture( game.TerrainAtlas.TexId ); + graphicsApi.BeginVbBatch( VertexFormat.Pos3fTex2fCol4b ); - bool setFog = game.EnvRenderer is StandardEnvRenderer; - IsometricBlockDrawer.SetupState( graphicsApi, setFog ); for( int i = 0; i < blocksTable.Length; i++ ) { int x, y; GetCoords( i, out x, out y ); @@ -46,7 +45,6 @@ namespace ClassicalSharp { IsometricBlockDrawer.Draw( game, (byte)blocksTable[selectedIndex], 20, x + blockSize / 2, y + 28 ); } - IsometricBlockDrawer.RestoreState( graphicsApi, setFog ); if( blockInfoTexture.IsValid ) blockInfoTexture.Render( graphicsApi ); diff --git a/ClassicalSharp/2D/Screens/ChatScreen.cs b/ClassicalSharp/2D/Screens/ChatScreen.cs index 576468a8e..6a0d87229 100644 --- a/ClassicalSharp/2D/Screens/ChatScreen.cs +++ b/ClassicalSharp/2D/Screens/ChatScreen.cs @@ -36,14 +36,24 @@ namespace ClassicalSharp { } } - Font chatFont, chatBoldFont, historyFont, announcementFont; + static FastColour backColour = new FastColour( 60, 60, 60, 180 ); + public void RenderBackground() { + int height = normalChat.GetUsedHeight(); + int y = normalChat.Y + normalChat.Height - height - 5; + int x = normalChat.X - 5; + int width = normalChat.Width + 10; + + if( height > 0 ) + graphicsApi.Draw2DQuad( x, y, width, height + 10, backColour ); + } + + Font chatFont, chatInputFont, announcementFont; public override void Init() { chatFont = new Font( "Arial", game.Chat.FontSize ); - chatBoldFont = new Font( "Arial", game.Chat.FontSize, FontStyle.Bold ); + chatInputFont = new Font( "Arial", game.Chat.FontSize, FontStyle.Bold ); announcementFont = new Font( "Arial", 14 ); - historyFont = new Font( "Arial", 12, FontStyle.Italic ); - textInput = new TextInputWidget( game, chatFont, chatBoldFont ); + textInput = new TextInputWidget( game, chatFont, chatInputFont ); textInput.ChatInputYOffset = ChatInputYOffset; status = new TextGroupWidget( game, 3, chatFont ); status.VerticalDocking = Docking.LeftOrTop; @@ -101,8 +111,7 @@ namespace ClassicalSharp { game.chatInInputBuffer = textInput.chatInputText.ToString(); } chatFont.Dispose(); - chatBoldFont.Dispose(); - historyFont.Dispose(); + chatInputFont.Dispose(); announcementFont.Dispose(); normalChat.Dispose(); diff --git a/ClassicalSharp/2D/Screens/NormalScreen.cs b/ClassicalSharp/2D/Screens/NormalScreen.cs index 1fd2ab6f9..56cb24dec 100644 --- a/ClassicalSharp/2D/Screens/NormalScreen.cs +++ b/ClassicalSharp/2D/Screens/NormalScreen.cs @@ -1,6 +1,7 @@ -using System; -using System.Drawing; -using OpenTK.Input; +using System; +using System.Drawing; +using ClassicalSharp.GraphicsAPI; +using OpenTK.Input; namespace ClassicalSharp { @@ -16,9 +17,16 @@ namespace ClassicalSharp { public override void Render( double delta ) { if( game.HideGui ) return; + + chat.RenderBackground(); graphicsApi.Texturing = true; chat.Render( delta ); hotbar.Render( delta ); + + //graphicsApi.BeginVbBatch( VertexFormat.Pos3fTex2fCol4b ); + //graphicsApi.BindTexture( game.TerrainAtlas.TexId ); + //IsometricBlockDrawer.Draw( game, (byte)Block.Brick, 30, game.Width - 50, game.Height - 20 ); + if( playerList != null ) { playerList.Render( delta ); // NOTE: Should usually be caught by KeyUp, but just in case. diff --git a/ClassicalSharp/2D/Widgets/BlockHotbarWidget.cs b/ClassicalSharp/2D/Widgets/BlockHotbarWidget.cs index b95b629a0..f44054484 100644 --- a/ClassicalSharp/2D/Widgets/BlockHotbarWidget.cs +++ b/ClassicalSharp/2D/Widgets/BlockHotbarWidget.cs @@ -1,6 +1,6 @@ using System; using System.Drawing; -using ClassicalSharp.Renderers; +using ClassicalSharp.GraphicsAPI; using OpenTK.Input; namespace ClassicalSharp { @@ -42,10 +42,9 @@ namespace ClassicalSharp { 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 ); + graphicsApi.BindTexture( game.TerrainAtlas.TexId ); + graphicsApi.BeginVbBatch( VertexFormat.Pos3fTex2fCol4b ); - 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, @@ -54,7 +53,6 @@ namespace ClassicalSharp { selectedBlock.X1 = x; } - IsometricBlockDrawer.RestoreState( graphicsApi, setFog ); selectedBlock.Render( graphicsApi ); graphicsApi.Texturing = false; } diff --git a/ClassicalSharp/2D/Widgets/TextGroupWidget.cs b/ClassicalSharp/2D/Widgets/TextGroupWidget.cs index 800065138..2d0116c94 100644 --- a/ClassicalSharp/2D/Widgets/TextGroupWidget.cs +++ b/ClassicalSharp/2D/Widgets/TextGroupWidget.cs @@ -77,6 +77,15 @@ namespace ClassicalSharp { return y; } + public int GetUsedHeight() { + int sum = 0; + for( int i = 0; i < textures.Length; i++ ) { + if( textures[i].IsValid ) + sum += textures[i].Height; + } + return sum; + } + void UpdateDimensions() { Height = 0; for( int i = 0; i < textures.Length; i++ ) { diff --git a/ClassicalSharp/Entities/Entity.cs b/ClassicalSharp/Entities/Entity.cs index ed4f3023e..d05c8fad6 100644 --- a/ClassicalSharp/Entities/Entity.cs +++ b/ClassicalSharp/Entities/Entity.cs @@ -19,17 +19,15 @@ namespace ClassicalSharp { public Vector3 Velocity; public float YawDegrees, PitchDegrees; protected float StepSize; - - const float deg2Rad = (float)( Math.PI / 180 ); - const float rad2Deg = (float)( 180 / Math.PI ); + public float YawRadians { - get { return YawDegrees * deg2Rad; } - set { YawDegrees = value * rad2Deg; } + get { return YawDegrees * Utils.Deg2Rad; } + set { YawDegrees = value * Utils.Rad2Deg; } } public float PitchRadians { - get { return PitchDegrees * deg2Rad; } - set { PitchDegrees = value * rad2Deg; } + get { return PitchDegrees * Utils.Deg2Rad; } + set { PitchDegrees = value * Utils.Rad2Deg; } } public virtual Vector3 CollisionSize { diff --git a/ClassicalSharp/Game/Game.cs b/ClassicalSharp/Game/Game.cs index 93aa972ea..2248e8cc9 100644 --- a/ClassicalSharp/Game/Game.cs +++ b/ClassicalSharp/Game/Game.cs @@ -229,12 +229,12 @@ namespace ClassicalSharp { SelectedPos.SetAsInvalid(); } - Graphics.Mode2D( Width, Height ); + Graphics.Mode2D( Width, Height, EnvRenderer is StandardEnvRenderer ); fpsScreen.Render( e.Time ); if( activeScreen != null ) { activeScreen.Render( e.Time ); } - Graphics.Mode3D(); + Graphics.Mode3D( EnvRenderer is StandardEnvRenderer ); if( screenshotRequested ) TakeScreenshot(); diff --git a/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs b/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs index df1b3c9d4..60ee7ad23 100644 --- a/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs +++ b/ClassicalSharp/GraphicsAPI/IGraphicsApi.cs @@ -225,7 +225,7 @@ namespace ClassicalSharp.GraphicsAPI { Draw2DTexture( ref tex, FastColour.White ); } - public void Mode2D( float width, float height ) { + public void Mode2D( float width, float height, bool setFog ) { SetMatrixMode( MatrixType.Projection ); PushMatrix(); DepthTest = false; @@ -234,6 +234,7 @@ namespace ClassicalSharp.GraphicsAPI { PushMatrix(); LoadIdentityMatrix(); AlphaBlending = true; + if( setFog ) Fog = false; } protected virtual void LoadOrthoMatrix( float width, float height ) { @@ -241,7 +242,7 @@ namespace ClassicalSharp.GraphicsAPI { LoadMatrix( ref matrix ); } - public void Mode3D() { + public void Mode3D( bool setFog ) { // Get rid of orthographic 2D matrix. SetMatrixMode( MatrixType.Projection ); PopMatrix(); @@ -249,6 +250,7 @@ namespace ClassicalSharp.GraphicsAPI { PopMatrix(); DepthTest = true; AlphaBlending = false; + if( setFog ) Fog = true; } internal unsafe int MakeDefaultIb() { diff --git a/ClassicalSharp/Utils/Camera.cs b/ClassicalSharp/Utils/Camera.cs index c10e10e1c..ea915324e 100644 --- a/ClassicalSharp/Utils/Camera.cs +++ b/ClassicalSharp/Utils/Camera.cs @@ -36,7 +36,7 @@ namespace ClassicalSharp { } public override Matrix4 GetProjection() { - float fovy = (float)Utils.DegreesToRadians( 70 ); + float fovy = 70 * Utils.Deg2Rad; float aspectRatio = (float)game.Width / game.Height; float zNear = game.Graphics.MinZNear; return Matrix4.CreatePerspectiveFieldOfView( fovy, aspectRatio, zNear, game.ViewDistance ); @@ -71,12 +71,13 @@ namespace ClassicalSharp { delta = Point.Empty; } - static readonly float sensiFactor = (float)Utils.RadiansToDegrees( 0.0002f ); + static readonly float sensiFactor = 0.0002f * Utils.Rad2Deg; private void UpdateMouseRotation() { float sensitivity = sensiFactor * game.MouseSensitivity; float yaw = player.nextYaw + delta.X * sensitivity; float pitch = player.nextPitch + delta.Y * sensitivity; LocationUpdate update = LocationUpdate.MakeOri( yaw, pitch ); + // Need to make sure we don't cross the vertical axes, because that gets weird. if( update.Pitch >= 90 && update.Pitch <= 270 ) update.Pitch = player.nextPitch < 180 ? 89.9f : 270.1f; diff --git a/ClassicalSharp/Utils/Utils.cs b/ClassicalSharp/Utils/Utils.cs index 8ba1f08f5..bfd449393 100644 --- a/ClassicalSharp/Utils/Utils.cs +++ b/ClassicalSharp/Utils/Utils.cs @@ -1,6 +1,5 @@ using System; using System.Drawing; -using System.Text; using OpenTK; namespace ClassicalSharp { @@ -101,16 +100,11 @@ namespace ClassicalSharp { } } - public static double DegreesToRadians( double degrees ) { - return degrees * Math.PI / 180.0; - } - - public static double RadiansToDegrees( double radians ) { - return radians * 180.0 / Math.PI; - } + public const float Deg2Rad = (float)(Math.PI / 180); + public const float Rad2Deg = (float)(180 / Math.PI); public static int DegreesToPacked( double degrees, int period ) { - return (int)( degrees * period / 360.0 ) % period; + return (int)(degrees * period / 360.0) % period; } public static double PackedToDegrees( byte packed ) { @@ -129,10 +123,16 @@ namespace ClassicalSharp { return new Vector3( cosA * x - sinA * z, y, sinA * x + cosA * z ); } - public static Vector3 RotateX( Vector3 v, float angle ) { - float cosA = (float)Math.Cos( angle ); - float sinA = (float)Math.Sin( angle ); - return new Vector3( v.X, cosA * v.Y + sinA * v.Z, -sinA * v.Y + cosA * v.Z ); + public static Vector3 RotateX( Vector3 p, float cosA, float sinA ) { + return new Vector3( p.X, cosA * p.Y + sinA * p.Z, -sinA * p.Y + cosA * p.Z ); + } + + public static Vector3 RotateY( Vector3 p, float cosA, float sinA ) { + return new Vector3( cosA * p.X - sinA * p.Z, p.Y, sinA * p.X + cosA * p.Z ); + } + + public static Vector3 RotateZ( Vector3 p, float cosA, float sinA ) { + return new Vector3( cosA * p.X + sinA * p.Y, -sinA * p.X + cosA * p.Y, p.Z ); } public static Vector3 RotateX( float x, float y, float z, float cosA, float sinA ) { @@ -222,7 +222,7 @@ namespace ClassicalSharp { } public static float Lerp( float a, float b, float t ) { - return a + ( b - a ) * t; + return a + (b - a) * t; } internal static int CountIndices( int axis1Len, int axis2Len, int axisSize ) {