Hello everyone,
I have tried to resolve with XDrawRectangles under C#:
Displaying = OK
Drawing = So bad!
How do I fix ff I have tried to created Rectangle as Structure for X11/Xlib?
Looks like this simple:
namespace X
{
public static partial class X11
{
...
[StructLayout(LayoutKind.Sequential)]
public struct Rectangle
{
public int X { get; set; }
public int Y { get; set; }
public uint W { get; set; }
public uint H { get; set; }
public Rectangle(int X, int Y, uint W, uint H)
{
this.X = X; this.Y = Y;
this.W = W; this.H = H;
}
public override string ToString()
{
return "X:" + X + " Y:" + Y + " Width:" + W + " Height:" + H;
}
}
...
}
}
And I have created p/invoke like this:
[DllImport(libX11)]
private extern static void XDrawRectangles(IntPtr display, IntPtr drawable, IntPtr gc, IntPtr rectangles, int number_rectangles);
public static void DrawRectangles(ref Display display, Drawable drawable, ref GContext gc, Rectangle[] rectangles, int number_rectangles)
{
var size = Marshal.SizeOf(typeof(Rectangle));
var rect = Marshal.AllocHGlobal(size);
rectangles = new Rectangle[number_rectangles];
for (int i = 0; i < number_rectangles; i++)
{
IntPtr rect_ptr = new IntPtr(rect.ToInt64() + i * size);
if (rect_ptr != IntPtr.Zero)
{
rectangles[i] = (Rectangle)Marshal.PtrToStructure(rect_ptr, typeof(Rectangle));
}
XDrawRectangles(display.Handle, drawable.Handle, gc.Handle, rect_ptr, number_rectangles);
}
}
And I have written example for drawing if drawing is not weird.
using System;
using X;
namespace XSharpTest
{
class Program
{
static void Main(string[] args)
{
X11.Display display = X11.OpenDisplay(string.Empty);
if (display.GetType() == null)
{
Console.WriteLine("X11 doesn't load");
}
else
{
Console.WriteLine("Test for X11");
}
int screen_number = X11.DefaultScreen(ref display);
Console.WriteLine("Screen number is " + Convert.ToString(screen_number));
uint black = X11.BlackPixel(ref display, screen_number);
Console.WriteLine("Black is " + Convert.ToString(black));
uint white = X11.WhitePixel(ref display, screen_number);
Console.WriteLine("White is " + Convert.ToString(white));
X11.GContext context = X11.DefaultGC(ref display, screen_number);
X11.Window root = X11.RootWindow(ref display, screen_number);
X11.Window window = X11.CreateSimpleWindow(ref display, root, 100, 100, 200, 200, 0, black, white);
X11.ClearWindow(ref display, window);
X11.MapWindow(ref display, window);
X11.Flush(ref display);
while (!Convert.ToBoolean(X11.Pending(ref display)))
{
X11.DrawRectangle(ref display, window, ref context, 20, 20, 160, 160);
X11.Rectangle[] rectangles = new X11.Rectangle[40];
for (int i = 0; i < 40; i++)
{
rectangles[i].X = 20 * i;
rectangles[i].Y = 20 * i;
rectangles[i].W = 20 * 2;
rectangles[i].H = 20 * 3;
X11.DrawRectangles(ref display, window, ref context, rectangles, i);
}
}
System.Threading.Thread.Sleep(15000);
X11.DestroyWindow(ref display, window);
X11.CloseDisplay(ref display);
}
}
}
Looks picture and uuuh drawing is really weird.
Imgur: The magic of the Internet
Workaround with XDrawRectangle and multiple in for-statement
Replace with "//":
X11.DrawRectangle(ref display, window, ref context, rectangles[i].X, rectangles[i].Y, rectangles[i].W, rectangles[i].H);
Look picture like it works fine. Yeah but it is really impossible because other functions are unusable. It is really stupid of faked methods for X11/Xlib.
I really appreciate that X11/Xlib has real functions like real drawing...
Is it bug or Mono has problem. I should move to Dotnet 3?
Thank you for replying my improvement.
// EDIT!!
PS: Please stop using Windows 10 libraries!
I am using Linux/FreeBSD.
Thanks!
What I have tried:
Drawing from XDrawRectangles weird.
I have tried any possibilities - No success... even class or structure...