Make CreateTexture/RenderTarget synchronous

This commit is contained in:
Hajime Hoshi 2014-05-03 17:50:42 +09:00
parent 4e6aeaa291
commit 61db71e453
4 changed files with 33 additions and 65 deletions

View File

@ -3,6 +3,7 @@ package blocks
import ( import (
"github.com/hajimehoshi/go-ebiten/graphics" "github.com/hajimehoshi/go-ebiten/graphics"
"github.com/hajimehoshi/go-ebiten/ui" "github.com/hajimehoshi/go-ebiten/ui"
_ "image/png"
) )
type Size struct { type Size struct {

View File

@ -3,7 +3,6 @@ package blocks
import ( import (
"github.com/hajimehoshi/go-ebiten/graphics" "github.com/hajimehoshi/go-ebiten/graphics"
"image" "image"
_ "image/png"
"os" "os"
"sync" "sync"
) )
@ -64,36 +63,30 @@ func (t *Textures) loop() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
ch := t.textureFactory.CreateTexture( id, err := t.textureFactory.CreateTexture(
img, img,
graphics.FilterNearest) graphics.FilterNearest)
go func() { if err != nil {
e := <-ch panic(err)
if e.Error != nil { }
panic(e.Error) t.Lock()
} defer t.Unlock()
t.Lock() t.textures[name] = id
defer t.Unlock()
t.textures[name] = e.Id
}()
}() }()
case s := <-t.renderTargetSizes: case s := <-t.renderTargetSizes:
name := s.name name := s.name
size := s.size size := s.size
go func() { go func() {
ch := t.textureFactory.CreateRenderTarget( id, err := t.textureFactory.CreateRenderTarget(
size.Width, size.Width,
size.Height, size.Height,
graphics.FilterNearest) graphics.FilterNearest)
go func() { if err != nil {
e := <-ch panic(err)
if e.Error != nil { }
panic(e.Error) t.Lock()
} defer t.Unlock()
t.Lock() t.renderTargets[name] = id
defer t.Unlock()
t.renderTargets[name] = e.Id
}()
}() }()
} }
} }

View File

@ -17,17 +17,7 @@ type TextureId int
// all alpha of a render target is maximum. // all alpha of a render target is maximum.
type RenderTargetId int type RenderTargetId int
type TextureCreatedEvent struct {
Id TextureId
Error error
}
type RenderTargetCreatedEvent struct {
Id RenderTargetId
Error error
}
type TextureFactory interface { type TextureFactory interface {
CreateRenderTarget(width, height int, filter Filter) <-chan RenderTargetCreatedEvent CreateRenderTarget(width, height int, filter Filter) (RenderTargetId, error)
CreateTexture(img image.Image, filter Filter) <-chan TextureCreatedEvent CreateTexture(img image.Image, filter Filter) (TextureId, error)
} }

View File

@ -73,40 +73,24 @@ func (t *sharedContext) createGameWindow(width, height, scale int, title string)
func (t *sharedContext) CreateTexture( func (t *sharedContext) CreateTexture(
img image.Image, img image.Image,
filter graphics.Filter) <-chan graphics.TextureCreatedEvent { filter graphics.Filter) (graphics.TextureId, error) {
ch := make(chan graphics.TextureCreatedEvent) <-t.inited
go func() { var id graphics.TextureId
<-t.inited var err error
var id graphics.TextureId t.useGLContext(func() {
var err error id, err = opengl.CreateTexture(img, filter)
t.useGLContext(func() { })
id, err = opengl.CreateTexture(img, filter) return id, err
})
ch <- graphics.TextureCreatedEvent{
Id: id,
Error: err,
}
close(ch)
}()
return ch
} }
func (t *sharedContext) CreateRenderTarget( func (t *sharedContext) CreateRenderTarget(
width, height int, width, height int,
filter graphics.Filter) <-chan graphics.RenderTargetCreatedEvent { filter graphics.Filter) (graphics.RenderTargetId, error) {
ch := make(chan graphics.RenderTargetCreatedEvent) <-t.inited
go func() { var id graphics.RenderTargetId
<-t.inited var err error
var id graphics.RenderTargetId t.useGLContext(func() {
var err error id, err = opengl.CreateRenderTarget(width, height, filter)
t.useGLContext(func() { })
id, err = opengl.CreateRenderTarget(width, height, filter) return id, err
})
ch <- graphics.RenderTargetCreatedEvent{
Id: id,
Error: err,
}
close(ch)
}()
return ch
} }