Skip to content

Instantly share code, notes, and snippets.

@Donaut Donaut/Game1.cs Secret

Last active Feb 6, 2020
Embed
What would you like to do?
NuklearDotnet Monogame
using System;
using System.Runtime.InteropServices;
using ExampleShared;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using NuklearDotNet;
using System.IO;
using Drawing = System.Drawing;
using Imaging = System.Drawing.Imaging;
namespace ExampleMonoGame
{
/// <summary>
/// This is the main type for your game.
/// </summary>
public class Game1 : Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
NuklearRenderer _device;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
Window.AllowUserResizing = true;
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
_device = new NuklearRenderer(graphics.GraphicsDevice);
// Draw the UI.
Shared.Init(_device);
}
protected override void UnloadContent()
{
}
protected override void Update(GameTime gameTime)
{
_device.Update(gameTime);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(new Color(50, 50, 50));
spriteBatch.Begin(samplerState: SamplerState.AnisotropicClamp, depthStencilState: DepthStencilState.None);
Shared.DrawLoop((float)gameTime.ElapsedGameTime.TotalSeconds);
spriteBatch.End();
base.Draw(gameTime);
}
}
public class NuklearRenderer : NuklearDeviceTex<Texture2D>
{
GraphicsDevice _graphics;
BasicEffect _basicEffect;
VertexBuffer _vertexBuffer;
NkVertex[] _verts;
ushort[] _inds;
public NuklearRenderer(GraphicsDevice graphics)
{
this._graphics = graphics;
this._basicEffect = new BasicEffect(graphics);
_basicEffect.TextureEnabled = true;
_basicEffect.VertexColorEnabled = true;
}
public override Texture2D CreateTexture(int W, int H, IntPtr Data)
{
Drawing.Bitmap Bmp = new Drawing.Bitmap(W, H);
MemoryStream memoryStream = new MemoryStream(W * H);
Imaging.BitmapData Dta = Bmp.LockBits(new System.Drawing.Rectangle(0, 0, W, H), Imaging.ImageLockMode.WriteOnly, Imaging.PixelFormat.Format32bppArgb);
for (int i = 0; i < W * H; i++)
Marshal.WriteInt32(Dta.Scan0, i * sizeof(int), Marshal.ReadInt32(Data, i * sizeof(int)));
Bmp.UnlockBits(Dta);
Bmp.Save(memoryStream, Imaging.ImageFormat.Png);
return Texture2D.FromStream(_graphics, memoryStream);
}
public void Update(GameTime gameTime)
{
_basicEffect.Projection = Matrix.CreateOrthographicOffCenter(
0, _graphics.Viewport.Width, _graphics.Viewport.Height, 0, 0, 1);
}
public override void Render(NkHandle Userdata, Texture2D Texture, NkRect ClipRect, uint Offset, uint Count)
{
VertexPositionColorTexture[] MonoVerts = new VertexPositionColorTexture[Count];
for (int i = 0; i < Count; i++)
{
NkVertex V = _verts[_inds[Offset + i]];
MonoVerts[i] = new VertexPositionColorTexture(new Vector3(V.Position.X, V.Position.Y, 0), new Color(V.Color.R, V.Color.G, V.Color.B, V.Color.A), new Vector2(V.UV.X, V.UV.Y));
}
_vertexBuffer = new VertexBuffer(_graphics, typeof(VertexPositionColorTexture), (int)Count, BufferUsage.WriteOnly);
_vertexBuffer.SetData<VertexPositionColorTexture>(MonoVerts);
_graphics.SetVertexBuffer(_vertexBuffer);
_basicEffect.Texture = Texture;
foreach (EffectPass pass in _basicEffect.CurrentTechnique.Passes)
{
pass.Apply();
_graphics.DrawPrimitives(PrimitiveType.TriangleList, 0, (int)Count);
}
}
public override void SetBuffer(NkVertex[] VertexBuffer, ushort[] IndexBuffer)
{
_verts = VertexBuffer;
_inds = IndexBuffer;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.