ebiten/graphics/opengl/ids.go

130 lines
3.6 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"
"github.com/hajimehoshi/go-ebiten/graphics/opengl/offscreen"
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
}
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
func (i *ids) DrawTexture(id graphics.TextureId, offscreen *offscreen.Offscreen,
geo matrix.Geometry, color matrix.Color) {
texture := i.textureAt(id)
offscreen.DrawTexture(texture, geo, color)
}
func (i *ids) DrawTextureParts(id graphics.TextureId, offscreen *offscreen.Offscreen,
parts []graphics.TexturePart, geo matrix.Geometry, color matrix.Color) {
texture := i.textureAt(id)
offscreen.DrawTextureParts(texture, parts, geo, color)
}
func (i *ids) DrawRenderTarget(id graphics.RenderTargetId, offscreen *offscreen.Offscreen,
geo matrix.Geometry, color matrix.Color) {
i.DrawTexture(i.toTexture(id), offscreen, geo, color)
}
func (i *ids) DrawRenderTargetParts(id graphics.RenderTargetId, offscreen *offscreen.Offscreen,
parts []graphics.TexturePart, geo matrix.Geometry, color matrix.Color) {
i.DrawTextureParts(i.toTexture(id), offscreen, parts, geo, color)
}
func (i *ids) SetRenderTargetAsOffscreen(id graphics.RenderTargetId, offscreen *offscreen.Offscreen) {
offscreen.Set(i.renderTargetAt(id))
}