mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-11-10 04:57:26 +01:00
Refactoring
This commit is contained in:
parent
6b1e76b669
commit
a8f42d6497
47
ebiten.go
47
ebiten.go
@ -30,34 +30,39 @@ type InputState struct {
|
||||
Y int
|
||||
}
|
||||
|
||||
func mainLoop(game Game, input <-chan InputState,
|
||||
deviceUpdate chan bool,
|
||||
gameDraw chan func(graphics.GraphicsContext, graphics.Texture)) {
|
||||
frameTime := time.Duration(int64(time.Second) / int64(game.Fps()))
|
||||
updateTick := time.Tick(frameTime)
|
||||
for {
|
||||
select {
|
||||
case <-updateTick:
|
||||
inputState := <-input
|
||||
game.Update(inputState)
|
||||
case <-deviceUpdate:
|
||||
ch := make(chan interface{})
|
||||
gameDraw <- func(g graphics.GraphicsContext,
|
||||
offscreen graphics.Texture) {
|
||||
game.Draw(g, offscreen)
|
||||
close(ch)
|
||||
}
|
||||
<-ch
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func OpenGLRun(game Game, ui UI, screenScale int, input <-chan InputState) {
|
||||
deviceUpdate := make(chan bool)
|
||||
commandSet := make(chan chan func(graphics.GraphicsContext))
|
||||
gameDraw := make(chan func(graphics.GraphicsContext, graphics.Texture))
|
||||
|
||||
graphicsDevice := opengl.NewDevice(
|
||||
game.ScreenWidth(), game.ScreenHeight(), screenScale, deviceUpdate, commandSet)
|
||||
game.ScreenWidth(), game.ScreenHeight(),
|
||||
screenScale, deviceUpdate, gameDraw)
|
||||
|
||||
game.Init(graphicsDevice.TextureFactory())
|
||||
|
||||
go func() {
|
||||
frameTime := time.Duration(int64(time.Second) / int64(game.Fps()))
|
||||
updateTick := time.Tick(frameTime)
|
||||
for {
|
||||
select {
|
||||
case <-updateTick:
|
||||
inputState := <-input
|
||||
game.Update(inputState)
|
||||
case <-deviceUpdate:
|
||||
commands := make(chan func(graphics.GraphicsContext))
|
||||
commandSet <- commands
|
||||
g := graphics.NewAsyncGraphicsContext(commands)
|
||||
// TODO: graphicsDevice is shared by multiple goroutines.
|
||||
game.Draw(g, graphicsDevice.OffscreenTexture())
|
||||
close(commands)
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
go mainLoop(game, input, deviceUpdate, gameDraw)
|
||||
|
||||
// UI should be executed on the main thread.
|
||||
ui.Run(graphicsDevice)
|
||||
|
@ -1,57 +0,0 @@
|
||||
package graphics
|
||||
|
||||
import (
|
||||
"github.com/hajimehoshi/go.ebiten/graphics/matrix"
|
||||
"image/color"
|
||||
)
|
||||
|
||||
type AsyncGraphicsContext struct {
|
||||
ch chan<- func(GraphicsContext)
|
||||
}
|
||||
|
||||
func NewAsyncGraphicsContext(ch chan<- func(GraphicsContext)) *AsyncGraphicsContext {
|
||||
return &AsyncGraphicsContext{
|
||||
ch: ch,
|
||||
}
|
||||
}
|
||||
|
||||
func (g *AsyncGraphicsContext) Clear() {
|
||||
g.ch <- func(g2 GraphicsContext) {
|
||||
g2.Clear()
|
||||
}
|
||||
}
|
||||
|
||||
func (g *AsyncGraphicsContext) Fill(clr color.Color) {
|
||||
g.ch <- func(g2 GraphicsContext) {
|
||||
g2.Fill(clr)
|
||||
}
|
||||
}
|
||||
|
||||
func (g *AsyncGraphicsContext) DrawRect(rect Rect, clr color.Color) {
|
||||
g.ch <- func(g2 GraphicsContext) {
|
||||
g2.DrawRect(rect, clr)
|
||||
}
|
||||
}
|
||||
|
||||
func (g *AsyncGraphicsContext) DrawTexture(textureID TextureID,
|
||||
geometryMatrix matrix.Geometry,
|
||||
colorMatrix matrix.Color) {
|
||||
g.ch <- func(g2 GraphicsContext) {
|
||||
g2.DrawTexture(textureID, geometryMatrix, colorMatrix)
|
||||
}
|
||||
}
|
||||
|
||||
func (g *AsyncGraphicsContext) DrawTextureParts(textureID TextureID,
|
||||
locations []TexturePart,
|
||||
geometryMatrix matrix.Geometry,
|
||||
colorMatrix matrix.Color) {
|
||||
g.ch <- func(g2 GraphicsContext) {
|
||||
g2.DrawTextureParts(textureID, locations, geometryMatrix, colorMatrix)
|
||||
}
|
||||
}
|
||||
|
||||
func (g *AsyncGraphicsContext) SetOffscreen(textureID TextureID) {
|
||||
g.ch <- func(g2 GraphicsContext) {
|
||||
g2.SetOffscreen(textureID)
|
||||
}
|
||||
}
|
@ -17,12 +17,13 @@ type Device struct {
|
||||
graphicsContext *GraphicsContext
|
||||
offscreenTexture graphics.Texture
|
||||
deviceUpdate chan<- bool
|
||||
commandSet <-chan chan func(graphics.GraphicsContext)
|
||||
gameDraw <-chan func(graphics.GraphicsContext, graphics.Texture)
|
||||
}
|
||||
|
||||
func NewDevice(screenWidth, screenHeight, screenScale int,
|
||||
deviceUpdate chan<- bool,
|
||||
commandSet <-chan chan func(graphics.GraphicsContext)) *Device {
|
||||
gameDraw <-chan func(graphics.GraphicsContext, graphics.Texture)) *Device {
|
||||
|
||||
graphicsContext := newGraphicsContext(screenWidth, screenHeight, screenScale)
|
||||
|
||||
device := &Device{
|
||||
@ -30,7 +31,7 @@ func NewDevice(screenWidth, screenHeight, screenScale int,
|
||||
screenHeight: screenHeight,
|
||||
screenScale: screenScale,
|
||||
deviceUpdate: deviceUpdate,
|
||||
commandSet: commandSet,
|
||||
gameDraw: gameDraw,
|
||||
graphicsContext: graphicsContext,
|
||||
}
|
||||
device.offscreenTexture =
|
||||
@ -51,10 +52,8 @@ func (device *Device) Update() {
|
||||
g.Clear()
|
||||
|
||||
device.deviceUpdate <- true
|
||||
commands := <-device.commandSet
|
||||
for command := range commands {
|
||||
command(g)
|
||||
}
|
||||
f := <-device.gameDraw
|
||||
f(g, device.offscreenTexture)
|
||||
|
||||
g.flush()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user