Gdx.graphics.getDensity() returns infinity inside VirtualBox
See original GitHub issueIssue details
Calling Gdx.graphics.getDensity()
inside VirtualBox (Linux guest/host) returns Infinity
(because display size reported by Lwjgl is 0). I understand that on some platforms the display density can not be computed, but I think there should be a special value UNKNOWN or an exception thrown to force client code to handle this case. I got an exception in the JNI code of FreeType (because I use density for font size scaling), and getDensity() doesn’t indicate it can return a completely invalid value.
It returns an estimate in any case, so if the value is too low / too high it can just default to 1 or something.
Version of LibGDX and/or relevant dependencies
GDX version 1.10.0
(latest at time of writing)
Please select the affected platforms
- Android
- iOS
- HTML/GWT
- Windows
- Linux
- MacOS
Issue Analytics
- State:
- Created 2 years ago
- Reactions:2
- Comments:10 (8 by maintainers)
Top Results From Across the Web
Pixels to dips, desktop and android - java - Stack Overflow
the Gdx.graphics.getDensity() method actually gets the SCALE, so it's already done for me. Now the problem, libgdx is cross platform which is good...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@crykn I will report to GLFW. The setup is not too crazy – virtualbox + linux (Arch). In any case I’m glad you worked around it. There is always a chance that GLFW will return wrong data.
@Timvisser1994 That won’t help with the original problem, which is a density of Infinity. Infinity wouldn’t be considered
<= 0
andisNaN()
returns false. NaN is an oddity, but looking at the OpenJDK source code gives a solution:The important thing here is that all valid finite floats are
<= Float.MAX_VALUE
(NaN is not, so it gets ruled out), and we only want positive and non-zero floats, so the check for> 0
also rules out negative infinity. The check against MAX_VALUE is used by isFinite() in Java 8. I changedgetDensity()
to single-return form because I think some JDKs can analyze that form a little easier, or at least it won’t be harder.There’s still the issue that an invalid density is now indistinguishable from a common one,
1f
. I think in the cases where we would get an invalid density, the display density doesn’t really matter as long as it doesn’t cause problems, so1f
is probably fine; users could check on their own ifgetPpiX()
returns a value they can handle, also.