Using SDL2, is there a way to determine at runtime whether the application is using OpenGL, OpenGLES, or DirectX?

EDIT: If there is a way to figure it out without using the SDL2 API, I'm willing to throw that code into my project.

  • \$\begingroup\$ Posting this as a comment, as I'm not 100% sure this is correct. But I think I saw in the code that it is decided at compile time what renderer is used so this information might not be available at run-time. \$\endgroup\$ – Roy T. Aug 15 '14 at 7:05
  • \$\begingroup\$ @RoyT. Yeah, I saw that. I believe it was meant for SDL2's internal use. I've edited the answer to allow for ways without using SDL's API. \$\endgroup\$ – 147ofthem Aug 15 '14 at 10:57
  • \$\begingroup\$ SDL just creates a window for you. Whether you use OpenGL(ES) or DirectX with it is up to you. Or are you asking about SDL renderers that use that window? In that case SDL_GetRendererInfo might be for you. \$\endgroup\$ – kolrabi Aug 15 '14 at 11:23

In SDL2 the creation of the window is separate from the rendering environment used to draw into that window. So, while you might pass "SDL_WINDOW_OPENGL" to SDL_CreateWindow(), this simply states that the window should support rendering from an OpenGL context later down the line and doesn't actually create an OpenGL context at that point. So, from SDL_GetCurrentVideoDriver() I would expect only something like "windows", "x11" or "psp" due to the fact that this is the module used for handling just the window system and the final blitting of whatever the renderer draws to the screen.

Assuming that you're using a Renderer to draw 2D content to the screen - at the point you create a Renderer a DirectX or OpenGL context will actually be created for the renderer. After this point, you should be able to query the Renderer you have created for the actual driver being used. This is untested - so might need some tweaks, but I think it should give you more useful information than what you're currently getting.

/// ... Create Window ... 

SDL_Renderer *renderer = SDL_CreateRenderer(sdlWindow, -1, 0);
SDL_RendererInfo rendererInfo;
SDL_GetRendererInfo(renderer, &rendererInfo);

std::cout << "Renderer: " << rendererInfo.name << std::endl;

You can request that SDL uses a given driver by setting SDL_HINT_RENDER_DRIVER like so...

SDL_SetHint(SDL_HINT_RENDER_DRIVER, "software");

But this is only a hint so it's not guaranteed. However, as the possible values for this hint are listed as 'direct3d, openGL, opengles2, opengles, software' I expect these to be the name values you would get back from the above SDL_GetRendererInfo call.

Hope this helps. Apologies if I'm barking up the wrong tree or this doesn't work as expected. Just thought I would throw a suggestion out there before heading to bed!

| improve this answer | |
  • \$\begingroup\$ This appears to work fine on Linux. It gives me "opengl" as expected. I'm away from a Windows machine at the moment, but I'll try it in a couple days when I have access to one (unless someone with Windows wants to test it :) ). \$\endgroup\$ – 147ofthem Aug 18 '14 at 11:42
  • \$\begingroup\$ Windows shows direct3d. \$\endgroup\$ – anatoly techtonik Feb 10 '16 at 10:28
  • 1
    \$\begingroup\$ +1 Based on this answer, I tested it on macOS trying to create a "metal" renderer. By calling SDL_SetHint() before SDL_CreateRenderer(), it will properly create "metal" one for me. Here is testing code gist.github.com/haxpor/c02a6ba98e3ff35800eeb3a4f4f5b0b4. Interested people can substitute value of "meta" to be others as needed and see the result. \$\endgroup\$ – haxpor Oct 27 '18 at 21:34

Once upon a time there was Google.

Now this seems to be what you are looking for:

const char* SDL_GetCurrentVideoDriver(void)

Returns the name of the current video driver or NULL if no driver has been initialized.

| improve this answer | |
  • 3
    \$\begingroup\$ Tested it on two computers: SDL_GetCurrentVideoDriver() returns string windows. When I tried to print all avalible "drivers", I got these three: windows, dummy and ` ` (empty string). \$\endgroup\$ – HolyBlackCat Aug 14 '14 at 19:44
  • \$\begingroup\$ Try going to the implementation of that function. See how it decides what to return. It's possible that you can find some clues there. \$\endgroup\$ – Eejin Aug 14 '14 at 20:54
  • \$\begingroup\$ I knew about this, but @HolyBlackCat is right. The title is misleading. There are a lot of functions that sound like they'd get what I need, but I can only find functions to get platform stuff and whether or not SDL is using software rendering or hardware accelerated rendering. \$\endgroup\$ – 147ofthem Aug 15 '14 at 4:16
  • \$\begingroup\$ @HolyBlackCat Maybe "windows" is a code for "DirectX". Have you tried to test this line on a Linux machine? Maybe the result will vary. \$\endgroup\$ – Alexandre Desbiens Aug 15 '14 at 11:33
  • \$\begingroup\$ I have not any PC with linux nor installed virtual machine. I can't test it. \$\endgroup\$ – HolyBlackCat Aug 15 '14 at 20:37

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.