Refactoring

This commit is contained in:
Hajime Hoshi 2014-10-12 14:07:44 +09:00
parent cfff7bf081
commit d570a397c5
4 changed files with 43 additions and 47 deletions

View File

@ -23,29 +23,26 @@ type Context struct {
screenId graphics.RenderTargetId screenId graphics.RenderTargetId
defaultId graphics.RenderTargetId defaultId graphics.RenderTargetId
currentId graphics.RenderTargetId currentId graphics.RenderTargetId
ids *ids
screenWidth int screenWidth int
screenHeight int screenHeight int
screenScale int screenScale int
} }
func newContext(ids *ids, screenWidth, screenHeight, screenScale int) *Context { func NewContext(screenWidth, screenHeight, screenScale int) *Context {
context := &Context{ context := &Context{
ids: ids,
screenWidth: screenWidth, screenWidth: screenWidth,
screenHeight: screenHeight, screenHeight: screenHeight,
screenScale: screenScale, screenScale: screenScale,
} }
defaultRenderTarget := &RenderTarget{ defaultRenderTarget := &RenderTarget{
framebuffer: C.GLuint(0), width: screenWidth * screenScale,
width: screenWidth * screenScale, height: screenHeight * screenScale,
height: screenHeight * screenScale, flipY: true,
flipY: true,
} }
context.defaultId = context.ids.addRenderTarget(defaultRenderTarget) context.defaultId = idsInstance.addRenderTarget(defaultRenderTarget)
var err error var err error
context.screenId, err = ids.createRenderTarget( context.screenId, err = idsInstance.createRenderTarget(
screenWidth, screenHeight, graphics.FilterNearest) screenWidth, screenHeight, graphics.FilterNearest)
if err != nil { if err != nil {
panic("initializing the offscreen failed: " + err.Error()) panic("initializing the offscreen failed: " + err.Error())
@ -60,7 +57,7 @@ func newContext(ids *ids, screenWidth, screenHeight, screenScale int) *Context {
func (c *Context) Dispose() { func (c *Context) Dispose() {
// TODO: remove the default framebuffer? // TODO: remove the default framebuffer?
c.ids.deleteRenderTarget(c.screenId) idsInstance.deleteRenderTarget(c.screenId)
} }
func (c *Context) Update(draw func(graphics.Context)) { func (c *Context) Update(draw func(graphics.Context)) {
@ -90,7 +87,7 @@ func (c *Context) Clear() {
} }
func (c *Context) Fill(r, g, b uint8) { 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 { 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 { 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() { func (c *Context) ResetOffscreen() {
@ -118,5 +115,5 @@ func (t *textureWithContext) Draw(
parts []graphics.TexturePart, parts []graphics.TexturePart,
geo matrix.Geometry, geo matrix.Geometry,
color matrix.Color) { 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)
} }

View File

@ -13,10 +13,30 @@ import (
"sync" "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 { type ids struct {
return newContext(idsInstance, screenWidth, screenHeight, screenScale) 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( func CreateRenderTarget(
@ -31,26 +51,6 @@ func CreateTexture(
return idsInstance.createTexture(img, filter) 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 { func (i *ids) textureAt(id graphics.TextureId) *Texture {
i.RLock() i.RLock()
defer i.RUnlock() defer i.RUnlock()
@ -94,7 +94,11 @@ func (i *ids) createRenderTarget(width, height int, filter graphics.Filter) (
framebuffer := createFramebuffer(texture.native) framebuffer := createFramebuffer(texture.native)
// The current binded framebuffer can be changed. // The current binded framebuffer can be changed.
i.currentRenderTargetId = -1 i.currentRenderTargetId = -1
renderTarget := &RenderTarget{framebuffer, texture.width, texture.height, false} renderTarget := &RenderTarget{
framebuffer: framebuffer,
width: texture.width,
height: texture.height,
}
i.Lock() i.Lock()
defer i.Unlock() defer i.Unlock()

View File

@ -9,16 +9,6 @@ import (
"github.com/hajimehoshi/go-ebiten/graphics" "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 { type RenderTarget struct {
framebuffer C.GLuint framebuffer C.GLuint
width int width int

View File

@ -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 Be careful that this library is now being developed actively and API can be
changed easily. changed easily.
## How to execute the example
:; cd example
:; go run *.go
## License ## License
Copyright 2014 Hajime Hoshi Copyright 2014 Hajime Hoshi