mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-12 20:18:59 +01:00
ui: Refactoring: Reduce global variables
This commit is contained in:
parent
4159c500bd
commit
6b12b02a05
31
window.go
31
window.go
@ -139,7 +139,7 @@ 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 := initWindowPosition(); ok {
|
if x, y, ok := getInitWindowPosition(); ok {
|
||||||
return x, y
|
return x, y
|
||||||
}
|
}
|
||||||
if w := uiDriver().Window(); w != nil {
|
if w := uiDriver().Window(); w != nil {
|
||||||
@ -166,30 +166,35 @@ func SetWindowPosition(x, y int) {
|
|||||||
|
|
||||||
var (
|
var (
|
||||||
windowM sync.Mutex
|
windowM sync.Mutex
|
||||||
mainLoopStarted bool
|
initWindowPosition = &struct {
|
||||||
initWindowPositionX = invalidPos
|
x int
|
||||||
initWindowPositionY = invalidPos
|
y int
|
||||||
|
}{
|
||||||
|
x: invalidPos,
|
||||||
|
y: invalidPos,
|
||||||
|
}
|
||||||
)
|
)
|
||||||
|
|
||||||
func initWindowPosition() (x, y int, ok bool) {
|
func getInitWindowPosition() (x, y int, ok bool) {
|
||||||
windowM.Lock()
|
windowM.Lock()
|
||||||
defer windowM.Unlock()
|
defer windowM.Unlock()
|
||||||
if mainLoopStarted {
|
if initWindowPosition == nil {
|
||||||
return 0, 0, false
|
return 0, 0, false
|
||||||
}
|
}
|
||||||
if initWindowPositionX == invalidPos || initWindowPositionY == invalidPos {
|
if initWindowPosition.x == invalidPos || initWindowPosition.y == invalidPos {
|
||||||
return 0, 0, false
|
return 0, 0, false
|
||||||
}
|
}
|
||||||
return initWindowPositionX, initWindowPositionY, true
|
return initWindowPosition.x, initWindowPosition.y, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func setInitWindowPosition(x, y int) bool {
|
func setInitWindowPosition(x, y int) bool {
|
||||||
windowM.Lock()
|
windowM.Lock()
|
||||||
defer windowM.Unlock()
|
defer windowM.Unlock()
|
||||||
if mainLoopStarted {
|
if initWindowPosition == nil {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
initWindowPositionX, initWindowPositionY = x, y
|
initWindowPosition.x = x
|
||||||
|
initWindowPosition.y = y
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -198,7 +203,7 @@ func fixWindowPosition(width, height int) {
|
|||||||
defer windowM.Unlock()
|
defer windowM.Unlock()
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
mainLoopStarted = true
|
initWindowPosition = nil
|
||||||
}()
|
}()
|
||||||
|
|
||||||
w := uiDriver().Window()
|
w := uiDriver().Window()
|
||||||
@ -206,14 +211,14 @@ func fixWindowPosition(width, height int) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if initWindowPositionX == invalidPos || initWindowPositionY == invalidPos {
|
if initWindowPosition.x == invalidPos || initWindowPosition.y == invalidPos {
|
||||||
mx, my := uiDriver().MonitorPosition()
|
mx, my := uiDriver().MonitorPosition()
|
||||||
sw, sh := uiDriver().ScreenSizeInFullscreen()
|
sw, sh := uiDriver().ScreenSizeInFullscreen()
|
||||||
x := mx + (sw-width)/2
|
x := mx + (sw-width)/2
|
||||||
y := my + (sh-height)/3
|
y := my + (sh-height)/3
|
||||||
w.SetPosition(x, y)
|
w.SetPosition(x, y)
|
||||||
} else {
|
} else {
|
||||||
w.SetPosition(initWindowPositionX, initWindowPositionY)
|
w.SetPosition(initWindowPosition.x, initWindowPosition.y)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user