diff --git a/example/game.go b/example/game.go index 328fca6d2..b08f75dde 100644 --- a/example/game.go +++ b/example/game.go @@ -122,7 +122,7 @@ func (game *Game) Update() { 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() { return } @@ -144,7 +144,7 @@ func (game *Game) Draw(g graphics.Canvas) { 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 letterHeight = 16 @@ -177,6 +177,6 @@ func (game *Game) drawText(g graphics.Canvas, text string, x, y int, clr color.C 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) } diff --git a/example/main.go b/example/main.go index ef59847a9..18aa6e4b5 100644 --- a/example/main.go +++ b/example/main.go @@ -62,7 +62,7 @@ func main() { }() } - drawing := make(chan *graphics.LazyCanvas) + drawing := make(chan *graphics.LazyContext) quit := make(chan struct{}) go func() { defer close(quit) @@ -89,9 +89,9 @@ func main() { return case <-tick: game.Update() - case canvas := <-drawing: - game.Draw(canvas) - drawing <- canvas + case context := <-drawing: + game.Draw(context) + drawing <- context } } }() @@ -100,11 +100,11 @@ func main() { ui.PollEvents() select { default: - drawing <- graphics.NewLazyCanvas() - canvas := <-drawing + drawing <- graphics.NewLazyContext() + context := <-drawing - window.Draw(func(actualCanvas graphics.Canvas) { - canvas.Flush(actualCanvas) + window.Draw(func(actualContext graphics.Context) { + context.Flush(actualContext) }) after := time.After(time.Duration(int64(time.Second) / 120)) ui.PollEvents() diff --git a/graphics/canvas.go b/graphics/context.go similarity index 59% rename from graphics/canvas.go rename to graphics/context.go index 782bb5030..9b7b26279 100644 --- a/graphics/canvas.go +++ b/graphics/context.go @@ -4,7 +4,7 @@ import ( "github.com/hajimehoshi/go-ebiten/graphics/matrix" ) -type Canvas interface { +type Context interface { Clear() Fill(r, g, b uint8) // TODO: Refacotring @@ -27,81 +27,81 @@ type Canvas interface { SetOffscreen(id RenderTargetId) } -type LazyCanvas struct { - funcs []func(Canvas) +type LazyContext struct { + funcs []func(Context) } -func NewLazyCanvas() *LazyCanvas { - return &LazyCanvas{ - funcs: []func(Canvas){}, +func NewLazyContext() *LazyContext { + return &LazyContext{ + funcs: []func(Context){}, } } -func (c *LazyCanvas) Flush(actual Canvas) { +func (c *LazyContext) Flush(actual Context) { for _, f := range c.funcs { f(actual) } - c.funcs = []func(Canvas){} + c.funcs = []func(Context){} } -func (c *LazyCanvas) Clear() { - c.funcs = append(c.funcs, func(actual Canvas) { +func (c *LazyContext) Clear() { + c.funcs = append(c.funcs, func(actual Context) { actual.Clear() }) } -func (c *LazyCanvas) Fill(r, g, b uint8) { - c.funcs = append(c.funcs, func(actual Canvas) { +func (c *LazyContext) Fill(r, g, b uint8) { + c.funcs = append(c.funcs, func(actual Context) { actual.Fill(r, g, b) }) } -func (c *LazyCanvas) DrawTexture(id TextureId, +func (c *LazyContext) DrawTexture(id TextureId, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { - c.funcs = append(c.funcs, func(actual Canvas) { + c.funcs = append(c.funcs, func(actual Context) { actual.DrawTexture(id, geometryMatrix, colorMatrix) }) } -func (c *LazyCanvas) DrawRenderTarget(id RenderTargetId, +func (c *LazyContext) DrawRenderTarget(id RenderTargetId, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { - c.funcs = append(c.funcs, func(actual Canvas) { + c.funcs = append(c.funcs, func(actual Context) { actual.DrawRenderTarget(id, geometryMatrix, colorMatrix) }) } -func (c *LazyCanvas) DrawTextureParts(id TextureId, +func (c *LazyContext) DrawTextureParts(id TextureId, parts []TexturePart, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { parts2 := make([]TexturePart, len(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) }) } -func (c *LazyCanvas) DrawRenderTargetParts(id RenderTargetId, +func (c *LazyContext) DrawRenderTargetParts(id RenderTargetId, parts []TexturePart, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { parts2 := make([]TexturePart, len(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) }) } -func (c *LazyCanvas) ResetOffscreen() { - c.funcs = append(c.funcs, func(actual Canvas) { +func (c *LazyContext) ResetOffscreen() { + c.funcs = append(c.funcs, func(actual Context) { actual.ResetOffscreen() }) } -func (c *LazyCanvas) SetOffscreen(id RenderTargetId) { - c.funcs = append(c.funcs, func(actual Canvas) { +func (c *LazyContext) SetOffscreen(id RenderTargetId) { + c.funcs = append(c.funcs, func(actual Context) { actual.SetOffscreen(id) }) } diff --git a/graphics/opengl/canvas.go b/graphics/opengl/context.go similarity index 51% rename from graphics/opengl/canvas.go rename to graphics/opengl/context.go index 32401c4d2..48074e218 100644 --- a/graphics/opengl/canvas.go +++ b/graphics/opengl/context.go @@ -13,56 +13,56 @@ import ( "math" ) -type Canvas struct { +type Context struct { screenId graphics.RenderTargetId ids *ids offscreen *offscreen.Offscreen screenScale int } -func newCanvas(ids *ids, screenWidth, screenHeight, screenScale int) *Canvas { - canvas := &Canvas{ +func newContext(ids *ids, screenWidth, screenHeight, screenScale int) *Context { + context := &Context{ ids: ids, offscreen: offscreen.New(screenWidth, screenHeight, screenScale), screenScale: screenScale, } var err error - canvas.screenId, err = ids.CreateRenderTarget( + context.screenId, err = ids.CreateRenderTarget( screenWidth, screenHeight, texture.FilterNearest) if err != nil { 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_BLEND) - canvas.ResetOffscreen() - canvas.Clear() + context.ResetOffscreen() + context.Clear() - draw(canvas) + draw(context) C.glFlush() - canvas.offscreen.SetMainFramebuffer() - canvas.Clear() + context.offscreen.SetMainFramebuffer() + context.Clear() - scale := float64(canvas.screenScale) + scale := float64(context.screenScale) geometryMatrix := matrix.IdentityGeometry() geometryMatrix.Scale(scale, scale) - canvas.DrawRenderTarget(canvas.screenId, + context.DrawRenderTarget(context.screenId, geometryMatrix, matrix.IdentityColor()) C.glFlush() } -func (canvas *Canvas) Clear() { - canvas.Fill(0, 0, 0) +func (context *Context) Clear() { + 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) C.glClearColor( C.GLclampf(float64(r)/max), @@ -72,41 +72,41 @@ func (canvas *Canvas) Fill(r, g, b uint8) { C.glClear(C.GL_COLOR_BUFFER_BIT) } -func (canvas *Canvas) DrawTexture( +func (context *Context) DrawTexture( id graphics.TextureId, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { - tex := canvas.ids.TextureAt(id) - canvas.offscreen.DrawTexture(tex, geometryMatrix, colorMatrix) + tex := context.ids.TextureAt(id) + context.offscreen.DrawTexture(tex, geometryMatrix, colorMatrix) } -func (canvas *Canvas) DrawRenderTarget( +func (context *Context) DrawRenderTarget( id graphics.RenderTargetId, 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, geometryMatrix matrix.Geometry, colorMatrix matrix.Color) { - tex := canvas.ids.TextureAt(id) - canvas.offscreen.DrawTextureParts(tex, parts, geometryMatrix, colorMatrix) + tex := context.ids.TextureAt(id) + context.offscreen.DrawTextureParts(tex, parts, geometryMatrix, colorMatrix) } -func (canvas *Canvas) DrawRenderTargetParts( +func (context *Context) DrawRenderTargetParts( id graphics.RenderTargetId, parts []graphics.TexturePart, 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) { - canvas.offscreen.DrawLines(lines) +func (context *Context) DrawLines(lines []graphics.Line) { + context.offscreen.DrawLines(lines) } -func (canvas *Canvas) ResetOffscreen() { - canvas.SetOffscreen(canvas.screenId) +func (context *Context) ResetOffscreen() { + context.SetOffscreen(context.screenId) } -func (canvas *Canvas) SetOffscreen(renderTargetId graphics.RenderTargetId) { - renderTarget := canvas.ids.RenderTargetAt(renderTargetId) - canvas.offscreen.Set(renderTarget) +func (context *Context) SetOffscreen(renderTargetId graphics.RenderTargetId) { + renderTarget := context.ids.RenderTargetAt(renderTargetId) + context.offscreen.Set(renderTarget) } diff --git a/graphics/opengl/device.go b/graphics/opengl/device.go index 2fc3a51c6..f06f1c6a9 100644 --- a/graphics/opengl/device.go +++ b/graphics/opengl/device.go @@ -17,12 +17,12 @@ func NewDevice() *Device { return device } -func (d *Device) CreateCanvas(screenWidth, screenHeight, screenScale int) *Canvas { - return newCanvas(d.ids, screenWidth, screenHeight, screenScale) +func (d *Device) CreateContext(screenWidth, screenHeight, screenScale int) *Context { + return newContext(d.ids, screenWidth, screenHeight, screenScale) } -func (d *Device) Update(canvas *Canvas, draw func(graphics.Canvas)) { - canvas.update(draw) +func (d *Device) Update(context *Context, draw func(graphics.Context)) { + context.update(draw) } func (d *Device) CreateRenderTarget(width, height int) (graphics.RenderTargetId, error) { diff --git a/ui/cocoa/texture_factory.go b/ui/cocoa/texture_factory.go index 3f9c3f0ae..f4425bd8b 100644 --- a/ui/cocoa/texture_factory.go +++ b/ui/cocoa/texture_factory.go @@ -45,7 +45,7 @@ func (t *textureFactory) loop() { } } -func (t *textureFactory) useContext(f func()) { +func (t *textureFactory) useGLContext(f func()) { t.funcs <- f <-t.funcsDone } diff --git a/ui/cocoa/ui.go b/ui/cocoa/ui.go index 9bfff9124..6fb2be9ec 100644 --- a/ui/cocoa/ui.go +++ b/ui/cocoa/ui.go @@ -31,7 +31,7 @@ func getCurrentUI() *cocoaUI { C.StartApplication() currentUI.textureFactory = runTextureFactory() - currentUI.textureFactory.useContext(func() { + currentUI.textureFactory.useGLContext(func() { currentUI.graphicsDevice = opengl.NewDevice() }) @@ -66,7 +66,7 @@ func (u *cocoaUI) CreateTexture(tag interface{}, img image.Image) { go func() { var id graphics.TextureId var err error - u.textureFactory.useContext(func() { + u.textureFactory.useGLContext(func() { id, err = u.graphicsDevice.CreateTexture(img) }) e := graphics.TextureCreatedEvent{ @@ -82,7 +82,7 @@ func (u *cocoaUI) CreateRenderTarget(tag interface{}, width, height int) { go func() { var id graphics.RenderTargetId var err error - u.textureFactory.useContext(func() { + u.textureFactory.useGLContext(func() { id, err = u.graphicsDevice.CreateRenderTarget(width, height) }) e := graphics.RenderTargetCreatedEvent{ diff --git a/ui/cocoa/window.go b/ui/cocoa/window.go index 245e23d46..f99a38beb 100644 --- a/ui/cocoa/window.go +++ b/ui/cocoa/window.go @@ -27,7 +27,7 @@ type Window struct { screenScale int closed bool native unsafe.Pointer - canvas *opengl.Canvas + context *opengl.Context funcs chan func() funcsDone chan struct{} windowEvents @@ -62,8 +62,8 @@ func runWindow(ui *cocoaUI, width, height, scale int, title string, sharedContex w.loop() }() <-ch - w.useContext(func() { - w.canvas = ui.graphicsDevice.CreateCanvas(width, height, scale) + w.useGLContext(func() { + w.context = ui.graphicsDevice.CreateContext(width, height, scale) }) 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 { return } - w.useContext(func() { - w.ui.graphicsDevice.Update(w.canvas, f) + w.useGLContext(func() { + w.ui.graphicsDevice.Update(w.context, f) }) } -func (w *Window) useContext(f func()) { +func (w *Window) useGLContext(f func()) { w.funcs <- f <-w.funcsDone } diff --git a/ui/ui.go b/ui/ui.go index 3c7455b96..8e73209ee 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -29,6 +29,6 @@ type WindowEvents interface { } type Window interface { - Draw(func(graphics.Canvas)) + Draw(func(graphics.Context)) WindowEvents }