C++ 如何同时使用 Qt 和 SDL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/118659/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 12:59:25  来源:igfitidea点击:

How do I use Qt and SDL together?

c++winapiqtsdl

提问by bineteri

I am building a physics simulation engine and editor in Windows. I want to build the editor part using Qt and I want to run the engine using SDL with OpenGL.

我正在 Windows 中构建物理模拟引擎和编辑器。我想使用 Qt 构建编辑器部分,并且我想使用 SDL 和 OpenGL 运行引擎。

My first idea was to build the editor using only Qt and share as much code with the engine (the resource manager, the renderer, the maths). But, I would also like to be able to run the simulation inside the editor. This means I also have to share the simulation code which uses SDL threads.

我的第一个想法是仅使用 Qt 构建编辑器,并与引擎(资源管理器、渲染器、数学)共享尽可能多的代码。但是,我也希望能够在编辑器中运行模拟。这意味着我还必须共享使用 SDL 线程的模拟代码。

So, my question is this: Is there a way to have an the render OpenGL to a Qt window by using SDL?

所以,我的问题是:有没有办法使用 SDL 将 OpenGL 渲染到 Qt 窗口?

I have read on the web that it might be possible to supply SDL with a window handle in which to render. Anybody has experience dong that?

我在网上读到有可能为 SDL 提供一个窗口句柄来呈现。任何人都有经验吗?

Also, the threaded part of the simulator might pose a problem since it uses SDL threads.

此外,模拟器的线程部分可能会造成问题,因为它使用 SDL 线程。

采纳答案by Luka Marinko

While you might get it to work like first answer suggest you will likely run into problems due to threading. There is no simple solutions when it comes to threading, and here you would have SDL Qt and OpenGL mainloop interacting. Not fun.

虽然您可能会像第一个答案一样让它工作,但您可能会因线程而遇到问题。在线程方面没有简单的解决方案,在这里您将有 SDL Qt 和 OpenGL 主循环交互。不好玩。

The easiest and sanest solution would be to decouple both parts. So that SDL and Qt run in separate processes and have them use some kind of messaging to communicate (I'd recommend d-bus here ). You can have SDL render into borderless window and your editor sends commands via messages.

最简单和最明智的解决方案是将两个部分解耦。这样 SDL 和 Qt 在不同的进程中运行,并让它们使用某种消息传递进行通信(我在这里推荐 d-bus )。您可以将 SDL 渲染到无边框窗口中,并且您的编辑器通过消息发送命令。

回答by Evan Teran

This is a simplification of what I do in my project. You can use it just like an ordinary widget, but as you need, you can using it's m_Screen object to draw to the SDL surface and it'll show in the widget :)

这是我在我的项目中所做的简化。您可以像使用普通小部件一样使用它,但根据需要,您可以使用它的 m_Screen 对象绘制到 SDL 表面,它会显示在小部件中 :)

#include "SDL.h"
#include <QWidget>

class SDLVideo : public QWidget {
    Q_OBJECT

public:
    SDLVideo(QWidget *parent = 0, Qt::WindowFlags f = 0) : QWidget(parent, f), m_Screen(0){
        setAttribute(Qt::WA_PaintOnScreen);
        setUpdatesEnabled(false);

        // Set the new video mode with the new window size
        char variable[64];
        snprintf(variable, sizeof(variable), "SDL_WINDOWID=0x%lx", winId());
        putenv(variable);

        SDL_InitSubSystem(SDL_INIT_VIDEO | SDL_INIT_NOPARACHUTE);

        // initialize default Video
        if((SDL_Init(SDL_INIT_VIDEO) == -1)) {
            std:cerr << "Could not initialize SDL: " << SDL_GetError() << std::endl;
        }

        m_Screen = SDL_SetVideoMode(640, 480, 8, SDL_HWSURFACE | SDL_DOUBLEBUF);
        if (m_Screen == 0) {
            std::cerr << "Couldn't set video mode: " << SDL_GetError() << std::endl;
        }
    }

    virtual ~SDLVideo() {
        if(SDL_WasInit(SDL_INIT_VIDEO) != 0) {
            SDL_QuitSubSystem(SDL_INIT_VIDEO);
            m_Screen = 0;
        }
    }
private:
    SDL_Surface *m_Screen;
};

Hope this helps

希望这可以帮助

Note: It usually makes sense to set both the min and max size of this widget to the SDL surface size.

注意:将此小部件的最小和最大大小设置为 SDL 表面大小通常是有意义的。

回答by Martin Beckett

Rendering onto opengl from QT is trivial (and works very well) No direct experience of SDL but there is an example app here about mixing them. http://www.devolution.com/pipermail/sdl/2003-January/051805.html

从 QT 渲染到 opengl 是微不足道的(并且效果很好)没有 SDL 的直接经验,但这里有一个关于混合它们的示例应用程序。 http://www.devolution.com/pipermail/sdl/2003-January/051805.html

There is a good article about mixing QT widgewts directly with the opengl here http://doc.trolltech.com/qq/qq26-openglcanvas.htmla bit beyond what you strictly need but rather clever!

这里有一篇关于将 QT widgewts 直接与 opengl 混合的好文章 http://doc.trolltech.com/qq/qq26-openglcanvas.html有点超出您的严格需要,但相当聪明!

回答by kronat

you could use this library (see the demo directory):

你可以使用这个库(见演示目录):

https://github.com/kronat/libqsdl

https://github.com/kronat/libqsdl

Have a nice day

祝你今天过得愉快