ebiten/graphics/opengl/ids.go

98 lines
2.4 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) {
2014-01-08 10:47:38 +01:00
texture, err := texture.Create(width, height, filter)
2013-10-27 14:58:56 +01:00
if err != nil {
return 0, err
}
2014-01-08 10:47:38 +01:00
renderTarget := texture.CreateRenderTarget()
2013-10-27 14:58:56 +01:00
textureId := graphics.TextureId(<-i.counts)
2014-01-08 10:47:38 +01:00
renderTargetId := graphics.RenderTargetId(<-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
2014-01-08 10:47:38 +01:00
i.renderTargets[renderTargetId] = renderTarget
2013-10-27 14:58:56 +01:00
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()
2014-01-08 10:47:38 +01:00
i.lock.Lock()
defer i.lock.Unlock()
delete(i.renderTargets, id)
2014-01-08 10:47:38 +01:00
// TODO: Remove the related texture
}
2014-01-08 10:47:38 +01:00