ebiten/graphics/opengl/ids.go

177 lines
4.3 KiB
Go
Raw Normal View History

2013-10-27 14:58:56 +01:00
package opengl
2014-05-03 19:13:43 +02:00
// #cgo LDFLAGS: -framework OpenGL
//
// #include <OpenGL/gl.h>
import "C"
2013-10-27 14:58:56 +01:00
import (
"github.com/hajimehoshi/go-ebiten/graphics"
2014-01-09 16:42:42 +01:00
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
2014-05-03 19:13:43 +02:00
"github.com/hajimehoshi/go-ebiten/graphics/opengl/shader"
2013-10-27 14:58:56 +01:00
"image"
2014-05-03 19:13:43 +02:00
"math"
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()
2014-05-03 06:58:18 +02:00
func NewContext(screenWidth, screenHeight, screenScale int) *Context {
2014-05-02 17:06:20 +02:00
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
2014-05-11 12:44:36 +02:00
lastId int
2014-05-03 19:13:43 +02:00
currentRenderTargetId graphics.RenderTargetId
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{},
2014-05-11 12:44:36 +02:00
lastId: 0,
2014-05-03 19:13:43 +02:00
currentRenderTargetId: -1,
2013-10-27 14:58:56 +01:00
}
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
}
2013-12-10 13:21:11 +01:00
2014-05-02 17:06:20 +02:00
i.Lock()
defer i.Unlock()
2014-05-11 12:44:36 +02:00
i.lastId++
textureId := graphics.TextureId(i.lastId)
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-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)
2014-05-03 19:13:43 +02:00
// The current binded framebuffer can be changed.
i.currentRenderTargetId = -1
2014-05-01 14:42:57 +02:00
renderTarget := &RenderTarget{framebuffer, texture.width, texture.height, false}
2014-01-08 10:47:38 +01:00
2014-05-02 17:06:20 +02:00
i.Lock()
defer i.Unlock()
2014-05-11 12:44:36 +02:00
i.lastId++
textureId := graphics.TextureId(i.lastId)
i.lastId++
renderTargetId := graphics.RenderTargetId(i.lastId)
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.
2014-05-02 17:06:20 +02:00
func (i *ids) addRenderTarget(renderTarget *RenderTarget) graphics.RenderTargetId {
i.Lock()
defer i.Unlock()
2014-05-11 12:44:36 +02:00
i.lastId++
id := graphics.RenderTargetId(i.lastId)
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()
renderTarget := i.renderTargets[id]
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
delete(i.renderTargets, id)
delete(i.renderTargetToTexture, id)
delete(i.textures, textureId)
}
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) {
2014-05-03 19:13:43 +02:00
i.setViewportIfNeeded(id)
const max = float64(math.MaxUint8)
C.glClearColor(
C.GLclampf(float64(r)/max),
C.GLclampf(float64(g)/max),
C.GLclampf(float64(b)/max),
1)
C.glClear(C.GL_COLOR_BUFFER_BIT)
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,
2014-05-03 19:13:43 +02:00
parts []graphics.TexturePart,
geo matrix.Geometry,
color matrix.Color) {
2014-01-09 16:42:42 +01:00
texture := i.textureAt(id)
2014-05-03 19:13:43 +02:00
i.setViewportIfNeeded(target)
r := i.renderTargetAt(target)
projectionMatrix := r.projectionMatrix()
quads := graphics.TextureQuads(parts, texture.width, texture.height)
shader.DrawTexture(
shader.NativeTexture(texture.native),
glMatrix(projectionMatrix),
quads,
geo,
color)
}
func (i *ids) setViewportIfNeeded(id graphics.RenderTargetId) {
r := i.renderTargetAt(id)
if i.currentRenderTargetId != id {
r.setAsViewport()
i.currentRenderTargetId = id
}
2014-01-09 16:42:42 +01:00
}