Skip to content

Commit 6c4a060

Browse files
author
James Chen
committed
issue cocos2d#1712: Getting DPI support for Linux.
1 parent ce95be6 commit 6c4a060

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

cocos2dx/platform/linux/CCDevice.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,31 @@
11
#include "platform/CCDevice.h"
2+
#include <X11/Xlib.h>
3+
#include <stdio.h>
24

35
NS_CC_BEGIN
46

57
float CCDevice::getDPI()
68
{
7-
9+
static int dpi = -1;
10+
if (dpi == -1)
11+
{
12+
Display *dpy;
13+
char *displayname = NULL;
14+
int scr = 0; /* Screen number */
15+
dpy = XOpenDisplay (displayname);
16+
/*
17+
* there are 2.54 centimeters to an inch; so there are 25.4 millimeters.
18+
*
19+
* dpi = N pixels / (M millimeters / (25.4 millimeters / 1 inch))
20+
* = N pixels / (M inch / 25.4)
21+
* = N * 25.4 pixels / M inch
22+
*/
23+
double xres = ((((double) DisplayWidth(dpy,scr)) * 25.4) /
24+
((double) DisplayWidthMM(dpy,scr)));
25+
dpi = (int) (xres + 0.5);
26+
printf("dpi = %d\n", dpi);
27+
}
28+
return dpi;
829
}
930

1031
NS_CC_END

cocos2dx/proj.linux/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ OBJECTS = ../actions/CCAction.o \
9696
../platform/linux/CCApplication.o \
9797
../platform/linux/CCEGLView.o \
9898
../platform/linux/CCImage.o \
99+
../platform/linux/CCDevice.o \
99100
../script_support/CCScriptSupport.o \
100101
../sprite_nodes/CCAnimation.o \
101102
../sprite_nodes/CCAnimationCache.o \

0 commit comments

Comments
 (0)