Change API of graphics.Context

This commit is contained in:
Hajime Hoshi 2014-05-01 22:45:48 +09:00
parent 2d6818c81e
commit 562d04cacc
7 changed files with 51 additions and 46 deletions

View File

@ -45,7 +45,7 @@ func drawText(context graphics.Context, str string, x, y, scale int, clr color.C
geoMat.Translate(float64(x), float64(y))
clrMat := matrix.IdentityColor()
clrMat.Scale(clr)
context.DrawTextureParts(fontTextureId, parts, geoMat, clrMat)
context.Texture(fontTextureId).DrawParts(parts, geoMat, clrMat)
}
func drawTextWithShadow(context graphics.Context, str string, x, y, scale int, clr color.Color) {

View File

@ -107,7 +107,7 @@ func (s *GameScene) Draw(context graphics.Context) {
geoMat.Translate(20, 20) // magic number?
colorMat := matrix.IdentityColor()
colorMat.Scale(color.RGBA{0, 0, 0, 0x80})
context.DrawTexture(field, geoMat, colorMat)
context.Texture(field).Draw(geoMat, colorMat)
geoMat = matrix.IdentityGeometry()
geoMat.Translate(20, 20)

View File

@ -144,7 +144,7 @@ func drawBlocks(context graphics.Context, blocks [][]BlockType, geo matrix.Geome
}
}
blocksTexture := drawInfo.textures["blocks"]
context.DrawTextureParts(blocksTexture, parts, geo, matrix.IdentityColor())
context.Texture(blocksTexture).DrawParts(parts, geo, matrix.IdentityColor())
}
func (p *Piece) InitialPosition() (int, int) {

View File

@ -62,11 +62,11 @@ func (s *SceneManager) Draw(context graphics.Context) {
context.ResetOffscreen()
color := matrix.IdentityColor()
context.DrawRenderTarget(from, matrix.IdentityGeometry(), color)
context.RenderTarget(from).Draw(matrix.IdentityGeometry(), color)
alpha := float64(s.transitionCount) / float64(transitionMaxCount)
color.Elements[3][3] = alpha
context.DrawRenderTarget(to, matrix.IdentityGeometry(), color)
context.RenderTarget(to).Draw(matrix.IdentityGeometry(), color)
}
func (s *SceneManager) GoTo(scene Scene) {

View File

@ -57,7 +57,7 @@ func drawTitleBackground(context graphics.Context, c int) {
geo := matrix.IdentityGeometry()
geo.Translate(float64(dx), float64(dy))
clr := matrix.IdentityColor()
context.DrawTextureParts(backgroundTextureId, parts, geo, clr)
context.Texture(backgroundTextureId).DrawParts(parts, geo, clr)
}
func drawLogo(context graphics.Context, str string) {

View File

@ -4,24 +4,19 @@ import (
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
)
type Drawer interface {
Draw(geometryMatrix matrix.Geometry,
colorMatrix matrix.Color)
DrawParts(parts []TexturePart,
geometryMatrix matrix.Geometry,
colorMatrix matrix.Color)
}
type Context interface {
Clear()
Fill(r, g, b uint8)
// TODO: Refacotring
DrawTexture(id TextureId,
geometryMatrix matrix.Geometry,
colorMatrix matrix.Color)
DrawRenderTarget(id RenderTargetId,
geometryMatrix matrix.Geometry,
colorMatrix matrix.Color)
DrawTextureParts(id TextureId,
parts []TexturePart,
geometryMatrix matrix.Geometry,
colorMatrix matrix.Color)
DrawRenderTargetParts(id RenderTargetId,
parts []TexturePart,
geometryMatrix matrix.Geometry,
colorMatrix matrix.Color)
Texture(id TextureId) Drawer
RenderTarget(id RenderTargetId) Drawer
ResetOffscreen()
SetOffscreen(id RenderTargetId)

View File

@ -54,41 +54,25 @@ func (context *Context) Update(draw func(graphics.Context)) {
scale := float64(context.screenScale)
geometryMatrix := matrix.IdentityGeometry()
geometryMatrix.Scale(scale, scale)
context.DrawRenderTarget(context.screenId,
geometryMatrix, matrix.IdentityColor())
context.RenderTarget(context.screenId).Draw(geometryMatrix, matrix.IdentityColor())
flush()
}
func (context *Context) Clear() {
context.Fill(0, 0, 0)
func (c *Context) Clear() {
c.Fill(0, 0, 0)
}
func (context *Context) Fill(r, g, b uint8) {
context.ids.FillRenderTarget(context.currentId, r, g, b)
func (c *Context) Fill(r, g, b uint8) {
c.ids.FillRenderTarget(c.currentId, r, g, b)
}
func (context *Context) DrawTexture(
id graphics.TextureId, geo matrix.Geometry, color matrix.Color) {
context.ids.DrawTexture(context.currentId, id, geo, color)
func (c *Context) Texture(id graphics.TextureId) graphics.Drawer {
return &TextureWithContext{id, c}
}
func (context *Context) DrawRenderTarget(
id graphics.RenderTargetId,
geo matrix.Geometry, color matrix.Color) {
context.ids.DrawRenderTarget(context.currentId, id, geo, color)
}
func (context *Context) DrawTextureParts(
id graphics.TextureId, parts []graphics.TexturePart,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
context.ids.DrawTextureParts(context.currentId, id, parts, geometryMatrix, colorMatrix)
}
func (context *Context) DrawRenderTargetParts(
id graphics.RenderTargetId, parts []graphics.TexturePart,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
context.ids.DrawRenderTargetParts(context.currentId, id, parts, geometryMatrix, colorMatrix)
func (c *Context) RenderTarget(id graphics.RenderTargetId) graphics.Drawer {
return &RenderTargetWithContext{id, c}
}
func (context *Context) ResetOffscreen() {
@ -98,3 +82,29 @@ func (context *Context) ResetOffscreen() {
func (context *Context) SetOffscreen(renderTargetId graphics.RenderTargetId) {
context.currentId = renderTargetId
}
type TextureWithContext struct {
id graphics.TextureId
context *Context
}
func (t *TextureWithContext) Draw(geo matrix.Geometry, color matrix.Color) {
t.context.ids.DrawTexture(t.context.currentId, t.id, geo, color)
}
func (t *TextureWithContext) DrawParts(parts []graphics.TexturePart, geo matrix.Geometry, color matrix.Color) {
t.context.ids.DrawTextureParts(t.context.currentId, t.id, parts, geo, color)
}
type RenderTargetWithContext struct {
id graphics.RenderTargetId
context *Context
}
func (r *RenderTargetWithContext) Draw(geo matrix.Geometry, color matrix.Color) {
r.context.ids.DrawRenderTarget(r.context.currentId, r.id, geo, color)
}
func (r *RenderTargetWithContext) DrawParts(parts []graphics.TexturePart, geo matrix.Geometry, color matrix.Color) {
r.context.ids.DrawRenderTargetParts(r.context.currentId, r.id, parts, geo, color)
}