From d570a397c5537634658505d01633e9ec24263a3f Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Sun, 12 Oct 2014 14:07:44 +0900 Subject: [PATCH] Refactoring --- graphics/opengl/context.go | 23 ++++++-------- graphics/opengl/ids.go | 52 +++++++++++++++++--------------- graphics/opengl/render_target.go | 10 ------ readme.rm | 5 +++ 4 files changed, 43 insertions(+), 47 deletions(-) diff --git a/graphics/opengl/context.go b/graphics/opengl/context.go index 6a671384a..29ae612b9 100644 --- a/graphics/opengl/context.go +++ b/graphics/opengl/context.go @@ -23,29 +23,26 @@ type Context struct { screenId graphics.RenderTargetId defaultId graphics.RenderTargetId currentId graphics.RenderTargetId - ids *ids screenWidth int screenHeight int screenScale int } -func newContext(ids *ids, screenWidth, screenHeight, screenScale int) *Context { +func NewContext(screenWidth, screenHeight, screenScale int) *Context { context := &Context{ - ids: ids, screenWidth: screenWidth, screenHeight: screenHeight, screenScale: screenScale, } defaultRenderTarget := &RenderTarget{ - framebuffer: C.GLuint(0), - width: screenWidth * screenScale, - height: screenHeight * screenScale, - flipY: true, + width: screenWidth * screenScale, + height: screenHeight * screenScale, + flipY: true, } - context.defaultId = context.ids.addRenderTarget(defaultRenderTarget) + context.defaultId = idsInstance.addRenderTarget(defaultRenderTarget) var err error - context.screenId, err = ids.createRenderTarget( + context.screenId, err = idsInstance.createRenderTarget( screenWidth, screenHeight, graphics.FilterNearest) if err != nil { panic("initializing the offscreen failed: " + err.Error()) @@ -60,7 +57,7 @@ func newContext(ids *ids, screenWidth, screenHeight, screenScale int) *Context { func (c *Context) Dispose() { // TODO: remove the default framebuffer? - c.ids.deleteRenderTarget(c.screenId) + idsInstance.deleteRenderTarget(c.screenId) } func (c *Context) Update(draw func(graphics.Context)) { @@ -90,7 +87,7 @@ func (c *Context) Clear() { } func (c *Context) Fill(r, g, b uint8) { - c.ids.fillRenderTarget(c.currentId, r, g, b) + idsInstance.fillRenderTarget(c.currentId, r, g, b) } func (c *Context) Texture(id graphics.TextureId) graphics.Drawer { @@ -98,7 +95,7 @@ func (c *Context) Texture(id graphics.TextureId) graphics.Drawer { } func (c *Context) RenderTarget(id graphics.RenderTargetId) graphics.Drawer { - return &textureWithContext{c.ids.toTexture(id), c} + return &textureWithContext{idsInstance.toTexture(id), c} } func (c *Context) ResetOffscreen() { @@ -118,5 +115,5 @@ func (t *textureWithContext) Draw( parts []graphics.TexturePart, geo matrix.Geometry, color matrix.Color) { - t.context.ids.drawTexture(t.context.currentId, t.id, parts, geo, color) + idsInstance.drawTexture(t.context.currentId, t.id, parts, geo, color) } diff --git a/graphics/opengl/ids.go b/graphics/opengl/ids.go index d7e988aa8..400e073b7 100644 --- a/graphics/opengl/ids.go +++ b/graphics/opengl/ids.go @@ -13,10 +13,30 @@ import ( "sync" ) -var idsInstance *ids = newIds() +func glMatrix(matrix [4][4]float64) [16]float32 { + result := [16]float32{} + for j := 0; j < 4; j++ { + for i := 0; i < 4; i++ { + result[i+j*4] = float32(matrix[i][j]) + } + } + return result +} -func NewContext(screenWidth, screenHeight, screenScale int) *Context { - return newContext(idsInstance, screenWidth, screenHeight, screenScale) +type ids struct { + textures map[graphics.TextureId]*Texture + renderTargets map[graphics.RenderTargetId]*RenderTarget + renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId + lastId int + currentRenderTargetId graphics.RenderTargetId + sync.RWMutex +} + +var idsInstance = &ids{ + textures: map[graphics.TextureId]*Texture{}, + renderTargets: map[graphics.RenderTargetId]*RenderTarget{}, + renderTargetToTexture: map[graphics.RenderTargetId]graphics.TextureId{}, + currentRenderTargetId: -1, } func CreateRenderTarget( @@ -31,26 +51,6 @@ func CreateTexture( return idsInstance.createTexture(img, filter) } -type ids struct { - textures map[graphics.TextureId]*Texture - renderTargets map[graphics.RenderTargetId]*RenderTarget - renderTargetToTexture map[graphics.RenderTargetId]graphics.TextureId - lastId int - currentRenderTargetId graphics.RenderTargetId - sync.RWMutex -} - -func newIds() *ids { - ids := &ids{ - textures: map[graphics.TextureId]*Texture{}, - renderTargets: map[graphics.RenderTargetId]*RenderTarget{}, - renderTargetToTexture: map[graphics.RenderTargetId]graphics.TextureId{}, - lastId: 0, - currentRenderTargetId: -1, - } - return ids -} - func (i *ids) textureAt(id graphics.TextureId) *Texture { i.RLock() defer i.RUnlock() @@ -94,7 +94,11 @@ func (i *ids) createRenderTarget(width, height int, filter graphics.Filter) ( framebuffer := createFramebuffer(texture.native) // The current binded framebuffer can be changed. i.currentRenderTargetId = -1 - renderTarget := &RenderTarget{framebuffer, texture.width, texture.height, false} + renderTarget := &RenderTarget{ + framebuffer: framebuffer, + width: texture.width, + height: texture.height, + } i.Lock() defer i.Unlock() diff --git a/graphics/opengl/render_target.go b/graphics/opengl/render_target.go index 5d3194733..e028a730f 100644 --- a/graphics/opengl/render_target.go +++ b/graphics/opengl/render_target.go @@ -9,16 +9,6 @@ import ( "github.com/hajimehoshi/go-ebiten/graphics" ) -func glMatrix(matrix [4][4]float64) [16]float32 { - result := [16]float32{} - for j := 0; j < 4; j++ { - for i := 0; i < 4; i++ { - result[i+j*4] = float32(matrix[i][j]) - } - } - return result -} - type RenderTarget struct { framebuffer C.GLuint width int diff --git a/readme.rm b/readme.rm index 5ec6d2e0b..1f081c994 100644 --- a/readme.rm +++ b/readme.rm @@ -9,6 +9,11 @@ Please read example/main.go and example/blocks/*. Be careful that this library is now being developed actively and API can be changed easily. +## How to execute the example + + :; cd example + :; go run *.go + ## License Copyright 2014 Hajime Hoshi