views:

67

answers:

2

I'm doing some research on making a game that will be able to scale its graphical resources to suit the DPI of whatever device it's on.

In order to do this, I would like to be able to query the DPI of the device, to appropriately scale the assets.

It's a 2D game, and the art style suits arbitrary scaling quite well.

example of what I would do with res/dpi combos:

iPhone/iTouch, at 320x480 (163 dpi) - text will be the normal size

iPhone4, at 640x960 (326 dpi) - text will be twice as large

iPad, at 768x1024 (132 dpi) - text will probably be capped at some minimum size, to take advantage of the greater screen real estate.

So, on iPhone OS, is there a way to query the screen's DPI?

(and as a side note - is this possible on Android devices at all?)

A: 

You can fetch this information in Android with the DisplayMetrics class.

iandisme
+1  A: 

I'm not sure if there's a direct way to get the DPI, as you generally don't need to know it. The UIScreen class does expose a scale factor. For iPhone <= 3GS and iPad it should be 1.0, for iPhone 4 it should be 2.0.

Sight compiled:

UIScreen *theScreen = [UIScreen mainScreen];
float scaleFactor = 1.0f;
if ([theScreen respondsToSelector:@selector(getScale)]) {
    scaleFactor = theScreen.scale;
}
// use scaleFactor to determine size of fonts/whatever

But, the way the new OS is implemented in regards to differing screen resolutions means you usually won't have to custom scale depending on DPI. Things like images will either be scaled up or it will automatically choose a specially named higher resolution image, and drawing operations are also handled automatically (like drawing a 1point line now draws a 2px line on the Iphone 4). Fonts may be handled similarly, I don't think I came across anything font specific. Read up on the new docs for info:

http://developer.apple.com/iphone/library/documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/SupportingResolutionIndependence/SupportingResolutionIndependence.html#//apple_ref/doc/uid/TP40007072-CH10

bensnider