ebiten/graphics/opengl/ids.go

89 lines
2.3 KiB
Go
Raw Normal View History

2013-10-27 14:58:56 +01:00
package opengl
import (
"github.com/hajimehoshi/go-ebiten/graphics"
"github.com/hajimehoshi/go-ebiten/graphics/opengl/rendertarget"
"github.com/hajimehoshi/go-ebiten/graphics/opengl/texture"
"image"
2013-12-10 13:21:11 +01:00
"sync"
2013-10-27 14:58:56 +01:00
)
type ids struct {
2013-12-10 13:21:11 +01:00
lock sync.RWMutex
2014-01-08 06:37:07 +01:00
textures map[graphics.TextureId]*texture.Texture
renderTargets map[graphics.RenderTargetId]*rendertarget.RenderTarget
2013-10-27 14:58:56 +01:00
renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId
counts chan int
}
func newIds() *ids {
ids := &ids{
2014-01-08 06:37:07 +01:00
textures: map[graphics.TextureId]*texture.Texture{},
renderTargets: map[graphics.RenderTargetId]*rendertarget.RenderTarget{},
2013-10-27 14:58:56 +01:00
renderTargetToTexture: map[graphics.RenderTargetId]graphics.TextureId{},
counts: make(chan int),
}
go func() {
for i := 1; ; i++ {
ids.counts <- i
}
}()
return ids
}
2014-01-08 06:37:07 +01:00
func (i *ids) TextureAt(id graphics.TextureId) *texture.Texture {
2013-12-10 13:21:11 +01:00
i.lock.RLock()
defer i.lock.RUnlock()
2013-10-27 14:58:56 +01:00
return i.textures[id]
}
2014-01-08 06:37:07 +01:00
func (i *ids) RenderTargetAt(id graphics.RenderTargetId) *rendertarget.RenderTarget {
2013-12-10 13:21:11 +01:00
i.lock.RLock()
defer i.lock.RUnlock()
2013-10-27 14:58:56 +01:00
return i.renderTargets[id]
}
func (i *ids) ToTexture(id graphics.RenderTargetId) graphics.TextureId {
2013-12-10 13:21:11 +01:00
i.lock.RLock()
defer i.lock.RUnlock()
2013-10-27 14:58:56 +01:00
return i.renderTargetToTexture[id]
}
2013-12-18 10:05:28 +01:00
func (i *ids) CreateTexture(img image.Image, filter graphics.Filter) (
2013-10-27 14:58:56 +01:00
graphics.TextureId, error) {
2013-12-18 10:05:28 +01:00
texture, err := texture.CreateFromImage(img, filter)
2013-10-27 14:58:56 +01:00
if err != nil {
return 0, err
}
textureId := graphics.TextureId(<-i.counts)
2013-12-10 13:21:11 +01:00
i.lock.Lock()
defer i.lock.Unlock()
2013-10-27 14:58:56 +01:00
i.textures[textureId] = texture
return textureId, nil
}
2013-12-18 10:05:28 +01:00
func (i *ids) CreateRenderTarget(width, height int, filter graphics.Filter) (
2013-10-27 14:58:56 +01:00
graphics.RenderTargetId, error) {
2013-11-28 18:38:18 +01:00
renderTarget, texture, err := rendertarget.Create(width, height, filter)
2013-10-27 14:58:56 +01:00
if err != nil {
return 0, err
}
renderTargetId := graphics.RenderTargetId(<-i.counts)
textureId := graphics.TextureId(<-i.counts)
2013-12-10 13:21:11 +01:00
i.lock.Lock()
defer i.lock.Unlock()
2013-10-27 14:58:56 +01:00
i.renderTargets[renderTargetId] = renderTarget
i.textures[textureId] = texture
i.renderTargetToTexture[renderTargetId] = textureId
return renderTargetId, nil
}
func (i *ids) DeleteRenderTarget(id graphics.RenderTargetId) {
renderTarget := i.renderTargets[id]
2014-01-08 06:37:07 +01:00
renderTarget.Dispose()
delete(i.renderTargets, id)
}