diff --git a/ClassicalSharp/Game/Game.cs b/ClassicalSharp/Game/Game.cs index 710dd5157..26bcdb691 100644 --- a/ClassicalSharp/Game/Game.cs +++ b/ClassicalSharp/Game/Game.cs @@ -108,8 +108,8 @@ namespace ClassicalSharp { void PrintGraphicsInfo() { Console.ForegroundColor = ConsoleColor.Green; Graphics.PrintApiSpecificInfo(); - Console.WriteLine( "Max 2D texture dimensions: " + Graphics.MaxTextureDimensions ); - Console.WriteLine( "== End of graphics info ==" ); + Utils.Log( "Max 2D texture dimensions: " + Graphics.MaxTextureDimensions ); + Utils.Log( "== End of graphics info ==" ); Console.ResetColor(); } diff --git a/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs b/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs index d1e0a1e20..e5f049f5c 100644 --- a/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs +++ b/ClassicalSharp/GraphicsAPI/Direct3D9Api.cs @@ -2,8 +2,8 @@ using System; using System.Drawing; using System.Drawing.Imaging; -using System.Reflection; using System.Runtime.InteropServices; +using System.Threading; using SharpDX; using SharpDX.Direct3D9; using D3D = SharpDX.Direct3D9; @@ -413,7 +413,24 @@ namespace ClassicalSharp.GraphicsAPI { public override void EndFrame( Game game ) { device.EndScene(); - device.Present(); + int code = device.Present().Code; + if( code >= 0 ) return; + + if( (uint)code != (uint)Direct3DError.DeviceLost ) + throw new SharpDXException( code ); + + // TODO: Make sure this actually works on all graphics cards. + Utils.LogDebug( "Lost Direct3D device." ); + while( true ) { + Thread.Sleep( 50 ); + code = device.TestCooperativeLevel().Code; + if( (uint)code == (uint)Direct3DError.DeviceNotReset ) { + Utils.Log( "Retrieved Direct3D device again." ); + RecreateDevice( game ); + break; + } + game.Network.Tick( 1 / 20.0 ); + } } bool vsync = false; @@ -547,10 +564,10 @@ namespace ClassicalSharp.GraphicsAPI { } public override void PrintApiSpecificInfo() { - Console.WriteLine( "D3D tex memory available: " + (uint)device.AvailableTextureMemory ); - Console.WriteLine( "D3D vertex processing: " + createFlags ); - Console.WriteLine( "D3D depth buffer format: " + depthFormat ); - Console.WriteLine( "D3D device caps: " + caps.DeviceCaps ); + Utils.Log( "D3D tex memory available: " + (uint)device.AvailableTextureMemory ); + Utils.Log( "D3D vertex processing: " + createFlags ); + Utils.Log( "D3D depth buffer format: " + depthFormat ); + Utils.Log( "D3D device caps: " + caps.DeviceCaps ); } public unsafe override void TakeScreenshot( string output, Size size ) { diff --git a/ClassicalSharp/GraphicsAPI/OpenGLApi.cs b/ClassicalSharp/GraphicsAPI/OpenGLApi.cs index a597d6b94..6c8651801 100644 --- a/ClassicalSharp/GraphicsAPI/OpenGLApi.cs +++ b/ClassicalSharp/GraphicsAPI/OpenGLApi.cs @@ -379,12 +379,12 @@ namespace ClassicalSharp.GraphicsAPI { } public unsafe override void PrintApiSpecificInfo() { - Console.WriteLine( "OpenGL vendor: " + new String( (sbyte*)GL.GetString( StringName.Vendor ) ) ); - Console.WriteLine( "OpenGL renderer: " + new String( (sbyte*)GL.GetString( StringName.Renderer ) ) ); - Console.WriteLine( "OpenGL version: " + new String( (sbyte*)GL.GetString( StringName.Version ) ) ); + Utils.Log( "OpenGL vendor: " + new String( (sbyte*)GL.GetString( StringName.Vendor ) ) ); + Utils.Log( "OpenGL renderer: " + new String( (sbyte*)GL.GetString( StringName.Renderer ) ) ); + Utils.Log( "OpenGL version: " + new String( (sbyte*)GL.GetString( StringName.Version ) ) ); int depthBits = 0; GL.GetIntegerv( GetPName.DepthBits, &depthBits ); - Console.WriteLine( "Depth buffer bits: " + depthBits ); + Utils.Log( "Depth buffer bits: " + depthBits ); if( depthBits < 24 ) { Utils.LogWarning( "Depth buffer is less than 24 bits, you may see some issues " + "with disappearing and/or 'white' graphics." ); diff --git a/ClassicalSharp/Program.cs b/ClassicalSharp/Program.cs index 8b2a6724e..4708cd662 100644 --- a/ClassicalSharp/Program.cs +++ b/ClassicalSharp/Program.cs @@ -14,7 +14,7 @@ namespace ClassicalSharp { AppDomain.CurrentDomain.UnhandledException += UnhandledException; } - Console.WriteLine( "Starting " + Utils.AppName + ".." ); + Utils.Log( "Starting " + Utils.AppName + ".." ); if( !AllResourcesExist( "terrain.png", "char.png", "clouds.png" ) ) { return; } @@ -60,7 +60,7 @@ namespace ClassicalSharp { static void Fail( string text ) { Utils.LogWarning( text ); - Console.WriteLine( "Press any key to exit.." ); + Utils.Log( "Press any key to exit.." ); Console.ReadKey( true ); } diff --git a/ClassicalSharp/Utils/Utils.cs b/ClassicalSharp/Utils/Utils.cs index 045febc69..8502dece9 100644 --- a/ClassicalSharp/Utils/Utils.cs +++ b/ClassicalSharp/Utils/Utils.cs @@ -156,18 +156,34 @@ namespace ClassicalSharp { return new Vector3( (float)x, (float)y, (float)z ); } + public static void Log( string text ) { + Console.WriteLine( text ); + } + + public static void Log( string text, params object[] args ) { + Log( String.Format( text, args ) ); + } + public static void LogWarning( string text ) { Console.ForegroundColor = ConsoleColor.Yellow; Console.WriteLine( text ); Console.ResetColor(); } + public static void LogWarning( string text, params object[] args ) { + LogWarning( String.Format( text, args ) ); + } + public static void LogError( string text ) { Console.ForegroundColor = ConsoleColor.Red; Console.WriteLine( text ); Console.ResetColor(); } + public static void LogError( string text, params object[] args ) { + LogError( String.Format( text, args ) ); + } + public static void LogDebug( string text ) { #if DEBUG Console.ForegroundColor = ConsoleColor.DarkGray; @@ -176,14 +192,6 @@ namespace ClassicalSharp { #endif } - public static void LogWarning( string text, params object[] args ) { - LogWarning( String.Format( text, args ) ); - } - - public static void LogError( string text, params object[] args ) { - LogError( String.Format( text, args ) ); - } - public static void LogDebug( string text, params object[] args ) { #if DEBUG LogDebug( String.Format( text, args ) ); diff --git a/SharpDX/SharpDX.Direct3D/Device.cs b/SharpDX/SharpDX.Direct3D/Device.cs index 6cd553988..9509eceb3 100644 --- a/SharpDX/SharpDX.Direct3D/Device.cs +++ b/SharpDX/SharpDX.Direct3D/Device.cs @@ -50,8 +50,8 @@ namespace SharpDX.Direct3D9 DrawIndexedPrimitiveUP(type, minimumVertexIndex, vertexCount, primitiveCount, (IntPtr)Interop.Fixed(ref indexData[startIndex]), indexDataFormat, (IntPtr)Interop.Fixed(ref vertexData[startVertex]), Interop.SizeOf()); } - public void Present() { - Present(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); + public Result Present() { + return Present(IntPtr.Zero, IntPtr.Zero, IntPtr.Zero, IntPtr.Zero); } public Result TestCooperativeLevel() { @@ -95,13 +95,12 @@ namespace SharpDX.Direct3D9 public void Reset(params PresentParameters[] presentationParametersRef) { Result res; fixed (void* presentParamsRef = presentationParametersRef) - res= Interop.Calli(comPointer, (IntPtr)presentParamsRef,(*(IntPtr**)comPointer)[16]); + res = Interop.Calli(comPointer, (IntPtr)presentParamsRef,(*(IntPtr**)comPointer)[16]); res.CheckError(); } - internal void Present(IntPtr sourceRectRef, IntPtr destRectRef, IntPtr hDestWindowOverride, IntPtr dirtyRegionRef) { - Result res = Interop.Calli(comPointer, sourceRectRef, destRectRef, hDestWindowOverride, dirtyRegionRef,(*(IntPtr**)comPointer)[17]); - res.CheckError(); + internal Result Present(IntPtr sourceRectRef, IntPtr destRectRef, IntPtr hDestWindowOverride, IntPtr dirtyRegionRef) { + return Interop.Calli(comPointer, sourceRectRef, destRectRef, hDestWindowOverride, dirtyRegionRef,(*(IntPtr**)comPointer)[17]); } public Surface GetBackBuffer(int iSwapChain, int iBackBuffer, BackBufferType type) {