ebiten: Refactoring: Call (*Window).SetPosition even before the run loop

This commit is contained in:
Hajime Hoshi 2021-04-17 17:19:36 +09:00
parent deba352384
commit 161b1965ba

View File

@ -16,7 +16,7 @@ package ebiten
import ( import (
"image" "image"
"sync" "sync/atomic"
) )
const ( const (
@ -121,9 +121,6 @@ func SetWindowIcon(iconImages []image.Image) {
// //
// WindowPosition is concurrent-safe. // WindowPosition is concurrent-safe.
func WindowPosition() (x, y int) { func WindowPosition() (x, y int) {
if x, y, ok := getInitWindowPosition(); ok {
return x, y
}
if w := uiDriver().Window(); w != nil { if w := uiDriver().Window(); w != nil {
return w.Position() return w.Position()
} }
@ -140,68 +137,27 @@ func WindowPosition() (x, y int) {
// //
// SetWindowPosition is concurrent-safe. // SetWindowPosition is concurrent-safe.
func SetWindowPosition(x, y int) { func SetWindowPosition(x, y int) {
if setInitWindowPosition(x, y) { atomic.StoreUint32(&windowPositionSetExplicitly, 1)
return
}
if w := uiDriver().Window(); w != nil { if w := uiDriver().Window(); w != nil {
w.SetPosition(x, y) w.SetPosition(x, y)
} }
} }
var ( var (
windowM sync.Mutex windowPositionSetExplicitly uint32
initWindowPosition = &struct {
x int
y int
}{
x: invalidPos,
y: invalidPos,
}
) )
func getInitWindowPosition() (x, y int, ok bool) {
windowM.Lock()
defer windowM.Unlock()
if initWindowPosition == nil {
return 0, 0, false
}
if initWindowPosition.x == invalidPos || initWindowPosition.y == invalidPos {
return 0, 0, false
}
return initWindowPosition.x, initWindowPosition.y, true
}
func setInitWindowPosition(x, y int) bool {
windowM.Lock()
defer windowM.Unlock()
if initWindowPosition == nil {
return false
}
initWindowPosition.x = x
initWindowPosition.y = y
return true
}
func fixWindowPosition(width, height int) { func fixWindowPosition(width, height int) {
windowM.Lock()
defer windowM.Unlock()
defer func() {
initWindowPosition = nil
}()
w := uiDriver().Window() w := uiDriver().Window()
if w == nil { if w == nil {
return return
} }
if initWindowPosition.x == invalidPos || initWindowPosition.y == invalidPos { if atomic.LoadUint32(&windowPositionSetExplicitly) == 0 {
sw, sh := uiDriver().ScreenSizeInFullscreen() sw, sh := uiDriver().ScreenSizeInFullscreen()
x := (sw - width) / 2 x := (sw - width) / 2
y := (sh - height) / 3 y := (sh - height) / 3
w.SetPosition(x, y) w.SetPosition(x, y)
} else {
w.SetPosition(initWindowPosition.x, initWindowPosition.y)
} }
} }