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

Author Topic: SFML 2.3 and GTK  (Read 4009 times)

0 Members and 1 Guest are viewing this topic.

IngloriousTom

  • Newbie
  • *
  • Posts: 17
    • View Profile
SFML 2.3 and GTK
« on: June 09, 2015, 03:08:01 pm »
Hello,

I'm trying to upgrade from SFML 2.1 to SFML 2.3, and I encouter some problems doing so.

I'm mixing GTK and SFML because I want to get touch events from GTK, and SFML does not provide touches yet.

My code was working fine with my previous SFML version, but updating it to 2.3 made my window displaying only white on a computer (not even my window.clear color), or displaying the background at start time on another computer.

It could be related to :
       
renderWindow.create(GDK_WINDOW_XID(gtk_widget_get_window(area)));
since it is where SFML and gtk are linked. But I don't really know what could be the problem since it build correctly.

So I've made a little project using the less code possible to show the behavior:
#include <SFML/Graphics/RenderWindow.hpp>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>

gboolean onDraw(GtkWidget *widget, GdkEvent *event, gpointer user);

class LinuxWindow
{
public:
    void create( sf::Vector2f size )
    {
        // Create the main window with a drawing area
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
        area = gtk_drawing_area_new();
        gtk_container_add(GTK_CONTAINER(window), area);

        // Enable events for the drawing area
        GdkEventMask mask;
        mask = GDK_ALL_EVENTS_MASK;
        gtk_widget_add_events(area,mask);

        //Request a starting size(Otherwise you have no size, ie 0)
        gtk_widget_set_size_request(area, size.x, size.y);

        //Make the window real(usually delayed to show_all, but we need it sooner)
        gtk_widget_realize(area);

        //It is double buffered in itself, does not need gtk's help
        gtk_widget_set_double_buffered(area, false);

        // Create the SFML RenderWindow, using the native window handler
        renderWindow.create(GDK_WINDOW_XID(gtk_widget_get_window(area)));
        renderWindow.setVerticalSyncEnabled(true);

        // Connect signal
        g_signal_connect(area, "draw", G_CALLBACK(onDraw), NULL);
        g_signal_connect(window, "destroy", G_CALLBACK(gtk_main_quit), NULL);

        gtk_widget_show_all(window);
    }

    sf::RenderWindow renderWindow;

private:
    GtkWidget* window;
    GtkWidget *area;
};

LinuxWindow linuxWindow;

//Gtk Render callback method
 gboolean onDraw(GtkWidget *widget, GdkEvent *event, gpointer user)
{
    linuxWindow.renderWindow.clear( sf::Color(157,210,237,255) );
    linuxWindow.renderWindow.display();
    gtk_widget_queue_draw(widget);
    return true;
}

int main(int argc, char** argv)
{
    gtk_init(&argc, &argv);
    linuxWindow.create(sf::Vector2f(800, 600));
    gtk_main();

    return 0;
}

This code works fine with SFML 2.1 but not with SFML 2.3.

Has anybody any idea/suggestion ?

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: SFML 2.3 and GTK
« Reply #1 on: June 09, 2015, 11:44:47 pm »
Do the non-graphical features of SFML windows (event handling, focus/visibility control, title/size control, etc.) work? If they don't work either, then it is an indication that something went wrong when SFML tried to take over the X window. Also, did you try SFML 2.3 by itself without GTK integration? If it doesn't work on its own, then you will need to fix that issue first.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

IngloriousTom

  • Newbie
  • *
  • Posts: 17
    • View Profile
Re: SFML 2.3 and GTK
« Reply #2 on: June 10, 2015, 11:50:55 am »
A simple project using SFML 2.3 is working correctly.

Since I am mixing GTK and SFML to handle events with GTK, SFML events seems to not work anymore.
But other RenderWindow methods work fine: setting framerate limit, size and position.
Other SFML features (not window only) work fine too: time, mouse state...

The only big issue seems to be displaying the window content inside a gtk widget.

Also I've tried this code with SFML 2.2 and it works too, if it can help narrowing the problem.

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: SFML 2.3 and GTK
« Reply #3 on: June 11, 2015, 01:15:56 am »
SFML switched over to using XCB as its X backend in 2.3. I don't know what GTK's track record with XCB integration looks like, but if you say that even SFML events no longer work, then it doesn't look too good. Internally, SFML tries to hijack the X event queue from the owning window (in your case, owned by GTK). If, for whatever reason, this cannot be done using XCB because of how GTK is implemented internally, then there is unfortunately no fix for this. SFML window creation is guaranteed to work with raw Xlib/XCB code that has somewhat "canonical" form (i.e. doesn't do really weird stuff). If GTK's implementation strays from this, then we can't really do anything.

I'm wondering, since you are already using GTK, why bother with SFML? GTK provides a superset of the features that SFML provides and using SFML within GTK doesn't really gain you much while adding a whole set of new dependencies. Like you said, you are only interested in touch events, so in this case, I wouldn't go straight for GTK but for some other implementation that is a bit more "lightweight" and still works with SFML. There are many libraries that abstract from the lower level X input/event protocols that are able to provide you with lower level access to touch devices. Maybe you could try one of those out instead.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

victorlevasseur

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: SFML 2.3 and GTK
« Reply #4 on: June 14, 2015, 10:56:50 pm »
I can confirm this bug : nothing is shown in the SFML Canvas. This is quite annoying as our software (GDevelop) embeds SFML in wxWidgets (that uses GTK on Linux)...

EDIT : It works sometimes but the SFML Window (embedded in a wxWidgets widgets, which is internally a GtkWidget) doesn't have the good size and sometimes it doesn't work at all.
« Last Edit: June 14, 2015, 11:00:50 pm by victorlevasseur »

 

anything