I want to use sfml with wxwidgets. I tried to follow a rather dated tutorial because there isn't a more recent one.
Tutorials here: http://sfml-dev.org/tutorials/1.6/graphics-wxwidgets.php
The tutorial is for sfml1.6 and wxwidgets 2.x however I want to use sfml2.1 with wxwidgets 3 but the code wont compile. Apparently win_gtk.h is now a private header and unaccessible in gtk3? This header contains gtk_pizza. As for the display() error I have no idea what's wrong there... Anyway to make everything play nice?
Here's my code:
#include <SFML/Graphics.hpp>
#include <wx/wx.h>
#ifdef __WXGTK__
#include <gdk/gdkx.h>
#include <gtk/gtk.h>
// #include <wx/gtk/win_gtk.h> this doesn't exist in wxwidgets3?
#endif
class wxSFMLCanvas : public wxControl, public sf::RenderWindow
{
public :
wxSFMLCanvas(wxWindow* Parent = NULL, wxWindowID Id = -1, const wxPoint& Position = wxDefaultPosition,
const wxSize& Size = wxDefaultSize, long Style = 0);
virtual ~wxSFMLCanvas();
private :
DECLARE_EVENT_TABLE()
virtual void OnUpdate();
void OnIdle(wxIdleEvent&);
void OnPaint(wxPaintEvent&);
void OnEraseBackground(wxEraseEvent&);
};
void wxSFMLCanvas::OnIdle(wxIdleEvent&)
{
// Send a paint message when the control is idle, to ensure maximum framerate
Refresh();
}
void wxSFMLCanvas::OnPaint(wxPaintEvent&)
{
// Prepare the control to be repainted
wxPaintDC Dc(this);
// Let the derived class do its specific stuff
OnUpdate();
// Display on screen
Display();
}
wxSFMLCanvas::wxSFMLCanvas(wxWindow* Parent, wxWindowID Id, const wxPoint& Position, const wxSize& Size, long Style) :
wxControl(Parent, Id, Position, Size, Style)
{
#ifdef __WXGTK__
// GTK implementation requires to go deeper to find the
// low-level X11 identifier of the widget
gtk_widget_realize(m_wxwindow);
gtk_widget_set_double_buffered(m_wxwindow, false);
GdkWindow* Win = GTK_PIZZA(m_wxwindow)->bin_window;
XFlush(GDK_WINDOW_XDISPLAY(Win));
//sf::RenderWindow::Create(GDK_WINDOW_XWINDOW(Win));
#else
// Tested under Windows XP only (should work with X11
// and other Windows versions - no idea about MacOS)
//sf::RenderWindow::Create(GetHandle());
#endif
}
Here are the errors:
[greg@greg-desktop polyedit]$ make
g++ -g -I. -I/usr/lib/wx/include/gtk2-unicode-3.0 -I/usr/include/wx-3.0 -D_FILE_OFFSET_BITS=64 -DWXUSINGDLL -D__WXGTK__ -pthread -I/usr/include/gtk-2.0 -I/usr/lib/gtk-2.0/include -I/usr/include/pango-1.0 -I/usr/include/atk-1.0 -I/usr/include/cairo -I/usr/include/pixman-1 -I/usr/include/libdrm -I/usr/include/gdk-pixbuf-2.0 -I/usr/include/libpng16 -I/usr/include/pango-1.0 -I/usr/include/glib-2.0 -I/usr/lib/glib-2.0/include -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/harfbuzz -I/usr/include/freetype2 -I/usr/include/libpng16 -I/usr/include/harfbuzz -MMD -MP -c -o .eobjs/./main.o main.cpp
main.cpp: In member function 'void wxSFMLCanvas::OnPaint(wxPaintEvent&)':
main.cpp:49:13: error: invalid use of incomplete type 'Display {aka struct _XDisplay}'
Display();
^
In file included from /usr/include/wx-3.0/wx/cursor.h:69:0,
from /usr/include/wx-3.0/wx/event.h:21,
from /usr/include/wx-3.0/wx/wx.h:24,
from main.cpp:3:
/usr/include/wx-3.0/wx/utils.h:778:15: error: forward declaration of 'Display {aka struct _XDisplay}'
inline struct _XDisplay *wxGetX11Display()
^
main.cpp: In constructor 'wxSFMLCanvas::wxSFMLCanvas(wxWindow*, wxWindowID, const wxPoint&, const wxSize&, long int)':
main.cpp:62:46: error: 'GTK_PIZZA' was not declared in this scope
GdkWindow* Win = GTK_PIZZA(m_wxwindow)->bin_window
Fixed it myself:
#include <SFML/Graphics.hpp>
#include <wx/wx.h>
#ifdef __WXGTK__
#include <gdk/gdkx.h>
#include <gtk/gtk.h>
#endif
class wxSFMLCanvas : public wxControl, public sf::RenderWindow
{
public :
wxSFMLCanvas(wxWindow* Parent = NULL, wxWindowID Id = -1, const wxPoint& Position = wxDefaultPosition,
const wxSize& Size = wxDefaultSize, long Style = 0);
virtual ~wxSFMLCanvas() {};
private :
DECLARE_EVENT_TABLE()
virtual void OnUpdate() {};
void OnIdle(wxIdleEvent&);
void OnPaint(wxPaintEvent&);
void OnEraseBackground(wxEraseEvent&);
};
BEGIN_EVENT_TABLE( wxSFMLCanvas, wxControl )
EVT_IDLE( wxSFMLCanvas::OnIdle )
EVT_PAINT( wxSFMLCanvas::OnPaint )
EVT_ERASE_BACKGROUND( wxSFMLCanvas::OnEraseBackground )
END_EVENT_TABLE()
void wxSFMLCanvas::OnIdle(wxIdleEvent&)
{
// Send a paint message when the control is idle, to ensure maximum framerate
Refresh();
}
void wxSFMLCanvas::OnEraseBackground(wxEraseEvent&)
{
}
void wxSFMLCanvas::OnPaint(wxPaintEvent&)
{
// Prepare the control to be repainted
wxPaintDC Dc(this);
// Let the derived class do its specific stuff
OnUpdate();
// Display on screen
display();
}
wxSFMLCanvas::wxSFMLCanvas(wxWindow* Parent, wxWindowID Id, const wxPoint& Position, const wxSize& Size, long Style) :
wxControl(Parent, Id, Position, Size, Style)
{
#ifdef __WXGTK__
// GTK implementation requires to go deeper to find the
// low-level X11 identifier of the widget
gtk_widget_realize(m_wxwindow);
gtk_widget_set_double_buffered(m_wxwindow, false);
GdkWindow* Win = gtk_widget_get_window( (GtkWidget *) GetHandle() );
XFlush(GDK_WINDOW_XDISPLAY(Win));
sf::RenderWindow::create(GDK_WINDOW_XWINDOW(Win));
#else
// Tested under Windows XP only (should work with X11
// and other Windows versions - no idea about MacOS)
sf::RenderWindow::Create(GetHandle());
#endif
}
class MyCanvas : public wxSFMLCanvas
{
public :
MyCanvas(wxWindow* Parent,
wxWindowID Id,
wxPoint Position,
wxSize Size,
long Style = 0) :
wxSFMLCanvas(Parent, Id, Position, Size, Style)
{
// Load an image and assign it to our sprite
//myImage.LoadFromFile("sprite.png");
//mySprite.SetImage(myImage);
}
private :
virtual void OnUpdate()
{
// Clear the view
clear(sf::Color::Blue);
// Display the sprite in the view
//Draw(mySprite);
}
//sf::Image myImage;
//sf::Sprite mySprite;
};
class MyFrame : public wxFrame
{
public :
MyFrame() :
wxFrame(NULL, wxID_ANY, "SFML wxWidgets", wxDefaultPosition, wxSize(800, 600))
{
new MyCanvas(this, wxID_ANY, wxPoint(50, 50), wxSize(700, 500));
}
};
class MyApplication : public wxApp
{
private :
virtual bool OnInit()
{
// Create the main window
MyFrame* MainFrame = new MyFrame;
MainFrame->Show();
return true;
}
};
IMPLEMENT_APP(MyApplication);