Michael Bethke Michael Bethke - 1 year ago 203
C++ Question

SDL Detect High DPI/Retina Display

I'm wondering what the best way to detect a high DPI display is. Currently I'm trying to use

SDL_GetDisplayDPI (int, *float, *float, *float)
, however this has only returned errors on the two different computers I tested with (MacBook Pro running OS X 10.11.5 and iMac running macOS 10.12 Beta (16A238m)). For reference, my code is bellow.

float diagDPI = -1;
float horiDPI = -1;
float vertDPI = -1;

int dpiReturn = SDL_GetDisplayDPI (0, &diagDPI, &horiDPI, &vertDPI);
std::cout << "GetDisplayDPI() returned " << dpiReturn << std::endl;

if (dpiReturn != 0)

std::cout << "Error: " << SDL_GetError () << std::endl;

std::cout << "DDPI: " << diagDPI << std::endl << "HDPI: " << horiDPI << std::endl << "VDPI: " << vertDPI << std::endl;

Unfortunately, this is only giving me something like this:

/* Output */
GetDisplayDPI() returned -1
DDPI: -1
HDPI: -1
VDPI: -1
Not Retina

I also tried comparing the OpenGL drawable size with the SDL window size, but
SDL_GetWindowSize (SDL_Window, *int, *int)
is returning 0s, too. That code is bellow, followed by the output.

int gl_w;
int gl_h;

SDL_GL_GetDrawableSize (window, &gl_w, &gl_h);
std::cout << "GL_W: " << gl_w << std::endl << "GL_H: " << gl_h << std::endl;

int sdl_w;
int sdl_h;

SDL_GetWindowSize (window, &sdl_w, &sdl_h);
std::cout << "SDL_W: " << sdl_w << std::endl << "SDL_H: " << sdl_h << std::endl;

/* Output */
GL_W: 1280
GL_H: 720
SDL_W: 0
SDL_H: 0

It's entirely possible that I'm doing something wrong here, or making these calls in the wrong place, but I think more likely is that I'm on the wrong track entirely. There's a hint to disallow high-dpi canvases, so there's probably a simple bool somewhere, or something that I'm missing. I have certainly looked through the wiki, and checked Google, but I can't really find any help for this. Any suggestions or feedback are welcome!

Thank you for your time!

Answer Source

I was using a .framework installation of SDL when I encountered this issue. For an unrelated reason, I trashed the .framework SDL files (image and ttf as well), and built SDL from source (thus transitioning to a "unix-style" SDL-installation). To my surprise, SDL_GetDisplayDPI () is now returning 0, setting the values of DDPI, HDPI, and VDPI, to 109 on a non-retina iMac, and 113.5 on a retina MacBook Pro. I'm not certain that these are correct/accurate, but it is consistent between launches, so I'll work with it.

At this point, I'm not sure if it was a bug, which has been fixed in the repo, or was an issue with my .framework files. On a somewhat unrelated note, SDL_GetBasePath () and SDL_GetPrefPath (), which also weren't working, now return the expected values. If you're also experiencing any of these problems on macOS, try compiling and installing SDL from source (https://hg.libsdl.org/SDL).

Thanks for your input, everyone!

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download