mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
ui: Recalc device scale repeatedly for multiple displays (#644)
This commit is contained in:
parent
26731f1ee2
commit
bf850e12a4
@ -19,15 +19,12 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
scale = 0.0
|
||||
m sync.Mutex
|
||||
m sync.Mutex
|
||||
)
|
||||
|
||||
func DeviceScale() float64 {
|
||||
m.Lock()
|
||||
defer m.Unlock()
|
||||
if scale == 0.0 {
|
||||
scale = impl()
|
||||
}
|
||||
return scale
|
||||
// TODO: Cache this value (again) for mobile platforms.
|
||||
return impl()
|
||||
}
|
||||
|
@ -51,6 +51,10 @@ type userInterface struct {
|
||||
runnableInBackground bool
|
||||
vsync bool
|
||||
|
||||
deviceScale float64
|
||||
deviceScaleUpdated int64
|
||||
lastActualScale float64
|
||||
|
||||
initFullscreen bool
|
||||
initCursorVisible bool
|
||||
initWindowDecorated bool
|
||||
@ -540,7 +544,13 @@ func (u *userInterface) getScale() float64 {
|
||||
|
||||
// actualScreenScale must be called from the main thread.
|
||||
func (u *userInterface) actualScreenScale() float64 {
|
||||
return u.getScale() * devicescale.DeviceScale()
|
||||
n := time.Now().UnixNano()
|
||||
// As devicescale.DeviceScale accesses OS API, not call this too often.
|
||||
if u.deviceScale == 0 || n-u.deviceScaleUpdated < int64(time.Second/2) {
|
||||
u.deviceScale = devicescale.DeviceScale()
|
||||
u.deviceScaleUpdated = n
|
||||
}
|
||||
return u.getScale() * u.deviceScale
|
||||
}
|
||||
|
||||
// pollEvents must be called from the main thread.
|
||||
@ -554,11 +564,14 @@ func (u *userInterface) updateGraphicsContext(g GraphicsContext) {
|
||||
sizeChanged := false
|
||||
// TODO: Is it possible to reduce 'runOnMainThread' calls?
|
||||
_ = u.runOnMainThread(func() error {
|
||||
if !u.toChangeSize {
|
||||
actualScale = u.actualScreenScale()
|
||||
|
||||
if !u.toChangeSize && u.lastActualScale == actualScale {
|
||||
return nil
|
||||
}
|
||||
|
||||
u.lastActualScale = actualScale
|
||||
u.toChangeSize = false
|
||||
actualScale = u.actualScreenScale()
|
||||
sizeChanged = true
|
||||
return nil
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user