【问题标题】:Using SDL2 with wxWidgets 3.0将 SDL2 与 wxWidgets 3.0 一起使用
【发布时间】:2014-09-10 08:00:25
【问题描述】:

我的目标是构建一个 Game Boy 模拟器。为此,我想将 SDL2 表面嵌入到 wxWidgets 窗口中。

我找到了这个教程:http://code.technoplaza.net/wx-sdl/part1/,但是我的程序一运行就崩溃了。但是我怀疑这是为 SDL1.2 准备的。部分程序如下所示。

似乎如果我调用 SDL_Init() 并尝试显示 wxFrame(在本例中为 MainWindow),它会显示窗口一秒钟,然后程序崩溃。到目前为止,我在我的程序中注释了所有其他对 SDL 的调用,因此问题似乎在于在 wxFrame 上调用 Show() 并在同一程序中初始化 SDL2。

所以问题是:SDL2 和 wxWidgets 3 可以一起工作吗?如果没有,你们能否向我推荐一个 Game Boy 模拟器的 GUI 替代品? wxWidgets 是否像 Qt 一样有自己的图形框架(我宁愿避免使用 Qt)?

非常感谢!

#include "MainApp.h"
#include "MainWindow.h"

#include <stdexcept>

namespace GBEmu {


static void initSDL() {

    //This and SDL_Quit() are the only calls to the SDL library in my code
    if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
       throw std::runtime_error("Fatal Error: Could not init SDL");
    }
}


bool MainApp::OnInit()
{
    try {
        //If I comment out this line, the MainWindow wxFrame shows up fine.
        //If I leave both uncommented, the window shows up quickly and then 
        //crashes.
        initSDL();


        //If I comment out this line and leave initSDL() uncommented,
        //the program will not crash, but just run forever.
        (new MainWindow("GBEmu", {50,50}, {640,480}))->Show(); 


    } catch(std::exception &e) {
        wxLogMessage(_("Fatal Error: " + std::string(e.what())));
    }

    return true;
}

int MainApp::OnExit() {
    SDL_Quit();

    return wxApp::OnExit();
}


}


wxIMPLEMENT_APP(GBEmu::MainApp);

编辑:以下是有关它如何崩溃的更多信息:它在似乎是 pthread_mutex_lock 反汇编文件中出现 Segfault 崩溃。这是控制台中带有堆栈跟踪的输出:

Starting /home/dan/Documents/devStuff/GBEmuWx-build/GBEmuWx...
The program has unexpectedly finished.
/home/dan/Documents/devStuff/GBEmuWx-build/GBEmuWx crashed

Stack trace:
Error: signal 11:
/home/dan/Documents/devStuff/GBEmuWx-build/GBEmuWx(_ZN5GBEmu7handlerEi+0x1c)[0x414805]
/lib/x86_64-linux-gnu/libc.so.6(+0x36ff0)[0x7fb88e136ff0]
/lib/x86_64-linux-gnu/libpthread.so.0(pthread_mutex_lock+0x30)[0x7fb88c12ffa0]
/usr/lib/x86_64-linux-gnu/libX11.so.6(XrmQGetResource+0x3c)[0x7fb88d1ca15c]
/usr/lib/x86_64-linux-gnu/libX11.so.6(XGetDefault+0xc2)[0x7fb88d1a7a92]
/usr/lib/x86_64-linux-gnu/libcairo.so.2(+0x94dcf)[0x7fb88af8edcf]
/usr/lib/x86_64-linux-gnu/libcairo.so.2(+0x97110)[0x7fb88af91110]
/usr/lib/x86_64-linux-gnu/libcairo.so.2(cairo_surface_get_font_options+0x87)[0x7fb88af63e07]
/usr/lib/x86_64-linux-gnu/libcairo.so.2(+0x2b61f)[0x7fb88af2561f]
/usr/lib/x86_64-linux-gnu/libcairo.so.2(+0x2ef95)[0x7fb88af28f95]

这是似乎失败的屏幕截图(第 7 行):

更新: 在我的 MainWindow 类中,我将菜单栏附加到窗口。但是,似乎当我注释掉菜单栏的设置时,即使启动了 SDL,窗口也会正常显示。如果我注释掉了 initSDL() 而不是菜单栏的设置,菜单栏会显示得很好。这是我设置菜单栏的地方:

MainWindow::MainWindow(const wxString &title, const wxPoint &pos, const wxSize &size)
    :wxFrame(nullptr, wxIDs::MainWindow, title, pos, size){

  wxMenu *fileMenu = new wxMenu;
    fileMenu->Append(wxID_EXIT);

    wxMenuBar *menuBar = new wxMenuBar;

    menuBar->Append(fileMenu, "&File");

    //commenting this line out will allow the window to showup
    //and not crash the program
    SetMenuBar(menuBar);



}

【问题讨论】:

  • 它是如何崩溃的? IE。什么是堆栈跟踪?
  • 我已在最新编辑中添加了此信息。另请注意,我似乎已将问题缩小到菜单栏的设置(如编辑中所示。谢谢!
  • 调用SetMenuBar() 可能导致的唯一变化是您收到一个额外的调整大小事件,因此在尝试重绘窗口时可能会发生一些不好的事情。没有符号,崩溃堆栈的信息量不是很大,所以我几乎不能再说什么......

标签: c++ sdl wxwidgets


【解决方案1】:

你正在经历一只老海森虫。

解决方法很简单:您必须在 wxWidgets 之前初始化 SDL(基本上是在 GTK 之前)。要做到这一点,你必须改变

wxIMPLEMENT_APP(GBEmu::MainApp);

wxIMPLEMENT_APP_NO_MAIN(GBEmu::MainApp);

这样 wxWidgets 就不会劫持你的 main()。

然后你必须手动创建 main()。在其中初始化SDL,然后调用wxEntry():

int main(int argc, char** argv)
{
    if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
    {
        std::cerr << "Could not initialize SDL.\n";
        return 1;
    }

    return wxEntry(argc, argv);
}

关于这个错误的更多信息:

我在谷歌上搜索了一下,发现这个错误多年来在几个地方出现过。许多错误跟踪器中都有公开的报告,其堆栈跟踪与您在此处获得的非常相似(带有调试符号)。

我能找到的最早的报告是 2005 年的(!!)来自 cairo 错误跟踪器 (https://bugs.freedesktop.org/show_bug.cgi?id=4373)。

我最好的猜测是这个错误的真正藏身之处在 GTK、cairo 或 X 中。不幸的是,我目前没有时间更深入地研究它。

【讨论】:

    猜你喜欢
    • 2015-04-08
    • 1970-01-01
    • 1970-01-01
    • 2014-08-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    相关资源
    最近更新 更多