Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Messages - DeafMan1983

Pages: [1]
1
DotNet / Problem with porting from NinePatch to C#
« on: March 12, 2021, 03:53:02 pm »
Hello I have ported from NinePatch to C#. But It can't display :(

using SFML.Graphics;
using SFML.System;

namespace SFML_Test
{
    public class NinePatch : Transformable, Drawable
    {
        private PrimitiveType m_primitiveType;
        private Vertex[] m_vertices;
        private Texture m_texture;
        private Vector2f m_trimmedSize;
        private Vector2f m_size;
        private Vector2f m_scaleTopLeft;
        private Vector2f m_scaleBottomRight;
        private Vector2f m_contentTopLeft;
        private Vector2f m_contentBottomRight;
        private IntRect m_textureRectangle;
        private Vector2f trimAmount = new Vector2f(1, 1);

        public NinePatch() : base()
        {
            m_primitiveType = PrimitiveType.Quads;
            m_vertices = new Vertex[36];
            m_texture = null;
            m_trimmedSize = new Vector2f(0, 0);
            m_size = new Vector2f(0, 0);
            m_scaleTopLeft = new Vector2f(0, 0);
            m_scaleBottomRight = new Vector2f(0, 0);
            m_contentTopLeft = new Vector2f(0, 0);
            m_contentBottomRight = new Vector2f(0, 0);
            m_textureRectangle = new IntRect(0, 0, 3, 3);
        }

        private void ExtractScalePositionsAndContentAreaFromTexture(Texture pTexture, IntRect textureRectangle, ref Vector2f topLeft, ref Vector2f bottomRight, ref Vector2f contentTopLeft, ref Vector2f contentBottomRight)
        {
            Image image = pTexture.CopyToImage();

            topLeft = new Vector2f(0, 0);
            bottomRight = new Vector2f(textureRectangle.Width - 2f, textureRectangle.Height - 2f);
            bool foundStart = false;
            for (uint x = 1u; x < textureRectangle.Width; x++)
            {
                if (!foundStart)
                {
                    if (image.GetPixel((uint)textureRectangle.Left + x, (uint)textureRectangle.Top) == Color.Black)
                    {
                        foundStart = true;
                        topLeft.X = x - 1f;
                    }
                    else
                        continue;
                }
                if (foundStart)
                {
                    if (image.GetPixel((uint)textureRectangle.Left + x, (uint)textureRectangle.Top) == Color.Black)
                        bottomRight.X = x - 1f;
                    else
                        break;
                }
            }

            for (uint y = 1u; y < textureRectangle.Height; y++)
            {
                if (!foundStart)
                {
                    if (image.GetPixel((uint)textureRectangle.Left, (uint)textureRectangle.Top + y) == Color.Black)
                    {
                        foundStart = true;
                        topLeft.Y = y - 1f;
                    }
                    else
                        continue;
                }
                if (foundStart)
                {
                    if (image.GetPixel((uint)textureRectangle.Left, (uint)textureRectangle.Top + y) == Color.Black)
                        bottomRight.Y = y - 1f;
                    else
                        break;
                }
            }

            contentTopLeft = new Vector2f(0, 0);
            contentBottomRight = new Vector2f(textureRectangle.Width - 2f, textureRectangle.Height - 2f);

            Vector2u textureBottomRightPixel = new Vector2u((uint)textureRectangle.Width - 1u, (uint)textureRectangle.Height - 1u);

            for (uint x = 1u; x < textureRectangle.Width; x++)
            {
                if (!foundStart)
                {
                    if (image.GetPixel((uint)textureRectangle.Left + x, (uint)textureRectangle.Top + textureBottomRightPixel.Y) == Color.Black)
                    {
                        foundStart = true;
                        contentTopLeft.X = x - 1f;
                    }
                    else
                        continue;
                }

                if (foundStart)
                {
                    if (image.GetPixel((uint)textureRectangle.Left + x, (uint)textureRectangle.Top + textureBottomRightPixel.Y) == Color.Black)
                        contentBottomRight.X = x - 1f;
                    else
                        break;
                }
            }

            for(uint y = 1u; y < textureRectangle.Height; y++)
            {
                if (!foundStart)
                {
                    if (image.GetPixel((uint)textureRectangle.Left + textureBottomRightPixel.X, (uint)textureRectangle.Top + y) == Color.Black)
                    {
                        foundStart = true;
                        contentTopLeft.Y = y - 1f;
                    }
                    else
                        continue;
                }

                if (foundStart)
                {
                    if (image.GetPixel((uint)textureRectangle.Left + textureBottomRightPixel.X, (uint)textureRectangle.Top + y) == Color.Black)
                        contentBottomRight.Y = y - 1f;
                    else
                        break;
                }
            }

        }

        public void SetTexture(Texture texture, bool resetsize = true, bool resetrect = true)
        {
            m_texture = texture;
            if (resetrect)
            {
                m_textureRectangle = new IntRect(new Vector2i(0, 0), (Vector2i)m_texture.Size);
            }

            m_trimmedSize = new Vector2f(m_textureRectangle.Width, m_textureRectangle.Height) - trimAmount * 2f;

            if (resetsize)
                m_size = m_trimmedSize;

            ExtractScalePositionsAndContentAreaFromTexture(m_texture, m_textureRectangle, ref m_scaleTopLeft, ref m_scaleBottomRight, ref m_contentTopLeft, ref m_contentBottomRight);
            UpdateVerticies();
        }

        public Vector2f Size
        {
            get
            {
                return m_size;
            }
            set
            {
                m_size = value;
                Vector2f minimumSize = m_scaleTopLeft + (m_trimmedSize - m_scaleBottomRight);
                if (m_size.X < minimumSize.X)
                    m_size.X = minimumSize.X;
                if (m_size.Y < minimumSize.Y)
                    m_size.Y = minimumSize.Y;
                UpdateVerticiesPositions();
            }
        }

        public Color Color
        {
            get
            {
                return m_vertices[0].Color;
            }
            set
            {
                foreach (Vertex vertex in m_vertices)
                {
                    Vertex tempvex = vertex;
                    tempvex.Color = value;
                }
            }
        }

        public FloatRect LocalBounds
        {
            get
            {
                return new FloatRect(new Vector2f(0, 0), m_size);
            }
        }

        public FloatRect GlocalBounds
        {
            get
            {
                return Transform.TransformRect(LocalBounds);
            }
        }

        public FloatRect LocalContentArea
        {
            get
            {
                Vector2f topleft = GetResultingPositionOfTexCoord(m_contentTopLeft);
                return new FloatRect(topleft, GetResultingPositionOfTexCoord(m_contentBottomRight) - topleft + new Vector2f(1, 1));
            }
        }

        public FloatRect GlocalContentArea
        {
            get
            {
                return Transform.TransformRect(LocalContentArea);
            }
        }

        public void SetTextureRect(IntRect texrect, bool resetsize = true)
        {
            m_textureRectangle = texrect;
            m_trimmedSize = new Vector2f(m_textureRectangle.Width, m_textureRectangle.Height) - trimAmount * 2f;

            if (resetsize)
                m_size = m_trimmedSize;

            if (m_texture != null)
            {
                ExtractScalePositionsAndContentAreaFromTexture(m_texture, m_textureRectangle, ref m_scaleTopLeft, ref m_scaleBottomRight, ref m_contentTopLeft, ref m_contentBottomRight);
                UpdateVerticies();
            }

        }

        public void ResetSize()
        {
            Size = m_trimmedSize;
        }

        public bool IsPointInsideTransformedContentArea(Vector2f point)
        {
            return LocalContentArea.Contains(InverseTransform.TransformPoint(point).X, InverseTransform.TransformPoint(point).Y);
        }

        public void Draw(RenderTarget target, RenderStates states)
        {
            states.Texture = m_texture;
            states.Transform = Transform;
            target.Draw(m_vertices, m_primitiveType, states);
        }

        private void UpdateVerticies()
        {
            UpdateVerticiesPositions();
            UpdateVerticiesTextCoords();
        }

        private void UpdateVerticiesPositions()
        {
            Vector2f newBottomRightScaled = m_size - (m_trimmedSize - m_scaleBottomRight);

            // top row
            m_vertices[0].Position = new Vector2f(0f, 0f);
            m_vertices[1].Position = new Vector2f(m_scaleTopLeft.X, 0f);
            m_vertices[2].Position = m_scaleTopLeft;
            m_vertices[3].Position = new Vector2f(0f, m_scaleTopLeft.Y);

            m_vertices[4].Position = new Vector2f(m_scaleTopLeft.Y, 0f);
            m_vertices[5].Position = new Vector2f(newBottomRightScaled.X, 0f);
            m_vertices[6].Position = new Vector2f(newBottomRightScaled.X, m_scaleTopLeft.Y);
            m_vertices[7].Position = m_scaleTopLeft;

            m_vertices[8].Position = new Vector2f(newBottomRightScaled.X, 0f );
            m_vertices[9].Position = new Vector2f(m_size.X, 0f);
            m_vertices[10].Position = new Vector2f( m_size.X, m_scaleTopLeft.Y );
            m_vertices[11].Position = new Vector2f(newBottomRightScaled.X, m_scaleTopLeft.X );

            // centre row
            m_vertices[12].Position = new Vector2f(0f, m_scaleTopLeft.Y);
            m_vertices[13].Position = new Vector2f(m_scaleTopLeft.X, m_scaleTopLeft.Y);
            m_vertices[14].Position = new Vector2f(m_scaleTopLeft.X, newBottomRightScaled.Y);
            m_vertices[15].Position = new Vector2f(0f, newBottomRightScaled.Y );

            m_vertices[16].Position = new Vector2f( m_scaleTopLeft.X, m_scaleTopLeft.Y);
            m_vertices[17].Position = new Vector2f(newBottomRightScaled.X, m_scaleTopLeft.Y);
            m_vertices[18].Position = new Vector2f(newBottomRightScaled.X, newBottomRightScaled.Y );
            m_vertices[19].Position = new Vector2f(m_scaleTopLeft.X, newBottomRightScaled.Y);

            m_vertices[20].Position = new Vector2f(newBottomRightScaled.X, m_scaleTopLeft.Y);
            m_vertices[21].Position = new Vector2f(m_size.X, m_scaleTopLeft.Y);
            m_vertices[22].Position = new Vector2f(m_size.X, newBottomRightScaled.Y);
            m_vertices[23].Position = new Vector2f(newBottomRightScaled.X, newBottomRightScaled.Y);

            // bottom row
            m_vertices[24].Position = new Vector2f(0f, newBottomRightScaled.Y);
            m_vertices[25].Position = new Vector2f(m_scaleTopLeft.X, newBottomRightScaled.Y);
            m_vertices[26].Position = new Vector2f(m_scaleTopLeft.X, m_size.Y);
            m_vertices[27].Position = new Vector2f(0f, m_size.Y);

            m_vertices[28].Position = new Vector2f(m_scaleTopLeft.X, newBottomRightScaled.Y);
            m_vertices[29].Position = new Vector2f(newBottomRightScaled.X, newBottomRightScaled.Y);
            m_vertices[30].Position = new Vector2f(newBottomRightScaled.X, m_size.Y );
            m_vertices[31].Position = new Vector2f(m_scaleTopLeft.X, m_size.Y);

            m_vertices[32].Position = new Vector2f(newBottomRightScaled.X, newBottomRightScaled.Y);
            m_vertices[33].Position = new Vector2f(m_size.X, newBottomRightScaled.Y);
            m_vertices[34].Position = new Vector2f(m_size.X, m_size.Y);
            m_vertices[35].Position = new Vector2f(newBottomRightScaled.X, m_size.Y);
        }

        private void UpdateVerticiesTextCoords()
        {
            Vector2f textureBottomRight = m_trimmedSize;

            // top row
            m_vertices[0].TexCoords = new Vector2f(0, 0);
            m_vertices[1].TexCoords = new Vector2f(m_scaleTopLeft.X, 0);
            m_vertices[2].TexCoords = m_scaleTopLeft;
            m_vertices[3].TexCoords = new Vector2f(0f, m_scaleTopLeft.Y);

            m_vertices[4].TexCoords = new Vector2f(m_scaleTopLeft.X, 0f );
            m_vertices[5].TexCoords = new Vector2f(m_scaleBottomRight.X, 0f );
            m_vertices[6].TexCoords = new Vector2f(m_scaleBottomRight.X, m_scaleTopLeft.Y );
            m_vertices[7].TexCoords = m_scaleTopLeft;

            m_vertices[8].TexCoords = new Vector2f(m_scaleBottomRight.X, 0f);
            m_vertices[9].TexCoords = new Vector2f(textureBottomRight.X, 0f );
            m_vertices[10].TexCoords = new Vector2f(textureBottomRight.X, m_scaleTopLeft.Y );
            m_vertices[11].TexCoords = new Vector2f(m_scaleBottomRight.X, m_scaleTopLeft.Y );

            // centre row
            m_vertices[12].TexCoords = new Vector2f( 0f, m_scaleTopLeft.Y );
            m_vertices[13].TexCoords = m_scaleTopLeft;
            m_vertices[14].TexCoords = new Vector2f( m_scaleTopLeft.X, m_scaleBottomRight.Y );
            m_vertices[15].TexCoords = new Vector2f(0f, m_scaleBottomRight.Y);

            m_vertices[16].TexCoords = m_scaleTopLeft;
            m_vertices[17].TexCoords = new Vector2f(m_scaleBottomRight.X, m_scaleTopLeft.Y);
            m_vertices[18].TexCoords = m_scaleBottomRight;
            m_vertices[19].TexCoords = new Vector2f(m_scaleTopLeft.X, m_scaleBottomRight.Y );

            m_vertices[20].TexCoords = new Vector2f(m_scaleBottomRight.X, m_scaleTopLeft.Y);
            m_vertices[21].TexCoords = new Vector2f(textureBottomRight.X, m_scaleTopLeft.Y );
            m_vertices[22].TexCoords = new Vector2f(textureBottomRight.X, m_scaleBottomRight.Y);
            m_vertices[23].TexCoords = m_scaleBottomRight;

            // bottom row
            m_vertices[24].TexCoords = new Vector2f(0f, m_scaleBottomRight.Y );
            m_vertices[25].TexCoords = new Vector2f(m_scaleTopLeft.X, m_scaleBottomRight.Y );
            m_vertices[26].TexCoords = new Vector2f(m_scaleTopLeft.X, textureBottomRight.Y );
            m_vertices[27].TexCoords = new Vector2f(0f, textureBottomRight.Y );

            m_vertices[28].TexCoords = new Vector2f(m_scaleTopLeft.X, m_scaleBottomRight.Y);
            m_vertices[29].TexCoords = m_scaleBottomRight;
            m_vertices[30].TexCoords = new Vector2f(m_scaleBottomRight.X, textureBottomRight.Y);
            m_vertices[31].TexCoords = new Vector2f(m_scaleTopLeft.X, textureBottomRight.Y );

            m_vertices[32].TexCoords = m_scaleBottomRight;
            m_vertices[33].TexCoords = new Vector2f(textureBottomRight.X, m_scaleBottomRight.Y );
            m_vertices[34].TexCoords = textureBottomRight;
            m_vertices[35].TexCoords = new Vector2f(m_scaleBottomRight.X, textureBottomRight.Y );

            Vector2f textureRectangleOffset = new Vector2f(m_textureRectangle.Left, m_textureRectangle.Top);
            foreach ( Vertex vertex in m_vertices)
            {
                Vector2f texCoords = vertex.TexCoords;
                texCoords += textureRectangleOffset + trimAmount;
            }
        }

        private Vector2f GetResultingPositionOfTexCoord(Vector2f textureCoords)
        {
            Vector2f result = new Vector2f();

            Vector2f newBottomRightScaled = m_size - (m_trimmedSize - m_scaleBottomRight);
            Vector2f scaleSize = m_scaleBottomRight - m_scaleTopLeft;
            Vector2f newScaleSize = newBottomRightScaled - m_scaleTopLeft;

            if (textureCoords.X <= m_scaleTopLeft.X)
                result.X = textureCoords.X;
            else if (textureCoords.X >= m_scaleTopLeft.X)
                result.X = newBottomRightScaled.X + (textureCoords.X - m_scaleBottomRight.X);
            else
                result.X = (textureCoords.X- m_scaleTopLeft.X) / scaleSize.X * newScaleSize.X + m_scaleTopLeft.X;

            if (textureCoords.Y <= m_scaleTopLeft.Y)
                result.Y = textureCoords.Y;
            else if (textureCoords.Y >= m_scaleTopLeft.Y)
                result.Y = newBottomRightScaled.Y + (textureCoords.Y - m_scaleBottomRight.Y);
            else
                result.Y = (textureCoords.Y - m_scaleTopLeft.Y) / scaleSize.Y * newScaleSize.Y + m_scaleTopLeft.Y;

            return result;
        }
    }
}
This code was ported from Github NinePatch

But It can't display and I have downloaded picture into root directory of my compiled Release version of executable.

I have tried...
using System;
using System.IO;

namespace SFML_Test
{
    class MainClass
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello SMFL.Net!");
            new SimpleWindow().Run();
        }

        class SimpleWindow
        {
            public void Run()
            {
                var mode = new SFML.Window.VideoMode(800, 600, 32);
                var window = new SFML.Graphics.RenderWindow(mode, "SFML works!", SFML.Window.Styles.Default);
                var orange = new SFML.Graphics.Color(255, 102, 0);
                var texture = new SFML.Graphics.Texture(Path.Combine("pixelcyan9patch.png"));
                var nine9sprite = new NinePatch();
                nine9sprite.SetTexture(texture);
                window.Position = new SFML.System.Vector2i((int)(SFML.Window.VideoMode.DesktopMode.Width - window.Size.X) / 2, (int)(SFML.Window.VideoMode.DesktopMode.Height - window.Size.Y) / 2);
                window.KeyPressed += Window_KeyPressed;
                window.Closed += Window_Closed;
                window.Resized += Window_Resized;

                var context = new SFML.Window.Context();
                context.SetActive(true);

                while (window.IsOpen)
                {
                    // Process events
                    window.DispatchEvents();
                    SFML.System.Vector2f mousePosition = window.MapPixelToCoords(SFML.Window.Mouse.GetPosition());
                    nine9sprite.Size = mousePosition;
                    window.Clear(orange);
                    window.Draw(nine9sprite);
                    window.Display();
                }
            }

            private void Window_KeyPressed(object sender, SFML.Window.KeyEventArgs e)
            {
                var window = (SFML.Window.Window)sender;
                if (e.Code == SFML.Window.Keyboard.Key.Escape)
                {
                    window.Close();
                }
            }

            private void Window_Closed(object sender, EventArgs e)
            {
                var window = (SFML.Graphics.RenderWindow)sender;
                window.Close();
            }

            private void Window_Resized(object sender, SFML.Window.SizeEventArgs e)
            {
                var window = (SFML.Graphics.RenderWindow)sender;
                window.DefaultView.Size = new SFML.System.Vector2f(e.Width, e.Height);
            }
        }
    }
}
And I have same picture name by SalbeWard.
Result: picture doesn't show....

How do I resolve if image loads into SFML and doesn't shows me.

Thanks!

2
DotNet / Re: Porting from SFML example with X11 - Nice one!
« on: March 01, 2021, 08:21:23 pm »
Because I try for porting from C++ to C# -

It works fine under GtkSharp 2 / 3 too yay. If I use Events = Gdk.EventMask.AllEventMasks than it will crash. So sad with Gdk.Event :(

using System;
using System.Runtime.InteropServices;

using SFML.Graphics;
using SFML.Window;

namespace GtkSharp2SFML
{
    public class RenderWidget : Gtk.DrawingArea
    {
        private const string libgdk_x11 = "libgdk-x11-2.0.so.0";

        [DllImport(libgdk_x11)]
        private extern static IntPtr gdk_x11_drawable_get_xid(IntPtr gdk_window);

        private IntPtr GraphicsHandle
        {
            get
            {
                return gdk_x11_drawable_get_xid(GdkWindow.Handle);
            }
        }

        public RenderWindow RenderWindow { get; private set; }

        public RenderWidget()
        {
            AppPaintable = true;
            DoubleBuffered = false;
            CanFocus = true;
        //    Events = Gdk.EventMask.ButtonPressMask | Gdk.EventMask.ButtonReleaseMask | Gdk.EventMask.KeyPressMask | Gdk.EventMask.KeyReleaseMask;
        }

        protected override bool OnExposeEvent(Gdk.EventExpose evnt)
        {
            bool ok = base.OnExposeEvent(evnt);
            ContextSettings settings = new ContextSettings
            {
                DepthBits = 24
            };
            RenderWindow = new RenderWindow(GraphicsHandle, settings);
            RenderWindow.DispatchEvents();
            RenderWindow.SetFramerateLimit(60);
            RenderWindow.DefaultView.Viewport = new FloatRect(evnt.Area.X, evnt.Area.Y, evnt.Area.Width, evnt.Area.Height);
            Realize();
            OnRender();
            evnt.Window.Display.Sync();
            RenderWindow.Display();
            QueueDraw();
            return ok;
        }

        public event EventHandler Rendered;

        protected virtual void OnRender()
        {
            if (Rendered != null)
                Rendered(this, EventArgs.Empty);
        }

        protected override void OnDestroyed()
        {
            RenderWindow.Close();
            base.OnDestroyed();
        }
    }
}

And I try to communicate with Gtk.Button - It works fine!!! Without SFML-Events. I can't expect that SDL2 is really loser and SFML is winner. Congratulations!! I am happy because I have tried since GtkSharp 2 / 3 with SDL2 = Crashed if you add Button. It stopped to run :( So sad. I try to communicate with any controls like you use C# example Windows.Forms, MonoMac or XlibSharp
using System;
using System.Runtime.InteropServices;

using SFML.Graphics;
using SFML.System;
using SFML.Window;

namespace GtkSharp2SFML
{
    class MainClass
    {
        static RenderWidget render_widget;
        //   static byte bvalue = 0x00;
        private static bool color_changed = true;

        [STAThread]
        static void Main(string[] args)
        {
            Gtk.Application.Init();
            Gtk.Window main_window = new Gtk.Window(Gtk.WindowType.Toplevel)
            {
                Title = "SFML in GtkSharp 2",
                WindowPosition = Gtk.WindowPosition.Center
            };
            main_window.SetSizeRequest(400, 300);

            Gtk.VBox main_area = new Gtk.VBox(false, 0);
            main_window.Add(main_area);
            render_widget = new RenderWidget();
            main_area.PackStart(render_widget, true, true, 0);
            color_changed = false;
            render_widget.Rendered += OnRender;

            Gtk.Button changecolor = new Gtk.Button
            {
                Label = "Change Color",
            };
            main_area.PackStart(changecolor, false, false, 0);
            changecolor.Clicked += OnChangeColor;

            main_window.Destroyed += delegate {
                Gtk.Application.Quit();
            };

            main_window.ShowAll();
            Gtk.Application.Run();
        }

        private static void OnChangeColor(object sender, EventArgs e)
        {
            color_changed = true;
            if (color_changed)
            {
                render_widget.RenderWindow.DispatchEvents();
                render_widget.RenderWindow.Clear(Color.Blue);
                render_widget.RenderWindow.Display();
            }
        }

        private static void OnRender(object sender, EventArgs e)
        {
            if (!color_changed)
            {
                render_widget.RenderWindow.Clear(Color.Red);
                color_changed = false;
            }
        }
    }
}
Result:
It runs app

And I clicked button to blue color.


Enjoy and happy coding with GtkSharp 2 and 3 = OK. Please remember and do not use EventMask in code.because EventMask can stop to run Gtk applications.

PS: You know I have found old thread of GraphicsWidget - But it seems like it doesn't work because it has forgotten:
AppPaintable and CanFocus and resizing viewport of SFML.

My own RenderWidget is better than GraphicsWidget.
Resizing = YES
Communicating = for Buttons/Menus = YES, for Text = NOT TESTED, Image into SFML Teexture, NOT TESTED and any...
Closing = YES , It works fine

GraphicsWidget from old thread since I found here.:
Resizing = FAILED NO - HAHAHA
Communicating = DON'T KNOW ???
Closing = FAILED NO - HAHAHA

// EDIT
Great news with Events = Gdk.EventMask.AllEventMasks - no crash because EventMask works fine under ExposeEvent.

It looks like my code and it fixes and it doesn't crash. But I am not sure if it works.
        protected override bool OnExposeEvent(Gdk.EventExpose evnt)
        {
            bool ok = base.OnExposeEvent(evnt);
            ContextSettings settings = new ContextSettings
            {
                DepthBits = 24,
                StencilBits = 8
            };
            RenderWindow = new RenderWindow(GraphicsHandle, settings);
            RenderWindow.SetVerticalSyncEnabled(true);
            RenderWindow.SetFramerateLimit(60);
            RenderWindow.DefaultView.Viewport = new FloatRect(evnt.Area.X, evnt.Area.Y, evnt.Area.Width, evnt.Area.Height);
            OnRender();
            evnt.Window.Display.Sync();
            while (ok && RenderWindow.IsOpen)
            {
                RenderWindow.DispatchEvents();
                Events = Gdk.EventMask.AllEventsMask;
            }
            RenderWindow.Display();
            QueueDraw();
            return ok;
        }

But it shows like common failure lines under library - I think SFML libraries have thrown failures.
Failed to create input context for window -- TextEntered event won't be able to return unicode

It looks like for my Gtk Application runs fine and doesn't crash. Yay! It works fine. - I know I have found old threads I have readden about "Failure to create input..." That is reason because it calls "code-failure", right or I understand wrongly? Sorry my bad English.

// Update
ColorButton can communicate to SFML's Clear(color) Woohoooo! ColorButton changes color of SFML's Clear() = OK 100 %

It is easy simple trick

Remember that Gdk.Color's bit is ushort and I calculate to byte
65535 / 257 = 255 -> byte maxvalue in SFML.Color's bit.

// EDIT 2:
Found bug GtkSharp 2 can't show when you maximize Gtk Window and RenderWindow can't change color. That is why I need move to GtkSharp 3.x = OK - Since I tested with Gtk.GLArea()

Sorry for disturbances of edits....

3
DotNet / Porting from SFML example with X11 - Nice one!
« on: February 28, 2021, 04:46:44 pm »
Hello everyone,

Thanks for welcome saying for me! I am new for here.

I thought SFML is better than SDL2 or Allegro5.

I have made one X11 with SFML with Mono 6.12.x = It works fine like chain. But I can't understand why does it throw BadDrawable in X11

I think VisualInfo  is not XDefaultVisual() - It looks like advanced mode with Visual like GLX on X11.

I have made one nice example:
using System;
using System.Runtime.InteropServices;
using SFML.Graphics;

using static X11SFML.X11;

namespace X11SFML
{
    static class X11
    {
        public enum XEventType
        {
            KeyPress = 2
        }

        [StructLayout(LayoutKind.Explicit)]
        public struct XEvent
        {
            [FieldOffset(0)]
            public XEventType type;
        }

        public enum XEventMask : long
        {
            KeyPressMask = 1L << 0
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct XSetWindowAttributes
        {
            public uint background_pixel;
            public XEventMask event_mask;
        }

        [Flags]
        public enum CWAttributes : long
        {
            CWBackPixel = 1L << 1,
            CWEventMask = 1L << 11
        }

        private const string libX11 = "libX11.so.6";

        [DllImport(libX11)]
        public extern static IntPtr XOpenDisplay(string display_name);

        [DllImport(libX11)]
        public extern static void XCloseDisplay(IntPtr x_display);

        [DllImport(libX11)]
        public extern static int XDefaultScreen(IntPtr x_display);

        [DllImport(libX11)]
        public extern static int XDefaultDepth(IntPtr x_display, int screen_number);

        [DllImport(libX11)]
        public extern static IntPtr XDefaultVisual(IntPtr x_display, int screen_number);

        [DllImport(libX11)]
        public extern static IntPtr XRootWindow(IntPtr x_display, int screen_number);

        [DllImport(libX11)]
        public extern static uint XBlackPixel(IntPtr x_display, int screen_number);

        [DllImport(libX11)]
        public extern static IntPtr XCreateWindow(IntPtr x_display, IntPtr x_window_parent, int x, int y, uint width, uint height, uint border_width, int depth, uint window_class, IntPtr visual, CWAttributes cws, ref XSetWindowAttributes attributes);

        [DllImport(libX11)]
        public extern static IntPtr XCreateWindow(IntPtr x_display, IntPtr x_window_parent, int x, int y, uint width, uint height, uint border_width, int depth, uint window_class, IntPtr visual, CWAttributes cws, IntPtr attributes);

        [DllImport(libX11)]
        public extern static void XStoreName(IntPtr x_display, IntPtr x_window, string title);

        [DllImport(libX11)]
        public extern static void XSync(IntPtr x_display, bool discard);

        [DllImport(libX11)]
        public extern static void XMapWindow(IntPtr x_display, IntPtr x_window);

        [DllImport(libX11)]
        public extern static void XFlush(IntPtr x_display);

        [DllImport(libX11)]
        public extern static bool XPending(IntPtr x_display);

        [DllImport(libX11)]
        public extern static void XNextEvent(IntPtr x_display, out XEvent x_event);
    }

    class MainClass
    {
        private const int EXIT_SUCCESS = 0;
        private const int EXIT_FAILURE = 1;

        static int Main(string[] args)
        {
            Console.WriteLine("Hello X11 with SFML!");
            IntPtr display = XOpenDisplay(null);
            if (display == IntPtr.Zero)
            {
                return EXIT_FAILURE;
            }
            else
            {
                Console.WriteLine("Display opened - SUCCESS");
            }

            int screen_number = XDefaultScreen(display);

            int depths = XDefaultDepth(display, screen_number);
            IntPtr root_window = XRootWindow(display, screen_number);
            IntPtr visual = XDefaultVisual(display, screen_number);

            XSetWindowAttributes attributes = new XSetWindowAttributes
            {
                background_pixel = XBlackPixel(display, screen_number),
                event_mask = XEventMask.KeyPressMask
            };

            IntPtr main_window = XCreateWindow(display, root_window, 0, 0, 650, 330, 0, depths, 1, visual, CWAttributes.CWBackPixel | CWAttributes.CWEventMask, ref attributes);
            if (main_window == IntPtr.Zero)
            {
                return EXIT_FAILURE;
            }
            else
            {
                Console.WriteLine("Window created - SUCCESS");
            }

            XStoreName(display, main_window, "SFML in X11 - Mono 6.12.x");

            IntPtr view1 = XCreateWindow(display, main_window, 10, 10, 310, 310, 0, depths, 1, visual, 0, IntPtr.Zero);
            IntPtr view2 = XCreateWindow(display, main_window, 330, 10, 310, 310, 0, depths, 1, visual, 0, IntPtr.Zero);

            XMapWindow(display, main_window);
            XFlush(display);

            RenderWindow forView1 = new RenderWindow(view1);
            RenderWindow forView2 = new RenderWindow(view2);

            forView1.SetActive();
            forView1.Clear(Color.Red);

            forView2.SetActive();
            forView2.Clear(new Color(255, 102, 0, 255));

            bool running = true;
            while (running)
            {
                while (XPending(display))
                {
                    XNextEvent(display, out XEvent evt);
                    switch (evt.type)
                    {
                        case XEventType.KeyPress:
                            running = false;
                            break;
                    }
                }

                CircleShape circle = new CircleShape
                {
                    FillColor = Color.Yellow,
                    Radius = 50f,
                    Position = new SFML.System.Vector2f(105, 105)
                };

                forView1.Draw(circle);

                RectangleShape rectangle = new RectangleShape
                {
                    FillColor = new Color(33, 33, 33),
                    Size = new SFML.System.Vector2f(100, 100),
                    Position = new SFML.System.Vector2f(105, 105)
                };

                forView2.Draw(rectangle);

                forView1.Display();
                forView2.Display();
            }

            XCloseDisplay(display);

            return EXIT_SUCCESS;
        }
    }
}

Screenshot:


Enjoy your coding with Mono/Dotnet

Pages: [1]
anything