mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 02:38:53 +01:00
graphics: Refactoring
This commit is contained in:
parent
375d71e085
commit
93f052de8c
@ -132,18 +132,18 @@ func (c *graphicsContext) drawToDefaultRenderTarget(context *opengl.Context) err
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *graphicsContext) UpdateAndDraw() error {
|
||||
func (c *graphicsContext) UpdateAndDraw(updateCount int) error {
|
||||
if err := c.initializeIfNeeded(ui.GLContext()); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.offscreen.Clear(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := c.f(c.offscreen); err != nil {
|
||||
return err
|
||||
}
|
||||
if IsRunningSlowly() {
|
||||
return nil
|
||||
for i := 0; i < updateCount; i++ {
|
||||
if err := c.offscreen.Clear(); err != nil {
|
||||
return err
|
||||
}
|
||||
setRunningSlowly(i < updateCount-1)
|
||||
if err := c.f(c.offscreen); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := c.offscreen2.Clear(); err != nil {
|
||||
return err
|
||||
|
@ -30,10 +30,6 @@ func IsRunning() bool {
|
||||
return currentRunContext.isRunning()
|
||||
}
|
||||
|
||||
func IsRunningSlowly() bool {
|
||||
return currentRunContext.isRunningSlowly()
|
||||
}
|
||||
|
||||
type runContext struct {
|
||||
running bool
|
||||
fps int
|
||||
@ -81,25 +77,9 @@ func (c *runContext) updateFPS(fps float64) {
|
||||
c.currentFPS = fps
|
||||
}
|
||||
|
||||
func (c *runContext) isRunningSlowly() bool {
|
||||
c.m.RLock()
|
||||
defer c.m.RUnlock()
|
||||
if !c.running {
|
||||
// TODO: Should panic here?
|
||||
return false
|
||||
}
|
||||
return c.runningSlowly
|
||||
}
|
||||
|
||||
func (c *runContext) setRunningSlowly(isRunningSlowly bool) {
|
||||
c.m.Lock()
|
||||
defer c.m.Unlock()
|
||||
c.runningSlowly = isRunningSlowly
|
||||
}
|
||||
|
||||
type GraphicsContext interface {
|
||||
SetSize(width, height int, scale float64) error
|
||||
UpdateAndDraw() error
|
||||
UpdateAndDraw(updateCount int) error
|
||||
Draw() error
|
||||
}
|
||||
|
||||
@ -161,7 +141,6 @@ func (c *runContext) render(g GraphicsContext) error {
|
||||
|
||||
// If lastUpdated is too old, we assume that screen is not shown.
|
||||
if 5*int64(time.Second)/int64(fps) < n-c.lastUpdated {
|
||||
c.setRunningSlowly(false)
|
||||
c.lastUpdated = n
|
||||
return nil
|
||||
}
|
||||
@ -176,12 +155,8 @@ func (c *runContext) render(g GraphicsContext) error {
|
||||
tt = 1
|
||||
}
|
||||
if 1 <= tt {
|
||||
for i := 0; i < tt; i++ {
|
||||
slow := i < tt-1
|
||||
c.setRunningSlowly(slow)
|
||||
if err := g.UpdateAndDraw(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := g.UpdateAndDraw(tt); err != nil {
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := g.Draw(); err != nil {
|
||||
|
16
run.go
16
run.go
@ -15,6 +15,8 @@
|
||||
package ebiten
|
||||
|
||||
import (
|
||||
"sync/atomic"
|
||||
|
||||
"github.com/hajimehoshi/ebiten/internal/loop"
|
||||
"github.com/hajimehoshi/ebiten/internal/ui"
|
||||
)
|
||||
@ -34,13 +36,25 @@ func CurrentFPS() float64 {
|
||||
return loop.CurrentFPS()
|
||||
}
|
||||
|
||||
var (
|
||||
isRunningSlowly = int32(0)
|
||||
)
|
||||
|
||||
func setRunningSlowly(slow bool) {
|
||||
v := int32(0)
|
||||
if slow {
|
||||
v = 1
|
||||
}
|
||||
atomic.StoreInt32(&isRunningSlowly, v)
|
||||
}
|
||||
|
||||
// IsRunningSlowly returns true if the game is running too slowly to keep 60 FPS of rendering.
|
||||
// The game screen is not updated when IsRunningSlowly is true.
|
||||
// It is recommended to skip heavy processing, especially drawing, when IsRunningSlowly is true.
|
||||
//
|
||||
// This function is concurrent-safe.
|
||||
func IsRunningSlowly() bool {
|
||||
return loop.IsRunningSlowly()
|
||||
return atomic.LoadInt32(&isRunningSlowly) != 0
|
||||
}
|
||||
|
||||
// Run runs the game.
|
||||
|
Loading…
Reference in New Issue
Block a user