Skip to content

Commit

Permalink
Implemented IFrameBuffer interface.
Browse files Browse the repository at this point in the history
TODO: Implement The UI redraw when the RenderTarget Size Changes.
TODO: Implement Mouse and keyboard events.
  • Loading branch information
Donaut committed Aug 31, 2020
1 parent aca9ca1 commit 3580de5
Showing 1 changed file with 69 additions and 5 deletions.
74 changes: 69 additions & 5 deletions Example_MonoGame/NuklearRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,52 @@
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<Texture2D>
public class NuklearRenderer : NuklearDeviceTex<Texture2D>, IFrameBuffered
{
GraphicsDevice _graphics;
BasicEffect _basicEffect;
VertexBuffer _vertexBuffer;
RenderTarget2D _renderTarget2D;

SpriteBatch _spriteBatch;

NkVertex[] _verts;
ushort[] _inds;

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)
Expand Down Expand Up @@ -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();
}
}
}
}

0 comments on commit 3580de5

Please sign in to comment.