Quick Links: Download Gideros Studio | Gideros Documentation | Gideros community chat | DONATE
Pixels per inch — Gideros Forum

Pixels per inch

totebototebo Member
edited May 2017 in General questions
How do you get pixels per inch when using Application.LETTERBOX? I'd like to position something a certain (real-world) distance from the edge of the screen, regardless of size of display.
My Gideros games: www.totebo.com

Comments

  • hgy29hgy29 Maintainer
    You should be able to retrieve device DPI and multiply or divide it by LogicalScale (X and Y)
  • totebototebo Member
    edited May 2017
    Could't get it to work at all. Could someone try to post some actual code? It feels like nothing works! :)
    My Gideros games: www.totebo.com
  • totebototebo Member
    edited May 2017
    I created a simple test, which also fails. Does this look right? I'm testing only the x axis at the moment to save time.
    -- Neither of these scale modes work
    --application:setScaleMode( "noScale" )
    application:setScaleMode( "letterbox" )
    local size = 5
    local sprite = Sprite.new()
    local pixel = Pixel.new( 0xff0000, 1, size, size )
    local x = -size/2
    local y = -size/2
    pixel:setPosition(x,y)
    sprite:addChild( pixel )
    stage:addChild( sprite )
    local top_left_x = -application:getLogicalTranslateX()/application:getLogicalScaleX()
    local top_left_y = -application:getLogicalTranslateY()/application:getLogicalScaleY()
    local dpi = application:getScreenDensity()
    local x = top_left_x + dpi/application:getLogicalScaleX()
    local y = top_left_y
    sprite:setPosition( x,y )
    My Gideros games: www.totebo.com
  • hgy29hgy29 Maintainer
    edited May 2017
    Try this:
    local sx,sy=application:getLogicalScaleX(),application:getLogicalScaleY()
    local ox,oy=application:getLogicalTranslateX(),application:getLogicalTranslateY()
    local dpi=application:getScreenDensity()
    local inchX,inchY=dpi/sx,dpi/sy
    local screenOriginX,screenOriginY=-ox/sx,-oy/sy
     
    local XdistanceInInches,YdistanceInInches=1,0.5
    local p=Pixel.new(0xFF0000,1,20,20)
    p:setPosition(screenOriginX+inchX*XdistanceInInches,screenOriginY+inchY*YdistanceInInches)
    stage:addChild(p)
    It will place the 20x20 box top/left corner at 1 inch from left screen border and 0.5 inch from top screen border, whatever your scale mode or device is.

    EDIT: yes, your code look fine :)
  • totebototebo Member
    Thanks @hgy29! I tried your code above on an iPod 5th gen, an iPad Mini Retina and a Pixel XL. The dot appears in varying distances on all of them; the iPod and Pixel are roughly in the same place, but the iPad is way off in both directions.

    Their respective dpi's are:

    Pixel XL: 560
    iPad Mini Retina: 264
    iPod 5th gen: 326
    My Gideros games: www.totebo.com
  • hgy29hgy29 Maintainer
    I tried on my iPhone 6, and it worked ok. Could it be that your app is in landscape ?
  • totebototebo Member
    It's a portrait app running on all devices. Did you test it on another device?
    My Gideros games: www.totebo.com
  • totebototebo Member
    edited May 2017
    To be more precise, here are the real world dimensions of a "1 inch" square drawn with the code above:

    Pixel XL: 1.02x1.02 inches
    iPod Touch 5th gen: 1x1 inches
    iPad Mini retina: 0.8x0.8 inches
    Nexus 5: 1x1 inches
    Nexus 6: 1.1x1.1 inches

    Maybe the device dpi isn't an exact science?
    My Gideros games: www.totebo.com
  • hgy29hgy29 Maintainer
    Or maybe reported DPI is wrong for some devices ? you said your iPad Mini retina was 264 dpi, while apple specs say it is 326. And 264/326 is almost 0.8, coincidence ?
  • totebototebo Member
    edited May 2017
    I think you're onto something. It's likely to be a hardware issue.

    I found this thread:
    http://stackoverflow.com/questions/13178186/how-can-i-detect-the-dpi-on-an-ipad-mini

    And:
    http://stackoverflow.com/questions/28573639/how-do-ruler-apps-stay-accurate-on-all-devices

    Looks like the only way to do it is to hard code the dpi per iOS device. Bummer.
    My Gideros games: www.totebo.com
  • hgy29hgy29 Maintainer
    Gideros uses the detection routine posted here: http://stackoverflow.com/questions/3860305/get-ppi-of-iphone-ipad-ipod-touch-at-runtime

    Comments in that thread confirm it doesn't work for iPad mini and maybe iphone6 plus, but there doesn't seem to be an universal solution yet, we have to resort on hardcoding them depending on the device type.
  • totebototebo Member
    edited May 2017
    Oh that's great, so this could potentially be updated? Found this at the bottom of that thread which appears updated and comprehensive:

    https://github.com/lmirosevic/GBDeviceInfo

    Is that what we're using now?
    My Gideros games: www.totebo.com
  • hgy29hgy29 Maintainer
    Yes, we can do something similar in gideros too.

    Likes: antix, totebo

    +1 -1 (+2 / -0 )Share on Facebook
Sign In or Register to comment.