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

View File

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

View File

@ -76,7 +76,7 @@ var (
}
)
func (i *Input) Update() error {
func (i *Input) Update() {
for k := range dirKeys {
if ebiten.IsKeyPressed(k) {
i.keyState[k]++
@ -84,7 +84,6 @@ func (i *Input) Update() error {
i.keyState[k] = 0
}
}
return nil
}
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
ni, nj := t.next.x, t.next.y
v := t.current.value
if v == 0 {
return nil
return
}
op := &ebiten.DrawImageOptions{}
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))
r, g, b, a := colorToScale(tileBackgroundColor(v))
op.ColorM.Scale(r, g, b, a)
if err := boardImage.DrawImage(tileImage, op); err != nil {
return err
}
boardImage.DrawImage(tileImage, op)
str := strconv.Itoa(v)
scale := 2
if 2 < len(str) {
@ -348,8 +346,5 @@ func (t *Tile) Draw(boardImage *ebiten.Image) error {
h := common.ArcadeFont.TextHeight(str) * scale
x = x + (tileSize-w)/2
y = y + (tileSize-h)/2
if err := common.ArcadeFont.DrawText(boardImage, str, x, y, scale, tileColor(v)); err != nil {
return err
}
return nil
common.ArcadeFont.DrawText(boardImage, str, x, y, scale, tileColor(v))
}

View File

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

View File

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

View File

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

View File

@ -66,17 +66,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

@ -102,9 +102,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--
@ -119,7 +119,6 @@ func (p *Player) updateVolume() error {
volume128 = 128
}
p.audioPlayer.SetVolume(float64(volume128) / 128)
return nil
}
func (p *Player) updatePlayPause() error {
@ -140,29 +139,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)
@ -170,7 +169,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 {
@ -191,26 +189,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()
@ -226,9 +218,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
@ -250,9 +240,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

@ -152,7 +152,7 @@ func (f *Field) flushLine(j int) bool {
return true
}
func (f *Field) Update() error {
func (f *Field) Update() {
if 0 <= f.flushCount {
f.flushCount--
if f.flushCount == 0 {
@ -162,7 +162,6 @@ func (f *Field) Update() error {
}
}
}
return nil
}
func min(a, b float64) float64 {
@ -186,7 +185,7 @@ func (f *Field) flushingColor() ebiten.ColorM {
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))
flushingBlocks := make([][]BlockType, len(f.blocks))
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 {
return err
}
if err := drawBlocks(r, flushingBlocks, x, y, f.flushingColor()); err != nil {
return err
}
return nil
drawBlocks(r, blocks, x, y, ebiten.ColorM{})
drawBlocks(r, flushingBlocks, x, y, f.flushingColor())
}

View File

@ -23,14 +23,14 @@ import (
"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
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
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
}
if !ebiten.IsRunningSlowly() {
if err := game.sceneManager.Draw(r); err != nil {
return err
}
game.sceneManager.Draw(r)
}
return nil
}

View File

@ -77,13 +77,11 @@ func (s *GamepadScene) Update(state *GameState) error {
return nil
}
func (s *GamepadScene) Draw(screen *ebiten.Image) error {
if err := screen.Fill(color.Black); err != nil {
return err
}
func (s *GamepadScene) Draw(screen *ebiten.Image) {
screen.Fill(color.Black)
if s.buttonStates == nil {
return nil
return
}
f := `GAMEPAD CONFIGURATION
@ -108,8 +106,5 @@ ROTATE RIGHT: %s
msg = "OK!"
}
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 {
return err
}
return nil
common.ArcadeFont.DrawTextWithShadow(screen, str, 16, 16, 1, color.White)
}

View File

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

View File

@ -106,34 +106,27 @@ func (s *TitleScene) Update(state *GameState) error {
return nil
}
func (s *TitleScene) Draw(r *ebiten.Image) error {
if err := s.drawTitleBackground(r, s.count); err != nil {
return err
}
if err := drawLogo(r, "BLOCKS"); err != nil {
return err
}
func (s *TitleScene) Draw(r *ebiten.Image) {
s.drawTitleBackground(r, s.count)
drawLogo(r, "BLOCKS")
message := "PRESS SPACE TO START"
x := (ScreenWidth - common.ArcadeFont.TextWidth(message)) / 2
y := ScreenHeight - 48
if err := common.ArcadeFont.DrawTextWithShadow(r, message, x, y, 1, color.NRGBA{0x80, 0, 0, 0xff}); err != nil {
return err
}
return nil
common.ArcadeFont.DrawTextWithShadow(r, message, x, y, 1, color.NRGBA{0x80, 0, 0, 0xff})
}
func (s *TitleScene) drawTitleBackground(r *ebiten.Image, c int) error {
func (s *TitleScene) drawTitleBackground(r *ebiten.Image, c int) {
s.parts.count = c
return r.DrawImage(imageBackground, &ebiten.DrawImageOptions{
r.DrawImage(imageBackground, &ebiten.DrawImageOptions{
ImageParts: s.parts,
})
}
func drawLogo(r *ebiten.Image, str string) error {
func drawLogo(r *ebiten.Image, str string) {
scale := 4
textWidth := common.ArcadeFont.TextWidth(str) * scale
x := (ScreenWidth - textWidth) / 2
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() {
img, err := assets.ArcadeFontImage()
if err != nil {
panic(err)
}
eimg, err := ebiten.NewImageFromImage(img, ebiten.FilterNearest)
if err != nil {
panic(err)
}
img := assets.ArcadeFontImage()
eimg, _ := ebiten.NewImageFromImage(img, ebiten.FilterNearest)
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
}
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{
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
}
options.ColorM.Scale(r, g, b, a)
return rt.DrawImage(f.image, options)
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}
for i := 0; i < parts.Len(); i++ {
dx0, dy0, dx1, dy1 := parts.Dst(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)
}
return nil
}
func (f *Font) DrawTextWithShadow(rt *ebiten.Image, str string, x, y, scale int, clr color.Color) error {
if err := f.DrawText(rt, str, x+1, y+1, scale, color.NRGBA{0, 0, 0, 0x80}); err != nil {
return err
}
if err := f.DrawText(rt, str, x, y, scale, clr); err != nil {
return err
}
return nil
func (f *Font) DrawTextWithShadow(rt *ebiten.Image, str string, x, y, scale int, clr color.Color) {
f.DrawText(rt, str, x+1, y+1, scale, color.NRGBA{0, 0, 0, 0x80})
f.DrawText(rt, str, x, y, scale, clr)
}

View File

@ -21,18 +21,16 @@ package assets
import (
"bytes"
"fmt"
"image"
_ "image/png"
)
func ArcadeFontImage() (image.Image, error) {
b, err := Asset("arcadefont.png")
if err != nil {
return nil, err
}
func ArcadeFontImage() image.Image {
b := MustAsset("arcadefont.png")
img, _, err := image.Decode(bytes.NewBuffer(b))
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)
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

@ -56,9 +56,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

@ -81,16 +81,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

@ -41,9 +41,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

@ -73,41 +73,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
}
@ -119,23 +105,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

@ -40,7 +40,7 @@ var keyboardKeys = [][]string{
{"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
width--
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)
if err := common.ArcadeFont.DrawTextOnImage(t, name, x+4, y+5); err != nil {
return err
}
return nil
common.ArcadeFont.DrawTextOnImage(t, name, x+4, y+5)
}
func outputKeyboardImage() (map[string]image.Rectangle, error) {

View File

@ -86,9 +86,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++ {
@ -116,9 +114,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

@ -181,9 +181,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

@ -65,31 +65,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
}
@ -118,10 +106,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})
@ -133,10 +118,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

@ -39,14 +39,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
}
@ -57,10 +53,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

@ -58,12 +58,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

@ -38,55 +38,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,
@ -94,22 +82,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

@ -146,9 +146,7 @@ func update(screen *ebiten.Image) error {
return err
}
}
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

@ -62,9 +62,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

@ -162,18 +162,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
@ -185,12 +176,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"}
@ -205,12 +192,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)
}
}
@ -225,16 +208,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

@ -42,9 +42,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

@ -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())
if err := ebitenutil.DebugPrint(screen, msg); err != nil {
return err
}
ebitenutil.DebugPrint(screen, msg)
if err := audioContext.Update(); err != nil {
return err
}

View File

@ -124,15 +124,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
}

View File

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