Followers 0

Supporting both DirectX & OpenGL

8 posts in this topic

Hello!  I re-writing my game engine from scratch and was trying to decide whether I should support DirectX & OpenGL or just DirectX?  I understand that OpenGL is cross-platform while DirectX is restricted to Windows & the Xbox.  I also have a couple more questions:

1. I am developing on Windows using Visual Studio 2013 Ultimate.  If I were to implement OpenGL,  how would I build for different platforms? Example: If I wanted to build on maybe, Linux or Mac.  Would I have to build my project on that operating system?
2. If I do develop for both API's, how should I setup my project? Should I create two separate projects? Or keep them both separated in one project using enums, and interfaces? Example:
enum NXAPIChoice
{
NXAPI_DirectX,
NXAPI_OpenGL,
}

NXAPIChoice NXGame::DefaultAPI = NXAPIChoice::NXAPI_DirectX;

void NXGame::FinalInitialize()
{
int sWidth = 0, sHeight = 0;
InitializeWindows(sWidth, sHeight);
SetupGPU(sWidth, sHeight);

//TODO: [HIGH PRIORITY] Initialize Input
//TODO: [HIGH PRIORITY] Initialize Audio

Initialize();

//TODO: [HIGH PRIORITY] Create & Start GameTime
}

void NXGame::SetupGPU(int width, int height)
{
//stuff...
NXGPU::Initialize(DefaultAPI, width, height, ...other);
}

void NXGPU::Initialize(NXAPIChoice choice, int width, int height, ...other)
{
if (choice == NXAPIChoice::NXAPI_DirectX)
InitializeDirect3D(width, height, D3D_FEATURE_LEVEL_11_0, ...other);
else
InitializeOpenGL(width, height, GL_VERSION, ...other)
}


Also if I were to develop for both, and I do have to compile my code on other platforms (linux or mac), will I be able to skip my DirectX code (assuming it would cause errors because DX is not available) and just compile GL code? Is it even worth adding support for both? Thanks!

[EDIT: Or perhaps, shall I finish my engine with DirectX first (creating empty methods for GL in the process) and just start working on GL support (fill in the empty methods) after DirectX is fully implemented?]

Edited by KamRandle
2

Share on other sites

If I was new to this stuff I would finish the engine with one, then make it again with the other, then I feel like I'd truly know how to make it the 3rd time with both. This would be if you are trying to use this as a learning experience of course. If you already know how both work well enough then this wouldn't be the way to go about it.

0

Share on other sites

I'd honestly recommend switching to OpenGL if you feel comfortable with the idea. Choosing DirectX means you either choose to limit your audience or you choose to limit your graphics options. Even if your game isn't coming out for 5 years, the same situation that exists today with Windows XP not supporting DX11 will exist with Windows 7 not supporting DX13 or whatever. OpenGL doesn't have that problem since driver manufacturers control support levels. And if there's any chance your game would sell in China or other countries that lag further behind on OS adoption, the impact of using DirectX will really, really hurt. Even in America, you're still talking about a larger group than all Mac and Linux users combined.

Is there a reason you're hesitant to use OpenGL? If you have tools that only work with DirectX and make your life insanely easier, obviously you should stick with DirectX. But if you're just worried OpenGL is slower or harder to use or something, just jump in and you'll find out all the FUD is unjustified.

-2

Share on other sites

Yes. The days of a virtual interface and run-time selectable graphics API are long gone.

Maybe I'm just being stupid but what is the reasoning behind that?
2

Share on other sites

That's what i did in my 3d engine in my signature, have a look at it if you wish (Camera.h and Camera.cpp are good example). If i want to use opengl, i simply declalre

a macro COMPILE_FOR_OPENGL, if i want dx9, COMPILE_FOR_DIRECTX9 and so on. It's a bit of pain to write at first, but it had the advantage of being

extremly simple to switch between apis. The other solution would be separate interfaces in dlls i guess.

Both have advantages and inconveniances, it's a double edged sword kinda deal.

Edited by Vortez
0

Share on other sites

Yes. The days of a virtual interface and run-time selectable graphics API are long gone.

Maybe I'm just being stupid but what is the reasoning behind that?

• Performance: Reducing virtual calls is usually a micro-optimization but when it’s virtual for every SetTexture(), SetBlendMode(), etc. it can add up. Plus other locations in code that have to become run-time if/else’s instead of just macro-excluded lines of code. With other options readily available there is just no need for the overhead.
• Code maintenance: But you still need macros around the Direct3D parts for your ports for Linux and OS X, so the macro spaghetti still exists to some degree.  On some platforms it is both and on some platforms it is only one.  It is easier to just make it, “On some platforms it is this and on others that—no cross-overs.”
• OpenGL on Windows is generally just not a good idea.  OpenGL hasn’t had any form of consistency standards by which vendors are required to adhere until very recently, meaning it may run as expected on some cards and not on others.  Again from #2, it is just best to use Direct3D on Windows and OpenGL on everything else.
#3 is the main reason.  Direct3D is near 100% consistent on all Windows machines and OpenGL more like 80%.  On Mac OS X OpenGL is near 100% consistent.  Linux OpenGL is somewhere between.  So you always want to use Direct3D on Windows; there is never really any reason for overlapping builds.

L. Spiro Edited by L. Spiro
2

Share on other sites
Hello everyone! Thanks for the answers. I think I may just write my DirectX version first. It is being developed alongside a game that I am using it for. It will be my first game to be released. After I am finished, then I'll focus on GL versions. How many people do you guys think use Bootcamp on OS X? Thanks! :)
0

Create an account

Register a new account