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 (
"github.com/hajimehoshi/go-ebiten/graphics"
"github.com/hajimehoshi/go-ebiten/ui"
_ "image/png"
)
type Size struct {

View File

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

View File

@ -17,17 +17,7 @@ type TextureId int
// all alpha of a render target is maximum.
type RenderTargetId int
type TextureCreatedEvent struct {
Id TextureId
Error error
}
type RenderTargetCreatedEvent struct {
Id RenderTargetId
Error error
}
type TextureFactory interface {
CreateRenderTarget(width, height int, filter Filter) <-chan RenderTargetCreatedEvent
CreateTexture(img image.Image, filter Filter) <-chan TextureCreatedEvent
CreateRenderTarget(width, height int, filter Filter) (RenderTargetId, error)
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(
img image.Image,
filter graphics.Filter) <-chan graphics.TextureCreatedEvent {
ch := make(chan graphics.TextureCreatedEvent)
go func() {
<-t.inited
var id graphics.TextureId
var err error
t.useGLContext(func() {
id, err = opengl.CreateTexture(img, filter)
})
ch <- graphics.TextureCreatedEvent{
Id: id,
Error: err,
}
close(ch)
}()
return ch
filter graphics.Filter) (graphics.TextureId, error) {
<-t.inited
var id graphics.TextureId
var err error
t.useGLContext(func() {
id, err = opengl.CreateTexture(img, filter)
})
return id, err
}
func (t *sharedContext) CreateRenderTarget(
width, height int,
filter graphics.Filter) <-chan graphics.RenderTargetCreatedEvent {
ch := make(chan graphics.RenderTargetCreatedEvent)
go func() {
<-t.inited
var id graphics.RenderTargetId
var err error
t.useGLContext(func() {
id, err = opengl.CreateRenderTarget(width, height, filter)
})
ch <- graphics.RenderTargetCreatedEvent{
Id: id,
Error: err,
}
close(ch)
}()
return ch
filter graphics.Filter) (graphics.RenderTargetId, error) {
<-t.inited
var id graphics.RenderTargetId
var err error
t.useGLContext(func() {
id, err = opengl.CreateRenderTarget(width, height, filter)
})
return id, err
}