Simplify examples

This commit is contained in:
Hajime Hoshi 2014-12-23 02:43:26 +09:00
parent b3581d1c7a
commit 0fe8387a5a
4 changed files with 35 additions and 40 deletions

View File

@ -36,7 +36,7 @@ var (
saved bool saved bool
) )
func Update(r *ebiten.Image) error { func Update(screen *ebiten.Image) error {
count++ count++
count %= 600 count %= 600
diff := float64(count) * 0.2 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++ { for i := 0; i < 10; i++ {
geo := ebiten.TranslateGeometry(0, float64(i)*(diff)) geo := ebiten.TranslateGeometry(0, float64(i)*(diff))
clr := ebiten.ColorMatrixI() 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 return err
} }
} }
if !saved && ebiten.IsKeyPressed(ebiten.KeySpace) { 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 return err
} }
saved = true saved = true

View File

@ -30,33 +30,32 @@ const (
const mosaicRatio = 16 const mosaicRatio = 16
type Game struct { var (
gophersImage *ebiten.Image gophersImage *ebiten.Image
gophersRenderTarget *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) 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) 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 return nil
} }
func main() { func main() {
g := new(Game)
var err error 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 { if err != nil {
log.Fatal(err) log.Fatal(err)
} }
w, h := g.gophersImage.Size() w, h := gophersImage.Size()
g.gophersRenderTarget, err = ebiten.NewImage(w/mosaicRatio, h/mosaicRatio, ebiten.FilterNearest) gophersRenderTarget, err = ebiten.NewImage(w/mosaicRatio, h/mosaicRatio, ebiten.FilterNearest)
if err != nil { if err != nil {
log.Fatal(err) 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) log.Fatal(err)
} }
} }

View File

@ -30,21 +30,15 @@ const (
screenHeight = 240 screenHeight = 240
) )
type Game struct { var (
inited bool
count int count int
brushRenderTarget *ebiten.Image brushRenderTarget *ebiten.Image
canvasRenderTarget *ebiten.Image canvasRenderTarget *ebiten.Image
} )
func (g *Game) Update(r *ebiten.Image) error { func Update(screen *ebiten.Image) error {
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
g.count++ count++
}
if !g.inited {
g.brushRenderTarget.Fill(color.White)
g.canvasRenderTarget.Fill(color.White)
g.inited = true
} }
mx, my := ebiten.CursorPosition() mx, my := ebiten.CursorPosition()
@ -52,29 +46,32 @@ func (g *Game) Update(r *ebiten.Image) error {
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) { if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
geo := ebiten.TranslateGeometry(float64(mx), float64(my)) geo := ebiten.TranslateGeometry(float64(mx), float64(my))
clr := ebiten.ScaleColor(1.0, 0.25, 0.25, 1.0) 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)) 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 return nil
} }
func main() { func main() {
g := new(Game)
var err error var err error
g.brushRenderTarget, err = ebiten.NewImage(1, 1, ebiten.FilterNearest) brushRenderTarget, err = ebiten.NewImage(4, 4, ebiten.FilterNearest)
if err != nil { if err != nil {
log.Fatal(err) 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 { if err != nil {
log.Fatal(err) 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) log.Fatal(err)
} }
} }

View File

@ -28,13 +28,13 @@ const (
screenHeight = 240 screenHeight = 240
) )
type Game struct { var (
gophersImage *ebiten.Image gophersImage *ebiten.Image
} )
func (g *Game) Update(r *ebiten.Image) error { func Update(screen *ebiten.Image) error {
parts := []ebiten.ImagePart{} parts := []ebiten.ImagePart{}
w, h := g.gophersImage.Size() w, h := gophersImage.Size()
for i := 0; i < h; i++ { for i := 0; i < h; i++ {
width := float64(w) + float64(i)*0.75 width := float64(w) + float64(i)*0.75
x := float64(h-i) * 0.75 / 2 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 := ebiten.TranslateGeometry(-maxWidth/2, -float64(h)/2)
geo.Concat(ebiten.ScaleGeometry(0.4, 0.4)) geo.Concat(ebiten.ScaleGeometry(0.4, 0.4))
geo.Concat(ebiten.TranslateGeometry(screenWidth/2, screenHeight/2)) 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 return nil
} }
func main() { func main() {
g := new(Game)
var err error 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 { if err != nil {
log.Fatal(err) 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) log.Fatal(err)
} }
} }