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 ( import (
"fmt" "fmt"
"sync"
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver" "github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
) )
@ -25,16 +24,12 @@ type gameForUI struct {
game Game game Game
offscreen *Image offscreen *Image
screen *Image screen *Image
m sync.Mutex
} }
var theGameForUI = &gameForUI{} func newGameForUI(game Game) *gameForUI {
return &gameForUI{
func (c *gameForUI) set(game Game) { game: game,
c.m.Lock() }
defer c.m.Unlock()
c.game = game
} }
func (c *gameForUI) Layout(outsideWidth, outsideHeight float64, deviceScaleFactor float64) (int, int) { 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) defer atomic.StoreInt32(&isRunGameEnded_, 1)
initializeWindowPositionIfNeeded(WindowSize()) initializeWindowPositionIfNeeded(WindowSize())
theGameForUI.set(&imageDumperGame{ g := newGameForUI(&imageDumperGame{
game: game, game: game,
}) })
if err := ui.Get().Run(theGameForUI); err != nil { if err := ui.Get().Run(g); err != nil {
if err == ui.RegularTermination { if err == ui.RegularTermination {
return nil return nil
} }

View File

@ -27,8 +27,7 @@ import (
// Ebiten users should NOT call RunGameWithoutMainLoop. // Ebiten users should NOT call RunGameWithoutMainLoop.
// Instead, functions in github.com/hajimehoshi/ebiten/v2/mobile package calls this. // 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) { func RunGameWithoutMainLoop(game Game) {
theGameForUI.set(game) ui.Get().RunWithoutMainLoop(newGameForUI(game))
ui.Get().RunWithoutMainLoop(theGameForUI)
} }