Question

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:

  • Window - Describes a Window
  • WindowContext - Describes a context for the Window (abstract class)
  • WindowDelegate - Used to determine when the context has changed, when the window will open/close, etc.
  • WindowEventListener - Listens to events from the Window (not sure if I should just stick to polling instead of call back functions)

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?

Was it helpful?

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:

  • Abstract differing things from the two applications behind a common interface
    • Such as, but not limited to, once you get into various platforms:
      • Application control loop
      • Renderer (i.e, graphics output)
      • File I/O
      • User input
      • Audio
      • System services (threads, etc)
  • Implement the common interfaces on all target platforms
  • Implement application functionality only through the abstraction interfaces

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top