Remove Drawer; Add GraphicsContext.DrawTexture

This commit is contained in:
Hajime Hoshi 2014-12-18 22:24:27 +09:00
parent 9bd0f9363c
commit 49bcf8bb32
10 changed files with 24 additions and 75 deletions

View File

@ -57,7 +57,7 @@ func (d *debugPrintState) drawText(gr ebiten.GraphicsContext, str string, x, y i
geom.Concat(ebiten.TranslateGeometry(float64(x)+1, float64(y))) geom.Concat(ebiten.TranslateGeometry(float64(x)+1, float64(y)))
clrm := ebiten.ColorMatrixI() clrm := ebiten.ColorMatrixI()
clrm.Concat(ebiten.ScaleColor(clr)) clrm.Concat(ebiten.ScaleColor(clr))
gr.Texture(d.textTexture).Draw(parts, geom, clrm) gr.DrawTexture(d.textTexture, parts, geom, clrm)
} }
func (d *debugPrintState) DebugPrint(gr ebiten.GraphicsContext, str string) { func (d *debugPrintState) DebugPrint(gr ebiten.GraphicsContext, str string) {

View File

@ -58,7 +58,7 @@ func drawText(context ebiten.GraphicsContext, textures *Textures, str string, x,
geoMat.Concat(ebiten.TranslateGeometry(float64(x), float64(y))) geoMat.Concat(ebiten.TranslateGeometry(float64(x), float64(y)))
clrMat := ebiten.ColorMatrixI() clrMat := ebiten.ColorMatrixI()
clrMat.Concat(ebiten.ScaleColor(clr)) clrMat.Concat(ebiten.ScaleColor(clr))
context.Texture(fontTextureId).Draw(parts, geoMat, clrMat) context.DrawTexture(fontTextureId, parts, geoMat, clrMat)
} }
func drawTextWithShadow(context ebiten.GraphicsContext, textures *Textures, str string, x, y, scale int, clr color.Color) { func drawTextWithShadow(context ebiten.GraphicsContext, textures *Textures, str string, x, y, scale int, clr color.Color) {

View File

@ -153,7 +153,7 @@ func drawBlocks(context ebiten.GraphicsContext, textures *Textures, blocks [][]B
} }
} }
blocksTexture := textures.GetTexture("blocks") blocksTexture := textures.GetTexture("blocks")
context.Texture(blocksTexture).Draw(parts, geo, ebiten.ColorMatrixI()) context.DrawTexture(blocksTexture, parts, geo, ebiten.ColorMatrixI())
} }
func (p *Piece) InitialPosition() (int, int) { func (p *Piece) InitialPosition() (int, int) {

View File

@ -78,11 +78,11 @@ func (s *SceneManager) Draw(context ebiten.GraphicsContext, textures *Textures)
context.PopRenderTarget() context.PopRenderTarget()
color := ebiten.ColorMatrixI() color := ebiten.ColorMatrixI()
ebiten.DrawWholeRenderTarget(context, from, ebiten.GeometryMatrixI(), color) ebiten.DrawWholeTexture(context, from.Texture(), ebiten.GeometryMatrixI(), color)
alpha := float64(s.transitionCount) / float64(transitionMaxCount) alpha := float64(s.transitionCount) / float64(transitionMaxCount)
color.Elements[3][3] = alpha color.Elements[3][3] = alpha
ebiten.DrawWholeRenderTarget(context, to, ebiten.GeometryMatrixI(), color) ebiten.DrawWholeTexture(context, to.Texture(), ebiten.GeometryMatrixI(), color)
} }
func (s *SceneManager) GoTo(scene Scene) { func (s *SceneManager) GoTo(scene Scene) {

View File

@ -54,7 +54,7 @@ func drawTitleBackground(context ebiten.GraphicsContext, textures *Textures, c i
const textureWidth = 32 const textureWidth = 32
const textureHeight = 32 const textureHeight = 32
backgroundTextureId := textures.GetTexture("background") backgroundTexture := textures.GetTexture("background")
parts := []ebiten.TexturePart{} parts := []ebiten.TexturePart{}
for j := -1; j < ScreenHeight/textureHeight+1; j++ { for j := -1; j < ScreenHeight/textureHeight+1; j++ {
for i := 0; i < ScreenWidth/textureWidth+1; i++ { for i := 0; i < ScreenWidth/textureWidth+1; i++ {
@ -70,7 +70,7 @@ func drawTitleBackground(context ebiten.GraphicsContext, textures *Textures, c i
geo := ebiten.GeometryMatrixI() geo := ebiten.GeometryMatrixI()
geo.Concat(ebiten.TranslateGeometry(float64(dx), float64(dy))) geo.Concat(ebiten.TranslateGeometry(float64(dx), float64(dy)))
clr := ebiten.ColorMatrixI() clr := ebiten.ColorMatrixI()
context.Texture(backgroundTextureId).Draw(parts, geo, clr) context.DrawTexture(backgroundTexture, parts, geo, clr)
} }
func drawLogo(context ebiten.GraphicsContext, textures *Textures, str string) { func drawLogo(context ebiten.GraphicsContext, textures *Textures, str string) {

View File

@ -59,11 +59,11 @@ func (g *Game) Update(gr ebiten.GraphicsContext) error {
clr := ebiten.ScaleColor(color.RGBA{0xff, 0x40, 0x40, 0xff}) clr := ebiten.ScaleColor(color.RGBA{0xff, 0x40, 0x40, 0xff})
theta := 2.0 * math.Pi * float64(g.count%60) / 60.0 theta := 2.0 * math.Pi * float64(g.count%60) / 60.0
clr.Concat(ebiten.RotateHue(theta)) clr.Concat(ebiten.RotateHue(theta))
ebiten.DrawWholeRenderTarget(gr, g.brushRenderTarget, geo, clr) ebiten.DrawWholeTexture(gr, g.brushRenderTarget.Texture(), geo, clr)
gr.PopRenderTarget() gr.PopRenderTarget()
} }
ebiten.DrawWholeRenderTarget(gr, g.canvasRenderTarget, ebiten.GeometryMatrixI(), ebiten.ColorMatrixI()) ebiten.DrawWholeTexture(gr, g.canvasRenderTarget.Texture(), ebiten.GeometryMatrixI(), ebiten.ColorMatrixI())
ebitenutil.DebugPrint(gr, fmt.Sprintf("(%d, %d)", mx, my)) ebitenutil.DebugPrint(gr, fmt.Sprintf("(%d, %d)", mx, my))
return nil return nil

View File

@ -48,7 +48,7 @@ func (g *Game) Update(gr ebiten.GraphicsContext) error {
geo := ebiten.TranslateGeometry(-maxWidth/2, -float64(h)/2) geo := ebiten.TranslateGeometry(-maxWidth/2, -float64(h)/2)
geo.Concat(ebiten.ScaleGeometry(0.4, 0.4)) geo.Concat(ebiten.ScaleGeometry(0.4, 0.4))
geo.Concat(ebiten.TranslateGeometry(screenWidth/2, screenHeight/2)) geo.Concat(ebiten.TranslateGeometry(screenWidth/2, screenHeight/2))
gr.Texture(g.gophersTexture).Draw(parts, geo, ebiten.ColorMatrixI()) gr.DrawTexture(g.gophersTexture, parts, geo, ebiten.ColorMatrixI())
return nil return nil
} }

View File

@ -35,42 +35,26 @@ type TexturePart struct {
Src Rect Src Rect
} }
// A Drawer is the interface that draws itself.
type Drawer interface {
Draw(parts []TexturePart, geo GeometryMatrix, color ColorMatrix) error
}
// DrawWholeTexture draws the whole texture. // DrawWholeTexture draws the whole texture.
func DrawWholeTexture(g GraphicsContext, texture *Texture, geo GeometryMatrix, color ColorMatrix) error { func DrawWholeTexture(g GraphicsContext, texture *Texture, geo GeometryMatrix, color ColorMatrix) error {
w, h := texture.Size() w, h := texture.Size()
parts := []TexturePart{ parts := []TexturePart{
{Rect{0, 0, float64(w), float64(h)}, Rect{0, 0, float64(w), float64(h)}}, {Rect{0, 0, float64(w), float64(h)}, Rect{0, 0, float64(w), float64(h)}},
} }
return g.Texture(texture).Draw(parts, geo, color) return g.DrawTexture(texture, parts, geo, color)
}
// DrawWholeRenderTarget draws the whole render target.
func DrawWholeRenderTarget(g GraphicsContext, renderTarget *RenderTarget, geo GeometryMatrix, color ColorMatrix) error {
w, h := renderTarget.Size()
parts := []TexturePart{
{Rect{0, 0, float64(w), float64(h)}, Rect{0, 0, float64(w), float64(h)}},
}
return g.RenderTarget(renderTarget).Draw(parts, geo, color)
} }
// A GraphicsContext is the interface that means a context of rendering. // A GraphicsContext is the interface that means a context of rendering.
type GraphicsContext interface { type GraphicsContext interface {
Clear() error Clear() error
Fill(r, g, b uint8) error Fill(r, g, b uint8) error
Texture(texture *Texture) Drawer DrawTexture(texture *Texture, parts []TexturePart, geo GeometryMatrix, color ColorMatrix) error
RenderTarget(id *RenderTarget) Drawer
// TODO: ScreenRenderTarget() Drawer // TODO: ScreenRenderTarget() Drawer
PushRenderTarget(id *RenderTarget) PushRenderTarget(id *RenderTarget)
PopRenderTarget() PopRenderTarget()
} }
// Filter represents the type of filter to be used when a texture or a render // Filter represents the type of filter to be used when a texture is maginified or minified.
// target is maginified or minified.
type Filter int type Filter int
// Filters // Filters
@ -97,6 +81,11 @@ type RenderTarget struct {
texture *Texture texture *Texture
} }
// Texture returns the texture of the render target.
func (r *RenderTarget) Texture() *Texture {
return r.texture
}
// Size returns the size of the render target. // Size returns the size of the render target.
func (r *RenderTarget) Size() (width int, height int) { func (r *RenderTarget) Size() (width int, height int) {
return r.glRenderTarget.Width(), r.glRenderTarget.Height() return r.glRenderTarget.Width(), r.glRenderTarget.Height()

View File

@ -73,12 +73,9 @@ func (c *graphicsContext) Fill(r, g, b uint8) error {
return idsInstance.fillRenderTarget(c.currents[len(c.currents)-1], r, g, b) return idsInstance.fillRenderTarget(c.currents[len(c.currents)-1], r, g, b)
} }
func (c *graphicsContext) Texture(texture *Texture) Drawer { func (c *graphicsContext) DrawTexture(texture *Texture, parts []TexturePart, geo GeometryMatrix, color ColorMatrix) error {
return &textureWithContext{texture, c} current := c.currents[len(c.currents)-1]
} return idsInstance.drawTexture(current, texture, parts, geo, color)
func (c *graphicsContext) RenderTarget(renderTarget *RenderTarget) Drawer {
return &textureWithContext{renderTarget.texture, c}
} }
func (c *graphicsContext) PushRenderTarget(renderTarget *RenderTarget) { func (c *graphicsContext) PushRenderTarget(renderTarget *RenderTarget) {
@ -103,17 +100,7 @@ func (c *graphicsContext) postUpdate() {
scale := float64(c.screenScale) scale := float64(c.screenScale)
geo := GeometryMatrixI() geo := GeometryMatrixI()
geo.Concat(ScaleGeometry(scale, scale)) geo.Concat(ScaleGeometry(scale, scale))
DrawWholeRenderTarget(c, c.screen, geo, ColorMatrixI()) DrawWholeTexture(c, c.screen.texture, geo, ColorMatrixI())
gl.Flush() gl.Flush()
} }
type textureWithContext struct {
texture *Texture
context *graphicsContext
}
func (t *textureWithContext) Draw(parts []TexturePart, geo GeometryMatrix, color ColorMatrix) error {
current := t.context.currents[len(t.context.currents)-1]
return idsInstance.drawTexture(current, t.texture, parts, geo, color)
}

View File

@ -41,22 +41,9 @@ func (c *syncGraphicsContext) Fill(r, g, b uint8) (err error) {
return return
} }
func (c *syncGraphicsContext) Texture(texture *Texture) (d Drawer) { func (c *syncGraphicsContext) DrawTexture(texture *Texture, parts []TexturePart, geo GeometryMatrix, color ColorMatrix) (err error) {
c.syncer.Sync(func() { c.syncer.Sync(func() {
d = &drawer{ err = c.innerGraphicsContext.DrawTexture(texture, parts, geo, color)
syncer: c.syncer,
innerDrawer: c.innerGraphicsContext.Texture(texture),
}
})
return
}
func (c *syncGraphicsContext) RenderTarget(renderTarget *RenderTarget) (d Drawer) {
c.syncer.Sync(func() {
d = &drawer{
syncer: c.syncer,
innerDrawer: c.innerGraphicsContext.RenderTarget(renderTarget),
}
}) })
return return
} }
@ -72,17 +59,3 @@ func (c *syncGraphicsContext) PushRenderTarget(renderTarget *RenderTarget) {
c.innerGraphicsContext.PushRenderTarget(renderTarget) c.innerGraphicsContext.PushRenderTarget(renderTarget)
}) })
} }
type drawer struct {
syncer syncer
innerDrawer Drawer
}
var _ Drawer = new(drawer)
func (d *drawer) Draw(parts []TexturePart, geo GeometryMatrix, color ColorMatrix) (err error) {
d.syncer.Sync(func() {
err = d.innerDrawer.Draw(parts, geo, color)
})
return
}