【发布时间】: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()可能导致的唯一变化是您收到一个额外的调整大小事件,因此在尝试重绘窗口时可能会发生一些不好的事情。没有符号,崩溃堆栈的信息量不是很大,所以我几乎不能再说什么......