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
|
|
|
"image"
|
2013-12-10 13:21:11 +01:00
|
|
|
"sync"
|
2013-10-27 14:58:56 +01:00
|
|
|
)
|
|
|
|
|
2014-05-02 17:06:20 +02:00
|
|
|
var idsInstance *ids = newIds()
|
|
|
|
|
|
|
|
func CreateContext(
|
|
|
|
screenWidth, screenHeight, screenScale int) *Context {
|
|
|
|
return newContext(idsInstance, screenWidth, screenHeight, screenScale)
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateRenderTarget(
|
|
|
|
width, height int,
|
|
|
|
filter graphics.Filter) (graphics.RenderTargetId, error) {
|
|
|
|
return idsInstance.createRenderTarget(width, height, filter)
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateTexture(
|
|
|
|
img image.Image,
|
|
|
|
filter graphics.Filter) (graphics.TextureId, error) {
|
|
|
|
return idsInstance.createTexture(img, filter)
|
|
|
|
}
|
|
|
|
|
2013-10-27 14:58:56 +01:00
|
|
|
type ids struct {
|
2014-05-01 14:42:57 +02:00
|
|
|
textures map[graphics.TextureId]*Texture
|
|
|
|
renderTargets map[graphics.RenderTargetId]*RenderTarget
|
2013-10-27 14:58:56 +01:00
|
|
|
renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId
|
|
|
|
counts chan int
|
2014-05-02 17:06:20 +02:00
|
|
|
sync.RWMutex
|
2013-10-27 14:58:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func newIds() *ids {
|
|
|
|
ids := &ids{
|
2014-05-01 14:42:57 +02:00
|
|
|
textures: map[graphics.TextureId]*Texture{},
|
|
|
|
renderTargets: map[graphics.RenderTargetId]*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-05-01 14:42:57 +02:00
|
|
|
func (i *ids) textureAt(id graphics.TextureId) *Texture {
|
2014-05-02 17:06:20 +02:00
|
|
|
i.RLock()
|
|
|
|
defer i.RUnlock()
|
2013-10-27 14:58:56 +01:00
|
|
|
return i.textures[id]
|
|
|
|
}
|
|
|
|
|
2014-05-01 14:42:57 +02:00
|
|
|
func (i *ids) renderTargetAt(id graphics.RenderTargetId) *RenderTarget {
|
2014-05-02 17:06:20 +02:00
|
|
|
i.RLock()
|
|
|
|
defer i.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 {
|
2014-05-02 17:06:20 +02:00
|
|
|
i.RLock()
|
|
|
|
defer i.RUnlock()
|
2013-10-27 14:58:56 +01:00
|
|
|
return i.renderTargetToTexture[id]
|
|
|
|
}
|
|
|
|
|
2014-05-02 17:06:20 +02:00
|
|
|
func (i *ids) createTexture(img image.Image, filter graphics.Filter) (
|
2013-10-27 14:58:56 +01:00
|
|
|
graphics.TextureId, error) {
|
2014-05-01 14:42:57 +02:00
|
|
|
texture, err := createTextureFromImage(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
|
|
|
|
2014-05-02 17:06:20 +02:00
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
2013-10-27 14:58:56 +01:00
|
|
|
i.textures[textureId] = texture
|
|
|
|
return textureId, nil
|
|
|
|
}
|
|
|
|
|
2014-05-02 17:06:20 +02: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:58:15 +01:00
|
|
|
|
2014-05-01 14:42:57 +02:00
|
|
|
texture, err := createTexture(width, height, filter)
|
2013-10-27 14:58:56 +01:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2014-05-01 14:42:57 +02:00
|
|
|
framebuffer := createFramebuffer(texture.native)
|
|
|
|
renderTarget := &RenderTarget{framebuffer, texture.width, texture.height, false}
|
2014-01-08 10:47:38 +01:00
|
|
|
|
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
|
|
|
|
2014-05-02 17:06:20 +02:00
|
|
|
i.Lock()
|
|
|
|
defer i.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-07 13:58:46 +01:00
|
|
|
|
2014-01-11 00:59:38 +01:00
|
|
|
// NOTE: renderTarget can't be used as a texture.
|
2014-05-02 17:06:20 +02:00
|
|
|
func (i *ids) addRenderTarget(renderTarget *RenderTarget) graphics.RenderTargetId {
|
2014-01-11 00:59:38 +01:00
|
|
|
id := graphics.RenderTargetId(<-i.counts)
|
|
|
|
|
2014-05-02 17:06:20 +02:00
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
2014-01-11 00:59:38 +01:00
|
|
|
i.renderTargets[id] = renderTarget
|
|
|
|
|
|
|
|
return id
|
|
|
|
}
|
|
|
|
|
2014-05-02 17:06:20 +02:00
|
|
|
func (i *ids) deleteRenderTarget(id graphics.RenderTargetId) {
|
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
2014-01-08 10:58:15 +01:00
|
|
|
|
2014-01-07 13:58:46 +01:00
|
|
|
renderTarget := i.renderTargets[id]
|
2014-01-08 10:58:15 +01:00
|
|
|
textureId := i.renderTargetToTexture[id]
|
|
|
|
texture := i.textures[textureId]
|
|
|
|
|
2014-05-02 17:06:20 +02:00
|
|
|
renderTarget.dispose()
|
|
|
|
texture.dispose()
|
2014-01-08 10:47:38 +01:00
|
|
|
|
2014-01-07 13:58:46 +01:00
|
|
|
delete(i.renderTargets, id)
|
2014-01-08 10:58:15 +01:00
|
|
|
delete(i.renderTargetToTexture, id)
|
|
|
|
delete(i.textures, textureId)
|
2014-01-07 13:58:46 +01:00
|
|
|
}
|
2014-01-09 16:42:42 +01:00
|
|
|
|
2014-05-02 17:06:20 +02:00
|
|
|
func (i *ids) fillRenderTarget(id graphics.RenderTargetId, r, g, b uint8) {
|
|
|
|
i.renderTargetAt(id).fill(r, g, b)
|
2014-01-11 00:59:38 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 17:06:20 +02:00
|
|
|
func (i *ids) drawTexture(
|
|
|
|
target graphics.RenderTargetId,
|
|
|
|
id graphics.TextureId,
|
|
|
|
geo matrix.Geometry,
|
|
|
|
color matrix.Color) {
|
2014-01-09 16:42:42 +01:00
|
|
|
texture := i.textureAt(id)
|
2014-05-02 17:06:20 +02:00
|
|
|
parts := []graphics.TexturePart{
|
|
|
|
{0, 0, graphics.Rect{0, 0, texture.width, texture.height}},
|
|
|
|
}
|
|
|
|
i.renderTargetAt(target).drawTexture(texture, parts, geo, color)
|
2014-01-09 16:42:42 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 17:06:20 +02: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-05-02 17:06:20 +02:00
|
|
|
i.renderTargetAt(target).drawTexture(texture, parts, geo, color)
|
2014-01-09 16:42:42 +01:00
|
|
|
}
|