views:

536

answers:

3

I'm basicly looking to get a native-like window GUI system into my OpenGL/Game. I need to display a single window to the user.

I'm looking into wxWidgets.

Because it works by "stealing" the WinMain/MainLoop, I'm trying to hack it so I can run its main loop on a separate thread.

Because I couldn't get its wxThread working well, I've done a "sample" with Windows Threads... but the initialization is still breaking on the wxWidgets internals...

Any feedback on this?

My code is this:

class MyApp: public wxApp
{
    virtual bool OnInit();
};

DECLARE_APP(MyApp) 
IMPLEMENT_APP_NO_MAIN(MyApp)

DWORD WINAPI MyThreadFunction( LPVOID lpParam ) 
{ 
    wxApp* pApp = new MyApp(); 
    wxApp::SetInstance(pApp);
    int argc = 0; wxChar ** argv = NULL;
    wxEntryStart(argc, argv);
    pApp->CallOnInit();
    pApp->OnRun();
    return 0;
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, wxCmdLineArgType lpCmdLine, int nCmdShow) {
    DWORD   id = 0;
    CreateThread(NULL,0, MyThreadFunction,NULL, 0,&id);
    assert(id != NULL);
    return 0;
}

I've tried making the wxWidgets initialization code in the main thread, and only calling the CallOnInit() in the separate thread, but same result.

A: 

The above code should work but it would be simpler to just call wxEntry() directly in your thread function. You might also prefer to use its WinMain-like overload or at least provide valid argc/argv (NULL for the latter might explain the crash).

VZ
A: 

You don't have to do anything special to wx to use opengl, jsut draw itno a wxGLCanvas.

Martin Beckett
A: 

Beware: most of wxWidgets isn't thread safe! You should only do GUI stuff from a single thread. This should be the thread that's running the Windows (or whatever) event loop - that's the main (startup) thread, or the one, where you call wxEntry. I managed to get OpenGL rendering from a separate thread working under Windows - the wxGLCanvas needs to be created on the main thread, need to call SetCurrent in the OpenGL render thread, and you must make sure you don't render while the window is being resized, etc...

Cornelius Scarabeus