examples: Remove unnecessary checks of returning values

This commit is contained in:
Hajime Hoshi 2017-03-04 23:00:19 +09:00
parent fb96125693
commit ef8da64680
38 changed files with 197 additions and 508 deletions

View File

@ -116,10 +116,8 @@ func (b *Board) Size() (int, int) {
return x, y return x, y
} }
func (b *Board) Draw(boardImage *ebiten.Image) error { func (b *Board) Draw(boardImage *ebiten.Image) {
if err := boardImage.Fill(frameColor); err != nil { boardImage.Fill(frameColor)
return err
}
for j := 0; j < b.size; j++ { for j := 0; j < b.size; j++ {
for i := 0; i < b.size; i++ { for i := 0; i < b.size; i++ {
v := 0 v := 0
@ -129,9 +127,7 @@ func (b *Board) Draw(boardImage *ebiten.Image) error {
op.GeoM.Translate(float64(x), float64(y)) op.GeoM.Translate(float64(x), float64(y))
r, g, b, a := colorToScale(tileBackgroundColor(v)) r, g, b, a := colorToScale(tileBackgroundColor(v))
op.ColorM.Scale(r, g, b, a) op.ColorM.Scale(r, g, b, a)
if err := boardImage.DrawImage(tileImage, op); err != nil { boardImage.DrawImage(tileImage, op)
return err
}
} }
} }
animatingTiles := map[*Tile]struct{}{} animatingTiles := map[*Tile]struct{}{}
@ -144,14 +140,9 @@ func (b *Board) Draw(boardImage *ebiten.Image) error {
} }
} }
for t := range nonAnimatingTiles { for t := range nonAnimatingTiles {
if err := t.Draw(boardImage); err != nil { t.Draw(boardImage)
return err
}
} }
for t := range animatingTiles { for t := range animatingTiles {
if err := t.Draw(boardImage); err != nil { t.Draw(boardImage)
return err
} }
}
return nil
} }

View File

@ -52,38 +52,25 @@ func NewGame() (*Game, error) {
} }
func (g *Game) Update() error { func (g *Game) Update() error {
if err := g.input.Update(); err != nil { g.input.Update()
return err
}
if err := g.board.Update(g.input); err != nil { if err := g.board.Update(g.input); err != nil {
return err return err
} }
return nil return nil
} }
func (g *Game) Draw(screen *ebiten.Image) error { func (g *Game) Draw(screen *ebiten.Image) {
if g.boardImage == nil { if g.boardImage == nil {
var err error
w, h := g.board.Size() w, h := g.board.Size()
g.boardImage, err = ebiten.NewImage(w, h, ebiten.FilterNearest) g.boardImage, _ = ebiten.NewImage(w, h, ebiten.FilterNearest)
if err != nil {
return err
}
}
if err := screen.Fill(backgroundColor); err != nil {
return err
}
if err := g.board.Draw(g.boardImage); err != nil {
return err
} }
screen.Fill(backgroundColor)
g.board.Draw(g.boardImage)
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
sw, sh := screen.Size() sw, sh := screen.Size()
bw, bh := g.boardImage.Size() bw, bh := g.boardImage.Size()
x := (sw - bw) / 2 x := (sw - bw) / 2
y := (sh - bh) / 2 y := (sh - bh) / 2
op.GeoM.Translate(float64(x), float64(y)) op.GeoM.Translate(float64(x), float64(y))
if err := screen.DrawImage(g.boardImage, op); err != nil { screen.DrawImage(g.boardImage, op)
return err
}
return nil
} }

View File

@ -76,7 +76,7 @@ var (
} }
) )
func (i *Input) Update() error { func (i *Input) Update() {
for k := range dirKeys { for k := range dirKeys {
if ebiten.IsKeyPressed(k) { if ebiten.IsKeyPressed(k) {
i.keyState[k]++ i.keyState[k]++
@ -84,7 +84,6 @@ func (i *Input) Update() error {
i.keyState[k] = 0 i.keyState[k] = 0
} }
} }
return nil
} }
func (i *Input) Dir() (Dir, bool) { func (i *Input) Dir() (Dir, bool) {

View File

@ -295,12 +295,12 @@ func init() {
} }
} }
func (t *Tile) Draw(boardImage *ebiten.Image) error { func (t *Tile) Draw(boardImage *ebiten.Image) {
i, j := t.current.x, t.current.y i, j := t.current.x, t.current.y
ni, nj := t.next.x, t.next.y ni, nj := t.next.x, t.next.y
v := t.current.value v := t.current.value
if v == 0 { if v == 0 {
return nil return
} }
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
x := i*tileSize + (i+1)*tileMargin x := i*tileSize + (i+1)*tileMargin
@ -336,9 +336,7 @@ func (t *Tile) Draw(boardImage *ebiten.Image) error {
op.GeoM.Translate(float64(x), float64(y)) op.GeoM.Translate(float64(x), float64(y))
r, g, b, a := colorToScale(tileBackgroundColor(v)) r, g, b, a := colorToScale(tileBackgroundColor(v))
op.ColorM.Scale(r, g, b, a) op.ColorM.Scale(r, g, b, a)
if err := boardImage.DrawImage(tileImage, op); err != nil { boardImage.DrawImage(tileImage, op)
return err
}
str := strconv.Itoa(v) str := strconv.Itoa(v)
scale := 2 scale := 2
if 2 < len(str) { if 2 < len(str) {
@ -348,8 +346,5 @@ func (t *Tile) Draw(boardImage *ebiten.Image) error {
h := common.ArcadeFont.TextHeight(str) * scale h := common.ArcadeFont.TextHeight(str) * scale
x = x + (tileSize-w)/2 x = x + (tileSize-w)/2
y = y + (tileSize-h)/2 y = y + (tileSize-h)/2
if err := common.ArcadeFont.DrawText(boardImage, str, x, y, scale, tileColor(v)); err != nil { common.ArcadeFont.DrawText(boardImage, str, x, y, scale, tileColor(v))
return err
}
return nil
} }

View File

@ -34,9 +34,7 @@ func update(screen *ebiten.Image) error {
if ebiten.IsRunningSlowly() { if ebiten.IsRunningSlowly() {
return nil return nil
} }
if err := game.Draw(screen); err != nil { game.Draw(screen)
return err
}
return nil return nil
} }

View File

@ -44,15 +44,11 @@ func update(screen *ebiten.Image) error {
} }
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(ox, oy) op.GeoM.Translate(ox, oy)
if err := screen.DrawImage(ebitenImage, op); err != nil { screen.DrawImage(ebitenImage, op)
return err
}
op = &ebiten.DrawImageOptions{} op = &ebiten.DrawImageOptions{}
op.GeoM.Translate(ox+float64(w), oy) op.GeoM.Translate(ox+float64(w), oy)
op.CompositeMode = ebiten.CompositeModeLighter op.CompositeMode = ebiten.CompositeModeLighter
if err := screen.DrawImage(ebitenImage, op); err != nil { screen.DrawImage(ebitenImage, op)
return err
}
return nil return nil
} }

View File

@ -119,10 +119,8 @@ func (p *player) Angle() int {
return p.angle return p.angle
} }
func updateGroundImage(ground *ebiten.Image) error { func updateGroundImage(ground *ebiten.Image) {
if err := ground.Clear(); err != nil { ground.Clear()
return err
}
x16, y16 := thePlayer.Position() x16, y16 := thePlayer.Position()
a := thePlayer.Angle() a := thePlayer.Angle()
gw, gh := ground.Size() gw, gh := ground.Size()
@ -132,10 +130,7 @@ func updateGroundImage(ground *ebiten.Image) error {
op.GeoM.Translate(float64(-w*2), float64(-h*2)) op.GeoM.Translate(float64(-w*2), float64(-h*2))
op.GeoM.Rotate(float64(-a)*2*math.Pi/maxAngle + math.Pi*3.0/2.0) op.GeoM.Rotate(float64(-a)*2*math.Pi/maxAngle + math.Pi*3.0/2.0)
op.GeoM.Translate(float64(gw)/2, float64(gh)-32) op.GeoM.Translate(float64(gw)/2, float64(gh)-32)
if err := ground.DrawImage(repeatedGophersImage, op); err != nil { ground.DrawImage(repeatedGophersImage, op)
return err
}
return nil
} }
type groundParts struct { type groundParts struct {
@ -166,7 +161,7 @@ func (g *groundParts) Dst(i int) (int, int, int, int) {
return -int(r), int(j1), w + int(r), int(math.Ceil(j2)) return -int(r), int(j1), w + int(r), int(math.Ceil(j2))
} }
func drawGroundImage(screen *ebiten.Image, ground *ebiten.Image) error { func drawGroundImage(screen *ebiten.Image, ground *ebiten.Image) {
w, _ := ground.Size() w, _ := ground.Size()
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(-float64(w)/2, 0) op.GeoM.Translate(-float64(w)/2, 0)
@ -174,18 +169,13 @@ func drawGroundImage(screen *ebiten.Image, ground *ebiten.Image) error {
op.GeoM.Translate(float64(w)/2, 0) op.GeoM.Translate(float64(w)/2, 0)
op.GeoM.Translate(float64(screenWidth-w)/2, screenHeight/3) op.GeoM.Translate(float64(screenWidth-w)/2, screenHeight/3)
op.ImageParts = &groundParts{ground} op.ImageParts = &groundParts{ground}
if err := screen.DrawImage(ground, op); err != nil { screen.DrawImage(ground, op)
return err
}
op = &ebiten.DrawImageOptions{} op = &ebiten.DrawImageOptions{}
op.GeoM.Translate(-float64(w)/2, 0) op.GeoM.Translate(-float64(w)/2, 0)
op.GeoM.Rotate(-1 * float64(thePlayer.lean) / maxLean * math.Pi / 8) op.GeoM.Rotate(-1 * float64(thePlayer.lean) / maxLean * math.Pi / 8)
op.GeoM.Translate(float64(w)/2, 0) op.GeoM.Translate(float64(w)/2, 0)
op.GeoM.Translate(float64(screenWidth-w)/2, screenHeight/3) op.GeoM.Translate(float64(screenWidth-w)/2, screenHeight/3)
if err := screen.DrawImage(fogImage, op); err != nil { screen.DrawImage(fogImage, op)
return err
}
return nil
} }
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
@ -205,21 +195,13 @@ func update(screen *ebiten.Image) error {
thePlayer.Stabilize() thePlayer.Stabilize()
} }
if err := screen.Fill(skyColor); err != nil { screen.Fill(skyColor)
return err updateGroundImage(groundImage)
} drawGroundImage(screen, groundImage)
if err := updateGroundImage(groundImage); err != nil {
return err
}
if err := drawGroundImage(screen, groundImage); err != nil {
return err
}
tutrial := "Space: Move forward\nLeft/Right: Rotate" tutrial := "Space: Move forward\nLeft/Right: Rotate"
msg := fmt.Sprintf("FPS: %0.2f\n%s", ebiten.CurrentFPS(), tutrial) msg := fmt.Sprintf("FPS: %0.2f\n%s", ebiten.CurrentFPS(), tutrial)
if err := ebitenutil.DebugPrint(screen, msg); err != nil { ebitenutil.DebugPrint(screen, msg)
return err
}
return nil return nil
} }

View File

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

View File

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

View File

@ -152,7 +152,7 @@ func (f *Field) flushLine(j int) bool {
return true return true
} }
func (f *Field) Update() error { func (f *Field) Update() {
if 0 <= f.flushCount { if 0 <= f.flushCount {
f.flushCount-- f.flushCount--
if f.flushCount == 0 { if f.flushCount == 0 {
@ -162,7 +162,6 @@ func (f *Field) Update() error {
} }
} }
} }
return nil
} }
func min(a, b float64) float64 { func min(a, b float64) float64 {
@ -186,7 +185,7 @@ func (f *Field) flushingColor() ebiten.ColorM {
return clr return clr
} }
func (f *Field) Draw(r *ebiten.Image, x, y int) error { func (f *Field) Draw(r *ebiten.Image, x, y int) {
blocks := make([][]BlockType, len(f.blocks)) blocks := make([][]BlockType, len(f.blocks))
flushingBlocks := make([][]BlockType, len(f.blocks)) flushingBlocks := make([][]BlockType, len(f.blocks))
for i := 0; i < fieldBlockNumX; i++ { for i := 0; i < fieldBlockNumX; i++ {
@ -204,11 +203,6 @@ func (f *Field) Draw(r *ebiten.Image, x, y int) error {
} }
} }
} }
if err := drawBlocks(r, blocks, x, y, ebiten.ColorM{}); err != nil { drawBlocks(r, blocks, x, y, ebiten.ColorM{})
return err drawBlocks(r, flushingBlocks, x, y, f.flushingColor())
}
if err := drawBlocks(r, flushingBlocks, x, y, f.flushingColor()); err != nil {
return err
}
return nil
} }

View File

@ -23,14 +23,14 @@ import (
"github.com/hajimehoshi/ebiten/examples/common" "github.com/hajimehoshi/ebiten/examples/common"
) )
func drawTextWithShadowCenter(rt *ebiten.Image, str string, x, y, scale int, clr color.Color, width int) error { func drawTextWithShadowCenter(rt *ebiten.Image, str string, x, y, scale int, clr color.Color, width int) {
w := common.ArcadeFont.TextWidth(str) * scale w := common.ArcadeFont.TextWidth(str) * scale
x += (width - w) / 2 x += (width - w) / 2
return common.ArcadeFont.DrawTextWithShadow(rt, str, x, y, scale, clr) common.ArcadeFont.DrawTextWithShadow(rt, str, x, y, scale, clr)
} }
func drawTextWithShadowRight(rt *ebiten.Image, str string, x, y, scale int, clr color.Color, width int) error { func drawTextWithShadowRight(rt *ebiten.Image, str string, x, y, scale int, clr color.Color, width int) {
w := common.ArcadeFont.TextWidth(str) * scale w := common.ArcadeFont.TextWidth(str) * scale
x += width - w x += width - w
return common.ArcadeFont.DrawTextWithShadow(rt, str, x, y, scale, clr) common.ArcadeFont.DrawTextWithShadow(rt, str, x, y, scale, clr)
} }

View File

@ -51,9 +51,7 @@ func (game *Game) Update(r *ebiten.Image) error {
return err return err
} }
if !ebiten.IsRunningSlowly() { if !ebiten.IsRunningSlowly() {
if err := game.sceneManager.Draw(r); err != nil { game.sceneManager.Draw(r)
return err
}
} }
return nil return nil
} }

View File

@ -77,13 +77,11 @@ func (s *GamepadScene) Update(state *GameState) error {
return nil return nil
} }
func (s *GamepadScene) Draw(screen *ebiten.Image) error { func (s *GamepadScene) Draw(screen *ebiten.Image) {
if err := screen.Fill(color.Black); err != nil { screen.Fill(color.Black)
return err
}
if s.buttonStates == nil { if s.buttonStates == nil {
return nil return
} }
f := `GAMEPAD CONFIGURATION f := `GAMEPAD CONFIGURATION
@ -108,8 +106,5 @@ ROTATE RIGHT: %s
msg = "OK!" msg = "OK!"
} }
str := fmt.Sprintf(f, s.buttonStates[0], s.buttonStates[1], s.buttonStates[2], s.buttonStates[3], s.buttonStates[4], msg) str := fmt.Sprintf(f, s.buttonStates[0], s.buttonStates[1], s.buttonStates[2], s.buttonStates[3], s.buttonStates[4], msg)
if err := common.ArcadeFont.DrawTextWithShadow(screen, str, 16, 16, 1, color.White); err != nil { common.ArcadeFont.DrawTextWithShadow(screen, str, 16, 16, 1, color.White)
return err
}
return nil
} }

View File

@ -70,98 +70,64 @@ func linesTextBoxPosition() (x, y int) {
} }
func init() { func init() {
var err error
// Empty // Empty
imageEmpty, err = ebiten.NewImage(16, 16, ebiten.FilterNearest) imageEmpty, _ = ebiten.NewImage(16, 16, ebiten.FilterNearest)
if err != nil { imageEmpty.Fill(color.White)
panic(err)
}
if err := imageEmpty.Fill(color.White); err != nil {
panic(err)
}
// Background // Background
var err error
imageGameBG, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterLinear) imageGameBG, _, err = ebitenutil.NewImageFromFile("_resources/images/gophers.jpg", ebiten.FilterLinear)
if err != nil { if err != nil {
panic(err) panic(err)
} }
// Windows // Windows
imageWindows, err = ebiten.NewImage(ScreenWidth, ScreenHeight, ebiten.FilterNearest) imageWindows, _ = ebiten.NewImage(ScreenWidth, ScreenHeight, ebiten.FilterNearest)
if err != nil {
panic(err)
}
// Windows: Field // Windows: Field
x, y := fieldWindowPosition() x, y := fieldWindowPosition()
if err := drawWindow(imageWindows, x, y, fieldWidth, fieldHeight); err != nil { drawWindow(imageWindows, x, y, fieldWidth, fieldHeight)
panic(err)
}
// Windows: Next // Windows: Next
x, y = nextWindowLabelPosition() x, y = nextWindowLabelPosition()
if err := common.ArcadeFont.DrawTextWithShadow(imageWindows, "NEXT", x, y, 1, fontColor); err != nil { common.ArcadeFont.DrawTextWithShadow(imageWindows, "NEXT", x, y, 1, fontColor)
panic(err)
}
x, y = nextWindowPosition() x, y = nextWindowPosition()
if err := drawWindow(imageWindows, x, y, 5*blockWidth, 5*blockHeight); err != nil { drawWindow(imageWindows, x, y, 5*blockWidth, 5*blockHeight)
panic(err)
}
// Windows: Score // Windows: Score
x, y = scoreTextBoxPosition() x, y = scoreTextBoxPosition()
if err := drawTextBox(imageWindows, "SCORE", x, y, textBoxWidth()); err != nil { drawTextBox(imageWindows, "SCORE", x, y, textBoxWidth())
panic(err)
}
// Windows: Level // Windows: Level
x, y = levelTextBoxPosition() x, y = levelTextBoxPosition()
if err := drawTextBox(imageWindows, "LEVEL", x, y, textBoxWidth()); err != nil { drawTextBox(imageWindows, "LEVEL", x, y, textBoxWidth())
panic(err)
}
// Windows: Lines // Windows: Lines
x, y = linesTextBoxPosition() x, y = linesTextBoxPosition()
if err := drawTextBox(imageWindows, "LINES", x, y, textBoxWidth()); err != nil { drawTextBox(imageWindows, "LINES", x, y, textBoxWidth())
panic(err)
}
// Gameover // Gameover
imageGameover, err = ebiten.NewImage(ScreenWidth, ScreenHeight, ebiten.FilterNearest) imageGameover, _ = ebiten.NewImage(ScreenWidth, ScreenHeight, ebiten.FilterNearest)
if err != nil { imageGameover.Fill(color.NRGBA{0x00, 0x00, 0x00, 0x80})
panic(err)
}
if err := imageGameover.Fill(color.NRGBA{0x00, 0x00, 0x00, 0x80}); err != nil {
panic(err)
}
y = (ScreenHeight - blockHeight) / 2 y = (ScreenHeight - blockHeight) / 2
if err := drawTextWithShadowCenter(imageGameover, "GAME OVER", 0, y, 1, color.White, ScreenWidth); err != nil { drawTextWithShadowCenter(imageGameover, "GAME OVER", 0, y, 1, color.White, ScreenWidth)
panic(err)
}
} }
func drawWindow(r *ebiten.Image, x, y, width, height int) error { func drawWindow(r *ebiten.Image, x, y, width, height int) {
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
w, h := imageEmpty.Size() w, h := imageEmpty.Size()
op.GeoM.Scale(float64(width)/float64(w), float64(height)/float64(h)) op.GeoM.Scale(float64(width)/float64(w), float64(height)/float64(h))
op.GeoM.Translate(float64(x), float64(y)) op.GeoM.Translate(float64(x), float64(y))
op.ColorM.Scale(0, 0, 0, 0.75) op.ColorM.Scale(0, 0, 0, 0.75)
return r.DrawImage(imageEmpty, op) r.DrawImage(imageEmpty, op)
} }
var fontColor = color.NRGBA{0x40, 0x40, 0xff, 0xff} var fontColor = color.NRGBA{0x40, 0x40, 0xff, 0xff}
func drawTextBox(r *ebiten.Image, label string, x, y, width int) error { func drawTextBox(r *ebiten.Image, label string, x, y, width int) {
if err := common.ArcadeFont.DrawTextWithShadow(r, label, x, y, 1, fontColor); err != nil { common.ArcadeFont.DrawTextWithShadow(r, label, x, y, 1, fontColor)
return err
}
y += blockWidth y += blockWidth
if err := drawWindow(r, x, y, width, 2*blockHeight); err != nil { drawWindow(r, x, y, width, 2*blockHeight)
return err
}
return nil
} }
func drawTextBoxContent(r *ebiten.Image, content string, x, y, width int) error { func drawTextBoxContent(r *ebiten.Image, content string, x, y, width int) {
y += blockWidth y += blockWidth
if err := drawTextWithShadowRight(r, content, x, y+blockHeight*3/4, 1, color.White, width-blockWidth/2); err != nil { drawTextWithShadowRight(r, content, x, y+blockHeight*3/4, 1, color.White, width-blockWidth/2)
return err
}
return nil
} }
type GameScene struct { type GameScene struct {
@ -187,10 +153,8 @@ func NewGameScene() *GameScene {
} }
} }
func (s *GameScene) drawBackground(r *ebiten.Image) error { func (s *GameScene) drawBackground(r *ebiten.Image) {
if err := r.Fill(color.White); err != nil { r.Fill(color.White)
return err
}
w, h := imageGameBG.Size() w, h := imageGameBG.Size()
scaleW := ScreenWidth / float64(w) scaleW := ScreenWidth / float64(w)
@ -211,7 +175,7 @@ func (s *GameScene) drawBackground(r *ebiten.Image) error {
op.ColorM.Scale(1-a, 1-a, 1-a, 1-a) op.ColorM.Scale(1-a, 1-a, 1-a, 1-a)
op.ColorM.Add(m) op.ColorM.Add(m)
op.ColorM.Translate(0.3, 0.3, 0.3, 0) op.ColorM.Translate(0.3, 0.3, 0.3, 0)
return r.DrawImage(imageGameBG, op) r.DrawImage(imageGameBG, op)
} }
const fieldWidth = blockWidth * fieldBlockNumX const fieldWidth = blockWidth * fieldBlockNumX
@ -254,9 +218,7 @@ func (s *GameScene) addScore(lines int) {
} }
func (s *GameScene) Update(state *GameState) error { func (s *GameScene) Update(state *GameState) error {
if err := s.field.Update(); err != nil { s.field.Update()
return err
}
if s.gameover { if s.gameover {
// TODO: Gamepad key? // TODO: Gamepad key?
@ -351,59 +313,39 @@ func (s *GameScene) goNextPiece() {
} }
} }
func (s *GameScene) Draw(r *ebiten.Image) error { func (s *GameScene) Draw(r *ebiten.Image) {
if err := s.drawBackground(r); err != nil { s.drawBackground(r)
return err
}
if err := r.DrawImage(imageWindows, nil); err != nil { r.DrawImage(imageWindows, nil)
return err
}
// Draw score // Draw score
x, y := scoreTextBoxPosition() x, y := scoreTextBoxPosition()
if err := drawTextBoxContent(r, strconv.Itoa(s.score), x, y, textBoxWidth()); err != nil { drawTextBoxContent(r, strconv.Itoa(s.score), x, y, textBoxWidth())
return err
}
// Draw level // Draw level
x, y = levelTextBoxPosition() x, y = levelTextBoxPosition()
if err := drawTextBoxContent(r, strconv.Itoa(s.level()), x, y, textBoxWidth()); err != nil { drawTextBoxContent(r, strconv.Itoa(s.level()), x, y, textBoxWidth())
return err
}
// Draw lines // Draw lines
x, y = linesTextBoxPosition() x, y = linesTextBoxPosition()
if err := drawTextBoxContent(r, strconv.Itoa(s.lines), x, y, textBoxWidth()); err != nil { drawTextBoxContent(r, strconv.Itoa(s.lines), x, y, textBoxWidth())
return err
}
// Draw blocks // Draw blocks
fieldX, fieldY := fieldWindowPosition() fieldX, fieldY := fieldWindowPosition()
if err := s.field.Draw(r, fieldX, fieldY); err != nil { s.field.Draw(r, fieldX, fieldY)
return err
}
if s.currentPiece != nil && !s.field.Flushing() { if s.currentPiece != nil && !s.field.Flushing() {
x := fieldX + s.currentPieceX*blockWidth x := fieldX + s.currentPieceX*blockWidth
y := fieldY + s.currentPieceY*blockHeight y := fieldY + s.currentPieceY*blockHeight
if err := s.currentPiece.Draw(r, x, y, s.currentPieceAngle); err != nil { s.currentPiece.Draw(r, x, y, s.currentPieceAngle)
return err
}
} }
if s.nextPiece != nil { if s.nextPiece != nil {
// TODO: Make functions to get these values. // TODO: Make functions to get these values.
x := fieldX + fieldWidth + blockWidth*2 x := fieldX + fieldWidth + blockWidth*2
y := fieldY + blockHeight y := fieldY + blockHeight
if err := s.nextPiece.DrawAtCenter(r, x, y, blockWidth*5, blockHeight*5, 0); err != nil { s.nextPiece.DrawAtCenter(r, x, y, blockWidth*5, blockHeight*5, 0)
return err
}
} }
if s.gameover { if s.gameover {
if err := r.DrawImage(imageGameover, nil); err != nil { r.DrawImage(imageGameover, nil)
return err
} }
}
return nil
} }

View File

@ -174,13 +174,13 @@ func (b blocksImageParts) Src(i int) (x0, y0, x1, y1 int) {
return x, 0, x + blockWidth, blockHeight return x, 0, x + blockWidth, blockHeight
} }
func drawBlocks(r *ebiten.Image, blocks [][]BlockType, x, y int, clr ebiten.ColorM) error { func drawBlocks(r *ebiten.Image, blocks [][]BlockType, x, y int, clr ebiten.ColorM) {
op := &ebiten.DrawImageOptions{ op := &ebiten.DrawImageOptions{
ImageParts: blocksImageParts(blocks), ImageParts: blocksImageParts(blocks),
ColorM: clr, ColorM: clr,
} }
op.GeoM.Translate(float64(x), float64(y)) op.GeoM.Translate(float64(x), float64(y))
return r.DrawImage(imageBlocks, op) r.DrawImage(imageBlocks, op)
} }
func (p *Piece) InitialPosition() (int, int) { func (p *Piece) InitialPosition() (int, int) {
@ -244,13 +244,13 @@ func (p *Piece) AbsorbInto(field *Field, x, y int, angle Angle) {
} }
} }
func (p *Piece) DrawAtCenter(r *ebiten.Image, x, y, width, height int, angle Angle) error { func (p *Piece) DrawAtCenter(r *ebiten.Image, x, y, width, height int, angle Angle) {
x += (width - len(p.blocks[0])*blockWidth) / 2 x += (width - len(p.blocks[0])*blockWidth) / 2
y += (height - len(p.blocks)*blockHeight) / 2 y += (height - len(p.blocks)*blockHeight) / 2
return p.Draw(r, x, y, angle) p.Draw(r, x, y, angle)
} }
func (p *Piece) Draw(r *ebiten.Image, x, y int, angle Angle) error { func (p *Piece) Draw(r *ebiten.Image, x, y int, angle Angle) {
size := len(p.blocks) size := len(p.blocks)
blocks := make([][]BlockType, size) blocks := make([][]BlockType, size)
for i := range p.blocks { for i := range p.blocks {
@ -261,5 +261,5 @@ func (p *Piece) Draw(r *ebiten.Image, x, y int, angle Angle) error {
} }
} }
} }
return drawBlocks(r, blocks, x, y, ebiten.ColorM{}) drawBlocks(r, blocks, x, y, ebiten.ColorM{})
} }

View File

@ -26,20 +26,13 @@ var (
) )
func init() { func init() {
var err error transitionFrom, _ = ebiten.NewImage(ScreenWidth, ScreenHeight, ebiten.FilterNearest)
transitionFrom, err = ebiten.NewImage(ScreenWidth, ScreenHeight, ebiten.FilterNearest) transitionTo, _ = ebiten.NewImage(ScreenWidth, ScreenHeight, ebiten.FilterNearest)
if err != nil {
panic(err)
}
transitionTo, err = ebiten.NewImage(ScreenWidth, ScreenHeight, ebiten.FilterNearest)
if err != nil {
panic(err)
}
} }
type Scene interface { type Scene interface {
Update(state *GameState) error Update(state *GameState) error
Draw(screen *ebiten.Image) error Draw(screen *ebiten.Image)
} }
const transitionMaxCount = 20 const transitionMaxCount = 20
@ -70,32 +63,23 @@ func (s *SceneManager) Update(state *GameState) error {
return nil return nil
} }
func (s *SceneManager) Draw(r *ebiten.Image) error { func (s *SceneManager) Draw(r *ebiten.Image) {
if s.transitionCount == -1 { if s.transitionCount == -1 {
return s.current.Draw(r) s.current.Draw(r)
} return
if err := transitionFrom.Clear(); err != nil {
return err
}
if err := s.current.Draw(transitionFrom); err != nil {
return err
} }
transitionFrom.Clear()
s.current.Draw(transitionFrom)
if err := transitionTo.Clear(); err != nil { transitionTo.Clear()
return err s.next.Draw(transitionTo)
}
if err := s.next.Draw(transitionTo); err != nil {
return err
}
if err := r.DrawImage(transitionFrom, nil); err != nil { r.DrawImage(transitionFrom, nil)
return err
}
alpha := float64(s.transitionCount) / float64(transitionMaxCount) alpha := float64(s.transitionCount) / float64(transitionMaxCount)
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
op.ColorM.Scale(1, 1, 1, alpha) op.ColorM.Scale(1, 1, 1, alpha)
return r.DrawImage(transitionTo, op) r.DrawImage(transitionTo, op)
} }
func (s *SceneManager) GoTo(scene Scene) { func (s *SceneManager) GoTo(scene Scene) {

View File

@ -106,34 +106,27 @@ func (s *TitleScene) Update(state *GameState) error {
return nil return nil
} }
func (s *TitleScene) Draw(r *ebiten.Image) error { func (s *TitleScene) Draw(r *ebiten.Image) {
if err := s.drawTitleBackground(r, s.count); err != nil { s.drawTitleBackground(r, s.count)
return err drawLogo(r, "BLOCKS")
}
if err := drawLogo(r, "BLOCKS"); err != nil {
return err
}
message := "PRESS SPACE TO START" message := "PRESS SPACE TO START"
x := (ScreenWidth - common.ArcadeFont.TextWidth(message)) / 2 x := (ScreenWidth - common.ArcadeFont.TextWidth(message)) / 2
y := ScreenHeight - 48 y := ScreenHeight - 48
if err := common.ArcadeFont.DrawTextWithShadow(r, message, x, y, 1, color.NRGBA{0x80, 0, 0, 0xff}); err != nil { common.ArcadeFont.DrawTextWithShadow(r, message, x, y, 1, color.NRGBA{0x80, 0, 0, 0xff})
return err
}
return nil
} }
func (s *TitleScene) drawTitleBackground(r *ebiten.Image, c int) error { func (s *TitleScene) drawTitleBackground(r *ebiten.Image, c int) {
s.parts.count = c s.parts.count = c
return r.DrawImage(imageBackground, &ebiten.DrawImageOptions{ r.DrawImage(imageBackground, &ebiten.DrawImageOptions{
ImageParts: s.parts, ImageParts: s.parts,
}) })
} }
func drawLogo(r *ebiten.Image, str string) error { func drawLogo(r *ebiten.Image, str string) {
scale := 4 scale := 4
textWidth := common.ArcadeFont.TextWidth(str) * scale textWidth := common.ArcadeFont.TextWidth(str) * scale
x := (ScreenWidth - textWidth) / 2 x := (ScreenWidth - textWidth) / 2
y := 32 y := 32
return common.ArcadeFont.DrawTextWithShadow(r, str, x, y, scale, color.NRGBA{0x00, 0x00, 0x80, 0xff}) common.ArcadeFont.DrawTextWithShadow(r, str, x, y, scale, color.NRGBA{0x00, 0x00, 0x80, 0xff})
} }

View File

@ -51,14 +51,8 @@ func (f *Font) TextHeight(str string) int {
} }
func init() { func init() {
img, err := assets.ArcadeFontImage() img := assets.ArcadeFontImage()
if err != nil { eimg, _ := ebiten.NewImageFromImage(img, ebiten.FilterNearest)
panic(err)
}
eimg, err := ebiten.NewImageFromImage(img, ebiten.FilterNearest)
if err != nil {
panic(err)
}
ArcadeFont = &Font{eimg, img, 32, 16, 8, 8} ArcadeFont = &Font{eimg, img, 32, 16, 8, 8}
} }
@ -92,7 +86,7 @@ func (f *fontImageParts) Src(i int) (x0, y0, x1, y1 int) {
return x, y, x + f.font.charWidth, y + f.font.charHeight return x, y, x + f.font.charWidth, y + f.font.charHeight
} }
func (f *Font) DrawText(rt *ebiten.Image, str string, ox, oy, scale int, c color.Color) error { func (f *Font) DrawText(rt *ebiten.Image, str string, ox, oy, scale int, c color.Color) {
options := &ebiten.DrawImageOptions{ options := &ebiten.DrawImageOptions{
ImageParts: &fontImageParts{str, f}, ImageParts: &fontImageParts{str, f},
} }
@ -111,26 +105,19 @@ func (f *Font) DrawText(rt *ebiten.Image, str string, ox, oy, scale int, c color
b /= a b /= a
} }
options.ColorM.Scale(r, g, b, a) options.ColorM.Scale(r, g, b, a)
rt.DrawImage(f.image, options)
return rt.DrawImage(f.image, options)
} }
func (f *Font) DrawTextOnImage(rt draw.Image, str string, ox, oy int) error { func (f *Font) DrawTextOnImage(rt draw.Image, str string, ox, oy int) {
parts := &fontImageParts{str, f} parts := &fontImageParts{str, f}
for i := 0; i < parts.Len(); i++ { for i := 0; i < parts.Len(); i++ {
dx0, dy0, dx1, dy1 := parts.Dst(i) dx0, dy0, dx1, dy1 := parts.Dst(i)
sx0, sy0, _, _ := parts.Src(i) sx0, sy0, _, _ := parts.Src(i)
draw.Draw(rt, image.Rect(dx0+ox, dy0+oy, dx1+ox, dy1+oy), f.origImage, image.Pt(sx0, sy0), draw.Over) draw.Draw(rt, image.Rect(dx0+ox, dy0+oy, dx1+ox, dy1+oy), f.origImage, image.Pt(sx0, sy0), draw.Over)
} }
return nil
} }
func (f *Font) DrawTextWithShadow(rt *ebiten.Image, str string, x, y, scale int, clr color.Color) error { func (f *Font) DrawTextWithShadow(rt *ebiten.Image, str string, x, y, scale int, clr color.Color) {
if err := f.DrawText(rt, str, x+1, y+1, scale, color.NRGBA{0, 0, 0, 0x80}); err != nil { f.DrawText(rt, str, x+1, y+1, scale, color.NRGBA{0, 0, 0, 0x80})
return err f.DrawText(rt, str, x, y, scale, clr)
}
if err := f.DrawText(rt, str, x, y, scale, clr); err != nil {
return err
}
return nil
} }

View File

@ -21,18 +21,16 @@ package assets
import ( import (
"bytes" "bytes"
"fmt"
"image" "image"
_ "image/png" _ "image/png"
) )
func ArcadeFontImage() (image.Image, error) { func ArcadeFontImage() image.Image {
b, err := Asset("arcadefont.png") b := MustAsset("arcadefont.png")
if err != nil {
return nil, err
}
img, _, err := image.Decode(bytes.NewBuffer(b)) img, _, err := image.Decode(bytes.NewBuffer(b))
if err != nil { if err != nil {
return nil, err panic(fmt.Sprintf("assets: image.Decode error: %v", err))
} }
return img, nil return img
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -40,7 +40,7 @@ var keyboardKeys = [][]string{
{"Left", "Down", "Right"}, {"Left", "Down", "Right"},
} }
func drawKey(t *image.NRGBA, name string, x, y, width int) error { func drawKey(t *image.NRGBA, name string, x, y, width int) {
const height = 16 const height = 16
width-- width--
shape := image.NewNRGBA(image.Rect(0, 0, width, height)) shape := image.NewNRGBA(image.Rect(0, 0, width, height))
@ -81,10 +81,7 @@ func drawKey(t *image.NRGBA, name string, x, y, width int) error {
} }
} }
draw.Draw(t, image.Rect(x, y, x+width, y+height), shape, image.ZP, draw.Over) draw.Draw(t, image.Rect(x, y, x+width, y+height), shape, image.ZP, draw.Over)
if err := common.ArcadeFont.DrawTextOnImage(t, name, x+4, y+5); err != nil { common.ArcadeFont.DrawTextOnImage(t, name, x+4, y+5)
return err
}
return nil
} }
func outputKeyboardImage() (map[string]image.Rectangle, error) { func outputKeyboardImage() (map[string]image.Rectangle, error) {

View File

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

View File

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

View File

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

View File

@ -39,14 +39,10 @@ var (
func update(screen *ebiten.Image) error { func update(screen *ebiten.Image) error {
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
op.GeoM.Scale(1.0/mosaicRatio, 1.0/mosaicRatio) op.GeoM.Scale(1.0/mosaicRatio, 1.0/mosaicRatio)
if err := gophersRenderTarget.DrawImage(gophersImage, op); err != nil { gophersRenderTarget.DrawImage(gophersImage, op)
return err
}
op = &ebiten.DrawImageOptions{} op = &ebiten.DrawImageOptions{}
op.GeoM.Scale(mosaicRatio, mosaicRatio) op.GeoM.Scale(mosaicRatio, mosaicRatio)
if err := screen.DrawImage(gophersRenderTarget, op); err != nil { screen.DrawImage(gophersRenderTarget, op)
return err
}
return nil return nil
} }
@ -57,10 +53,7 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
w, h := gophersImage.Size() w, h := gophersImage.Size()
gophersRenderTarget, err = ebiten.NewImage(w/mosaicRatio, h/mosaicRatio, ebiten.FilterNearest) gophersRenderTarget, _ = ebiten.NewImage(w/mosaicRatio, h/mosaicRatio, ebiten.FilterNearest)
if err != nil {
log.Fatal(err)
}
if err := ebiten.Run(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

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

View File

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

View File

@ -146,9 +146,7 @@ func update(screen *ebiten.Image) error {
return err return err
} }
} }
if err := ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS())); err != nil { ebitenutil.DebugPrint(screen, fmt.Sprintf("FPS: %0.2f", ebiten.CurrentFPS()))
return err
}
if err := audioContext.Update(); err != nil { if err := audioContext.Update(); err != nil {
return err return err
} }

View File

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

View File

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

View File

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

View File

@ -100,9 +100,7 @@ func update(screen *ebiten.Image) error {
} }
} }
msg := fmt.Sprintf("FPS: %0.2f\nThis is an example using infinite audio stream.", ebiten.CurrentFPS()) msg := fmt.Sprintf("FPS: %0.2f\nThis is an example using infinite audio stream.", ebiten.CurrentFPS())
if err := ebitenutil.DebugPrint(screen, msg); err != nil { ebitenutil.DebugPrint(screen, msg)
return err
}
if err := audioContext.Update(); err != nil { if err := audioContext.Update(); err != nil {
return err return err
} }

View File

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

View File

@ -86,25 +86,19 @@ func update(screen *ebiten.Image) error {
ebiten.SetScreenSize(screenWidth, screenHeight) ebiten.SetScreenSize(screenWidth, screenHeight)
ebiten.SetScreenScale(screenScale) ebiten.SetScreenScale(screenScale)
if err := screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff}); err != nil { screen.Fill(color.RGBA{0x80, 0x80, 0xc0, 0xff})
return err
}
w, h := gophersImage.Size() w, h := gophersImage.Size()
w2, h2 := screen.Size() w2, h2 := screen.Size()
op := &ebiten.DrawImageOptions{} op := &ebiten.DrawImageOptions{}
op.GeoM.Translate(float64(-w+w2)/2, float64(-h+h2)/2) op.GeoM.Translate(float64(-w+w2)/2, float64(-h+h2)/2)
if err := screen.DrawImage(gophersImage, op); err != nil { screen.DrawImage(gophersImage, op)
return err
}
x, y := ebiten.CursorPosition() x, y := ebiten.CursorPosition()
msg := fmt.Sprintf(`Press arrow keys to change the window size msg := fmt.Sprintf(`Press arrow keys to change the window size
Press S key to change the window scale Press S key to change the window scale
Cursor: (%d, %d) Cursor: (%d, %d)
FPS: %0.2f`, x, y, ebiten.CurrentFPS()) FPS: %0.2f`, x, y, ebiten.CurrentFPS())
if err := ebitenutil.DebugPrint(screen, msg); err != nil { ebitenutil.DebugPrint(screen, msg)
return err
}
return nil return nil
} }