Remove graphics.Drawer.DrawParts

This commit is contained in:
Hajime Hoshi 2014-05-03 13:58:18 +09:00
parent f0356748be
commit 322d0f1924
9 changed files with 77 additions and 57 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)) geoMat.Translate(float64(x), float64(y))
clrMat := matrix.IdentityColor() clrMat := matrix.IdentityColor()
clrMat.Scale(clr) clrMat.Scale(clr)
context.Texture(fontTextureId).DrawParts(parts, geoMat, clrMat) context.Texture(fontTextureId).Draw(parts, geoMat, clrMat)
} }
func drawTextWithShadow(context graphics.Context, str string, x, y, scale int, clr color.Color) { func drawTextWithShadow(context graphics.Context, str string, x, y, scale int, clr color.Color) {

View File

@ -102,19 +102,29 @@ func (s *GameScene) Draw(context graphics.Context) {
field := drawInfo.textures["empty"] field := drawInfo.textures["empty"]
geoMat := matrix.IdentityGeometry() geoMat := matrix.IdentityGeometry()
geoMat.Scale(float64(fieldWidth)/float64(emptyWidth), geoMat.Scale(
float64(fieldWidth)/float64(emptyWidth),
float64(fieldHeight)/float64(emptyHeight)) float64(fieldHeight)/float64(emptyHeight))
geoMat.Translate(20, 20) // magic number? geoMat.Translate(20, 20) // magic number?
colorMat := matrix.IdentityColor() colorMat := matrix.IdentityColor()
colorMat.Scale(color.RGBA{0, 0, 0, 0x80}) colorMat.Scale(color.RGBA{0, 0, 0, 0x80})
context.Texture(field).Draw(geoMat, colorMat) graphics.DrawWhole(
context.Texture(field),
emptyWidth,
emptyHeight,
geoMat,
colorMat)
geoMat = matrix.IdentityGeometry() geoMat = matrix.IdentityGeometry()
geoMat.Translate(20, 20) geoMat.Translate(20, 20)
s.field.Draw(context, geoMat) s.field.Draw(context, geoMat)
if s.currentPiece != nil { if s.currentPiece != nil {
s.currentPiece.Draw(context, 20, 20, s.currentPiece.Draw(
s.currentPieceX, s.currentPieceY, s.currentPieceAngle) context,
20, 20,
s.currentPieceX,
s.currentPieceY,
s.currentPieceAngle)
} }
} }

View File

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

View File

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

View File

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

View File

@ -18,13 +18,22 @@ type TexturePart struct {
} }
type Drawer interface { type Drawer interface {
Draw(geometryMatrix matrix.Geometry, Draw(parts []TexturePart,
colorMatrix matrix.Color)
DrawParts(parts []TexturePart,
geometryMatrix matrix.Geometry, geometryMatrix matrix.Geometry,
colorMatrix matrix.Color) colorMatrix matrix.Color)
} }
func DrawWhole(
drawer Drawer,
width, height int,
geo matrix.Geometry,
color matrix.Color) {
parts := []TexturePart{
{0, 0, Rect{0, 0, width, height}},
}
drawer.Draw(parts, geo, color)
}
type Context interface { type Context interface {
Clear() Clear()
Fill(r, g, b uint8) Fill(r, g, b uint8)

View File

@ -6,17 +6,21 @@ import (
) )
type Context struct { type Context struct {
screenId graphics.RenderTargetId screenId graphics.RenderTargetId
mainId graphics.RenderTargetId mainId graphics.RenderTargetId
currentId graphics.RenderTargetId currentId graphics.RenderTargetId
ids *ids ids *ids
screenScale int screenWidth int
screenHeight int
screenScale int
} }
func newContext(ids *ids, screenWidth, screenHeight, screenScale int) *Context { func newContext(ids *ids, screenWidth, screenHeight, screenScale int) *Context {
context := &Context{ context := &Context{
ids: ids, ids: ids,
screenScale: screenScale, screenWidth: screenWidth,
screenHeight: screenHeight,
screenScale: screenScale,
} }
mainRenderTarget := newRTWithCurrentFramebuffer( mainRenderTarget := newRTWithCurrentFramebuffer(
screenWidth*screenScale, screenWidth*screenScale,
@ -37,25 +41,29 @@ func newContext(ids *ids, screenWidth, screenHeight, screenScale int) *Context {
return context return context
} }
func (context *Context) Dispose() { func (c *Context) Dispose() {
// TODO: remove main framebuffer? // TODO: remove main framebuffer?
context.ids.deleteRenderTarget(context.screenId) c.ids.deleteRenderTarget(c.screenId)
} }
func (context *Context) Update(draw func(graphics.Context)) { func (c *Context) Update(draw func(graphics.Context)) {
context.ResetOffscreen() c.ResetOffscreen()
context.Clear() c.Clear()
draw(context) draw(c)
context.SetOffscreen(context.mainId) c.SetOffscreen(c.mainId)
context.Clear() c.Clear()
scale := float64(context.screenScale) scale := float64(c.screenScale)
geometryMatrix := matrix.IdentityGeometry() geo := matrix.IdentityGeometry()
geometryMatrix.Scale(scale, scale) geo.Scale(scale, scale)
context.RenderTarget(context.screenId).Draw( graphics.DrawWhole(
geometryMatrix, matrix.IdentityColor()) c.RenderTarget(c.screenId),
c.screenWidth,
c.screenHeight,
geo,
matrix.IdentityColor())
flush() flush()
} }
@ -76,12 +84,12 @@ func (c *Context) RenderTarget(id graphics.RenderTargetId) graphics.Drawer {
return &TextureWithContext{c.ids.toTexture(id), c} return &TextureWithContext{c.ids.toTexture(id), c}
} }
func (context *Context) ResetOffscreen() { func (c *Context) ResetOffscreen() {
context.currentId = context.screenId c.currentId = c.screenId
} }
func (context *Context) SetOffscreen(renderTargetId graphics.RenderTargetId) { func (c *Context) SetOffscreen(renderTargetId graphics.RenderTargetId) {
context.currentId = renderTargetId c.currentId = renderTargetId
} }
type TextureWithContext struct { type TextureWithContext struct {
@ -89,13 +97,9 @@ type TextureWithContext struct {
context *Context context *Context
} }
func (t *TextureWithContext) Draw(geo matrix.Geometry, color matrix.Color) { func (t *TextureWithContext) Draw(
t.context.ids.drawTexture(t.context.currentId, t.id, geo, color)
}
func (t *TextureWithContext) DrawParts(
parts []graphics.TexturePart, parts []graphics.TexturePart,
geo matrix.Geometry, geo matrix.Geometry,
color matrix.Color) { color matrix.Color) {
t.context.ids.drawTextureParts(t.context.currentId, t.id, parts, geo, color) t.context.ids.drawTexture(t.context.currentId, t.id, parts, geo, color)
} }

View File

@ -9,8 +9,7 @@ import (
var idsInstance *ids = newIds() var idsInstance *ids = newIds()
func CreateContext( func NewContext(screenWidth, screenHeight, screenScale int) *Context {
screenWidth, screenHeight, screenScale int) *Context {
return newContext(idsInstance, screenWidth, screenHeight, screenScale) return newContext(idsInstance, screenWidth, screenHeight, screenScale)
} }
@ -135,18 +134,6 @@ func (i *ids) fillRenderTarget(id graphics.RenderTargetId, r, g, b uint8) {
} }
func (i *ids) drawTexture( func (i *ids) drawTexture(
target graphics.RenderTargetId,
id graphics.TextureId,
geo matrix.Geometry,
color matrix.Color) {
texture := i.textureAt(id)
parts := []graphics.TexturePart{
{0, 0, graphics.Rect{0, 0, texture.width, texture.height}},
}
i.renderTargetAt(target).drawTexture(texture, parts, geo, color)
}
func (i *ids) drawTextureParts(
target graphics.RenderTargetId, target graphics.RenderTargetId,
id graphics.TextureId, id graphics.TextureId,
parts []graphics.TexturePart, geo matrix.Geometry, color matrix.Color) { parts []graphics.TexturePart, geo matrix.Geometry, color matrix.Color) {

View File

@ -69,7 +69,7 @@ func (w *GameWindow) run(sharedGLContext *C.NSOpenGLContext) {
close(ch) close(ch)
C.UseGLContext(glContext) C.UseGLContext(glContext)
context := opengl.CreateContext( context := opengl.NewContext(
w.screenWidth, w.screenHeight, w.screenScale) w.screenWidth, w.screenHeight, w.screenScale)
C.UnuseGLContext() C.UnuseGLContext()