From 0fe8387a5a7bc60af5d522daf47ab07616eea8ca Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Tue, 23 Dec 2014 02:43:26 +0900 Subject: [PATCH] Simplify examples --- example/alphablending/main.go | 8 ++++---- example/mosaic/main.go | 19 +++++++++---------- example/paint/main.go | 33 +++++++++++++++------------------ example/perspective/main.go | 15 +++++++-------- 4 files changed, 35 insertions(+), 40 deletions(-) diff --git a/example/alphablending/main.go b/example/alphablending/main.go index 2aed8af66..c1453b6a6 100644 --- a/example/alphablending/main.go +++ b/example/alphablending/main.go @@ -36,7 +36,7 @@ var ( saved bool ) -func Update(r *ebiten.Image) error { +func Update(screen *ebiten.Image) error { count++ count %= 600 diff := float64(count) * 0.2 @@ -58,17 +58,17 @@ func Update(r *ebiten.Image) error { } } - r.Fill(color.NRGBA{0x00, 0x00, 0x80, 0xff}) + screen.Fill(color.NRGBA{0x00, 0x00, 0x80, 0xff}) for i := 0; i < 10; i++ { geo := ebiten.TranslateGeometry(0, float64(i)*(diff)) clr := ebiten.ColorMatrixI() - if err := ebiten.DrawWholeImage(r, tmpRenderTarget, geo, clr); err != nil { + if err := ebiten.DrawWholeImage(screen, tmpRenderTarget, geo, clr); err != nil { return err } } if !saved && ebiten.IsKeyPressed(ebiten.KeySpace) { - if err := ebitenutil.SaveImageAsPNG("out.png", r); err != nil { + if err := ebitenutil.SaveImageAsPNG("out.png", screen); err != nil { return err } saved = true diff --git a/example/mosaic/main.go b/example/mosaic/main.go index 3957ad7d7..8a2d00771 100644 --- a/example/mosaic/main.go +++ b/example/mosaic/main.go @@ -30,33 +30,32 @@ const ( const mosaicRatio = 16 -type Game struct { +var ( gophersImage *ebiten.Image gophersRenderTarget *ebiten.Image -} +) -func (g *Game) Update(r *ebiten.Image) error { +func Update(screen *ebiten.Image) error { geo := ebiten.ScaleGeometry(1.0/mosaicRatio, 1.0/mosaicRatio) - ebiten.DrawWholeImage(g.gophersRenderTarget, g.gophersImage, geo, ebiten.ColorMatrixI()) + ebiten.DrawWholeImage(gophersRenderTarget, gophersImage, geo, ebiten.ColorMatrixI()) geo = ebiten.ScaleGeometry(mosaicRatio/2.0, mosaicRatio/2.0) - ebiten.DrawWholeImage(r, g.gophersRenderTarget, geo, ebiten.ColorMatrixI()) + ebiten.DrawWholeImage(screen, gophersRenderTarget, geo, ebiten.ColorMatrixI()) return nil } func main() { - g := new(Game) var err error - g.gophersImage, _, err = ebitenutil.NewImageFromFile("images/gophers.jpg", ebiten.FilterNearest) + gophersImage, _, err = ebitenutil.NewImageFromFile("images/gophers.jpg", ebiten.FilterNearest) if err != nil { log.Fatal(err) } - w, h := g.gophersImage.Size() - g.gophersRenderTarget, err = ebiten.NewImage(w/mosaicRatio, h/mosaicRatio, ebiten.FilterNearest) + w, h := gophersImage.Size() + gophersRenderTarget, err = ebiten.NewImage(w/mosaicRatio, h/mosaicRatio, ebiten.FilterNearest) if err != nil { log.Fatal(err) } - if err := ebiten.Run(g.Update, screenWidth, screenHeight, 2, "Mosaic (Ebiten Demo)"); err != nil { + if err := ebiten.Run(Update, screenWidth, screenHeight, 2, "Mosaic (Ebiten Demo)"); err != nil { log.Fatal(err) } } diff --git a/example/paint/main.go b/example/paint/main.go index fb7fe0761..8c030d160 100644 --- a/example/paint/main.go +++ b/example/paint/main.go @@ -30,21 +30,15 @@ const ( screenHeight = 240 ) -type Game struct { - inited bool +var ( count int brushRenderTarget *ebiten.Image canvasRenderTarget *ebiten.Image -} +) -func (g *Game) Update(r *ebiten.Image) error { +func Update(screen *ebiten.Image) error { if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { - g.count++ - } - if !g.inited { - g.brushRenderTarget.Fill(color.White) - g.canvasRenderTarget.Fill(color.White) - g.inited = true + count++ } mx, my := ebiten.CursorPosition() @@ -52,29 +46,32 @@ func (g *Game) Update(r *ebiten.Image) error { if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { geo := ebiten.TranslateGeometry(float64(mx), float64(my)) clr := ebiten.ScaleColor(1.0, 0.25, 0.25, 1.0) - theta := 2.0 * math.Pi * float64(g.count%60) / 60.0 + theta := 2.0 * math.Pi * float64(count%60) / 60.0 clr.Concat(ebiten.RotateHue(theta)) - ebiten.DrawWholeImage(g.canvasRenderTarget, g.brushRenderTarget, geo, clr) + ebiten.DrawWholeImage(canvasRenderTarget, brushRenderTarget, geo, clr) } - ebiten.DrawWholeImage(r, g.canvasRenderTarget, ebiten.GeometryMatrixI(), ebiten.ColorMatrixI()) + ebiten.DrawWholeImage(screen, canvasRenderTarget, ebiten.GeometryMatrixI(), ebiten.ColorMatrixI()) - ebitenutil.DebugPrint(r, fmt.Sprintf("(%d, %d)", mx, my)) + ebitenutil.DebugPrint(screen, fmt.Sprintf("(%d, %d)", mx, my)) return nil } func main() { - g := new(Game) var err error - g.brushRenderTarget, err = ebiten.NewImage(1, 1, ebiten.FilterNearest) + brushRenderTarget, err = ebiten.NewImage(4, 4, ebiten.FilterNearest) if err != nil { log.Fatal(err) } - g.canvasRenderTarget, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest) + brushRenderTarget.Fill(color.White) + + canvasRenderTarget, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest) if err != nil { log.Fatal(err) } - if err := ebiten.Run(g.Update, screenWidth, screenHeight, 2, "Paint (Ebiten Demo)"); err != nil { + canvasRenderTarget.Fill(color.White) + + if err := ebiten.Run(Update, screenWidth, screenHeight, 2, "Paint (Ebiten Demo)"); err != nil { log.Fatal(err) } } diff --git a/example/perspective/main.go b/example/perspective/main.go index 83f155492..44afbe4e0 100644 --- a/example/perspective/main.go +++ b/example/perspective/main.go @@ -28,13 +28,13 @@ const ( screenHeight = 240 ) -type Game struct { +var ( gophersImage *ebiten.Image -} +) -func (g *Game) Update(r *ebiten.Image) error { +func Update(screen *ebiten.Image) error { parts := []ebiten.ImagePart{} - w, h := g.gophersImage.Size() + w, h := gophersImage.Size() for i := 0; i < h; i++ { width := float64(w) + float64(i)*0.75 x := float64(h-i) * 0.75 / 2 @@ -48,18 +48,17 @@ func (g *Game) Update(r *ebiten.Image) error { geo := ebiten.TranslateGeometry(-maxWidth/2, -float64(h)/2) geo.Concat(ebiten.ScaleGeometry(0.4, 0.4)) geo.Concat(ebiten.TranslateGeometry(screenWidth/2, screenHeight/2)) - r.DrawImage(g.gophersImage, parts, geo, ebiten.ColorMatrixI()) + screen.DrawImage(gophersImage, parts, geo, ebiten.ColorMatrixI()) return nil } func main() { - g := new(Game) var err error - g.gophersImage, _, err = ebitenutil.NewImageFromFile("images/gophers.jpg", ebiten.FilterNearest) + gophersImage, _, err = ebitenutil.NewImageFromFile("images/gophers.jpg", ebiten.FilterNearest) if err != nil { log.Fatal(err) } - if err := ebiten.Run(g.Update, screenWidth, screenHeight, 2, "Perspective (Ebiten Demo)"); err != nil { + if err := ebiten.Run(Update, screenWidth, screenHeight, 2, "Perspective (Ebiten Demo)"); err != nil { log.Fatal(err) } }