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
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)
}

View File

@ -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()

View File

@ -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

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
changed easily.
## How to execute the example
:; cd example
:; go run *.go
## License
Copyright 2014 Hajime Hoshi