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"
|
|
|
|
gtexture "github.com/hajimehoshi/go-ebiten/graphics/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
|
2013-10-27 14:58:56 +01:00
|
|
|
textures map[graphics.TextureId]*gtexture.Texture
|
2013-11-28 01:36:31 +01:00
|
|
|
renderTargets map[graphics.RenderTargetId]*gtexture.RenderTarget
|
2013-10-27 14:58:56 +01:00
|
|
|
renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId
|
|
|
|
counts chan int
|
|
|
|
}
|
|
|
|
|
|
|
|
func newIds() *ids {
|
|
|
|
ids := &ids{
|
|
|
|
textures: map[graphics.TextureId]*gtexture.Texture{},
|
2013-11-28 01:36:31 +01:00
|
|
|
renderTargets: map[graphics.RenderTargetId]*gtexture.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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *ids) TextureAt(id graphics.TextureId) *gtexture.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]
|
|
|
|
}
|
|
|
|
|
2013-11-28 01:36:31 +01:00
|
|
|
func (i *ids) RenderTargetAt(id graphics.RenderTargetId) *gtexture.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
|
|
|
|
}
|