Mac: Implemented SDL_GetDisplayDPI (thanks, Kirill!).
authorRyan C. Gordon <icculus@icculus.org>
Thu, 07 Jan 2016 14:02:37 -0500
changeset 10038 2bab6f8d403e
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
--- a/src/video/cocoa/SDL_cocoamodes.h	Wed Jan 06 22:39:29 2016 +0100
+++ b/src/video/cocoa/SDL_cocoamodes.h	Thu Jan 07 14:02:37 2016 -0500
@@ -37,6 +37,7 @@
 extern int Cocoa_GetDisplayBounds(_THIS, SDL_VideoDisplay * display, SDL_Rect * rect);
 extern int Cocoa_GetDisplayUsableBounds(_THIS, SDL_VideoDisplay * display, SDL_Rect * rect);
 extern void Cocoa_GetDisplayModes(_THIS, SDL_VideoDisplay * display);
+extern int Cocoa_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hpdi, float * vdpi);
 extern int Cocoa_SetDisplayMode(_THIS, SDL_VideoDisplay * display, SDL_DisplayMode * mode);
 extern void Cocoa_QuitModes(_THIS);
 
--- a/src/video/cocoa/SDL_cocoamodes.m	Wed Jan 06 22:39:29 2016 +0100
+++ b/src/video/cocoa/SDL_cocoamodes.m	Thu Jan 07 14:02:37 2016 -0500
@@ -374,6 +374,24 @@
     return 0;
 }
 
+int
+Cocoa_GetDisplayDPI(_THIS, SDL_VideoDisplay * display, float * ddpi, float * hdpi, float * vdpi)
+{
+    const float MM_IN_INCH = 25.4f;
+
+    SDL_DisplayData *data = (SDL_DisplayData *) display->driverdata;
+
+    CGSize displaySize = CGDisplayScreenSize(data->display);
+    size_t pixelWidth = CGDisplayPixelsWide(data->display);
+    size_t pixelHeight = CGDisplayPixelsHigh(data->display);
+
+    *ddpi = SDL_ComputeDiagonalDPI(pixelWidth, pixelHeight, displaySize.width / MM_IN_INCH, displaySize.height / MM_IN_INCH);
+    *hdpi = pixelWidth * MM_IN_INCH / displaySize.width;
+    *vdpi = pixelHeight * MM_IN_INCH / displaySize.height;
+
+    return 0;
+}
+
 void
 Cocoa_GetDisplayModes(_THIS, SDL_VideoDisplay * display)
 {
--- a/src/video/cocoa/SDL_cocoavideo.m	Wed Jan 06 22:39:29 2016 +0100
+++ b/src/video/cocoa/SDL_cocoavideo.m	Thu Jan 07 14:02:37 2016 -0500
@@ -74,6 +74,7 @@
     device->VideoQuit = Cocoa_VideoQuit;
     device->GetDisplayBounds = Cocoa_GetDisplayBounds;
     device->GetDisplayUsableBounds = Cocoa_GetDisplayUsableBounds;
+    device->GetDisplayDPI = Cocoa_GetDisplayDPI;
     device->GetDisplayModes = Cocoa_GetDisplayModes;
     device->SetDisplayMode = Cocoa_SetDisplayMode;
     device->PumpEvents = Cocoa_PumpEvents;
--- a/test/testdisplayinfo.c	Wed Jan 06 22:39:29 2016 +0100
+++ b/test/testdisplayinfo.c	Thu Jan 07 14:02:37 2016 -0500
@@ -51,11 +51,18 @@
     for (dpy = 0; dpy < num_displays; dpy++) {
         const int num_modes = SDL_GetNumDisplayModes(dpy);
         SDL_Rect rect = { 0, 0, 0, 0 };
+        float ddpi, hdpi, vdpi;
         int m;
 
         SDL_GetDisplayBounds(dpy, &rect);
         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);
 
+        if (SDL_GetDisplayDPI(dpy, &ddpi, &hdpi, &vdpi) == -1) {
+            SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "    DPI: failed to query (%s)\n", SDL_GetError());
+        } else {
+            SDL_Log("    DPI: ddpi=%f; hdpi=%f; vdpi=%f\n", ddpi, hdpi, vdpi);
+        }
+
         if (SDL_GetCurrentDisplayMode(dpy, &mode) == -1) {
             SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "    CURRENT: failed to query (%s)\n", SDL_GetError());
         } else {