android: populate dpi_scale from DisplayMetrics.density#647
Open
benface wants to merge 1 commit into
Open
Conversation
e8bae76 to
ff92ef2
Compare
Upstream miniquad leaves dpi_scale at its 1.0 default on Android. As a result, macroquad's screen_width() / mouse_position() (which divide by dpi_scale to return density-independent units) return physical-pixel values instead of dp — so the same code that lays out UI correctly on macOS Retina has to special-case Android. This change queries the activity's Resources.getDisplayMetrics().density via JNI when init_with_activity sets up NativeDisplayData and again on every SurfaceChanged. With dpi_scale set, callers get consistent dp units across iOS / Android / macOS / Windows. query_display_density() is fail-soft — every nullable JNI lookup falls back to 1.0, matching upstream's behaviour for apps that don't yet expect a non-1 dpi_scale on Android.
ff92ef2 to
bccf70b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Upstream leaves
NativeDisplayData::dpi_scaleat1.0on Android. macroquad'sscreen_width()/mouse_position()divide bydpi_scaleto return density-independent units, so the same code that returns dp on a Retina Mac returns physical pixels on Android — UI laid out in dp lands at the wrong screen position, and apps have to special-case Android to compensate.This populates
dpi_scalefromResources.getDisplayMetrics().density(mdpi = 1.0, xhdpi = 2.0, etc.) via JNI:init_with_activitywhenNativeDisplayDatais first constructedSurfaceChanged(covers device density changes the OS may push at runtime)query_display_density()is fail-soft — every nullable JNI lookup falls back to1.0, preserving upstream behaviour for apps that don't expect a non-1dpi_scaleon Android.Test plan
density = 2.625):screen_width()now reports ≈ 411 dp instead of ≈ 1080 px; touch coords land on dp-positioned UI without per-platform conversion.cargo check --target aarch64-linux-androidclean (no new warnings).