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)))
clrm := ebiten.ColorMatrixI()
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) {

View File

@ -58,7 +58,7 @@ func drawText(context ebiten.GraphicsContext, textures *Textures, str string, x,
geoMat.Concat(ebiten.TranslateGeometry(float64(x), float64(y)))
clrMat := ebiten.ColorMatrixI()
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) {

View File

@ -153,7 +153,7 @@ func drawBlocks(context ebiten.GraphicsContext, textures *Textures, blocks [][]B
}
}
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) {

View File

@ -78,11 +78,11 @@ func (s *SceneManager) Draw(context ebiten.GraphicsContext, textures *Textures)
context.PopRenderTarget()
color := ebiten.ColorMatrixI()
ebiten.DrawWholeRenderTarget(context, from, ebiten.GeometryMatrixI(), color)
ebiten.DrawWholeTexture(context, from.Texture(), ebiten.GeometryMatrixI(), color)
alpha := float64(s.transitionCount) / float64(transitionMaxCount)
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) {

View File

@ -54,7 +54,7 @@ func drawTitleBackground(context ebiten.GraphicsContext, textures *Textures, c i
const textureWidth = 32
const textureHeight = 32
backgroundTextureId := textures.GetTexture("background")
backgroundTexture := textures.GetTexture("background")
parts := []ebiten.TexturePart{}
for j := -1; j < ScreenHeight/textureHeight+1; j++ {
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.Concat(ebiten.TranslateGeometry(float64(dx), float64(dy)))
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) {

View File

@ -59,11 +59,11 @@ func (g *Game) Update(gr ebiten.GraphicsContext) error {
clr := ebiten.ScaleColor(color.RGBA{0xff, 0x40, 0x40, 0xff})
theta := 2.0 * math.Pi * float64(g.count%60) / 60.0
clr.Concat(ebiten.RotateHue(theta))
ebiten.DrawWholeRenderTarget(gr, g.brushRenderTarget, geo, clr)
ebiten.DrawWholeTexture(gr, g.brushRenderTarget.Texture(), geo, clr)
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))
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.Concat(ebiten.ScaleGeometry(0.4, 0.4))
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
}

View File

@ -35,42 +35,26 @@ type TexturePart struct {
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.
func DrawWholeTexture(g GraphicsContext, texture *Texture, geo GeometryMatrix, color ColorMatrix) error {
w, h := texture.Size()
parts := []TexturePart{
{Rect{0, 0, float64(w), float64(h)}, Rect{0, 0, float64(w), float64(h)}},
}
return g.Texture(texture).Draw(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)
return g.DrawTexture(texture, parts, geo, color)
}
// A GraphicsContext is the interface that means a context of rendering.
type GraphicsContext interface {
Clear() error
Fill(r, g, b uint8) error
Texture(texture *Texture) Drawer
RenderTarget(id *RenderTarget) Drawer
DrawTexture(texture *Texture, parts []TexturePart, geo GeometryMatrix, color ColorMatrix) error
// TODO: ScreenRenderTarget() Drawer
PushRenderTarget(id *RenderTarget)
PopRenderTarget()
}
// Filter represents the type of filter to be used when a texture or a render
// target is maginified or minified.
// Filter represents the type of filter to be used when a texture is maginified or minified.
type Filter int
// Filters
@ -97,6 +81,11 @@ type RenderTarget struct {
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.
func (r *RenderTarget) Size() (width int, height int) {
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)
}
func (c *graphicsContext) Texture(texture *Texture) Drawer {
return &textureWithContext{texture, c}
}
func (c *graphicsContext) RenderTarget(renderTarget *RenderTarget) Drawer {
return &textureWithContext{renderTarget.texture, c}
func (c *graphicsContext) DrawTexture(texture *Texture, parts []TexturePart, geo GeometryMatrix, color ColorMatrix) error {
current := c.currents[len(c.currents)-1]
return idsInstance.drawTexture(current, texture, parts, geo, color)
}
func (c *graphicsContext) PushRenderTarget(renderTarget *RenderTarget) {
@ -103,17 +100,7 @@ func (c *graphicsContext) postUpdate() {
scale := float64(c.screenScale)
geo := GeometryMatrixI()
geo.Concat(ScaleGeometry(scale, scale))
DrawWholeRenderTarget(c, c.screen, geo, ColorMatrixI())
DrawWholeTexture(c, c.screen.texture, geo, ColorMatrixI())
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
}
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() {
d = &drawer{
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),
}
err = c.innerGraphicsContext.DrawTexture(texture, parts, geo, color)
})
return
}
@ -72,17 +59,3 @@ func (c *syncGraphicsContext) PushRenderTarget(renderTarget *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
}