docs: Update example pages

This commit is contained in:
Hajime Hoshi 2017-03-04 23:27:25 +09:00
parent ef8da64680
commit db9c5d00ce
17 changed files with 74 additions and 211 deletions

View File

@ -79,17 +79,13 @@ func update(screen *ebiten.Image) error {
diff = float64(480-count) * 0.2
}
if err := screen.Fill(color.NRGBA{0x00, 0x00, 0x80, 0xff}); err != nil {
return err
}
screen.Fill(color.NRGBA{0x00, 0x00, 0x80, 0xff})
op := &ebiten.DrawImageOptions{}
op.ColorM.Scale(1.0, 1.0, 1.0, 0.5)
op.ImageParts = &imageParts{
diff: diff,
}
if err := screen.DrawImage(ebitenImage, op); err != nil {
return err
}
screen.DrawImage(ebitenImage, op)
return nil
}

View File

@ -115,9 +115,9 @@ func (p *Player) updateSE() error {
return sePlayer.Play()
}
func (p *Player) updateVolume() error {
func (p *Player) updateVolume() {
if p.audioPlayer == nil {
return nil
return
}
if ebiten.IsKeyPressed(ebiten.KeyZ) {
volume128--
@ -132,7 +132,6 @@ func (p *Player) updateVolume() error {
volume128 = 128
}
p.audioPlayer.SetVolume(float64(volume128) / 128)
return nil
}
func (p *Player) updatePlayPause() error {
@ -153,29 +152,29 @@ func (p *Player) updatePlayPause() error {
return p.audioPlayer.Play()
}
func (p *Player) updateBar() error {
func (p *Player) updateBar() {
if p.audioPlayer == nil {
return nil
return
}
if p.seekedCh != nil {
return nil
return
}
if !ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
mouseButtonState[ebiten.MouseButtonLeft] = 0
return nil
return
}
mouseButtonState[ebiten.MouseButtonLeft]++
if mouseButtonState[ebiten.MouseButtonLeft] != 1 {
return nil
return
}
x, y := ebiten.CursorPosition()
bx, by, bw, bh := playerBarRect()
const padding = 4
if y < by-padding || by+bh+padding <= y {
return nil
return
}
if x < bx || bx+bw <= x {
return nil
return
}
pos := time.Duration(x-bx) * p.total / time.Duration(bw)
p.seekedCh = make(chan error, 1)
@ -183,7 +182,6 @@ func (p *Player) updateBar() error {
// This can't be done parallely! !?!?
p.seekedCh <- p.audioPlayer.Seek(pos)
}()
return nil
}
func (p *Player) close() error {
@ -204,26 +202,20 @@ func update(screen *ebiten.Image) error {
}
}
if musicPlayer != nil {
if err := musicPlayer.updateBar(); err != nil {
return err
}
musicPlayer.updateBar()
if err := musicPlayer.updatePlayPause(); err != nil {
return err
}
if err := musicPlayer.updateSE(); err != nil {
return err
}
if err := musicPlayer.updateVolume(); err != nil {
return err
}
musicPlayer.updateVolume()
}
op := &ebiten.DrawImageOptions{}
x, y, w, h := playerBarRect()
op.GeoM.Translate(float64(x), float64(y))
if err := screen.DrawImage(playerBarImage, op); err != nil {
return err
}
screen.DrawImage(playerBarImage, op)
currentTimeStr := "00:00"
if musicPlayer != nil {
c := musicPlayer.audioPlayer.Current()
@ -239,9 +231,7 @@ func update(screen *ebiten.Image) error {
cy := y - (ch-h)/2
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(cx), float64(cy))
if err := screen.DrawImage(playerCurrentImage, op); err != nil {
return err
}
screen.DrawImage(playerCurrentImage, op)
}
msg := fmt.Sprintf(`FPS: %0.2f
@ -263,9 +253,7 @@ Press Z or X to change volume of the music
msg += "\nSeeking..."
}
}
if err := ebitenutil.DebugPrint(screen, msg); err != nil {
return err
}
ebitenutil.DebugPrint(screen, msg)
if err := audioContext.Update(); err != nil {
return err
}

View File

@ -97,22 +97,17 @@ func parseFont() error {
d.DrawString(s)
y += size
}
return textImage.ReplacePixels(dst.Pix)
textImage.ReplacePixels(dst.Pix)
return nil
}
func update(screen *ebiten.Image) error {
if err := screen.DrawImage(textImage, &ebiten.DrawImageOptions{}); err != nil {
return err
}
screen.DrawImage(textImage, &ebiten.DrawImageOptions{})
return nil
}
func main() {
var err error
textImage, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}
textImage, _ = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest)
if err := parseFont(); err != nil {
log.Fatal(err)
}

View File

@ -69,9 +69,7 @@ func update(screen *ebiten.Image) error {
Pressed Buttons: {{.Buttons}}`
str = strings.Replace(str, "{{.Axes}}", strings.Join(axes, "\n "), -1)
str = strings.Replace(str, "{{.Buttons}}", strings.Join(pressedButtons, ", "), -1)
if err := ebitenutil.DebugPrint(screen, str); err != nil {
return err
}
ebitenutil.DebugPrint(screen, str)
return nil
}

View File

@ -94,16 +94,12 @@ func update(screen *ebiten.Image) error {
saturation := float64(saturationInt) / 128
value := float64(valueInt) / 128
op.ColorM.ChangeHSV(hue, saturation, value)
if err := screen.DrawImage(gophersImage, op); err != nil {
return err
}
screen.DrawImage(gophersImage, op)
msg := fmt.Sprintf(`Hue: %0.2f [Q][W]
Saturation: %0.2f [A][S]
Value: %0.2f [Z][X]`, hue, saturation, value)
if err := ebitenutil.DebugPrint(screen, msg); err != nil {
return err
}
ebitenutil.DebugPrint(screen, msg)
return nil
}

View File

@ -54,9 +54,7 @@ func update(screen *ebiten.Image) error {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(screenWidth-w)/2, float64(screenHeight-h)/2)
op.ColorM.RotateHue(float64(count%360) * 2 * math.Pi / 360)
if err := screen.DrawImage(gophersImage, op); err != nil {
return err
}
screen.DrawImage(gophersImage, op)
return nil
}

View File

@ -86,41 +86,27 @@ func (p *viewport) Position() (int, int) {
return p.x16, p.y16
}
func updateGroundImage(ground *ebiten.Image) error {
if err := ground.Clear(); err != nil {
return err
}
func updateGroundImage(ground *ebiten.Image) {
ground.Clear()
x16, y16 := theViewport.Position()
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(-x16)/16, float64(-y16)/16)
if err := ground.DrawImage(repeatedBgImage, op); err != nil {
return err
}
return nil
ground.DrawImage(repeatedBgImage, op)
}
func drawGroundImage(screen *ebiten.Image, ground *ebiten.Image) error {
func drawGroundImage(screen *ebiten.Image, ground *ebiten.Image) {
op := &ebiten.DrawImageOptions{}
if err := screen.DrawImage(ground, op); err != nil {
return err
}
return nil
screen.DrawImage(ground, op)
}
func update(screen *ebiten.Image) error {
theViewport.Move()
if err := updateGroundImage(groundImage); err != nil {
return err
}
if err := drawGroundImage(screen, groundImage); err != nil {
return err
}
updateGroundImage(groundImage)
drawGroundImage(screen, groundImage)
msg := fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS())
if err := ebitenutil.DebugPrint(screen, msg); err != nil {
return err
}
ebitenutil.DebugPrint(screen, msg)
return nil
}
@ -132,23 +118,15 @@ func main() {
}
w, h := bgImage.Size()
const repeat = 5
repeatedBgImage, err = ebiten.NewImage(w*repeat, h*repeat, ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}
repeatedBgImage, _ = ebiten.NewImage(w*repeat, h*repeat, ebiten.FilterNearest)
for j := 0; j < repeat; j++ {
for i := 0; i < repeat; i++ {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(w*i), float64(h*j))
if err := repeatedBgImage.DrawImage(bgImage, op); err != nil {
log.Fatal(err)
}
repeatedBgImage.DrawImage(bgImage, op)
}
}
groundImage, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}
groundImage, _ = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest)
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "infinite scroll"); err != nil {
log.Fatal(err)

View File

@ -99,9 +99,7 @@ func update(screen *ebiten.Image) error {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(offsetX, offsetY)
op.ColorM.Scale(0.5, 0.5, 0.5, 1)
if err := screen.DrawImage(keyboardImage, op); err != nil {
return err
}
screen.DrawImage(keyboardImage, op)
pressed := []string{}
for i := 0; i <= 9; i++ {
@ -129,9 +127,7 @@ func update(screen *ebiten.Image) error {
ImageParts: pressedKeysParts(pressed),
}
op.GeoM.Translate(offsetX, offsetY)
if err := screen.DrawImage(keyboardImage, op); err != nil {
return err
}
screen.DrawImage(keyboardImage, op)
return nil
}

View File

@ -185,9 +185,7 @@ var (
func update(screen *ebiten.Image) error {
world.Progress()
world.DrawImage(noiseImage)
if err := screen.ReplacePixels(noiseImage.Pix); err != nil {
return err
}
screen.ReplacePixels(noiseImage.Pix)
return nil
}

View File

@ -78,31 +78,19 @@ func update(screen *ebiten.Image) error {
spotLightVY = -spotLightVY
}
if err := maskImage.Clear(); err != nil {
return err
}
maskImage.Clear()
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(spotLightX), float64(spotLightY))
if err := maskImage.DrawImage(spotLightImage, op); err != nil {
return err
}
maskImage.DrawImage(spotLightImage, op)
op = &ebiten.DrawImageOptions{}
op.CompositeMode = ebiten.CompositeModeSourceOut
if err := maskImage.DrawImage(fiveyearsImage, op); err != nil {
return err
}
maskImage.DrawImage(fiveyearsImage, op)
if err := screen.Fill(color.RGBA{0x00, 0x00, 0x80, 0xff}); err != nil {
return err
}
if err := screen.DrawImage(gophersImage, &ebiten.DrawImageOptions{}); err != nil {
return err
}
if err := screen.DrawImage(maskImage, &ebiten.DrawImageOptions{}); err != nil {
return err
}
screen.Fill(color.RGBA{0x00, 0x00, 0x80, 0xff})
screen.DrawImage(gophersImage, &ebiten.DrawImageOptions{})
screen.DrawImage(maskImage, &ebiten.DrawImageOptions{})
return nil
}
@ -131,10 +119,7 @@ func main() {
if err != nil {
log.Fatal(err)
}
maskImage, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}
maskImage, _ = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest)
as := image.Point{128, 128}
a := image.NewAlpha(image.Rectangle{image.ZP, as})
@ -146,10 +131,7 @@ func main() {
a.SetAlpha(i, j, color.Alpha{b})
}
}
spotLightImage, err = ebiten.NewImageFromImage(a, ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}
spotLightImage, _ = ebiten.NewImageFromImage(a, ebiten.FilterNearest)
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Masking (Ebiten Demo)"); err != nil {
log.Fatal(err)
}

View File

@ -52,14 +52,10 @@ var (
func update(screen *ebiten.Image) error {
op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(1.0/mosaicRatio, 1.0/mosaicRatio)
if err := gophersRenderTarget.DrawImage(gophersImage, op); err != nil {
return err
}
gophersRenderTarget.DrawImage(gophersImage, op)
op = &ebiten.DrawImageOptions{}
op.GeoM.Scale(mosaicRatio, mosaicRatio)
if err := screen.DrawImage(gophersRenderTarget, op); err != nil {
return err
}
screen.DrawImage(gophersRenderTarget, op)
return nil
}
@ -70,10 +66,7 @@ func main() {
log.Fatal(err)
}
w, h := gophersImage.Size()
gophersRenderTarget, err = ebiten.NewImage(w/mosaicRatio, h/mosaicRatio, ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}
gophersRenderTarget, _ = ebiten.NewImage(w/mosaicRatio, h/mosaicRatio, ebiten.FilterNearest)
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Mosaic (Ebiten Demo)"); err != nil {
log.Fatal(err)
}

View File

@ -71,12 +71,8 @@ func update(screen *ebiten.Image) error {
noiseImage.Pix[4*i+2] = uint8(x >> 8)
noiseImage.Pix[4*i+3] = 0xff
}
if err := screen.ReplacePixels(noiseImage.Pix); err != nil {
return err
}
if err := ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %f", ebiten.CurrentFPS())); err != nil {
return err
}
screen.ReplacePixels(noiseImage.Pix)
ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %f", ebiten.CurrentFPS()))
return nil
}

View File

@ -51,55 +51,43 @@ var (
canvasImage *ebiten.Image
)
func paint(screen *ebiten.Image, x, y int) error {
func paint(screen *ebiten.Image, x, y int) {
op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(x), float64(y))
op.ColorM.Scale(1.0, 0.50, 0.125, 1.0)
theta := 2.0 * math.Pi * float64(count%60) / ebiten.FPS
op.ColorM.RotateHue(theta)
if err := canvasImage.DrawImage(brushImage, op); err != nil {
return err
}
return nil
canvasImage.DrawImage(brushImage, op)
}
func update(screen *ebiten.Image) error {
drawn := false
mx, my := ebiten.CursorPosition()
if ebiten.IsMouseButtonPressed(ebiten.MouseButtonLeft) {
if err := paint(screen, mx, my); err != nil {
return err
}
paint(screen, mx, my)
drawn = true
}
for _, t := range ebiten.Touches() {
x, y := t.Position()
if err := paint(screen, x, y); err != nil {
return err
}
paint(screen, x, y)
drawn = true
}
if drawn {
count++
}
if err := screen.DrawImage(canvasImage, nil); err != nil {
return err
}
screen.DrawImage(canvasImage, nil)
msg := fmt.Sprintf("(%d, %d)", mx, my)
for _, t := range ebiten.Touches() {
x, y := t.Position()
msg += fmt.Sprintf("\n(%d, %d) touch %d", x, y, t.ID())
}
if err := ebitenutil.DebugPrint(screen, msg); err != nil {
return err
}
ebitenutil.DebugPrint(screen, msg)
return nil
}
func main() {
var err error
const a0, a1, a2 = 0x40, 0xc0, 0xff
pixels := []uint8{
a0, a1, a1, a0,
@ -107,22 +95,14 @@ func main() {
a1, a2, a2, a1,
a0, a1, a1, a0,
}
brushImage, err = ebiten.NewImageFromImage(&image.Alpha{
brushImage, _ = ebiten.NewImageFromImage(&image.Alpha{
Pix: pixels,
Stride: 4,
Rect: image.Rect(0, 0, 4, 4),
}, ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}
canvasImage, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}
if err := canvasImage.Fill(color.White); err != nil {
log.Fatal(err)
}
canvasImage, _ = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest)
canvasImage.Fill(color.White)
if err := ebiten.Run(update, screenWidth, screenHeight, 2, "Paint (Ebiten Demo)"); err != nil {
log.Fatal(err)

View File

@ -75,9 +75,7 @@ func update(screen *ebiten.Image) error {
maxWidth := float64(w) + float64(h)*0.75
op.GeoM.Translate(-maxWidth/2, -float64(h)/2)
op.GeoM.Translate(screenWidth/2, screenHeight/2)
if err := screen.DrawImage(gophersImage, op); err != nil {
return err
}
screen.DrawImage(gophersImage, op)
return nil
}

View File

@ -175,18 +175,9 @@ var (
)
func init() {
var err error
imageEmpty, err := ebiten.NewImage(16, 16, ebiten.FilterNearest)
if err != nil {
panic(err)
}
if err := imageEmpty.Fill(color.White); err != nil {
panic(err)
}
imagePiano, err = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest)
if err != nil {
panic(err)
}
imageEmpty, _ := ebiten.NewImage(16, 16, ebiten.FilterNearest)
imageEmpty.Fill(color.White)
imagePiano, _ = ebiten.NewImage(screenWidth, screenHeight, ebiten.FilterNearest)
whiteKeys := []string{"A", "S", "D", "F", "G", "H", "J", "K", "L"}
width := 24
y := 48
@ -198,12 +189,8 @@ func init() {
op.GeoM.Scale(float64(width-1)/float64(w), float64(height)/float64(h))
op.GeoM.Translate(float64(x), float64(y))
op.ColorM.Scale(1, 1, 1, 1)
if err := imagePiano.DrawImage(imageEmpty, op); err != nil {
panic(err)
}
if err := common.ArcadeFont.DrawText(imagePiano, k, x+8, y+height-16, 1, color.Black); err != nil {
panic(err)
}
imagePiano.DrawImage(imageEmpty, op)
common.ArcadeFont.DrawText(imagePiano, k, x+8, y+height-16, 1, color.Black)
}
blackKeys := []string{"Q", "W", "", "R", "T", "", "U", "I", "O"}
@ -218,12 +205,8 @@ func init() {
op.GeoM.Scale(float64(width-1)/float64(w), float64(height)/float64(h))
op.GeoM.Translate(float64(x), float64(y))
op.ColorM.Scale(0, 0, 0, 1)
if err := imagePiano.DrawImage(imageEmpty, op); err != nil {
panic(err)
}
if err := common.ArcadeFont.DrawText(imagePiano, k, x+8, y+height-16, 1, color.White); err != nil {
panic(err)
}
imagePiano.DrawImage(imageEmpty, op)
common.ArcadeFont.DrawText(imagePiano, k, x+8, y+height-16, 1, color.White)
}
}
@ -238,16 +221,10 @@ func update(screen *ebiten.Image) error {
}
}
if err := screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff}); err != nil {
return err
}
if err := screen.DrawImage(imagePiano, nil); err != nil {
return err
}
screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff})
screen.DrawImage(imagePiano, nil)
if err := ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS())); err != nil {
return err
}
ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS()))
if err := audioContext.Update(); err != nil {
return err

View File

@ -55,9 +55,7 @@ func update(screen *ebiten.Image) error {
op.GeoM.Translate(-float64(w)/2, -float64(h)/2)
op.GeoM.Rotate(float64(count%360) * 2 * math.Pi / 360)
op.GeoM.Translate(screenWidth/2, screenHeight/2)
if err := screen.DrawImage(gophersImage, op); err != nil {
return err
}
screen.DrawImage(gophersImage, op)
return nil
}

View File

@ -137,15 +137,11 @@ func update(screen *ebiten.Image) error {
ImageParts: sprites,
}
op.ColorM.Scale(1.0, 1.0, 1.0, 0.5)
if err := screen.DrawImage(ebitenImage, op); err != nil {
return err
}
screen.DrawImage(ebitenImage, op)
msg := fmt.Sprintf(`FPS: %0.2f
Num of sprites: %d
Press <- or -> to change the number of sprites`, ebiten.CurrentFPS(), sprites.Len())
if err := ebitenutil.DebugPrint(screen, msg); err != nil {
return err
}
ebitenutil.DebugPrint(screen, msg)
return nil
}