question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Gdx.graphics.getDensity() returns infinity inside VirtualBox

See original GitHub issue

Issue 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:closed
  • Created 2 years ago
  • Reactions:2
  • Comments:10 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
ichernevcommented, Jun 9, 2021

@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.

1reaction
tommyettingercommented, May 22, 2021

@Timvisser1994 That won’t help with the original problem, which is a density of Infinity. Infinity wouldn’t be considered <= 0 and isNaN() returns false. NaN is an oddity, but looking at the OpenJDK source code gives a solution:

public float getDensity () {
	float ppiX = getPpiX();
	return (ppiX > 0 && ppiX <= Float.MAX_VALUE) ? ppiX / 160f : 1f;
}

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 changed getDensity() 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, so 1f is probably fine; users could check on their own if getPpiX() returns a value they can handle, also.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found