From 2ba835373b16c707b8f57d51317071fb8f293faa Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Thu, 14 Jul 2016 01:20:45 +0900 Subject: [PATCH] graphics: Refactoring: passing a GL context from the top level --- image.go | 17 +++++++++-------- imageimpl.go | 9 ++++----- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/image.go b/image.go index 524652921..1b0cbd121 100644 --- a/image.go +++ b/image.go @@ -21,6 +21,7 @@ import ( "sync" "github.com/hajimehoshi/ebiten/internal/graphics/opengl" + "github.com/hajimehoshi/ebiten/internal/ui" ) type images struct { @@ -51,11 +52,11 @@ func (i *images) remove(img *Image) { runtime.SetFinalizer(img, nil) } -func (i *images) resetHistoryIfNeeded(target *Image) error { +func (i *images) resetHistoryIfNeeded(target *Image, context *opengl.Context) error { i.m.Lock() defer i.m.Unlock() for img := range i.images { - if err := img.resetHistoryIfNeeded(target); err != nil { + if err := img.resetHistoryIfNeeded(target, context); err != nil { return err } } @@ -129,7 +130,7 @@ func (i *Image) Size() (width, height int) { // // This function is concurrent-safe. func (i *Image) Clear() error { - if err := theImagesForRestoring.resetHistoryIfNeeded(i); err != nil { + if err := theImagesForRestoring.resetHistoryIfNeeded(i, ui.GLContext()); err != nil { return err } return i.impl.Fill(color.Transparent) @@ -139,7 +140,7 @@ func (i *Image) Clear() error { // // This function is concurrent-safe. func (i *Image) Fill(clr color.Color) error { - if err := theImagesForRestoring.resetHistoryIfNeeded(i); err != nil { + if err := theImagesForRestoring.resetHistoryIfNeeded(i, ui.GLContext()); err != nil { return err } return i.impl.Fill(clr) @@ -162,7 +163,7 @@ func (i *Image) Fill(clr color.Color) error { // // This function is concurrent-safe. func (i *Image) DrawImage(image *Image, options *DrawImageOptions) error { - if err := theImagesForRestoring.resetHistoryIfNeeded(i); err != nil { + if err := theImagesForRestoring.resetHistoryIfNeeded(i, ui.GLContext()); err != nil { return err } return i.impl.DrawImage(image, options) @@ -190,7 +191,7 @@ func (i *Image) ColorModel() color.Model { // // This function is concurrent-safe. func (i *Image) At(x, y int) color.Color { - return i.impl.At(x, y) + return i.impl.At(x, y, ui.GLContext()) } // Dispose disposes the image data. After disposing, the image becomes invalid. @@ -200,7 +201,7 @@ func (i *Image) At(x, y int) color.Color { // // This function is concurrent-safe. func (i *Image) Dispose() error { - if err := theImagesForRestoring.resetHistoryIfNeeded(i); err != nil { + if err := theImagesForRestoring.resetHistoryIfNeeded(i, ui.GLContext()); err != nil { return err } if i.impl.isDisposed() { @@ -217,7 +218,7 @@ func (i *Image) Dispose() error { // // This function is concurrent-safe. func (i *Image) ReplacePixels(p []uint8) error { - if err := theImagesForRestoring.resetHistoryIfNeeded(i); err != nil { + if err := theImagesForRestoring.resetHistoryIfNeeded(i, ui.GLContext()); err != nil { return err } return i.impl.ReplacePixels(p) diff --git a/imageimpl.go b/imageimpl.go index 38ddb4846..152895f4c 100644 --- a/imageimpl.go +++ b/imageimpl.go @@ -26,7 +26,6 @@ import ( "github.com/hajimehoshi/ebiten/internal/graphics" "github.com/hajimehoshi/ebiten/internal/graphics/opengl" "github.com/hajimehoshi/ebiten/internal/loop" - "github.com/hajimehoshi/ebiten/internal/ui" ) type drawImageHistoryItem struct { @@ -192,7 +191,7 @@ func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error { return nil } -func (i *imageImpl) At(x, y int) color.Color { +func (i *imageImpl) At(x, y int, context *opengl.Context) color.Color { if !loop.IsRunning() { panic("ebiten: At can't be called when the GL context is not initialized (this panic happens as of version 1.4.0-alpha)") } @@ -203,7 +202,7 @@ func (i *imageImpl) At(x, y int) color.Color { } if i.pixels == nil || i.drawImageHistory != nil { var err error - i.pixels, err = i.image.Pixels(ui.GLContext()) + i.pixels, err = i.image.Pixels(context) if err != nil { panic(err) } @@ -223,7 +222,7 @@ func (i *imageImpl) hasHistoryWith(target *Image) bool { return false } -func (i *imageImpl) resetHistoryIfNeeded(target *Image) error { +func (i *imageImpl) resetHistoryIfNeeded(target *Image, context *opengl.Context) error { i.m.Lock() defer i.m.Unlock() if i.disposed { @@ -236,7 +235,7 @@ func (i *imageImpl) resetHistoryIfNeeded(target *Image) error { return nil } var err error - i.pixels, err = i.image.Pixels(ui.GLContext()) + i.pixels, err = i.image.Pixels(context) if err != nil { return nil }