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