From 2d6818c81e54cb1e77f62742f4e54221a914c5ed Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 1 May 2014 21:42:57 +0900 Subject: [PATCH] Refactoring --- graphics/opengl/context.go | 3 +-- graphics/opengl/ids.go | 23 +++++++++---------- .../{rendertarget => }/render_target.go | 22 ++++-------------- graphics/opengl/{texture => }/texture.go | 12 +++------- 4 files changed, 19 insertions(+), 41 deletions(-) rename graphics/opengl/{rendertarget => }/render_target.go (81%) rename graphics/opengl/{texture => }/texture.go (87%) diff --git a/graphics/opengl/context.go b/graphics/opengl/context.go index b5b0245b8..19355d5ea 100644 --- a/graphics/opengl/context.go +++ b/graphics/opengl/context.go @@ -3,7 +3,6 @@ package opengl import ( "github.com/hajimehoshi/go-ebiten/graphics" "github.com/hajimehoshi/go-ebiten/graphics/matrix" - "github.com/hajimehoshi/go-ebiten/graphics/opengl/rendertarget" ) type Context struct { @@ -19,7 +18,7 @@ func newContext(ids *ids, screenWidth, screenHeight, screenScale int) *Context { ids: ids, screenScale: screenScale, } - mainRenderTarget := rendertarget.NewWithCurrentFramebuffer( + mainRenderTarget := newRTWithCurrentFramebuffer( screenWidth*screenScale, screenHeight*screenScale) context.mainId = context.ids.AddRenderTarget(mainRenderTarget) diff --git a/graphics/opengl/ids.go b/graphics/opengl/ids.go index 3daeca8b4..b731ba3c6 100644 --- a/graphics/opengl/ids.go +++ b/graphics/opengl/ids.go @@ -3,24 +3,22 @@ package opengl import ( "github.com/hajimehoshi/go-ebiten/graphics" "github.com/hajimehoshi/go-ebiten/graphics/matrix" - "github.com/hajimehoshi/go-ebiten/graphics/opengl/rendertarget" - "github.com/hajimehoshi/go-ebiten/graphics/opengl/texture" "image" "sync" ) type ids struct { lock sync.RWMutex - textures map[graphics.TextureId]*texture.Texture - renderTargets map[graphics.RenderTargetId]*rendertarget.RenderTarget + textures map[graphics.TextureId]*Texture + renderTargets map[graphics.RenderTargetId]*RenderTarget renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId counts chan int } func newIds() *ids { ids := &ids{ - textures: map[graphics.TextureId]*texture.Texture{}, - renderTargets: map[graphics.RenderTargetId]*rendertarget.RenderTarget{}, + textures: map[graphics.TextureId]*Texture{}, + renderTargets: map[graphics.RenderTargetId]*RenderTarget{}, renderTargetToTexture: map[graphics.RenderTargetId]graphics.TextureId{}, counts: make(chan int), } @@ -32,13 +30,13 @@ func newIds() *ids { return ids } -func (i *ids) textureAt(id graphics.TextureId) *texture.Texture { +func (i *ids) textureAt(id graphics.TextureId) *Texture { i.lock.RLock() defer i.lock.RUnlock() return i.textures[id] } -func (i *ids) renderTargetAt(id graphics.RenderTargetId) *rendertarget.RenderTarget { +func (i *ids) renderTargetAt(id graphics.RenderTargetId) *RenderTarget { i.lock.RLock() defer i.lock.RUnlock() return i.renderTargets[id] @@ -52,7 +50,7 @@ func (i *ids) toTexture(id graphics.RenderTargetId) graphics.TextureId { func (i *ids) CreateTexture(img image.Image, filter graphics.Filter) ( graphics.TextureId, error) { - texture, err := texture.CreateFromImage(img, filter) + texture, err := createTextureFromImage(img, filter) if err != nil { return 0, err } @@ -67,11 +65,12 @@ func (i *ids) CreateTexture(img image.Image, filter graphics.Filter) ( func (i *ids) CreateRenderTarget(width, height int, filter graphics.Filter) ( graphics.RenderTargetId, error) { - texture, err := texture.Create(width, height, filter) + texture, err := createTexture(width, height, filter) if err != nil { return 0, err } - renderTarget := texture.CreateRenderTarget() + framebuffer := createFramebuffer(texture.native) + renderTarget := &RenderTarget{framebuffer, texture.width, texture.height, false} textureId := graphics.TextureId(<-i.counts) renderTargetId := graphics.RenderTargetId(<-i.counts) @@ -86,7 +85,7 @@ func (i *ids) CreateRenderTarget(width, height int, filter graphics.Filter) ( } // NOTE: renderTarget can't be used as a texture. -func (i *ids) AddRenderTarget(renderTarget *rendertarget.RenderTarget) graphics.RenderTargetId { +func (i *ids) AddRenderTarget(renderTarget *RenderTarget) graphics.RenderTargetId { id := graphics.RenderTargetId(<-i.counts) i.lock.Lock() diff --git a/graphics/opengl/rendertarget/render_target.go b/graphics/opengl/render_target.go similarity index 81% rename from graphics/opengl/rendertarget/render_target.go rename to graphics/opengl/render_target.go index a9b7a146c..b68fa41e5 100644 --- a/graphics/opengl/rendertarget/render_target.go +++ b/graphics/opengl/render_target.go @@ -1,4 +1,4 @@ -package rendertarget +package opengl // #cgo LDFLAGS: -framework OpenGL // @@ -11,15 +11,6 @@ import ( "math" ) -type Texture interface { - Draw(projectionMatrix [4][4]float64, - geometryMatrix matrix.Geometry, colorMatrix matrix.Color) - DrawParts(parts []graphics.TexturePart, projectionMatrix [4][4]float64, - geometryMatrix matrix.Geometry, colorMatrix matrix.Color) -} - -type NativeTexture C.GLuint - type RenderTarget struct { framebuffer C.GLuint width int @@ -27,7 +18,7 @@ type RenderTarget struct { flipY bool } -func NewWithCurrentFramebuffer(width, height int) *RenderTarget { +func newRTWithCurrentFramebuffer(width, height int) *RenderTarget { framebuffer := C.GLint(0) C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, &framebuffer) return &RenderTarget{C.GLuint(framebuffer), width, height, true} @@ -58,11 +49,6 @@ func createFramebuffer(nativeTexture C.GLuint) C.GLuint { return framebuffer } -func CreateFromTexture(native NativeTexture, width, height int) *RenderTarget { - framebuffer := createFramebuffer(C.GLuint(native)) - return &RenderTarget{framebuffer, width, height, false} -} - func (r *RenderTarget) setAsViewport() { current := C.GLint(0) C.glGetIntegerv(C.GL_FRAMEBUFFER_BINDING, ¤t) @@ -113,14 +99,14 @@ func (r *RenderTarget) Fill(red, green, blue uint8) { C.glClear(C.GL_COLOR_BUFFER_BIT) } -func (r *RenderTarget) DrawTexture(texture Texture, +func (r *RenderTarget) DrawTexture(texture *Texture, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { r.setAsViewport() projectionMatrix := r.projectionMatrix() texture.Draw(projectionMatrix, geometryMatrix, colorMatrix) } -func (r *RenderTarget) DrawTextureParts(texture Texture, +func (r *RenderTarget) DrawTextureParts(texture *Texture, parts []graphics.TexturePart, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { r.setAsViewport() diff --git a/graphics/opengl/texture/texture.go b/graphics/opengl/texture.go similarity index 87% rename from graphics/opengl/texture/texture.go rename to graphics/opengl/texture.go index 52c02432c..cb7c3880e 100644 --- a/graphics/opengl/texture/texture.go +++ b/graphics/opengl/texture.go @@ -1,4 +1,4 @@ -package texture +package opengl // #cgo LDFLAGS: -framework OpenGL // @@ -7,7 +7,6 @@ import "C" import ( "github.com/hajimehoshi/go-ebiten/graphics" "github.com/hajimehoshi/go-ebiten/graphics/matrix" - "github.com/hajimehoshi/go-ebiten/graphics/opengl/rendertarget" "github.com/hajimehoshi/go-ebiten/graphics/opengl/shader" "image" "unsafe" @@ -64,25 +63,20 @@ func createNativeTexture(textureWidth, textureHeight int, pixels []uint8, return nativeTexture } -func Create(width, height int, filter graphics.Filter) (*Texture, error) { +func createTexture(width, height int, filter graphics.Filter) (*Texture, error) { native := createNativeTexture( graphics.AdjustSizeForTexture(width), graphics.AdjustSizeForTexture(height), nil, filter) return &Texture{native, width, height}, nil } -func CreateFromImage(img image.Image, filter graphics.Filter) (*Texture, error) { +func createTextureFromImage(img image.Image, filter graphics.Filter) (*Texture, error) { adjustedImage := graphics.AdjustImageForTexture(img) size := adjustedImage.Bounds().Size() native := createNativeTexture(size.X, size.Y, adjustedImage.Pix, filter) return &Texture{native, size.X, size.Y}, nil } -func (t *Texture) CreateRenderTarget() *rendertarget.RenderTarget { - return rendertarget.CreateFromTexture( - rendertarget.NativeTexture(t.native), t.width, t.height) -} - func (t *Texture) Draw(projectionMatrix [4][4]float64, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { quad := graphics.TextureQuadForTexture(t.width, t.height) shader.DrawTexture(shader.NativeTexture(t.native),