mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 02:38:53 +01:00
internal/ui: rename types and members: uiContext -> gameForUI
This commit is contained in:
parent
29382b424b
commit
0c1c40995c
@ -21,7 +21,7 @@ import (
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/graphicsdriver"
|
||||
)
|
||||
|
||||
type uiContext struct {
|
||||
type gameForUI struct {
|
||||
game Game
|
||||
offscreen *Image
|
||||
screen *Image
|
||||
@ -29,15 +29,15 @@ type uiContext struct {
|
||||
m sync.Mutex
|
||||
}
|
||||
|
||||
var theUIContext = &uiContext{}
|
||||
var theGameForUI = &gameForUI{}
|
||||
|
||||
func (c *uiContext) set(game Game) {
|
||||
func (c *gameForUI) set(game Game) {
|
||||
c.m.Lock()
|
||||
defer c.m.Unlock()
|
||||
c.game = game
|
||||
}
|
||||
|
||||
func (c *uiContext) UpdateOffscreen(outsideWidth, outsideHeight float64, deviceScaleFactor float64) (int, int) {
|
||||
func (c *gameForUI) Layout(outsideWidth, outsideHeight float64, deviceScaleFactor float64) (int, int) {
|
||||
ow, oh := c.game.Layout(int(outsideWidth), int(outsideHeight))
|
||||
if ow <= 0 || oh <= 0 {
|
||||
panic("ebiten: Layout must return positive numbers")
|
||||
@ -74,7 +74,7 @@ func (c *uiContext) UpdateOffscreen(outsideWidth, outsideHeight float64, deviceS
|
||||
return ow, oh
|
||||
}
|
||||
|
||||
func (c *uiContext) setScreenClearedEveryFrame(cleared bool) {
|
||||
func (c *gameForUI) setScreenClearedEveryFrame(cleared bool) {
|
||||
c.m.Lock()
|
||||
defer c.m.Unlock()
|
||||
|
||||
@ -83,11 +83,11 @@ func (c *uiContext) setScreenClearedEveryFrame(cleared bool) {
|
||||
}
|
||||
}
|
||||
|
||||
func (c *uiContext) UpdateGame() error {
|
||||
func (c *gameForUI) Update() error {
|
||||
return c.game.Update()
|
||||
}
|
||||
|
||||
func (c *uiContext) DrawGame(screenScale float64, offsetX, offsetY float64, needsClearingScreen bool, framebufferYDirection graphicsdriver.YDirection) error {
|
||||
func (c *gameForUI) Draw(screenScale float64, offsetX, offsetY float64, needsClearingScreen bool, framebufferYDirection graphicsdriver.YDirection) error {
|
||||
// Even though updateCount == 0, the offscreen is cleared and Draw is called.
|
||||
// Draw should not update the game state and then the screen should not be updated without Update, but
|
||||
// users might want to process something at Draw with the time intervals of FPS.
|
@ -29,29 +29,29 @@ import (
|
||||
|
||||
const DefaultTPS = 60
|
||||
|
||||
type Context interface {
|
||||
UpdateOffscreen(outsideWidth, outsideHeight float64, deviceScaleFactor float64) (int, int)
|
||||
UpdateGame() error
|
||||
DrawGame(screenScale float64, offsetX, offsetY float64, needsClearingScreen bool, framebufferYDirection graphicsdriver.YDirection) error
|
||||
type Game interface {
|
||||
Layout(outsideWidth, outsideHeight float64, deviceScaleFactor float64) (int, int)
|
||||
Update() error
|
||||
Draw(screenScale float64, offsetX, offsetY float64, needsClearingScreen bool, framebufferYDirection graphicsdriver.YDirection) error
|
||||
}
|
||||
|
||||
type contextImpl struct {
|
||||
context Context
|
||||
game Game
|
||||
|
||||
updateCalled bool
|
||||
|
||||
// The following members must be protected by the mutex m.
|
||||
outsideWidth float64
|
||||
outsideHeight float64
|
||||
offscreenWidth int
|
||||
offscreenHeight int
|
||||
outsideWidth float64
|
||||
outsideHeight float64
|
||||
screenWidth int
|
||||
screenHeight int
|
||||
|
||||
m sync.Mutex
|
||||
}
|
||||
|
||||
func newContextImpl(context Context) *contextImpl {
|
||||
func newContextImpl(game Game) *contextImpl {
|
||||
return &contextImpl{
|
||||
context: context,
|
||||
game: game,
|
||||
}
|
||||
}
|
||||
|
||||
@ -70,7 +70,7 @@ func (c *contextImpl) updateFrameImpl(updateCount int, deviceScaleFactor float64
|
||||
}
|
||||
|
||||
// ForceUpdate can be invoked even if the context is not initialized yet (#1591).
|
||||
if w, h := c.updateOffscreenSize(deviceScaleFactor); w == 0 || h == 0 {
|
||||
if w, h := c.layoutGame(deviceScaleFactor); w == 0 || h == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -92,7 +92,7 @@ func (c *contextImpl) updateFrameImpl(updateCount int, deviceScaleFactor float64
|
||||
if err := hooks.RunBeforeUpdateHooks(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.context.UpdateGame(); err != nil {
|
||||
if err := c.game.Update(); err != nil {
|
||||
return err
|
||||
}
|
||||
Get().resetForTick()
|
||||
@ -100,7 +100,7 @@ func (c *contextImpl) updateFrameImpl(updateCount int, deviceScaleFactor float64
|
||||
|
||||
// Draw the game.
|
||||
screenScale, offsetX, offsetY := c.screenScaleAndOffsets(deviceScaleFactor)
|
||||
if err := c.context.DrawGame(screenScale, offsetX, offsetY, graphics().NeedsClearingScreen(), graphics().FramebufferYDirection()); err != nil {
|
||||
if err := c.game.Draw(screenScale, offsetX, offsetY, graphics().NeedsClearingScreen(), graphics().FramebufferYDirection()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@ -114,13 +114,13 @@ func (c *contextImpl) updateFrameImpl(updateCount int, deviceScaleFactor float64
|
||||
})
|
||||
}
|
||||
|
||||
func (c *contextImpl) updateOffscreenSize(deviceScaleFactor float64) (int, int) {
|
||||
func (c *contextImpl) layoutGame(deviceScaleFactor float64) (int, int) {
|
||||
c.m.Lock()
|
||||
defer c.m.Unlock()
|
||||
|
||||
w, h := c.context.UpdateOffscreen(c.outsideWidth, c.outsideHeight, deviceScaleFactor)
|
||||
c.offscreenWidth = w
|
||||
c.offscreenHeight = h
|
||||
w, h := c.game.Layout(c.outsideWidth, c.outsideHeight, deviceScaleFactor)
|
||||
c.screenWidth = w
|
||||
c.screenHeight = h
|
||||
return w, h
|
||||
}
|
||||
|
||||
@ -139,7 +139,7 @@ func (c *contextImpl) layout(outsideWidth, outsideHeight float64) {
|
||||
|
||||
func (c *contextImpl) adjustPosition(x, y float64, deviceScaleFactor float64) (float64, float64) {
|
||||
s, ox, oy := c.screenScaleAndOffsets(deviceScaleFactor)
|
||||
// The scale 0 indicates that the offscreen is not initialized yet.
|
||||
// The scale 0 indicates that the screen is not initialized yet.
|
||||
// As any cursor values don't make sense, just return NaN.
|
||||
if s == 0 {
|
||||
return math.NaN(), math.NaN()
|
||||
@ -151,15 +151,15 @@ func (c *contextImpl) screenScaleAndOffsets(deviceScaleFactor float64) (float64,
|
||||
c.m.Lock()
|
||||
defer c.m.Unlock()
|
||||
|
||||
if c.offscreenWidth == 0 || c.offscreenHeight == 0 {
|
||||
if c.screenWidth == 0 || c.screenHeight == 0 {
|
||||
return 0, 0, 0
|
||||
}
|
||||
|
||||
scaleX := c.outsideWidth / float64(c.offscreenWidth) * deviceScaleFactor
|
||||
scaleY := c.outsideHeight / float64(c.offscreenHeight) * deviceScaleFactor
|
||||
scaleX := c.outsideWidth / float64(c.screenWidth) * deviceScaleFactor
|
||||
scaleY := c.outsideHeight / float64(c.screenHeight) * deviceScaleFactor
|
||||
scale := math.Min(scaleX, scaleY)
|
||||
width := float64(c.offscreenWidth) * scale
|
||||
height := float64(c.offscreenHeight) * scale
|
||||
width := float64(c.screenWidth) * scale
|
||||
height := float64(c.screenHeight) * scale
|
||||
x := (c.outsideWidth*deviceScaleFactor - width) / 2
|
||||
y := (c.outsideHeight*deviceScaleFactor - height) / 2
|
||||
return scale, x, y
|
||||
|
@ -22,8 +22,8 @@ import (
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/thread"
|
||||
)
|
||||
|
||||
func (u *UserInterface) Run(context Context) error {
|
||||
u.context = newContextImpl(context)
|
||||
func (u *UserInterface) Run(game Game) error {
|
||||
u.context = newContextImpl(game)
|
||||
|
||||
// Initialize the main thread first so the thread is available at u.run (#809).
|
||||
u.t = thread.NewOSThread()
|
||||
|
@ -22,8 +22,8 @@ import (
|
||||
"github.com/hajimehoshi/ebiten/v2/internal/thread"
|
||||
)
|
||||
|
||||
func (u *UserInterface) Run(context Context) error {
|
||||
u.context = newContextImpl(context)
|
||||
func (u *UserInterface) Run(game Game) error {
|
||||
u.context = newContextImpl(game)
|
||||
|
||||
// Initialize the main thread first so the thread is available at u.run (#809).
|
||||
u.t = thread.NewNoopThread()
|
||||
|
@ -40,8 +40,8 @@ func Get() *UserInterface {
|
||||
return &theUserInterface
|
||||
}
|
||||
|
||||
func (u *UserInterface) Run(context Context) error {
|
||||
u.context = newContextImpl(context)
|
||||
func (u *UserInterface) Run(game Game) error {
|
||||
u.context = newContextImpl(game)
|
||||
cbackend.InitializeGame()
|
||||
for {
|
||||
w, h := cbackend.ScreenSize()
|
||||
@ -58,7 +58,7 @@ func (u *UserInterface) Run(context Context) error {
|
||||
}
|
||||
}
|
||||
|
||||
func (*UserInterface) RunWithoutMainLoop(context Context) {
|
||||
func (*UserInterface) RunWithoutMainLoop(game Game) {
|
||||
panic("ui: RunWithoutMainLoop is not implemented")
|
||||
}
|
||||
|
||||
|
@ -642,7 +642,7 @@ func init() {
|
||||
runtime.LockOSThread()
|
||||
}
|
||||
|
||||
func (u *UserInterface) RunWithoutMainLoop(context Context) {
|
||||
func (u *UserInterface) RunWithoutMainLoop(game Game) {
|
||||
panic("ui: RunWithoutMainLoop is not implemented")
|
||||
}
|
||||
|
||||
|
@ -314,8 +314,8 @@ func (u *UserInterface) needsUpdate() bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (u *UserInterface) loop(context Context) <-chan error {
|
||||
u.context = newContextImpl(context)
|
||||
func (u *UserInterface) loop(game Game) <-chan error {
|
||||
u.context = newContextImpl(game)
|
||||
|
||||
errCh := make(chan error, 1)
|
||||
reqStopAudioCh := make(chan struct{})
|
||||
@ -587,7 +587,7 @@ func (u *UserInterface) forceUpdateOnMinimumFPSMode() {
|
||||
u.updateImpl(true)
|
||||
}
|
||||
|
||||
func (u *UserInterface) Run(context Context) error {
|
||||
func (u *UserInterface) Run(game Game) error {
|
||||
if u.initFocused && window.Truthy() {
|
||||
// Do not focus the canvas when the current document is in an iframe.
|
||||
// Otherwise, the parent page tries to focus the iframe on every loading, which is annoying (#1373).
|
||||
@ -597,10 +597,10 @@ func (u *UserInterface) Run(context Context) error {
|
||||
}
|
||||
}
|
||||
u.running = true
|
||||
return <-u.loop(context)
|
||||
return <-u.loop(game)
|
||||
}
|
||||
|
||||
func (u *UserInterface) RunWithoutMainLoop(context Context) {
|
||||
func (u *UserInterface) RunWithoutMainLoop(game Game) {
|
||||
panic("ui: RunWithoutMainLoop is not implemented")
|
||||
}
|
||||
|
||||
|
@ -238,10 +238,10 @@ func (u *UserInterface) SetForeground(foreground bool) error {
|
||||
}
|
||||
}
|
||||
|
||||
func (u *UserInterface) Run(context Context) error {
|
||||
func (u *UserInterface) Run(game Game) error {
|
||||
u.setGBuildSizeCh = make(chan struct{})
|
||||
go func() {
|
||||
if err := u.run(context, true); err != nil {
|
||||
if err := u.run(game, true); err != nil {
|
||||
// As mobile apps never ends, Loop can't return. Just panic here.
|
||||
panic(err)
|
||||
}
|
||||
@ -250,15 +250,15 @@ func (u *UserInterface) Run(context Context) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *UserInterface) RunWithoutMainLoop(context Context) {
|
||||
func (u *UserInterface) RunWithoutMainLoop(game Game) {
|
||||
go func() {
|
||||
if err := u.run(context, false); err != nil {
|
||||
if err := u.run(game, false); err != nil {
|
||||
u.errCh <- err
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func (u *UserInterface) run(context Context, mainloop bool) (err error) {
|
||||
func (u *UserInterface) run(game Game, mainloop bool) (err error) {
|
||||
// Convert the panic to a regular error so that Java/Objective-C layer can treat this easily e.g., for
|
||||
// Crashlytics. A panic is treated as SIGABRT, and there is no way to handle this on Java/Objective-C layer
|
||||
// unfortunately.
|
||||
@ -273,7 +273,7 @@ func (u *UserInterface) run(context Context, mainloop bool) (err error) {
|
||||
u.sizeChanged = true
|
||||
u.m.Unlock()
|
||||
|
||||
u.context = newContextImpl(context)
|
||||
u.context = newContextImpl(game)
|
||||
|
||||
if mainloop {
|
||||
// When mainloop is true, gomobile-build is used. In this case, GL functions must be called via
|
||||
|
6
run.go
6
run.go
@ -91,7 +91,7 @@ func SetScreenClearedEveryFrame(cleared bool) {
|
||||
v = 1
|
||||
}
|
||||
atomic.StoreInt32(&isScreenClearedEveryFrame, v)
|
||||
theUIContext.setScreenClearedEveryFrame(cleared)
|
||||
theGameForUI.setScreenClearedEveryFrame(cleared)
|
||||
}
|
||||
|
||||
// IsScreenClearedEveryFrame returns true if the frame isn't cleared at the beginning.
|
||||
@ -156,10 +156,10 @@ func RunGame(game Game) error {
|
||||
defer atomic.StoreInt32(&isRunGameEnded_, 1)
|
||||
|
||||
initializeWindowPositionIfNeeded(WindowSize())
|
||||
theUIContext.set(&imageDumperGame{
|
||||
theGameForUI.set(&imageDumperGame{
|
||||
game: game,
|
||||
})
|
||||
if err := ui.Get().Run(theUIContext); err != nil {
|
||||
if err := ui.Get().Run(theGameForUI); err != nil {
|
||||
if err == ui.RegularTermination {
|
||||
return nil
|
||||
}
|
||||
|
@ -29,6 +29,6 @@ import (
|
||||
//
|
||||
// TODO: Remove this. In order to remove this, the uiContext should be in another package.
|
||||
func RunGameWithoutMainLoop(game Game) {
|
||||
theUIContext.set(game)
|
||||
ui.Get().RunWithoutMainLoop(theUIContext)
|
||||
theGameForUI.set(game)
|
||||
ui.Get().RunWithoutMainLoop(theGameForUI)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user