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....