Hello I am here - I already tried to fix for Gtk Sharp 3.x for Windows but it looks very bright - It doesnāt make normal color.
using Cairo;
using Gtk;
using System;
namespace MonoGame.Framework.Gtk3
{
public class RenderArea : DrawingArea
{
private Cairo.Color _clearColor;
private Microsoft.Xna.Framework.Graphics.Viewport _view;
public event EventHandler Rendered;
public RenderArea()
{
BackgroundColor = BackgroundColor;
_view = new Microsoft.Xna.Framework.Graphics.Viewport(0, 0, Allocation.Width, Allocation.Height);
Rendered += new EventHandler(OnRender);
Drawn += new DrawnHandler(drawnHandler);
}
protected virtual void OnRender(object sender, EventArgs e)
{
}
private void drawnHandler(object o, DrawnArgs args)
{
Context cr = args.Cr;
PointD p1, p2, p3, p4;
p1 = new PointD(0, 0);
p2 = new PointD(Allocation.Width, 0);
p3 = new PointD(Allocation.Width, Allocation.Height);
p4 = new PointD(0, Allocation.Height);
cr.MoveTo(p1);
cr.LineTo(p2);
cr.LineTo(p3);
cr.LineTo(p4);
cr.LineTo(p1);
cr.ClosePath();
#pragma warning disable CS0612 // Type or member is obsolete
#pragma warning disable CS0618 // Type or member is obsolete
cr.Color = _clearColor;
cr.FillPreserve();
Surface surf = Surface.CreateForImage(Format.Argb32, Allocation.Width, Allocation.Height);
cr.SetSourceSurface(surf, 0, 0);
Gtk3Backend.BeginDraw(Allocation.Width, Allocation.Height);
Gtk3Backend.GraphicsDevice.Clear(new Microsoft.Xna.Framework.Color((byte)_clearColor.R, (byte)_clearColor.G, (byte)_clearColor.B, (byte)_clearColor.A));
Gtk3Backend.GraphicsDevice.Viewport = _view;
Rendered?.Invoke(o, EventArgs.Empty);
Gtk3Backend.EndDraw();
Gdk.CairoHelper.SetSourceWindow(cr, GdkWindow, 0, 0);
cr.Paint();
cr.GetTarget().Dispose();
cr.GetTarget().Destroy();
#pragma warning restore CS0618 // Type or member is obsolete
#pragma warning restore CS0612 // Type or member is obsolete
}
protected override void OnSizeAllocated(Gdk.Rectangle allocation)
{
_view = new Microsoft.Xna.Framework.Graphics.Viewport(allocation.X, allocation.Y, allocation.Width, allocation.Height);
base.OnSizeAllocated(allocation);
}
protected override void Dispose(bool disposing)
{
Destroy();
base.Dispose(disposing);
}
public Color BackgroundColor
{
get
{
return _clearColor;
}
set
{
_clearColor = new Color(value.R, value.G, value.B, value.A);
}
}
}
}
I already tried with Present() But it is really hard to resolve ā¦ I have any possiblities and Gtk.Dotnet.Graphics.fromDrawable()
// EDIT:
From RenderArea.cs:
protected override bool OnDrawn(Context cr)
{
PointD p1, p2, p3, p4;
p1 = new PointD(0, 0);
p2 = new PointD(Allocation.Width, 0);
p3 = new PointD(Allocation.Width, Allocation.Height);
p4 = new PointD(0, Allocation.Height);
cr.MoveTo(p1);
cr.LineTo(p2);
cr.LineTo(p3);
cr.LineTo(p4);
cr.LineTo(p1);
cr.ClosePath();
#pragma warning disable CS0612 // Type or member is obsolete
#pragma warning disable CS0618 // Type or member is obsolete
cr.Color = _clearColor;
cr.FillPreserve();
Surface surf = Surface.CreateForImage(Format.Argb32, Allocation.Width, Allocation.Height);
cr.SetSourceSurface(surf, 0, 0);
Gtk3Backend.BeginDraw(Allocation.Width, Allocation.Height);
Gtk3Backend.GraphicsDevice.Clear(new Microsoft.Xna.Framework.Color((byte)_clearColor.R, (byte)_clearColor.G, (byte)_clearColor.B, (byte)_clearColor.A));
Gtk3Backend.GraphicsDevice.Viewport = _view;
Rendered?.Invoke(null, EventArgs.Empty);
Gtk3Backend.EndDraw();
Gdk.CairoHelper.SetSourceWindow(cr, GdkWindow, 0, 0);
cr.Paint();
System.Drawing.Graphics graphics = Gtk.DotNet.Graphics.FromDrawable(GdkWindow);
graphics.DrawImage(Gtk3Backend.Present(), 0, 0, Allocation.Width, Allocation.Height);
cr.GetTarget().Dispose();
cr.GetTarget().Destroy();
#pragma warning restore CS0618 // Type or member is obsolete
#pragma warning restore CS0612 // Type or member is obsolete
return base.OnDrawn(cr);
}
Gtk3Backend.cs ( in MasterWindow )
I add someting lines for support with Cairo.
internal void EndRender()
{
// Exit if not drawing anything
if (!isDrawing) return;
// Create Surface Image
var width = DeviceManager.PreferredBackBufferWidth;
var height = DeviceManager.PreferredBackBufferHeight;
var bmp = new Bitmap(width, height, PixelFormat.Format32bppRgb);
var bmpData = bmp.LockBits(
new System.Drawing.Rectangle(0, 0, width, height),
ImageLockMode.WriteOnly,
PixelFormat.Format32bppRgb);
var pixelData = new int[width * height];
// Get buffer data
GraphicsDevice.GetBackBufferData(pixelData);
for (int i = 0; i < pixelData.Length; i++)
#pragma warning disable // Caused by bitwise function requiring uint to int conversion
pixelData[i] = (int)( // Swap bgra - rgba
(pixelData[i] & 0x000000ff) << 16 |
(pixelData[i] & 0x0000FF00) |
(pixelData[i] & 0x00FF0000) >> 16 |
(pixelData[i] & 0xFF000000));
#pragma warning disable
// Convert to bitmap
Cairo.Context cr;
Cairo.ImageSurface imgsrf = new Cairo.ImageSurface(bmpData.Scan0,
Cairo.Format.Argb32, width, height, width * 4);
cr = new Cairo.Context(imgsrf);
Marshal.Copy(pixelData, 0, bmpData.Scan0, pixelData.Length);
bmp.UnlockBits(bmpData);
BackBuffer = bmp;
// Mark as done drawing.
isDrawing = false;
}
Result: NotsupportedException
So close my success I wish that Gtk Sharp 3.x works like Winforms
// EDIT. I tried Gtk.Bin but Gtk.Bin is same like Gtk.DrawingArea
Gtk.Widget or Gtk.Container are quick closing gtk App.