mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +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
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *graphicsContext) UpdateAndDraw() error {
|
func (c *graphicsContext) UpdateAndDraw(updateCount int) error {
|
||||||
if err := c.initializeIfNeeded(ui.GLContext()); err != nil {
|
if err := c.initializeIfNeeded(ui.GLContext()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
for i := 0; i < updateCount; i++ {
|
||||||
if err := c.offscreen.Clear(); err != nil {
|
if err := c.offscreen.Clear(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
setRunningSlowly(i < updateCount-1)
|
||||||
if err := c.f(c.offscreen); err != nil {
|
if err := c.f(c.offscreen); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if IsRunningSlowly() {
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
if err := c.offscreen2.Clear(); err != nil {
|
if err := c.offscreen2.Clear(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -30,10 +30,6 @@ func IsRunning() bool {
|
|||||||
return currentRunContext.isRunning()
|
return currentRunContext.isRunning()
|
||||||
}
|
}
|
||||||
|
|
||||||
func IsRunningSlowly() bool {
|
|
||||||
return currentRunContext.isRunningSlowly()
|
|
||||||
}
|
|
||||||
|
|
||||||
type runContext struct {
|
type runContext struct {
|
||||||
running bool
|
running bool
|
||||||
fps int
|
fps int
|
||||||
@ -81,25 +77,9 @@ func (c *runContext) updateFPS(fps float64) {
|
|||||||
c.currentFPS = fps
|
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 {
|
type GraphicsContext interface {
|
||||||
SetSize(width, height int, scale float64) error
|
SetSize(width, height int, scale float64) error
|
||||||
UpdateAndDraw() error
|
UpdateAndDraw(updateCount int) error
|
||||||
Draw() 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 lastUpdated is too old, we assume that screen is not shown.
|
||||||
if 5*int64(time.Second)/int64(fps) < n-c.lastUpdated {
|
if 5*int64(time.Second)/int64(fps) < n-c.lastUpdated {
|
||||||
c.setRunningSlowly(false)
|
|
||||||
c.lastUpdated = n
|
c.lastUpdated = n
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@ -176,13 +155,9 @@ func (c *runContext) render(g GraphicsContext) error {
|
|||||||
tt = 1
|
tt = 1
|
||||||
}
|
}
|
||||||
if 1 <= tt {
|
if 1 <= tt {
|
||||||
for i := 0; i < tt; i++ {
|
if err := g.UpdateAndDraw(tt); err != nil {
|
||||||
slow := i < tt-1
|
|
||||||
c.setRunningSlowly(slow)
|
|
||||||
if err := g.UpdateAndDraw(); err != nil {
|
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if err := g.Draw(); err != nil {
|
if err := g.Draw(); err != nil {
|
||||||
return err
|
return err
|
||||||
|
16
run.go
16
run.go
@ -15,6 +15,8 @@
|
|||||||
package ebiten
|
package ebiten
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/internal/loop"
|
"github.com/hajimehoshi/ebiten/internal/loop"
|
||||||
"github.com/hajimehoshi/ebiten/internal/ui"
|
"github.com/hajimehoshi/ebiten/internal/ui"
|
||||||
)
|
)
|
||||||
@ -34,13 +36,25 @@ func CurrentFPS() float64 {
|
|||||||
return loop.CurrentFPS()
|
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.
|
// 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.
|
// The game screen is not updated when IsRunningSlowly is true.
|
||||||
// It is recommended to skip heavy processing, especially drawing, when IsRunningSlowly is true.
|
// It is recommended to skip heavy processing, especially drawing, when IsRunningSlowly is true.
|
||||||
//
|
//
|
||||||
// This function is concurrent-safe.
|
// This function is concurrent-safe.
|
||||||
func IsRunningSlowly() bool {
|
func IsRunningSlowly() bool {
|
||||||
return loop.IsRunningSlowly()
|
return atomic.LoadInt32(&isRunningSlowly) != 0
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run runs the game.
|
// Run runs the game.
|
||||||
|
Loading…
Reference in New Issue
Block a user