Add ids.lock

This commit is contained in:
Hajime Hoshi 2013-12-10 21:21:11 +09:00
parent a0cf1e5d55
commit df836df45e

View File

@ -6,9 +6,11 @@ import (
"github.com/hajimehoshi/go-ebiten/graphics/opengl/texture"
gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture"
"image"
"sync"
)
type ids struct {
lock sync.RWMutex
textures map[graphics.TextureId]*gtexture.Texture
renderTargets map[graphics.RenderTargetId]*gtexture.RenderTarget
renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId
@ -31,14 +33,20 @@ func newIds() *ids {
}
func (i *ids) TextureAt(id graphics.TextureId) *gtexture.Texture {
i.lock.RLock()
defer i.lock.RUnlock()
return i.textures[id]
}
func (i *ids) RenderTargetAt(id graphics.RenderTargetId) *gtexture.RenderTarget {
i.lock.RLock()
defer i.lock.RUnlock()
return i.renderTargets[id]
}
func (i *ids) ToTexture(id graphics.RenderTargetId) graphics.TextureId {
i.lock.RLock()
defer i.lock.RUnlock()
return i.renderTargetToTexture[id]
}
@ -49,6 +57,9 @@ func (i *ids) CreateTextureFromImage(img image.Image) (
return 0, err
}
textureId := graphics.TextureId(<-i.counts)
i.lock.Lock()
defer i.lock.Unlock()
i.textures[textureId] = texture
return textureId, nil
}
@ -61,6 +72,9 @@ func (i *ids) CreateRenderTarget(width, height int, filter texture.Filter) (
}
renderTargetId := graphics.RenderTargetId(<-i.counts)
textureId := graphics.TextureId(<-i.counts)
i.lock.Lock()
defer i.lock.Unlock()
i.renderTargets[renderTargetId] = renderTarget
i.textures[textureId] = texture
i.renderTargetToTexture[renderTargetId] = textureId