ebiten/graphics/context.go

110 lines
2.5 KiB
Go
Raw Normal View History

2013-12-02 13:45:10 +01:00
package graphics
import (
"github.com/hajimehoshi/go-ebiten/graphics/matrix"
)
2013-12-13 22:10:24 +01:00
type Context interface {
2013-12-02 13:45:10 +01:00
Clear()
Fill(r, g, b uint8)
// TODO: Refacotring
2013-12-02 13:45:10 +01:00
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)
ResetOffscreen()
SetOffscreen(id RenderTargetId)
// TODO: glTextureSubImage2D
2013-12-02 13:45:10 +01:00
}
2013-12-13 22:10:24 +01:00
type LazyContext struct {
funcs []func(Context)
2013-12-02 13:45:10 +01:00
}
2013-12-13 22:10:24 +01:00
func NewLazyContext() *LazyContext {
return &LazyContext{
funcs: []func(Context){},
2013-12-02 13:45:10 +01:00
}
}
2013-12-13 22:10:24 +01:00
func (c *LazyContext) Flush(actual Context) {
2013-12-02 13:45:10 +01:00
for _, f := range c.funcs {
f(actual)
}
2013-12-13 22:10:24 +01:00
c.funcs = []func(Context){}
2013-12-02 13:45:10 +01:00
}
2013-12-13 22:10:24 +01:00
func (c *LazyContext) Clear() {
c.funcs = append(c.funcs, func(actual Context) {
2013-12-02 13:45:10 +01:00
actual.Clear()
})
}
2013-12-13 22:10:24 +01:00
func (c *LazyContext) Fill(r, g, b uint8) {
c.funcs = append(c.funcs, func(actual Context) {
2013-12-02 13:45:10 +01:00
actual.Fill(r, g, b)
})
}
2013-12-13 22:10:24 +01:00
func (c *LazyContext) DrawTexture(id TextureId,
2013-12-02 13:45:10 +01:00
geometryMatrix matrix.Geometry,
colorMatrix matrix.Color) {
2013-12-13 22:10:24 +01:00
c.funcs = append(c.funcs, func(actual Context) {
2013-12-02 13:45:10 +01:00
actual.DrawTexture(id, geometryMatrix, colorMatrix)
})
}
2013-12-13 22:10:24 +01:00
func (c *LazyContext) DrawRenderTarget(id RenderTargetId,
2013-12-02 13:45:10 +01:00
geometryMatrix matrix.Geometry,
colorMatrix matrix.Color) {
2013-12-13 22:10:24 +01:00
c.funcs = append(c.funcs, func(actual Context) {
2013-12-02 13:45:10 +01:00
actual.DrawRenderTarget(id, geometryMatrix, colorMatrix)
})
}
2013-12-13 22:10:24 +01:00
func (c *LazyContext) DrawTextureParts(id TextureId,
2013-12-02 13:45:10 +01:00
parts []TexturePart,
geometryMatrix matrix.Geometry,
colorMatrix matrix.Color) {
2013-12-13 21:55:13 +01:00
parts2 := make([]TexturePart, len(parts))
copy(parts2, parts)
2013-12-13 22:10:24 +01:00
c.funcs = append(c.funcs, func(actual Context) {
2013-12-13 21:55:13 +01:00
actual.DrawTextureParts(id, parts2, geometryMatrix, colorMatrix)
2013-12-02 13:45:10 +01:00
})
}
2013-12-13 22:10:24 +01:00
func (c *LazyContext) DrawRenderTargetParts(id RenderTargetId,
2013-12-02 13:45:10 +01:00
parts []TexturePart,
geometryMatrix matrix.Geometry,
colorMatrix matrix.Color) {
2013-12-13 21:55:13 +01:00
parts2 := make([]TexturePart, len(parts))
copy(parts2, parts)
2013-12-13 22:10:24 +01:00
c.funcs = append(c.funcs, func(actual Context) {
2013-12-13 21:55:13 +01:00
actual.DrawRenderTargetParts(id, parts2, geometryMatrix, colorMatrix)
2013-12-02 13:45:10 +01:00
})
}
2013-12-13 22:10:24 +01:00
func (c *LazyContext) ResetOffscreen() {
c.funcs = append(c.funcs, func(actual Context) {
2013-12-02 13:45:10 +01:00
actual.ResetOffscreen()
})
}
2013-12-13 22:10:24 +01:00
func (c *LazyContext) SetOffscreen(id RenderTargetId) {
c.funcs = append(c.funcs, func(actual Context) {
2013-12-02 13:45:10 +01:00
actual.SetOffscreen(id)
})
}