ebiten: refactoring: remove the global variable theGameForUI

This commit is contained in:
Hajime Hoshi 2022-02-13 20:47:32 +09:00
parent 82b9f7dc56
commit 0529fa955e
3 changed files with 8 additions and 14 deletions

View File

@ -16,7 +16,6 @@ package ebiten
import (
"fmt"
"sync"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
)
@ -25,16 +24,12 @@ type gameForUI struct {
game Game
offscreen *Image
screen *Image
m sync.Mutex
}
var theGameForUI = &gameForUI{}
func (c *gameForUI) set(game Game) {
c.m.Lock()
defer c.m.Unlock()
c.game = game
func newGameForUI(game Game) *gameForUI {
return &gameForUI{
game: game,
}
}
func (c *gameForUI) Layout(outsideWidth, outsideHeight float64, deviceScaleFactor float64) (int, int) {

4
run.go
View File

@ -150,10 +150,10 @@ func RunGame(game Game) error {
defer atomic.StoreInt32(&isRunGameEnded_, 1)
initializeWindowPositionIfNeeded(WindowSize())
theGameForUI.set(&imageDumperGame{
g := newGameForUI(&imageDumperGame{
game: game,
})
if err := ui.Get().Run(theGameForUI); err != nil {
if err := ui.Get().Run(g); err != nil {
if err == ui.RegularTermination {
return nil
}

View File

@ -27,8 +27,7 @@ import (
// Ebiten users should NOT call RunGameWithoutMainLoop.
// Instead, functions in github.com/hajimehoshi/ebiten/v2/mobile package calls this.
//
// TODO: Remove this. In order to remove this, the uiContext should be in another package.
// TODO: Remove this. In order to remove this, the gameForUI should be in another package.
func RunGameWithoutMainLoop(game Game) {
theGameForUI.set(game)
ui.Get().RunWithoutMainLoop(theGameForUI)
ui.Get().RunWithoutMainLoop(newGameForUI(game))
}