graphics: Better error message for At and Set (#790)

This commit is contained in:
Hajime Hoshi 2019-01-19 02:59:22 +09:00
parent dd59d8ae6e
commit 58cc5cb105
2 changed files with 12 additions and 0 deletions

View File

@ -20,6 +20,7 @@ import (
"image/color"
"math"
"runtime"
"sync/atomic"
"github.com/hajimehoshi/ebiten/internal/graphics"
"github.com/hajimehoshi/ebiten/internal/shareable"
@ -535,6 +536,10 @@ func (i *Image) ColorModel() color.Model {
//
// At can't be called outside the main loop (ebiten.Run's updating function) starts (as of version 1.4.0-alpha).
func (i *Image) At(x, y int) color.Color {
if atomic.LoadInt32(&isRunning) == 0 {
panic("ebiten: (*Image).At is not available outside the main loop so far")
}
if i.isDisposed() {
return color.RGBA{}
}
@ -554,6 +559,10 @@ func (i *Image) At(x, y int) color.Color {
//
// If the image is disposed, Set does nothing.
func (img *Image) Set(x, y int, clr color.Color) {
if atomic.LoadInt32(&isRunning) == 0 {
panic("ebiten: (*Image).Set is not available outside the main loop so far")
}
img.copyCheck()
if img.isDisposed() {
return

3
run.go
View File

@ -44,6 +44,7 @@ func CurrentFPS() float64 {
var (
isDrawingSkipped = int32(0)
currentMaxTPS = int32(DefaultTPS)
isRunning = int32(0)
)
func setDrawingSkipped(skipped bool) {
@ -89,6 +90,8 @@ func IsRunningSlowly() bool {
var theGraphicsContext atomic.Value
func run(width, height int, scale float64, title string, g *graphicsContext, mainloop bool) error {
atomic.StoreInt32(&isRunning, 1)
defer atomic.StoreInt32(&isRunning, 0)
if err := ui.Run(width, height, scale, title, g, mainloop); err != nil {
if err == ui.RegularTermination {
return nil