From 1dc09ca9f3dfaa65ae8ee3e1160bb1ea32d11c16 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Thu, 9 Jul 2015 10:20:26 +1000 Subject: [PATCH] Add VSync support to Direct3D api. --- 2D/Screens/PauseScreen.cs | 2 +- Game/Game.InputHandling.cs | 2 +- Game/Game.cs | 5 ++--- GraphicsAPI/DirectXApi.cs | 17 ++++++++++++++--- GraphicsAPI/IGraphicsApi.cs | 7 +++---- GraphicsAPI/OpenGLApi.cs | 8 ++++++-- 6 files changed, 27 insertions(+), 14 deletions(-) diff --git a/2D/Screens/PauseScreen.cs b/2D/Screens/PauseScreen.cs index fe925c6f6..8132db388 100644 --- a/2D/Screens/PauseScreen.cs +++ b/2D/Screens/PauseScreen.cs @@ -48,7 +48,7 @@ namespace ClassicalSharp { MakeKeys( KeyMapping.Forward, descriptionsLeft, 10, out keysLeft ); leftEnd = CalculateMaxWidth( keysLeft ); - string[] descriptionsRight = { "Take screenshot", "Toggle fullscreen", "Toggle 3rd person camera", "Toggle VSync", + string[] descriptionsRight = { "Take screenshot", "Toggle fullscreen", "Toggle VSync", "Toggle 3rd person camera", "Change view distance", "Toggle fly", "Speed", "Toggle noclip", "Fly up", "Fly down", "Display player list" }; MakeKeys( KeyMapping.Screenshot, descriptionsRight, leftEnd + 30, out keysRight ); } diff --git a/Game/Game.InputHandling.cs b/Game/Game.InputHandling.cs index 4340602d3..08279e3e3 100644 --- a/Game/Game.InputHandling.cs +++ b/Game/Game.InputHandling.cs @@ -102,7 +102,7 @@ namespace ClassicalSharp { bool useThirdPerson = Camera is FirstPersonCamera; SetCamera( useThirdPerson ); } else if( key == Keys[KeyMapping.VSync] ) { - VSync = VSync == VSyncMode.Off ? VSyncMode.On : VSyncMode.Off; + Graphics.SetVSync( this, VSync == VSyncMode.Off ); } else if( key == Keys[KeyMapping.ViewDistance] ) { for( int i = 0; i < viewDistances.Length; i++ ) { int newDist = viewDistances[i]; diff --git a/Game/Game.cs b/Game/Game.cs index 994781aa7..42c219080 100644 --- a/Game/Game.cs +++ b/Game/Game.cs @@ -148,7 +148,7 @@ namespace ClassicalSharp { WeatherRenderer = new WeatherRenderer( this ); WeatherRenderer.Init(); - VSync = VSyncMode.On; + Graphics.SetVSync( this, true ); Graphics.DepthTest = true; Graphics.DepthTestFunc( CompareFunc.LessEqual ); //Graphics.DepthWrite = true; @@ -274,7 +274,6 @@ namespace ClassicalSharp { Picking.Dispose(); ParticleManager.Dispose(); Players.Dispose(); - Graphics.CheckResources(); AsyncDownloader.Dispose(); if( writer != null ) { writer.Close(); @@ -294,7 +293,7 @@ namespace ClassicalSharp { protected override void OnResize( EventArgs e ) { base.OnResize( e ); - Graphics.OnWindowResize( Width, Height ); + Graphics.OnWindowResize( this ); UpdateProjection(); if( activeScreen != null ) { activeScreen.OnResize( width, height, Width, Height ); diff --git a/GraphicsAPI/DirectXApi.cs b/GraphicsAPI/DirectXApi.cs index ab615730e..f81b2f371 100644 --- a/GraphicsAPI/DirectXApi.cs +++ b/GraphicsAPI/DirectXApi.cs @@ -392,8 +392,19 @@ namespace ClassicalSharp.GraphicsAPI { device.Present(); } - public override void OnWindowResize( int newWidth, int newHeight ) { - PresentParameters args = GetPresentArgs( newWidth, newHeight ); + bool vsync = false; + public override void SetVSync( Game game, bool value ) { + vsync = value; + game.VSync = value ? OpenTK.VSyncMode.On : OpenTK.VSyncMode.Off; + RecreateDevice( game ); + } + + public override void OnWindowResize( Game game ) { + RecreateDevice( game ); + } + + void RecreateDevice( Game game ) { + PresentParameters args = GetPresentArgs( game.Width, game.Height ); device.Reset( args ); SetDefaultRenderStates(); device.SetRenderState( RenderStates.AlphaTestEnable, alphaTest ); @@ -428,7 +439,7 @@ namespace ClassicalSharp.GraphicsAPI { args.BackBufferWidth = width; args.BackBufferHeight = height; args.EnableAutoDepthStencil = true; - args.PresentationInterval = PresentInterval.Immediate; + args.PresentationInterval = vsync ? PresentInterval.One : PresentInterval.Immediate; args.SwapEffect = SwapEffect.Discard; args.Windowed = true; return args; diff --git a/GraphicsAPI/IGraphicsApi.cs b/GraphicsAPI/IGraphicsApi.cs index 9efdece50..6c95c8bcd 100644 --- a/GraphicsAPI/IGraphicsApi.cs +++ b/GraphicsAPI/IGraphicsApi.cs @@ -153,9 +153,6 @@ namespace ClassicalSharp.GraphicsAPI { public abstract void TakeScreenshot( string output, Size size ); - public virtual void CheckResources() { - } - public virtual void PrintApiSpecificInfo() { } @@ -163,7 +160,9 @@ namespace ClassicalSharp.GraphicsAPI { public abstract void EndFrame( Game game ); - public abstract void OnWindowResize( int newWidth, int newHeight ); + public abstract void SetVSync( Game game, bool value ); + + public abstract void OnWindowResize( Game game ); protected void InitDynamicBuffers() { quadVb = CreateDynamicVb( VertexFormat.Pos3fCol4b, 4 ); diff --git a/GraphicsAPI/OpenGLApi.cs b/GraphicsAPI/OpenGLApi.cs index 6326182dc..f28eb38d4 100644 --- a/GraphicsAPI/OpenGLApi.cs +++ b/GraphicsAPI/OpenGLApi.cs @@ -374,6 +374,10 @@ namespace ClassicalSharp.GraphicsAPI { game.SwapBuffers(); } + public override void SetVSync( Game game, bool value ) { + game.VSync = value ? VSyncMode.On : VSyncMode.Off; + } + public unsafe override void PrintApiSpecificInfo() { Console.WriteLine( "OpenGL vendor: " + new String( (sbyte*)Gl.glGetString( StringName.Vendor ) ) ); Console.WriteLine( "OpenGL renderer: " + new String( (sbyte*)Gl.glGetString( StringName.Renderer ) ) ); @@ -400,8 +404,8 @@ namespace ClassicalSharp.GraphicsAPI { } } - public override void OnWindowResize( int newWidth, int newHeight ) { - Gl.glViewport( 0, 0, newWidth, newHeight ); + public override void OnWindowResize( Game game ) { + Gl.glViewport( 0, 0, game.Width, game.Height ); } static void ToggleCap( EnableCap cap, bool value ) {