From e90f6d3222074f3047ece69cd26eb69d8f21e90f Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 27 Oct 2013 22:58:56 +0900 Subject: [PATCH] Add ids.go --- graphics/opengl/context.go | 71 +++++-------------- graphics/opengl/ids.go | 70 ++++++++++++++++++ graphics/opengl/rendertarget/render_target.go | 9 ++- graphics/opengl/texture/texture.go | 6 +- 4 files changed, 97 insertions(+), 59 deletions(-) create mode 100644 graphics/opengl/ids.go diff --git a/graphics/opengl/context.go b/graphics/opengl/context.go index edb58a4b8..5ef699075 100644 --- a/graphics/opengl/context.go +++ b/graphics/opengl/context.go @@ -23,23 +23,18 @@ type Context struct { screenWidth int screenHeight int screenScale int - textures map[graphics.TextureId]*gtexture.Texture - renderTargets map[graphics.RenderTargetId]*grendertarget.RenderTarget - renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId + ids *ids mainFramebufferTexture *grendertarget.RenderTarget projectionMatrix [16]float32 } func newContext(screenWidth, screenHeight, screenScale int) *Context { - context := &Context{ - screenWidth: screenWidth, - screenHeight: screenHeight, - screenScale: screenScale, - textures: map[graphics.TextureId]*gtexture.Texture{}, - renderTargets: map[graphics.RenderTargetId]*grendertarget.RenderTarget{}, - renderTargetToTexture: map[graphics.RenderTargetId]graphics.TextureId{}, + return &Context{ + screenWidth: screenWidth, + screenHeight: screenHeight, + screenScale: screenScale, + ids: newIds(), } - return context } func (context *Context) Init() { @@ -57,7 +52,7 @@ func (context *Context) Init() { panic("creating main framebuffer failed: " + err.Error()) } - context.screenId, err = context.newRenderTarget( + context.screenId, err = context.createRenderTarget( context.screenWidth, context.screenHeight, texture.FilterNearest) if err != nil { panic("initializing the offscreen failed: " + err.Error()) @@ -65,7 +60,7 @@ func (context *Context) Init() { } func (context *Context) ToTexture(renderTargetId graphics.RenderTargetId) graphics.TextureId { - return context.renderTargetToTexture[renderTargetId] + return context.ids.ToTexture(renderTargetId) } func (context *Context) Clear() { @@ -85,10 +80,7 @@ func (context *Context) Fill(r, g, b uint8) { func (context *Context) DrawTexture( textureId graphics.TextureId, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { - tex, ok := context.textures[textureId] - if !ok { - panic("invalid texture ID") - } + tex := context.ids.TextureAt(textureId) tex.Draw(func(native interface{}, quads []gtexture.Quad) { shader.DrawTexture(native.(texture.Native), context.projectionMatrix, quads, @@ -99,10 +91,7 @@ func (context *Context) DrawTexture( func (context *Context) DrawTextureParts( textureId graphics.TextureId, parts []graphics.TexturePart, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { - tex, ok := context.textures[textureId] - if !ok { - panic("invalid texture ID") - } + tex := context.ids.TextureAt(textureId) tex.DrawParts(parts, func(native interface{}, quads []gtexture.Quad) { shader.DrawTexture(native.(texture.Native), context.projectionMatrix, quads, @@ -115,7 +104,7 @@ func (context *Context) ResetOffscreen() { } func (context *Context) SetOffscreen(renderTargetId graphics.RenderTargetId) { - renderTarget := context.renderTargets[renderTargetId] + renderTarget := context.ids.RenderTargetAt(renderTargetId) context.setOffscreen(renderTarget) } @@ -175,49 +164,21 @@ func (context *Context) flush() { 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) { - renderTarget, texture, err := rendertarget.New(width, height, filter) + renderTargetId, err := context.ids.CreateRenderTarget(width, height, filter) 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 } func (context *Context) NewRenderTarget(width, height int) ( graphics.RenderTargetId, error) { - return context.newRenderTarget(width, height, texture.FilterLinear) + return context.createRenderTarget(width, height, texture.FilterLinear) } func (context *Context) NewTextureFromImage(img image.Image) ( graphics.TextureId, error) { - texture, err := texture.NewFromImage(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 - } - }() + return context.ids.CreateTextureFromImage(img) } diff --git a/graphics/opengl/ids.go b/graphics/opengl/ids.go new file mode 100644 index 000000000..8c9a0c1d7 --- /dev/null +++ b/graphics/opengl/ids.go @@ -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 +} diff --git a/graphics/opengl/rendertarget/render_target.go b/graphics/opengl/rendertarget/render_target.go index 717a36959..fc5869930 100644 --- a/graphics/opengl/rendertarget/render_target.go +++ b/graphics/opengl/rendertarget/render_target.go @@ -6,8 +6,8 @@ package rendertarget import "C" import ( "github.com/hajimehoshi/go-ebiten/graphics/opengl/texture" - gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture" "github.com/hajimehoshi/go-ebiten/graphics/rendertarget" + gtexture "github.com/hajimehoshi/go-ebiten/graphics/texture" ) type Framebuffer C.GLuint @@ -29,6 +29,11 @@ func createFramebuffer(nativeTexture C.GLuint) C.GLuint { 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 } @@ -38,7 +43,7 @@ func New(width, height int, filter texture.Filter) ( if err != nil { return nil, nil, err } - f := func(native interface{}) interface{}{ + f := func(native interface{}) interface{} { return createFramebuffer(C.GLuint(native.(texture.Native))) } framebuffer := tex.CreateFramebuffer(f) diff --git a/graphics/opengl/texture/texture.go b/graphics/opengl/texture/texture.go index b6e2ee9d2..cdce6cb64 100644 --- a/graphics/opengl/texture/texture.go +++ b/graphics/opengl/texture/texture.go @@ -54,8 +54,10 @@ func createNativeTexture(textureWidth, textureHeight int, pixels []uint8, return Native(nativeTexture) } -func create(textureWidth, textureHeight int, filter Filter) (interface{}, error) { - return createNativeTexture(textureWidth, textureHeight, nil, filter), nil +func create(textureWidth, textureHeight int, filter Filter) ( + interface{}, error) { + return createNativeTexture(textureWidth, textureHeight, + nil, filter), nil } func createFromImage(img *image.NRGBA) (interface{}, error) {