Mac: Implemented SDL_GetDisplayDPI (thanks, Kirill!).
authorRyan C. Gordon <icculus@icculus.org>
Thu, 07 Jan 2016 14:02:37 -0500
changeset 100382bab6f8d403e
parent 10037 e2343ac4788e
child 10039 56c514efb1fa
Mac: Implemented SDL_GetDisplayDPI (thanks, Kirill!).

Fixes Bugzilla #3223.
src/video/cocoa/SDL_cocoamodes.h
src/video/cocoa/SDL_cocoamodes.m
src/video/cocoa/SDL_cocoavideo.m
test/testdisplayinfo.c
     1.1 --- a/src/video/cocoa/SDL_cocoamodes.h	Wed Jan 06 22:39:29 2016 +0100
     1.2 +++ b/src/video/cocoa/SDL_cocoamodes.h	Thu Jan 07 14:02:37 2016 -0500
     1.3 @@ -37,6 +37,7 @@
     1.4  extern int Cocoa_GetDisplayBounds(_THIS, SDL_VideoDisplay * display, SDL_Rect * rect);
     1.5  extern int Cocoa_GetDisplayUsableBounds(_THIS, SDL_VideoDisplay * display, SDL_Rect * rect);
     1.6  extern void Cocoa_GetDisplayModes(_THIS, SDL_VideoDisplay * display);
     1.7 +extern int Cocoa_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hpdi, float * vdpi);
     1.8  extern int Cocoa_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode);
     1.9  extern void Cocoa_QuitModes(_THIS);
    1.10  
     2.1 --- a/src/video/cocoa/SDL_cocoamodes.m	Wed Jan 06 22:39:29 2016 +0100
     2.2 +++ b/src/video/cocoa/SDL_cocoamodes.m	Thu Jan 07 14:02:37 2016 -0500
     2.3 @@ -374,6 +374,24 @@
     2.4      return 0;
     2.5  }
     2.6  
     2.7 +int
     2.8 +Cocoa_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hdpi, float * vdpi)
     2.9 +{
    2.10 +    const float MM_IN_INCH = 25.4f;
    2.11 +
    2.12 +    SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
    2.13 +
    2.14 +    CGSize displaySize = CGDisplayScreenSize(data->display);
    2.15 +    size_t pixelWidth = CGDisplayPixelsWide(data->display);
    2.16 +    size_t pixelHeight = CGDisplayPixelsHigh(data->display);
    2.17 +
    2.18 +    *ddpi = SDL_ComputeDiagonalDPI(pixelWidth, pixelHeight, displaySize.width / MM_IN_INCH, displaySize.height / MM_IN_INCH);
    2.19 +    *hdpi = pixelWidth * MM_IN_INCH / displaySize.width;
    2.20 +    *vdpi = pixelHeight * MM_IN_INCH / displaySize.height;
    2.21 +
    2.22 +    return 0;
    2.23 +}
    2.24 +
    2.25  void
    2.26  Cocoa_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
    2.27  {
     3.1 --- a/src/video/cocoa/SDL_cocoavideo.m	Wed Jan 06 22:39:29 2016 +0100
     3.2 +++ b/src/video/cocoa/SDL_cocoavideo.m	Thu Jan 07 14:02:37 2016 -0500
     3.3 @@ -74,6 +74,7 @@
     3.4      device->VideoQuit = Cocoa_VideoQuit;
     3.5      device->GetDisplayBounds = Cocoa_GetDisplayBounds;
     3.6      device->GetDisplayUsableBounds = Cocoa_GetDisplayUsableBounds;
     3.7 +    device->GetDisplayDPI = Cocoa_GetDisplayDPI;
     3.8      device->GetDisplayModes = Cocoa_GetDisplayModes;
     3.9      device->SetDisplayMode = Cocoa_SetDisplayMode;
    3.10      device->PumpEvents = Cocoa_PumpEvents;
     4.1 --- a/test/testdisplayinfo.c	Wed Jan 06 22:39:29 2016 +0100
     4.2 +++ b/test/testdisplayinfo.c	Thu Jan 07 14:02:37 2016 -0500
     4.3 @@ -51,11 +51,18 @@
     4.4      for (dpy = 0; dpy < num_displays; dpy++) {
     4.5          const int num_modes = SDL_GetNumDisplayModes(dpy);
     4.6          SDL_Rect rect = { 0, 0, 0, 0 };
     4.7 +        float ddpi, hdpi, vdpi;
     4.8          int m;
     4.9  
    4.10          SDL_GetDisplayBounds(dpy, &rect);
    4.11          SDL_Log("%d: \"%s\" (%dx%d, (%d, %d)), %d modes.\n", dpy, SDL_GetDisplayName(dpy), rect.w, rect.h, rect.x, rect.y, num_modes);
    4.12  
    4.13 +        if (SDL_GetDisplayDPI(dpy, &ddpi, &hdpi, &vdpi) == -1) {
    4.14 +            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "    DPI: failed to query (%s)\n", SDL_GetError());
    4.15 +        } else {
    4.16 +            SDL_Log("    DPI: ddpi=%f; hdpi=%f; vdpi=%f\n", ddpi, hdpi, vdpi);
    4.17 +        }
    4.18 +
    4.19          if (SDL_GetCurrentDisplayMode(dpy, &mode) == -1) {
    4.20              SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "    CURRENT: failed to query (%s)\n", SDL_GetError());
    4.21          } else {