mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-12 20:18:59 +01:00
graphics: Refactoring: passing a GL context from the top level
This commit is contained in:
parent
b733f03489
commit
2ba835373b
17
image.go
17
image.go
@ -21,6 +21,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
|
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
|
||||||
|
"github.com/hajimehoshi/ebiten/internal/ui"
|
||||||
)
|
)
|
||||||
|
|
||||||
type images struct {
|
type images struct {
|
||||||
@ -51,11 +52,11 @@ func (i *images) remove(img *Image) {
|
|||||||
runtime.SetFinalizer(img, nil)
|
runtime.SetFinalizer(img, nil)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *images) resetHistoryIfNeeded(target *Image) error {
|
func (i *images) resetHistoryIfNeeded(target *Image, context *opengl.Context) error {
|
||||||
i.m.Lock()
|
i.m.Lock()
|
||||||
defer i.m.Unlock()
|
defer i.m.Unlock()
|
||||||
for img := range i.images {
|
for img := range i.images {
|
||||||
if err := img.resetHistoryIfNeeded(target); err != nil {
|
if err := img.resetHistoryIfNeeded(target, context); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -129,7 +130,7 @@ func (i *Image) Size() (width, height int) {
|
|||||||
//
|
//
|
||||||
// This function is concurrent-safe.
|
// This function is concurrent-safe.
|
||||||
func (i *Image) Clear() error {
|
func (i *Image) Clear() error {
|
||||||
if err := theImagesForRestoring.resetHistoryIfNeeded(i); err != nil {
|
if err := theImagesForRestoring.resetHistoryIfNeeded(i, ui.GLContext()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
return i.impl.Fill(color.Transparent)
|
return i.impl.Fill(color.Transparent)
|
||||||
@ -139,7 +140,7 @@ func (i *Image) Clear() error {
|
|||||||
//
|
//
|
||||||
// This function is concurrent-safe.
|
// This function is concurrent-safe.
|
||||||
func (i *Image) Fill(clr color.Color) error {
|
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 err
|
||||||
}
|
}
|
||||||
return i.impl.Fill(clr)
|
return i.impl.Fill(clr)
|
||||||
@ -162,7 +163,7 @@ func (i *Image) Fill(clr color.Color) error {
|
|||||||
//
|
//
|
||||||
// This function is concurrent-safe.
|
// This function is concurrent-safe.
|
||||||
func (i *Image) DrawImage(image *Image, options *DrawImageOptions) error {
|
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 err
|
||||||
}
|
}
|
||||||
return i.impl.DrawImage(image, options)
|
return i.impl.DrawImage(image, options)
|
||||||
@ -190,7 +191,7 @@ func (i *Image) ColorModel() color.Model {
|
|||||||
//
|
//
|
||||||
// This function is concurrent-safe.
|
// This function is concurrent-safe.
|
||||||
func (i *Image) At(x, y int) color.Color {
|
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.
|
// 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.
|
// This function is concurrent-safe.
|
||||||
func (i *Image) Dispose() error {
|
func (i *Image) Dispose() error {
|
||||||
if err := theImagesForRestoring.resetHistoryIfNeeded(i); err != nil {
|
if err := theImagesForRestoring.resetHistoryIfNeeded(i, ui.GLContext()); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if i.impl.isDisposed() {
|
if i.impl.isDisposed() {
|
||||||
@ -217,7 +218,7 @@ func (i *Image) Dispose() error {
|
|||||||
//
|
//
|
||||||
// This function is concurrent-safe.
|
// This function is concurrent-safe.
|
||||||
func (i *Image) ReplacePixels(p []uint8) error {
|
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 err
|
||||||
}
|
}
|
||||||
return i.impl.ReplacePixels(p)
|
return i.impl.ReplacePixels(p)
|
||||||
|
@ -26,7 +26,6 @@ import (
|
|||||||
"github.com/hajimehoshi/ebiten/internal/graphics"
|
"github.com/hajimehoshi/ebiten/internal/graphics"
|
||||||
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
|
"github.com/hajimehoshi/ebiten/internal/graphics/opengl"
|
||||||
"github.com/hajimehoshi/ebiten/internal/loop"
|
"github.com/hajimehoshi/ebiten/internal/loop"
|
||||||
"github.com/hajimehoshi/ebiten/internal/ui"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type drawImageHistoryItem struct {
|
type drawImageHistoryItem struct {
|
||||||
@ -192,7 +191,7 @@ func (i *imageImpl) DrawImage(image *Image, options *DrawImageOptions) error {
|
|||||||
return nil
|
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() {
|
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)")
|
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 {
|
if i.pixels == nil || i.drawImageHistory != nil {
|
||||||
var err error
|
var err error
|
||||||
i.pixels, err = i.image.Pixels(ui.GLContext())
|
i.pixels, err = i.image.Pixels(context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
}
|
}
|
||||||
@ -223,7 +222,7 @@ func (i *imageImpl) hasHistoryWith(target *Image) bool {
|
|||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
func (i *imageImpl) resetHistoryIfNeeded(target *Image) error {
|
func (i *imageImpl) resetHistoryIfNeeded(target *Image, context *opengl.Context) error {
|
||||||
i.m.Lock()
|
i.m.Lock()
|
||||||
defer i.m.Unlock()
|
defer i.m.Unlock()
|
||||||
if i.disposed {
|
if i.disposed {
|
||||||
@ -236,7 +235,7 @@ func (i *imageImpl) resetHistoryIfNeeded(target *Image) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
var err error
|
var err error
|
||||||
i.pixels, err = i.image.Pixels(ui.GLContext())
|
i.pixels, err = i.image.Pixels(context)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user