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))
clrMat := matrix.IdentityColor()
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) {

View File

@ -102,19 +102,29 @@ func (s *GameScene) Draw(context graphics.Context) {
field := drawInfo.textures["empty"]
geoMat := matrix.IdentityGeometry()
geoMat.Scale(float64(fieldWidth)/float64(emptyWidth),
geoMat.Scale(
float64(fieldWidth)/float64(emptyWidth),
float64(fieldHeight)/float64(emptyHeight))
geoMat.Translate(20, 20) // magic number?
colorMat := matrix.IdentityColor()
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.Translate(20, 20)
s.field.Draw(context, geoMat)
if s.currentPiece != nil {
s.currentPiece.Draw(context, 20, 20,
s.currentPieceX, s.currentPieceY, s.currentPieceAngle)
s.currentPiece.Draw(
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"]
context.Texture(blocksTexture).DrawParts(parts, geo, matrix.IdentityColor())
context.Texture(blocksTexture).Draw(parts, geo, matrix.IdentityColor())
}
func (p *Piece) InitialPosition() (int, int) {

View File

@ -62,11 +62,21 @@ func (s *SceneManager) Draw(context graphics.Context) {
context.ResetOffscreen()
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)
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) {

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.Texture(backgroundTextureId).DrawParts(parts, geo, clr)
context.Texture(backgroundTextureId).Draw(parts, geo, clr)
}
func drawLogo(context graphics.Context, str string) {

View File

@ -18,13 +18,22 @@ type TexturePart struct {
}
type Drawer interface {
Draw(geometryMatrix matrix.Geometry,
colorMatrix matrix.Color)
DrawParts(parts []TexturePart,
Draw(parts []TexturePart,
geometryMatrix matrix.Geometry,
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 {
Clear()
Fill(r, g, b uint8)

View File

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

View File

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