From 3580de56b9aea3ee73cb8c3afdf1d31aa2e4eb20 Mon Sep 17 00:00:00 2001 From: "sz.krisz20010615" Date: Mon, 31 Aug 2020 07:28:04 +0200 Subject: [PATCH] Implemented IFrameBuffer interface. TODO: Implement The UI redraw when the RenderTarget Size Changes. TODO: Implement Mouse and keyboard events. --- Example_MonoGame/NuklearRenderer.cs | 74 +++++++++++++++++++++++++++-- 1 file changed, 69 insertions(+), 5 deletions(-) diff --git a/Example_MonoGame/NuklearRenderer.cs b/Example_MonoGame/NuklearRenderer.cs index d41736e..c5db08d 100644 --- a/Example_MonoGame/NuklearRenderer.cs +++ b/Example_MonoGame/NuklearRenderer.cs @@ -3,16 +3,20 @@ using NuklearDotNet; using System; using System.Collections.Generic; +using System.Diagnostics.Contracts; using System.Runtime.InteropServices; using System.Text; namespace Example_MonoGame { - public class NuklearRenderer : NuklearDeviceTex + public class NuklearRenderer : NuklearDeviceTex, IFrameBuffered { GraphicsDevice _graphics; BasicEffect _basicEffect; VertexBuffer _vertexBuffer; + RenderTarget2D _renderTarget2D; + + SpriteBatch _spriteBatch; NkVertex[] _verts; ushort[] _inds; @@ -20,10 +24,31 @@ public class NuklearRenderer : NuklearDeviceTex public NuklearRenderer(GraphicsDevice graphics) { this._graphics = graphics; - this._basicEffect = new BasicEffect(graphics); - - _basicEffect.TextureEnabled = true; - _basicEffect.VertexColorEnabled = true; + this._basicEffect = new BasicEffect(_graphics) + { + TextureEnabled = true, + VertexColorEnabled = true + }; + + _renderTarget2D = new RenderTarget2D(_graphics, + _graphics.PresentationParameters.BackBufferWidth, + _graphics.PresentationParameters.BackBufferHeight, + false, + _graphics.PresentationParameters.BackBufferFormat, + DepthFormat.Depth24); + + _spriteBatch = new SpriteBatch(_graphics); + + // This BlendState is no longer neccesary. + //BlendState _blendState = new BlendState(); + //_blendState.ColorBlendFunction = BlendFunction.Add; + //_blendState.ColorSourceBlend = Blend.SourceAlpha; + //_blendState.ColorDestinationBlend = Blend.InverseSourceAlpha; + //SpriteBatch.Begin(SpriteSortMode.Immediate, + // blendState: _blendstate, + // samplerState: SamplerState.PointClamp, + // depthStencilState: DepthStencilState.None, + // rasterizerState: RasterizerState.CullNone); } public override Texture2D CreateTexture(int W, int H, IntPtr Data) @@ -73,5 +98,44 @@ public override void SetBuffer(NkVertex[] VertexBuffer, ushort[] IndexBuffer) _verts = VertexBuffer; _inds = IndexBuffer; } + + public void BeginBuffering() + { + _graphics.SetRenderTarget(_renderTarget2D); + _graphics.Clear(Color.CornflowerBlue); + _spriteBatch.Begin(SpriteSortMode.Immediate, blendState: BlendState.NonPremultiplied, samplerState: SamplerState.PointClamp, depthStencilState: DepthStencilState.None, rasterizerState: RasterizerState.CullNone); + } + + public void EndBuffering() + { + _spriteBatch.End(); + _graphics.SetRenderTarget(null); + //_graphics.RasterizerState.ScissorTestEnable = false; + } + + void IFrameBuffered.RenderFinal() + { + _spriteBatch.Begin(blendState: BlendState.Opaque); + _spriteBatch.Draw(_renderTarget2D, Vector2.Zero, Color.White); + _spriteBatch.End(); + + RenderFinalEnded(); + } + + void RenderFinalEnded() + { + if (_graphics.PresentationParameters.BackBufferWidth != _renderTarget2D.Width + || _graphics.PresentationParameters.BackBufferHeight != _renderTarget2D.Height) + { + _renderTarget2D = new RenderTarget2D(_graphics, + _graphics.PresentationParameters.BackBufferWidth, + _graphics.PresentationParameters.BackBufferHeight, + false, + _graphics.PresentationParameters.BackBufferFormat, + DepthFormat.None); + + ForceUpdate(); + } + } } }