Add ids.go

This commit is contained in:
Hajime Hoshi 2013-10-27 22:58:56 +09:00
parent f6523b28f4
commit e90f6d3222
4 changed files with 97 additions and 59 deletions

View File

@ -23,23 +23,18 @@ type Context struct {
screenWidth int screenWidth int
screenHeight int screenHeight int
screenScale int screenScale int
textures map[graphics.TextureId]*gtexture.Texture ids *ids
renderTargets map[graphics.RenderTargetId]*grendertarget.RenderTarget
renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId
mainFramebufferTexture *grendertarget.RenderTarget mainFramebufferTexture *grendertarget.RenderTarget
projectionMatrix [16]float32 projectionMatrix [16]float32
} }
func newContext(screenWidth, screenHeight, screenScale int) *Context { func newContext(screenWidth, screenHeight, screenScale int) *Context {
context := &Context{ return &Context{
screenWidth: screenWidth, screenWidth: screenWidth,
screenHeight: screenHeight, screenHeight: screenHeight,
screenScale: screenScale, screenScale: screenScale,
textures: map[graphics.TextureId]*gtexture.Texture{}, ids: newIds(),
renderTargets: map[graphics.RenderTargetId]*grendertarget.RenderTarget{},
renderTargetToTexture: map[graphics.RenderTargetId]graphics.TextureId{},
} }
return context
} }
func (context *Context) Init() { func (context *Context) Init() {
@ -57,7 +52,7 @@ func (context *Context) Init() {
panic("creating main framebuffer failed: " + err.Error()) panic("creating main framebuffer failed: " + err.Error())
} }
context.screenId, err = context.newRenderTarget( context.screenId, err = context.createRenderTarget(
context.screenWidth, context.screenHeight, texture.FilterNearest) context.screenWidth, context.screenHeight, texture.FilterNearest)
if err != nil { if err != nil {
panic("initializing the offscreen failed: " + err.Error()) panic("initializing the offscreen failed: " + err.Error())
@ -65,7 +60,7 @@ func (context *Context) Init() {
} }
func (context *Context) ToTexture(renderTargetId graphics.RenderTargetId) graphics.TextureId { func (context *Context) ToTexture(renderTargetId graphics.RenderTargetId) graphics.TextureId {
return context.renderTargetToTexture[renderTargetId] return context.ids.ToTexture(renderTargetId)
} }
func (context *Context) Clear() { func (context *Context) Clear() {
@ -85,10 +80,7 @@ func (context *Context) Fill(r, g, b uint8) {
func (context *Context) DrawTexture( func (context *Context) DrawTexture(
textureId graphics.TextureId, textureId graphics.TextureId,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
tex, ok := context.textures[textureId] tex := context.ids.TextureAt(textureId)
if !ok {
panic("invalid texture ID")
}
tex.Draw(func(native interface{}, quads []gtexture.Quad) { tex.Draw(func(native interface{}, quads []gtexture.Quad) {
shader.DrawTexture(native.(texture.Native), shader.DrawTexture(native.(texture.Native),
context.projectionMatrix, quads, context.projectionMatrix, quads,
@ -99,10 +91,7 @@ func (context *Context) DrawTexture(
func (context *Context) DrawTextureParts( func (context *Context) DrawTextureParts(
textureId graphics.TextureId, parts []graphics.TexturePart, textureId graphics.TextureId, parts []graphics.TexturePart,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
tex, ok := context.textures[textureId] tex := context.ids.TextureAt(textureId)
if !ok {
panic("invalid texture ID")
}
tex.DrawParts(parts, func(native interface{}, quads []gtexture.Quad) { tex.DrawParts(parts, func(native interface{}, quads []gtexture.Quad) {
shader.DrawTexture(native.(texture.Native), shader.DrawTexture(native.(texture.Native),
context.projectionMatrix, quads, context.projectionMatrix, quads,
@ -115,7 +104,7 @@ func (context *Context) ResetOffscreen() {
} }
func (context *Context) SetOffscreen(renderTargetId graphics.RenderTargetId) { func (context *Context) SetOffscreen(renderTargetId graphics.RenderTargetId) {
renderTarget := context.renderTargets[renderTargetId] renderTarget := context.ids.RenderTargetAt(renderTargetId)
context.setOffscreen(renderTarget) context.setOffscreen(renderTarget)
} }
@ -175,49 +164,21 @@ func (context *Context) flush() {
C.glFlush() C.glFlush()
} }
func (context *Context) newRenderTarget(width, height int, filter texture.Filter) ( func (context *Context) createRenderTarget(width, height int, filter texture.Filter) (
graphics.RenderTargetId, error) { graphics.RenderTargetId, error) {
renderTarget, texture, err := rendertarget.New(width, height, filter) renderTargetId, err := context.ids.CreateRenderTarget(width, height, filter)
if err != nil { if err != nil {
return 0, nil return 0, err
} }
renderTargetId := graphics.RenderTargetId(<-newId)
textureId := graphics.TextureId(<-newId)
context.renderTargets[renderTargetId] = renderTarget
context.textures[textureId] = texture
context.renderTargetToTexture[renderTargetId] = textureId
context.setOffscreen(renderTarget)
context.Clear()
// TODO: Is it OK to revert he main framebuffer?
context.setMainFramebufferOffscreen()
return renderTargetId, nil return renderTargetId, nil
} }
func (context *Context) NewRenderTarget(width, height int) ( func (context *Context) NewRenderTarget(width, height int) (
graphics.RenderTargetId, error) { graphics.RenderTargetId, error) {
return context.newRenderTarget(width, height, texture.FilterLinear) return context.createRenderTarget(width, height, texture.FilterLinear)
} }
func (context *Context) NewTextureFromImage(img image.Image) ( func (context *Context) NewTextureFromImage(img image.Image) (
graphics.TextureId, error) { graphics.TextureId, error) {
texture, err := texture.NewFromImage(img) return context.ids.CreateTextureFromImage(img)
if err != nil {
return 0, err
}
textureId := graphics.TextureId(<-newId)
context.textures[textureId] = texture
return textureId, nil
}
var newId chan int
func init() {
newId = make(chan int)
go func() {
for i := 0; ; i++ {
newId <- i
}
}()
} }

70
graphics/opengl/ids.go Normal file
View File

@ -0,0 +1,70 @@
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"
grendertarget "github.com/hajimehoshi/go-ebiten/graphics/rendertarget"
gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture"
"image"
)
type ids struct {
textures map[graphics.TextureId]*gtexture.Texture
renderTargets map[graphics.RenderTargetId]*grendertarget.RenderTarget
renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId
counts chan int
}
func newIds() *ids {
ids := &ids{
textures: map[graphics.TextureId]*gtexture.Texture{},
renderTargets: map[graphics.RenderTargetId]*grendertarget.RenderTarget{},
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 {
return i.textures[id]
}
func (i *ids) RenderTargetAt(id graphics.RenderTargetId) *grendertarget.RenderTarget {
return i.renderTargets[id]
}
func (i *ids) ToTexture(id graphics.RenderTargetId) graphics.TextureId {
return i.renderTargetToTexture[id]
}
func (i *ids) CreateTextureFromImage(img image.Image) (
graphics.TextureId, error) {
texture, err := texture.NewFromImage(img)
if err != nil {
return 0, err
}
textureId := graphics.TextureId(<-i.counts)
i.textures[textureId] = texture
return textureId, nil
}
func (i *ids) CreateRenderTarget(width, height int, filter texture.Filter) (
graphics.RenderTargetId, error) {
renderTarget, texture, err := rendertarget.New(width, height, filter)
if err != nil {
return 0, err
}
renderTargetId := graphics.RenderTargetId(<-i.counts)
textureId := graphics.TextureId(<-i.counts)
i.renderTargets[renderTargetId] = renderTarget
i.textures[textureId] = texture
i.renderTargetToTexture[renderTargetId] = textureId
return renderTargetId, nil
}

View File

@ -6,8 +6,8 @@ package rendertarget
import "C" import "C"
import ( import (
"github.com/hajimehoshi/go-ebiten/graphics/opengl/texture" "github.com/hajimehoshi/go-ebiten/graphics/opengl/texture"
gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture"
"github.com/hajimehoshi/go-ebiten/graphics/rendertarget" "github.com/hajimehoshi/go-ebiten/graphics/rendertarget"
gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture"
) )
type Framebuffer C.GLuint type Framebuffer C.GLuint
@ -29,6 +29,11 @@ func createFramebuffer(nativeTexture C.GLuint) C.GLuint {
panic("creating framebuffer failed") panic("creating framebuffer failed")
} }
// Set this framebuffer opaque because alpha values on a target might be
// confusing.
C.glClearColor(0, 0, 0, 1)
C.glClear(C.GL_COLOR_BUFFER_BIT)
return framebuffer return framebuffer
} }
@ -38,7 +43,7 @@ func New(width, height int, filter texture.Filter) (
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
f := func(native interface{}) interface{}{ f := func(native interface{}) interface{} {
return createFramebuffer(C.GLuint(native.(texture.Native))) return createFramebuffer(C.GLuint(native.(texture.Native)))
} }
framebuffer := tex.CreateFramebuffer(f) framebuffer := tex.CreateFramebuffer(f)

View File

@ -54,8 +54,10 @@ func createNativeTexture(textureWidth, textureHeight int, pixels []uint8,
return Native(nativeTexture) return Native(nativeTexture)
} }
func create(textureWidth, textureHeight int, filter Filter) (interface{}, error) { func create(textureWidth, textureHeight int, filter Filter) (
return createNativeTexture(textureWidth, textureHeight, nil, filter), nil interface{}, error) {
return createNativeTexture(textureWidth, textureHeight,
nil, filter), nil
} }
func createFromImage(img *image.NRGBA) (interface{}, error) { func createFromImage(img *image.NRGBA) (interface{}, error) {