c++opengldirectxsdlopenglcontext

Switching from an OpenGL context to a D3D/DX context (or another context) during Runtime


EDIT: IS there any pre-built libraries to do this for me, that either uses SDL OR runs on Window, Linux, Mac, iOS and Android?

At the moment, I am not sure if the way I am changing contexts is quite a good design, I haven't completely finished it. I'm just really concerned about wasting my time. I am using SDL for Window management, and event handling.

I currently wrap SDL (specifically SDL 2) in classes to manage this. These are my classes and what they do:

Anyway, here's an example of how it works:

OglWindowContext* context = new OglWindowContext;
// change context's settings
Window window(ipoint2(), idimension2(640, 480), "Test Window", 
              Window::Resizeable, context, &windowListener, 
              NULL /* window delegate */);

while(window.isOpen())
{
    window.processEvents(); // calls back events to the WindowEventListener
    window.draw(); // calls the context's draw method
}

NOTE: ipoint2 is a 2d point

And I can change the context midway by calling:

window.setContext(new Dx9Context); // or whatever context you would like

Is there any easier way, or should I say more efficient way, to do this?


Solution

  • Switching between renderers doesn't make much sense on any other platform that Windows at the moment, so your request for windows, linux, mac, ios and android support sounds strange. Perhaps you're looking for a library that helps you to be portable in general?

    To achieve running with one renderer or the other (opengl/d3d), or to achieve portability in general, you need to implement some kind of portability layer.

    Basically, you could start by writing two programs: one that draws a rotating cube in OpenGL, and another, completely separate that does the same with D3D. Next, write a third program that combines these, by:

    The D3D and OpenGL on windows can practically be considered separate platforms, but you could pick one or the other during application startup - the choise would probably affect renderer and also the application control loop.

    Switching during runtime is also entirely possible, but probably way too much hassle to be worth it.

    You may note that SDL actually does a lot of the above.