gtksfmlspriteblurrydrawingarea

GTK and SFML 2 Blurred sprites


I want to use GTK+ and SFML 2.0 together. I created a simple window to test that, but doesn't work well. Sprites are blurred. Below is a screenshot describing the problem. /edit: The window renders only the first pixel of a texture.

GTK+ SFML problem

It should look like this:

enter image description here

The code:

#include <gtk/gtk.h>
#include <SFML/Graphics.hpp>
#include <gdk/gdkwin32.h>

sf::RenderWindow Win;
sf::Sprite tile[ MAP_SIZE ][ MAP_SIZE ];

void on_expose( GtkWidget * widget, gpointer data )
{
    MapView * view =( MapView * ) data;
    sf::Event event;
    while( Win.pollEvent( event ) )
    {
        if( event.type == sf::Event::Closed )
             Win.close();

        view->updateEvents( & event, Win );
    }

    Win.clear( sf::Color::White );
    view->render( tile, Win );
    Win.display();

    gtk_widget_queue_draw(widget);
}

int main( int argc, char ** argv )
{
    sf::Texture tex; // Testing texture
    if( !tex.loadFromFile( "gfx/7.png" ) )
         std::cout << "Unable to load texture!\n";

    for( int i = 0; i < MAP_SIZE; ++i )
    for( int j = 0; j < MAP_SIZE; ++j )
         tile[ j ][ i ].setTexture( tex ); // Default texture, a.k.a MAP_NONE

    MapView view( tile, sf::Vector2f( 0, 0 ) );

    gtk_init( & argc, & argv );

    GtkWidget * window;
    GtkWidget * area;

    window = gtk_window_new( GTK_WINDOW_TOPLEVEL );
    area = gtk_drawing_area_new();

    gtk_container_add( GTK_CONTAINER( window ), area );

    GdkEventMask mask;
    mask = GDK_ALL_EVENTS_MASK;
    gtk_widget_add_events( area, mask );

    gtk_widget_set_can_focus( area, true );
    gtk_widget_set_size_request( area, 16 * 25, 16 * 25 ); // Preview size.

    gtk_widget_realize( area );
    gtk_widget_set_double_buffered( area, false );

    Win.create( sf::WindowHandle( GDK_WINDOW_HWND( area->window ) ) ); // Windows-only solution
    Win.setFramerateLimit( 30 );

    g_signal_connect( window, "destroy", G_CALLBACK( gtk_main_quit ), NULL );
    g_signal_connect( area, "expose-event", G_CALLBACK( on_expose ), & view );

    gtk_widget_show_all( window );
    gtk_main();

    return 0;
}

Solution

  • I had to call

    gdk_window_resize(area->window, MAP_SIZE*25, MAP_SIZE*25);
    

    before creating a SFML window inside drawing-area.