Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

c++ - How do I use Qt and SDL together?

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.

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.

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

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?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 :)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...