From 19f7f44f1cab4b5b42c82f67a8d1d8c4bade62d6 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 29 Jan 2018 23:04:11 +0900 Subject: [PATCH] examples/highdpi: Async loading --- examples/highdpi/main.go | 51 ++++++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 15 deletions(-) diff --git a/examples/highdpi/main.go b/examples/highdpi/main.go index 22321e4e7..d85ba6783 100644 --- a/examples/highdpi/main.go +++ b/examples/highdpi/main.go @@ -28,8 +28,9 @@ import ( ) var ( - count int - highDPIImage *ebiten.Image + count int + highDPIImage *ebiten.Image + highDPIImageCh = make(chan *ebiten.Image) ) func init() { @@ -37,28 +38,48 @@ func init() { // https://commons.wikimedia.org/wiki/File:As08-16-2593.jpg const url = "https://upload.wikimedia.org/wikipedia/commons/1/1f/As08-16-2593.jpg" - res, err := http.Get(url) - if err != nil { - log.Fatal(err) - } - defer res.Body.Close() + // Load the image asynchronously. + go func() { + res, err := http.Get(url) + if err != nil { + log.Fatal(err) + } + defer res.Body.Close() - img, _, err := image.Decode(res.Body) - if err != nil { - log.Fatal(err) - } + img, _, err := image.Decode(res.Body) + if err != nil { + log.Fatal(err) + } - highDPIImage, err = ebiten.NewImageFromImage(img, ebiten.FilterLinear) - if err != nil { - log.Fatal(err) - } + eimg, err := ebiten.NewImageFromImage(img, ebiten.FilterLinear) + if err != nil { + log.Fatal(err) + } + + highDPIImageCh <- eimg + close(highDPIImageCh) + }() } func update(screen *ebiten.Image) error { + if highDPIImage == nil { + // Use select and 'default' clause for non-blocking receiving. + select { + case img := <-highDPIImageCh: + highDPIImage = img + default: + } + } + if ebiten.IsRunningSlowly() { return nil } + if highDPIImage == nil { + ebitenutil.DebugPrint(screen, "Loading the image...") + return nil + } + scale := ebiten.DeviceScaleFactor() sw, sh := screen.Size()