ebiten/graphics/opengl/ids.go

140 lines
3.8 KiB
Go
Raw Normal View History

2013-10-27 14:58:56 +01:00
package opengl
import (
"github.com/hajimehoshi/go-ebiten/graphics"
2014-01-09 16:42:42 +01:00
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
2013-10-27 14:58:56 +01:00
"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-09 16:42:42 +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-09 16:42:42 +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]
}
2014-01-09 16:42:42 +01:00
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
}
2014-01-11 00:59:38 +01:00
// NOTE: renderTarget can't be used as a texture.
func (i *ids) AddRenderTarget(renderTarget *rendertarget.RenderTarget) graphics.RenderTargetId {
id := graphics.RenderTargetId(<-i.counts)
i.lock.Lock()
defer i.lock.Unlock()
i.renderTargets[id] = renderTarget
return id
}
func (i *ids) DeleteRenderTarget(id graphics.RenderTargetId) {
i.lock.Lock()
defer i.lock.Unlock()
renderTarget := i.renderTargets[id]
textureId := i.renderTargetToTexture[id]
texture := i.textures[textureId]
2014-01-08 06:37:07 +01:00
renderTarget.Dispose()
texture.Dispose()
2014-01-08 10:47:38 +01:00
delete(i.renderTargets, id)
delete(i.renderTargetToTexture, id)
delete(i.textures, textureId)
}
2014-01-09 16:42:42 +01:00
2014-01-11 00:59:38 +01:00
func (i *ids) FillRenderTarget(id graphics.RenderTargetId, r, g, b uint8) {
i.renderTargetAt(id).Fill(r, g, b)
}
func (i *ids) DrawTexture(target graphics.RenderTargetId, id graphics.TextureId,
2014-01-09 16:42:42 +01:00
geo matrix.Geometry, color matrix.Color) {
texture := i.textureAt(id)
2014-01-11 00:59:38 +01:00
i.renderTargetAt(target).DrawTexture(texture, geo, color)
2014-01-09 16:42:42 +01:00
}
2014-01-11 00:59:38 +01:00
func (i *ids) DrawTextureParts(target graphics.RenderTargetId, id graphics.TextureId,
2014-01-09 16:42:42 +01:00
parts []graphics.TexturePart, geo matrix.Geometry, color matrix.Color) {
texture := i.textureAt(id)
2014-01-11 00:59:38 +01:00
i.renderTargetAt(target).DrawTextureParts(texture, parts, geo, color)
2014-01-09 16:42:42 +01:00
}
2014-01-11 00:59:38 +01:00
func (i *ids) DrawRenderTarget(target graphics.RenderTargetId, id graphics.RenderTargetId,
2014-01-09 16:42:42 +01:00
geo matrix.Geometry, color matrix.Color) {
2014-01-11 00:59:38 +01:00
i.DrawTexture(target, i.toTexture(id), geo, color)
2014-01-09 16:42:42 +01:00
}
2014-01-11 00:59:38 +01:00
func (i *ids) DrawRenderTargetParts(target graphics.RenderTargetId, id graphics.RenderTargetId,
2014-01-09 16:42:42 +01:00
parts []graphics.TexturePart, geo matrix.Geometry, color matrix.Color) {
2014-01-11 00:59:38 +01:00
i.DrawTextureParts(target, i.toTexture(id), parts, geo, color)
2014-01-09 16:42:42 +01:00
}