0
\$\begingroup\$

I have written a little OpenGL framework in the past year that i would like to extend to support multiple windows in the near future. I have an idea about what I would like to do but I am not sure if that is possible so far.

I a nutshell the inner working of my framework is as following:

  1. A window is created.
  2. A render context for that window is created.
  3. An application deriving from an interface is created.
  4. window.run() is called, passing the application running in the window and the render context used as parameter.

    sglr::W32Window w(nCmdShow);
    w.create(L"DemoApp", sglr::DISPLAY_MODE::ENUM::WINDOWED, sglr::Resolution(800, 800));
    
    sglr::W32Rendercontext r;
    r.create(w, 4, 0, sglr::CONTEXT_TYPE::ENUM::DEBUG);
    
    TestApp app;
    
    return w.run(app, r);
    

The run method on the Win32 implementation looks like following (just some of the code):

        mRunningApp->onInit();

        // start message loop
        while (1)
        {

            while (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
            {

                TranslateMessage(&msg);

                DispatchMessage(&msg);
            }

            // if message is WM_QUIT, exit loop
            if (msg.message == WM_QUIT)
                break;



            mRunningApp->onUpdate(mDt);

            mRenderContext->setAsCurrent();

            mRunningApp->onRender();

            mRenderContext->disableAsCurrent();

        }

    }

What I would like to do is to create a seperate window, rendercontext and application and start a seperate main loop by calling run() just like with the first window. I already have a seperate rendercontext for each window and know about setting it active before rendering to each window.
What possible solutions would exist to do that? Just running each run() method in a seperate threads sound like a kinda naive approach to me. (I admit I already tried it with failure :D)

\$\endgroup\$
2
\$\begingroup\$

I think you mis-designed your API. Why does a window take an app, instead of an app taking multiple windows?

An application can have multiple windows, but a window can only belong to one application.

If you change this around there will be no need for multiple run loops. There will just be one in the application, and the application will handle events and dispatch rendering calls etc. for each window.

\$\endgroup\$
  • \$\begingroup\$ When I started with the implementation I first didn´t had multiple windows in mind to be honest. I thought about your idea as well and it would work better with multiple windows indeed. Unfortunately my whole "app principle" would not work as planned anymore and there would be more work to do within the app like processing input, polling message etc. But maybe I can work something out. Thanks for your suggestion :) \$\endgroup\$ – Chris Jan 15 '15 at 15:38
  • \$\begingroup\$ Maybe you also misnamed the App class. Maybe you really have an App instance (takes care of main loop), multiple Window instances (take care of only their own input, passed to them by the App) and a Game instance (takes care of logic, rendering etc)? \$\endgroup\$ – bogglez Jan 15 '15 at 18:00
  • \$\begingroup\$ Well polling new events is still part of the platform dependant implementation of the window. So I would rather poll the events within the app´s mainloop from the current active window. \$\endgroup\$ – Chris Jan 16 '15 at 9:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.