为什么 wxwidgets 和 sfml 不能很好地玩?

Why can't wxwidgets and sfml play nice?

本文关键字:很好 不能 wxwidgets sfml 为什么      更新时间:2023-10-16

我想将sfml与wxwidgets一起使用。我试着遵循一个相当过时的教程,因为没有最近的教程了。

此处的教程:http://sfml-dev.org/tutorials/1.6/graphics-wxwidgets.php

本教程适用于sfml1.6和wxwidgets 2.x,但我想将sfml2.1与wxwidget 3一起使用,但代码无法编译。显然win_gtk.h现在是一个私有头,在gtk3中是不可访问的?此标头包含gtk_pizza。至于display()错误,我不知道那里出了什么问题。。。无论如何,让一切都变得美好?

这是我的代码:

#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
}

以下是错误:

[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

本教程确实已经过时了,在任何情况下使用私有wxGTK标头和实现细节都不是一个好主意。我对SFML一无所知,但您需要了解其文档中描述的本机WindowHandle到底是什么,然后才能从wxGTK获得它。

仅供参考,m_wxwindowGtkFixed,它本身就是GtkContainer,它是GtkWidget,所以如果你能找到如何将SFML与原生GtkContainerGtkWidget一起使用,你应该也能将它与wxGTK一起使用,而不会有太多问题。

祝你好运!