ebiten: Bug fix: AdjustPosition could return Inf

Before the initialization finishes, AdjustPosition could return Inf
values and in this case AdjustPosition's returning values don't make
sense. Let's return NaN in this case.

Closes #1545
This commit is contained in:
Hajime Hoshi 2021-03-27 18:40:39 +09:00
parent d415e9c771
commit 19702619ee
2 changed files with 11 additions and 1 deletions

View File

@ -19,6 +19,7 @@
package glfw
import (
"math"
"sync"
"unicode"
@ -307,7 +308,11 @@ func (i *Input) update(window *glfw.Window, context driver.UIContext) {
cx = fromGLFWMonitorPixel(cx, s)
cy = fromGLFWMonitorPixel(cy, s)
cx, cy = context.AdjustPosition(cx, cy, s)
i.cursorX, i.cursorY = int(cx), int(cy)
// AdjustPosition can return NaN at the initialization.
if !math.IsNaN(cx) && !math.IsNaN(cy) {
i.cursorX, i.cursorY = int(cx), int(cy)
}
for id := glfw.Joystick(0); id < glfw.Joystick(len(i.gamepads)); id++ {
i.gamepads[id].valid = false

View File

@ -235,5 +235,10 @@ func (c *uiContext) update(updateCount int) error {
func (c *uiContext) AdjustPosition(x, y float64, deviceScaleFactor float64) (float64, float64) {
ox, oy := c.offsets(deviceScaleFactor)
s := c.screenScale(deviceScaleFactor)
// The scale 0 indicates that the offscreen is not initialized yet.
// As any cursor values don't make sense, just return NaN.
if s == 0 {
return math.NaN(), math.NaN()
}
return (x*deviceScaleFactor - ox) / s, (y*deviceScaleFactor - oy) / s
}