driver: Add UIDriver.Draw

This is a preparation for XNA.

Updates #1078
This commit is contained in:
Hajime Hoshi 2020-04-01 02:55:48 +09:00
parent ef5e917feb
commit a718ddbf7e
5 changed files with 33 additions and 6 deletions

View File

@ -21,6 +21,7 @@ import (
type UIContext interface {
Update(afterFrameUpdate func()) error
Draw() error
Layout(outsideWidth, outsideHeight float64)
AdjustPosition(x, y float64) (float64, float64)
}

View File

@ -813,6 +813,9 @@ func (u *UserInterface) update(context driver.UIContext) error {
}); err != nil {
return err
}
if err := context.Draw(); err != nil {
return err
}
// Update the screen size when the window is resizable.
var w, h int

View File

@ -172,6 +172,9 @@ func (u *UserInterface) update() error {
}); err != nil {
return err
}
if err := u.context.Draw(); err != nil {
return err
}
return nil
}

View File

@ -345,6 +345,9 @@ func (u *UserInterface) update(context driver.UIContext) error {
}); err != nil {
return err
}
if err := context.Draw(); err != nil {
return err
}
return nil
}

View File

@ -242,14 +242,27 @@ func (c *uiContext) Update(afterFrameUpdate func()) error {
if err := buffered.EndFrame(); err != nil {
return err
}
return nil
}
func (c *uiContext) Draw() error {
if err, ok := c.err.Load().(error); ok && err != nil {
return err
}
if err := buffered.BeginFrame(); err != nil {
return err
}
c.draw()
if err := buffered.EndFrame(); err != nil {
return err
}
return nil
}
func (c *uiContext) update(afterFrameUpdate func()) error {
updateCount := clock.Update(MaxTPS())
if game, ok := c.game.(interface{ Draw(*Image) }); ok {
if _, ok := c.game.(interface{ Draw(*Image) }); ok {
for i := 0; i < updateCount; i++ {
c.updateOffscreen()
@ -269,9 +282,6 @@ func (c *uiContext) update(afterFrameUpdate func()) error {
uiDriver().Input().ResetForFrame()
afterFrameUpdate()
}
c.offscreen.Clear()
game.Draw(c.offscreen)
} else {
for i := 0; i < updateCount; i++ {
c.updateOffscreen()
@ -290,10 +300,18 @@ func (c *uiContext) update(afterFrameUpdate func()) error {
afterFrameUpdate()
}
}
return nil
}
func (c *uiContext) draw() {
// c.screen might be nil when updateCount is 0 in the initial state (#1039).
if c.screen == nil {
return nil
return
}
if game, ok := c.game.(interface{ Draw(*Image) }); ok {
c.offscreen.Clear()
game.Draw(c.offscreen)
}
// This clear is needed for fullscreen mode or some mobile platforms (#622).
@ -326,7 +344,6 @@ func (c *uiContext) update(afterFrameUpdate func()) error {
op.Filter = FilterLinear
}
_ = c.screen.DrawImage(c.offscreen, op)
return nil
}
func (c *uiContext) AdjustPosition(x, y float64) (float64, float64) {