Rename: Canvas -> Context

This commit is contained in:
Hajime Hoshi 2013-12-14 06:10:24 +09:00
parent 4d62fb0b01
commit f61d9280e4
9 changed files with 84 additions and 84 deletions

View File

@ -122,7 +122,7 @@ func (game *Game) Update() {
game.inputPrevX, game.inputPrevY = game.inputX, game.inputY game.inputPrevX, game.inputPrevY = game.inputX, game.inputY
} }
func (game *Game) Draw(g graphics.Canvas) { func (game *Game) Draw(g graphics.Context) {
if !game.isInitialized() { if !game.isInitialized() {
return return
} }
@ -144,7 +144,7 @@ func (game *Game) Draw(g graphics.Canvas) {
g.DrawRenderTarget(whole, wholeGeo, wholeColor) g.DrawRenderTarget(whole, wholeGeo, wholeColor)
} }
func (game *Game) drawText(g graphics.Canvas, text string, x, y int, clr color.Color) { func (game *Game) drawText(g graphics.Context, text string, x, y int, clr color.Color) {
const letterWidth = 6 const letterWidth = 6
const letterHeight = 16 const letterHeight = 16
@ -177,6 +177,6 @@ func (game *Game) drawText(g graphics.Canvas, text string, x, y int, clr color.C
geometryMatrix, colorMatrix) geometryMatrix, colorMatrix)
} }
func (game *Game) drawTexture(g graphics.Canvas, geo matrix.Geometry, color matrix.Color) { func (game *Game) drawTexture(g graphics.Context, geo matrix.Geometry, color matrix.Color) {
g.DrawTexture(game.drawInfo.textures["ebiten"], geo, color) g.DrawTexture(game.drawInfo.textures["ebiten"], geo, color)
} }

View File

@ -62,7 +62,7 @@ func main() {
}() }()
} }
drawing := make(chan *graphics.LazyCanvas) drawing := make(chan *graphics.LazyContext)
quit := make(chan struct{}) quit := make(chan struct{})
go func() { go func() {
defer close(quit) defer close(quit)
@ -89,9 +89,9 @@ func main() {
return return
case <-tick: case <-tick:
game.Update() game.Update()
case canvas := <-drawing: case context := <-drawing:
game.Draw(canvas) game.Draw(context)
drawing <- canvas drawing <- context
} }
} }
}() }()
@ -100,11 +100,11 @@ func main() {
ui.PollEvents() ui.PollEvents()
select { select {
default: default:
drawing <- graphics.NewLazyCanvas() drawing <- graphics.NewLazyContext()
canvas := <-drawing context := <-drawing
window.Draw(func(actualCanvas graphics.Canvas) { window.Draw(func(actualContext graphics.Context) {
canvas.Flush(actualCanvas) context.Flush(actualContext)
}) })
after := time.After(time.Duration(int64(time.Second) / 120)) after := time.After(time.Duration(int64(time.Second) / 120))
ui.PollEvents() ui.PollEvents()

View File

@ -4,7 +4,7 @@ import (
"github.com/hajimehoshi/go-ebiten/graphics/matrix" "github.com/hajimehoshi/go-ebiten/graphics/matrix"
) )
type Canvas interface { type Context interface {
Clear() Clear()
Fill(r, g, b uint8) Fill(r, g, b uint8)
// TODO: Refacotring // TODO: Refacotring
@ -27,81 +27,81 @@ type Canvas interface {
SetOffscreen(id RenderTargetId) SetOffscreen(id RenderTargetId)
} }
type LazyCanvas struct { type LazyContext struct {
funcs []func(Canvas) funcs []func(Context)
} }
func NewLazyCanvas() *LazyCanvas { func NewLazyContext() *LazyContext {
return &LazyCanvas{ return &LazyContext{
funcs: []func(Canvas){}, funcs: []func(Context){},
} }
} }
func (c *LazyCanvas) Flush(actual Canvas) { func (c *LazyContext) Flush(actual Context) {
for _, f := range c.funcs { for _, f := range c.funcs {
f(actual) f(actual)
} }
c.funcs = []func(Canvas){} c.funcs = []func(Context){}
} }
func (c *LazyCanvas) Clear() { func (c *LazyContext) Clear() {
c.funcs = append(c.funcs, func(actual Canvas) { c.funcs = append(c.funcs, func(actual Context) {
actual.Clear() actual.Clear()
}) })
} }
func (c *LazyCanvas) Fill(r, g, b uint8) { func (c *LazyContext) Fill(r, g, b uint8) {
c.funcs = append(c.funcs, func(actual Canvas) { c.funcs = append(c.funcs, func(actual Context) {
actual.Fill(r, g, b) actual.Fill(r, g, b)
}) })
} }
func (c *LazyCanvas) DrawTexture(id TextureId, func (c *LazyContext) DrawTexture(id TextureId,
geometryMatrix matrix.Geometry, geometryMatrix matrix.Geometry,
colorMatrix matrix.Color) { colorMatrix matrix.Color) {
c.funcs = append(c.funcs, func(actual Canvas) { c.funcs = append(c.funcs, func(actual Context) {
actual.DrawTexture(id, geometryMatrix, colorMatrix) actual.DrawTexture(id, geometryMatrix, colorMatrix)
}) })
} }
func (c *LazyCanvas) DrawRenderTarget(id RenderTargetId, func (c *LazyContext) DrawRenderTarget(id RenderTargetId,
geometryMatrix matrix.Geometry, geometryMatrix matrix.Geometry,
colorMatrix matrix.Color) { colorMatrix matrix.Color) {
c.funcs = append(c.funcs, func(actual Canvas) { c.funcs = append(c.funcs, func(actual Context) {
actual.DrawRenderTarget(id, geometryMatrix, colorMatrix) actual.DrawRenderTarget(id, geometryMatrix, colorMatrix)
}) })
} }
func (c *LazyCanvas) DrawTextureParts(id TextureId, func (c *LazyContext) DrawTextureParts(id TextureId,
parts []TexturePart, parts []TexturePart,
geometryMatrix matrix.Geometry, geometryMatrix matrix.Geometry,
colorMatrix matrix.Color) { colorMatrix matrix.Color) {
parts2 := make([]TexturePart, len(parts)) parts2 := make([]TexturePart, len(parts))
copy(parts2, parts) copy(parts2, parts)
c.funcs = append(c.funcs, func(actual Canvas) { c.funcs = append(c.funcs, func(actual Context) {
actual.DrawTextureParts(id, parts2, geometryMatrix, colorMatrix) actual.DrawTextureParts(id, parts2, geometryMatrix, colorMatrix)
}) })
} }
func (c *LazyCanvas) DrawRenderTargetParts(id RenderTargetId, func (c *LazyContext) DrawRenderTargetParts(id RenderTargetId,
parts []TexturePart, parts []TexturePart,
geometryMatrix matrix.Geometry, geometryMatrix matrix.Geometry,
colorMatrix matrix.Color) { colorMatrix matrix.Color) {
parts2 := make([]TexturePart, len(parts)) parts2 := make([]TexturePart, len(parts))
copy(parts2, parts) copy(parts2, parts)
c.funcs = append(c.funcs, func(actual Canvas) { c.funcs = append(c.funcs, func(actual Context) {
actual.DrawRenderTargetParts(id, parts2, geometryMatrix, colorMatrix) actual.DrawRenderTargetParts(id, parts2, geometryMatrix, colorMatrix)
}) })
} }
func (c *LazyCanvas) ResetOffscreen() { func (c *LazyContext) ResetOffscreen() {
c.funcs = append(c.funcs, func(actual Canvas) { c.funcs = append(c.funcs, func(actual Context) {
actual.ResetOffscreen() actual.ResetOffscreen()
}) })
} }
func (c *LazyCanvas) SetOffscreen(id RenderTargetId) { func (c *LazyContext) SetOffscreen(id RenderTargetId) {
c.funcs = append(c.funcs, func(actual Canvas) { c.funcs = append(c.funcs, func(actual Context) {
actual.SetOffscreen(id) actual.SetOffscreen(id)
}) })
} }

View File

@ -13,56 +13,56 @@ import (
"math" "math"
) )
type Canvas struct { type Context struct {
screenId graphics.RenderTargetId screenId graphics.RenderTargetId
ids *ids ids *ids
offscreen *offscreen.Offscreen offscreen *offscreen.Offscreen
screenScale int screenScale int
} }
func newCanvas(ids *ids, screenWidth, screenHeight, screenScale int) *Canvas { func newContext(ids *ids, screenWidth, screenHeight, screenScale int) *Context {
canvas := &Canvas{ context := &Context{
ids: ids, ids: ids,
offscreen: offscreen.New(screenWidth, screenHeight, screenScale), offscreen: offscreen.New(screenWidth, screenHeight, screenScale),
screenScale: screenScale, screenScale: screenScale,
} }
var err error var err error
canvas.screenId, err = ids.CreateRenderTarget( context.screenId, err = ids.CreateRenderTarget(
screenWidth, screenHeight, texture.FilterNearest) screenWidth, screenHeight, texture.FilterNearest)
if err != nil { if err != nil {
panic("initializing the offscreen failed: " + err.Error()) panic("initializing the offscreen failed: " + err.Error())
} }
return canvas return context
} }
func (canvas *Canvas) update(draw func(graphics.Canvas)) { func (context *Context) update(draw func(graphics.Context)) {
C.glEnable(C.GL_TEXTURE_2D) C.glEnable(C.GL_TEXTURE_2D)
C.glEnable(C.GL_BLEND) C.glEnable(C.GL_BLEND)
canvas.ResetOffscreen() context.ResetOffscreen()
canvas.Clear() context.Clear()
draw(canvas) draw(context)
C.glFlush() C.glFlush()
canvas.offscreen.SetMainFramebuffer() context.offscreen.SetMainFramebuffer()
canvas.Clear() context.Clear()
scale := float64(canvas.screenScale) scale := float64(context.screenScale)
geometryMatrix := matrix.IdentityGeometry() geometryMatrix := matrix.IdentityGeometry()
geometryMatrix.Scale(scale, scale) geometryMatrix.Scale(scale, scale)
canvas.DrawRenderTarget(canvas.screenId, context.DrawRenderTarget(context.screenId,
geometryMatrix, matrix.IdentityColor()) geometryMatrix, matrix.IdentityColor())
C.glFlush() C.glFlush()
} }
func (canvas *Canvas) Clear() { func (context *Context) Clear() {
canvas.Fill(0, 0, 0) context.Fill(0, 0, 0)
} }
func (canvas *Canvas) Fill(r, g, b uint8) { func (context *Context) Fill(r, g, b uint8) {
const max = float64(math.MaxUint8) const max = float64(math.MaxUint8)
C.glClearColor( C.glClearColor(
C.GLclampf(float64(r)/max), C.GLclampf(float64(r)/max),
@ -72,41 +72,41 @@ func (canvas *Canvas) Fill(r, g, b uint8) {
C.glClear(C.GL_COLOR_BUFFER_BIT) C.glClear(C.GL_COLOR_BUFFER_BIT)
} }
func (canvas *Canvas) DrawTexture( func (context *Context) DrawTexture(
id graphics.TextureId, id graphics.TextureId,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
tex := canvas.ids.TextureAt(id) tex := context.ids.TextureAt(id)
canvas.offscreen.DrawTexture(tex, geometryMatrix, colorMatrix) context.offscreen.DrawTexture(tex, geometryMatrix, colorMatrix)
} }
func (canvas *Canvas) DrawRenderTarget( func (context *Context) DrawRenderTarget(
id graphics.RenderTargetId, id graphics.RenderTargetId,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
canvas.DrawTexture(canvas.ids.ToTexture(id), geometryMatrix, colorMatrix) context.DrawTexture(context.ids.ToTexture(id), geometryMatrix, colorMatrix)
} }
func (canvas *Canvas) DrawTextureParts( func (context *Context) DrawTextureParts(
id graphics.TextureId, parts []graphics.TexturePart, id graphics.TextureId, parts []graphics.TexturePart,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
tex := canvas.ids.TextureAt(id) tex := context.ids.TextureAt(id)
canvas.offscreen.DrawTextureParts(tex, parts, geometryMatrix, colorMatrix) context.offscreen.DrawTextureParts(tex, parts, geometryMatrix, colorMatrix)
} }
func (canvas *Canvas) DrawRenderTargetParts( func (context *Context) DrawRenderTargetParts(
id graphics.RenderTargetId, parts []graphics.TexturePart, id graphics.RenderTargetId, parts []graphics.TexturePart,
geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { geometryMatrix matrix.Geometry, colorMatrix matrix.Color) {
canvas.DrawTextureParts(canvas.ids.ToTexture(id), parts, geometryMatrix, colorMatrix) context.DrawTextureParts(context.ids.ToTexture(id), parts, geometryMatrix, colorMatrix)
} }
func (canvas *Canvas) DrawLines(lines []graphics.Line) { func (context *Context) DrawLines(lines []graphics.Line) {
canvas.offscreen.DrawLines(lines) context.offscreen.DrawLines(lines)
} }
func (canvas *Canvas) ResetOffscreen() { func (context *Context) ResetOffscreen() {
canvas.SetOffscreen(canvas.screenId) context.SetOffscreen(context.screenId)
} }
func (canvas *Canvas) SetOffscreen(renderTargetId graphics.RenderTargetId) { func (context *Context) SetOffscreen(renderTargetId graphics.RenderTargetId) {
renderTarget := canvas.ids.RenderTargetAt(renderTargetId) renderTarget := context.ids.RenderTargetAt(renderTargetId)
canvas.offscreen.Set(renderTarget) context.offscreen.Set(renderTarget)
} }

View File

@ -17,12 +17,12 @@ func NewDevice() *Device {
return device return device
} }
func (d *Device) CreateCanvas(screenWidth, screenHeight, screenScale int) *Canvas { func (d *Device) CreateContext(screenWidth, screenHeight, screenScale int) *Context {
return newCanvas(d.ids, screenWidth, screenHeight, screenScale) return newContext(d.ids, screenWidth, screenHeight, screenScale)
} }
func (d *Device) Update(canvas *Canvas, draw func(graphics.Canvas)) { func (d *Device) Update(context *Context, draw func(graphics.Context)) {
canvas.update(draw) context.update(draw)
} }
func (d *Device) CreateRenderTarget(width, height int) (graphics.RenderTargetId, error) { func (d *Device) CreateRenderTarget(width, height int) (graphics.RenderTargetId, error) {

View File

@ -45,7 +45,7 @@ func (t *textureFactory) loop() {
} }
} }
func (t *textureFactory) useContext(f func()) { func (t *textureFactory) useGLContext(f func()) {
t.funcs <- f t.funcs <- f
<-t.funcsDone <-t.funcsDone
} }

View File

@ -31,7 +31,7 @@ func getCurrentUI() *cocoaUI {
C.StartApplication() C.StartApplication()
currentUI.textureFactory = runTextureFactory() currentUI.textureFactory = runTextureFactory()
currentUI.textureFactory.useContext(func() { currentUI.textureFactory.useGLContext(func() {
currentUI.graphicsDevice = opengl.NewDevice() currentUI.graphicsDevice = opengl.NewDevice()
}) })
@ -66,7 +66,7 @@ func (u *cocoaUI) CreateTexture(tag interface{}, img image.Image) {
go func() { go func() {
var id graphics.TextureId var id graphics.TextureId
var err error var err error
u.textureFactory.useContext(func() { u.textureFactory.useGLContext(func() {
id, err = u.graphicsDevice.CreateTexture(img) id, err = u.graphicsDevice.CreateTexture(img)
}) })
e := graphics.TextureCreatedEvent{ e := graphics.TextureCreatedEvent{
@ -82,7 +82,7 @@ func (u *cocoaUI) CreateRenderTarget(tag interface{}, width, height int) {
go func() { go func() {
var id graphics.RenderTargetId var id graphics.RenderTargetId
var err error var err error
u.textureFactory.useContext(func() { u.textureFactory.useGLContext(func() {
id, err = u.graphicsDevice.CreateRenderTarget(width, height) id, err = u.graphicsDevice.CreateRenderTarget(width, height)
}) })
e := graphics.RenderTargetCreatedEvent{ e := graphics.RenderTargetCreatedEvent{

View File

@ -27,7 +27,7 @@ type Window struct {
screenScale int screenScale int
closed bool closed bool
native unsafe.Pointer native unsafe.Pointer
canvas *opengl.Canvas context *opengl.Context
funcs chan func() funcs chan func()
funcsDone chan struct{} funcsDone chan struct{}
windowEvents windowEvents
@ -62,8 +62,8 @@ func runWindow(ui *cocoaUI, width, height, scale int, title string, sharedContex
w.loop() w.loop()
}() }()
<-ch <-ch
w.useContext(func() { w.useGLContext(func() {
w.canvas = ui.graphicsDevice.CreateCanvas(width, height, scale) w.context = ui.graphicsDevice.CreateContext(width, height, scale)
}) })
return w return w
} }
@ -81,16 +81,16 @@ func (w *Window) loop() {
} }
} }
func (w *Window) Draw(f func(graphics.Canvas)) { func (w *Window) Draw(f func(graphics.Context)) {
if w.closed { if w.closed {
return return
} }
w.useContext(func() { w.useGLContext(func() {
w.ui.graphicsDevice.Update(w.canvas, f) w.ui.graphicsDevice.Update(w.context, f)
}) })
} }
func (w *Window) useContext(f func()) { func (w *Window) useGLContext(f func()) {
w.funcs <- f w.funcs <- f
<-w.funcsDone <-w.funcsDone
} }

View File

@ -29,6 +29,6 @@ type WindowEvents interface {
} }
type Window interface { type Window interface {
Draw(func(graphics.Canvas)) Draw(func(graphics.Context))
WindowEvents WindowEvents
} }