From f0277727d14490723bfc2ca1a34712cdb30083b0 Mon Sep 17 00:00:00 2001 From: Hajime Hoshi Date: Mon, 29 Dec 2014 13:36:29 +0900 Subject: [PATCH] Refactoring --- ebitenutil/image.go | 14 +------------ example/alphablending/main.go | 7 ------- example/blocks/blocks/field.go | 4 ++-- example/blocks/blocks/game.go | 5 ----- example/blocks/blocks/gamescene.go | 24 ++++++++++++++-------- example/blocks/blocks/input.go | 6 ++---- example/blocks/blocks/piece.go | 11 +++++----- example/blocks/blocks/scenemanager.go | 29 ++++++++++++++++----------- example/blocks/blocks/titlescene.go | 29 +++++++++++++-------------- 9 files changed, 57 insertions(+), 72 deletions(-) diff --git a/ebitenutil/image.go b/ebitenutil/image.go index f505e4c69..4f24447c2 100644 --- a/ebitenutil/image.go +++ b/ebitenutil/image.go @@ -17,10 +17,10 @@ package ebitenutil import ( "github.com/hajimehoshi/ebiten" "image" - "image/png" "os" ) +// NewImageFromFile loads the file path and returns ebiten.Image and image.Image. func NewImageFromFile(path string, filter ebiten.Filter) (*ebiten.Image, image.Image, error) { file, err := os.Open(path) if err != nil { @@ -37,15 +37,3 @@ func NewImageFromFile(path string, filter ebiten.Filter) (*ebiten.Image, image.I } return img2, img, err } - -func SaveImageAsPNG(path string, img *ebiten.Image) error { - file, err := os.Create(path) - if err != nil { - return err - } - defer file.Close() - if err := png.Encode(file, img); err != nil { - return err - } - return nil -} diff --git a/example/alphablending/main.go b/example/alphablending/main.go index a2c5efdf0..647a5dfda 100644 --- a/example/alphablending/main.go +++ b/example/alphablending/main.go @@ -68,13 +68,6 @@ func update(screen *ebiten.Image) error { } } - if !saved && ebiten.IsKeyPressed(ebiten.KeySpace) { - if err := ebitenutil.SaveImageAsPNG("out.png", screen); err != nil { - return err - } - saved = true - } - return nil } diff --git a/example/blocks/blocks/field.go b/example/blocks/blocks/field.go index 97d0f2250..52a377178 100644 --- a/example/blocks/blocks/field.go +++ b/example/blocks/blocks/field.go @@ -110,11 +110,11 @@ func (f *Field) flushLine(j int) bool { return true } -func (f *Field) Draw(r *ebiten.Image, x, y int) { +func (f *Field) Draw(r *ebiten.Image, x, y int) error { blocks := make([][]BlockType, len(f.blocks)) for i, blockCol := range f.blocks { blocks[i] = make([]BlockType, len(blockCol)) copy(blocks[i], blockCol[:]) } - drawBlocks(r, blocks, x, y) + return drawBlocks(r, blocks, x, y) } diff --git a/example/blocks/blocks/game.go b/example/blocks/blocks/game.go index c2a6323f2..10bff8fca 100644 --- a/example/blocks/blocks/game.go +++ b/example/blocks/blocks/game.go @@ -19,11 +19,6 @@ import ( "sync" ) -type Size struct { - Width int - Height int -} - const ScreenWidth = 256 const ScreenHeight = 240 diff --git a/example/blocks/blocks/gamescene.go b/example/blocks/blocks/gamescene.go index e16203a13..f6c269d6a 100644 --- a/example/blocks/blocks/gamescene.go +++ b/example/blocks/blocks/gamescene.go @@ -54,7 +54,7 @@ const fieldWidth = blockWidth * fieldBlockNumX const fieldHeight = blockHeight * fieldBlockNumY func (s *GameScene) choosePiece() *Piece { - num := NormalBlockTypeNum + num := int(BlockTypeMax) blockType := BlockType(s.rand.Intn(num) + 1) return Pieces[blockType] } @@ -67,7 +67,7 @@ func (s *GameScene) initCurrentPiece(piece *Piece) { s.currentPieceAngle = Angle0 } -func (s *GameScene) Update(state *GameState) { +func (s *GameScene) Update(state *GameState) error { const maxLandingCount = 60 if s.currentPiece == nil { @@ -112,23 +112,31 @@ func (s *GameScene) Update(state *GameState) { s.landingCount = 0 } } + return nil } -func (s *GameScene) Draw(r *ebiten.Image) { - r.Fill(color.White) +func (s *GameScene) Draw(r *ebiten.Image) error { + if err := r.Fill(color.White); err != nil { + return err + } field := imageEmpty w, h := field.Size() geo := ebiten.ScaleGeo(float64(fieldWidth)/float64(w), float64(fieldHeight)/float64(h)) geo.Concat(ebiten.TranslateGeo(20, 20)) // TODO: magic number? - r.DrawImage(field, &ebiten.DrawImageOptions{ + if err := r.DrawImage(field, &ebiten.DrawImageOptions{ GeoM: geo, ColorM: ebiten.ScaleColor(0.0, 0.0, 0.0, 0.5), - }) + }); err != nil { + return err + } - s.field.Draw(r, 20, 20) + if err := s.field.Draw(r, 20, 20); err != nil { + return err + } if s.currentPiece != nil { - s.currentPiece.Draw(r, 20, 20, s.currentPieceX, s.currentPieceY, s.currentPieceAngle) + return s.currentPiece.Draw(r, 20, 20, s.currentPieceX, s.currentPieceY, s.currentPieceAngle) } + return nil } diff --git a/example/blocks/blocks/input.go b/example/blocks/blocks/input.go index 7b6eec39e..36f6344ea 100644 --- a/example/blocks/blocks/input.go +++ b/example/blocks/blocks/input.go @@ -27,9 +27,7 @@ func NewInput() *Input { for key := ebiten.Key(0); key < ebiten.KeyMax; key++ { states[key] = 0 } - return &Input{ - states: states, - } + return &Input{states} } func (i *Input) StateForKey(key ebiten.Key) int { @@ -42,6 +40,6 @@ func (i *Input) Update() { i.states[key] = 0 continue } - i.states[key] += 1 + i.states[key]++ } } diff --git a/example/blocks/blocks/piece.go b/example/blocks/blocks/piece.go index 880381a27..87d0adc07 100644 --- a/example/blocks/blocks/piece.go +++ b/example/blocks/blocks/piece.go @@ -57,10 +57,9 @@ const ( BlockType5 BlockType6 BlockType7 + BlockTypeMax = BlockType7 ) -const NormalBlockTypeNum = 7 - type Piece struct { blockType BlockType blocks [][]bool @@ -144,7 +143,7 @@ const blockHeight = 10 const fieldBlockNumX = 10 const fieldBlockNumY = 20 -func drawBlocks(r *ebiten.Image, blocks [][]BlockType, x, y int) { +func drawBlocks(r *ebiten.Image, blocks [][]BlockType, x, y int) error { parts := []ebiten.ImagePart{} for i, blockCol := range blocks { for j, block := range blockCol { @@ -160,7 +159,7 @@ func drawBlocks(r *ebiten.Image, blocks [][]BlockType, x, y int) { }) } } - r.DrawImage(imageBlocks, &ebiten.DrawImageOptions{ + return r.DrawImage(imageBlocks, &ebiten.DrawImageOptions{ Parts: parts, }) } @@ -222,7 +221,7 @@ func (p *Piece) AbsorbInto(field *Field, x, y int, angle Angle) { } } -func (p *Piece) Draw(r *ebiten.Image, fieldX, fieldY int, pieceX, pieceY int, angle Angle) { +func (p *Piece) Draw(r *ebiten.Image, fieldX, fieldY int, pieceX, pieceY int, angle Angle) error { size := len(p.blocks) blocks := make([][]BlockType, size) for i := range p.blocks { @@ -236,5 +235,5 @@ func (p *Piece) Draw(r *ebiten.Image, fieldX, fieldY int, pieceX, pieceY int, an x := fieldX + pieceX*blockWidth y := fieldY + pieceY*blockHeight - drawBlocks(r, blocks, x, y) + return drawBlocks(r, blocks, x, y) } diff --git a/example/blocks/blocks/scenemanager.go b/example/blocks/blocks/scenemanager.go index dc097b63d..d1cb979c5 100644 --- a/example/blocks/blocks/scenemanager.go +++ b/example/blocks/blocks/scenemanager.go @@ -36,8 +36,8 @@ func init() { } type Scene interface { - Update(state *GameState) - Draw(r *ebiten.Image) + Update(state *GameState) error + Draw(screen *ebiten.Image) error } const transitionMaxCount = 20 @@ -55,10 +55,9 @@ func NewSceneManager(initScene Scene) *SceneManager { } } -func (s *SceneManager) Update(state *GameState) { +func (s *SceneManager) Update(state *GameState) error { if s.transitionCount == -1 { - s.current.Update(state) - return + return s.current.Update(state) } s.transitionCount++ if transitionMaxCount <= s.transitionCount { @@ -66,23 +65,29 @@ func (s *SceneManager) Update(state *GameState) { s.next = nil s.transitionCount = -1 } + return nil } -func (s *SceneManager) Draw(r *ebiten.Image) { +func (s *SceneManager) Draw(r *ebiten.Image) error { if s.transitionCount == -1 { - s.current.Draw(r) - return + return s.current.Draw(r) } transitionFrom.Clear() - s.current.Draw(transitionFrom) + if err := s.current.Draw(transitionFrom); err != nil { + return err + } transitionTo.Clear() - s.next.Draw(transitionTo) + if err := s.next.Draw(transitionTo); err != nil { + return err + } - r.DrawImage(transitionFrom, nil) + if err := r.DrawImage(transitionFrom, nil); err != nil { + return err + } alpha := float64(s.transitionCount) / float64(transitionMaxCount) - r.DrawImage(transitionTo, &ebiten.DrawImageOptions{ + return r.DrawImage(transitionTo, &ebiten.DrawImageOptions{ ColorM: ebiten.ScaleColor(1, 1, 1, alpha), }) } diff --git a/example/blocks/blocks/titlescene.go b/example/blocks/blocks/titlescene.go index 181e150fe..45e674174 100644 --- a/example/blocks/blocks/titlescene.go +++ b/example/blocks/blocks/titlescene.go @@ -39,14 +39,15 @@ func NewTitleScene() *TitleScene { return &TitleScene{} } -func (s *TitleScene) Update(state *GameState) { +func (s *TitleScene) Update(state *GameState) error { s.count++ if state.Input.StateForKey(ebiten.KeySpace) == 1 { state.SceneManager.GoTo(NewGameScene()) } + return nil } -func (s *TitleScene) Draw(r *ebiten.Image) { +func (s *TitleScene) Draw(r *ebiten.Image) error { drawTitleBackground(r, s.count) drawLogo(r, "BLOCKS") @@ -54,29 +55,27 @@ func (s *TitleScene) Draw(r *ebiten.Image) { x := (ScreenWidth - textWidth(message)) / 2 y := ScreenHeight - 48 drawTextWithShadow(r, message, x, y, 1, color.NRGBA{0x80, 0, 0, 0xff}) + return nil } func drawTitleBackground(r *ebiten.Image, c int) { - const imageWidth = 32 - const imageHeight = 32 + w, h := imageBackground.Size() + dx := (-c / 4) % w + dy := (c / 4) % h - dx := (-c / 4) % imageWidth - dy := (c / 4) % imageHeight - - backgroundImage := imageBackground parts := []ebiten.ImagePart{} - for j := -1; j < ScreenHeight/imageHeight+1; j++ { - for i := 0; i < ScreenWidth/imageWidth+1; i++ { - dstX := i*imageWidth + dx - dstY := j*imageHeight + dy + for j := -1; j < ScreenHeight/h+1; j++ { + for i := 0; i < ScreenWidth/w+1; i++ { + dstX := i*w + dx + dstY := j*h + dy parts = append(parts, ebiten.ImagePart{ - Dst: image.Rect(dstX, dstY, dstX+imageWidth, dstY+imageHeight), - Src: image.Rect(0, 0, imageWidth, imageHeight), + Dst: image.Rect(dstX, dstY, dstX+w, dstY+h), + Src: image.Rect(0, 0, w, h), }) } } - r.DrawImage(backgroundImage, &ebiten.DrawImageOptions{ + r.DrawImage(imageBackground, &ebiten.DrawImageOptions{ Parts: parts, }) }