mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2025-01-27 11:12:44 +01:00
Remove blocks.Images
This commit is contained in:
parent
0a2a76be3b
commit
83d129e674
@ -110,11 +110,11 @@ func (f *Field) flushLine(j int) bool {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Field) Draw(r *ebiten.Image, images *Images, x, y int) {
|
func (f *Field) Draw(r *ebiten.Image, x, y int) {
|
||||||
blocks := make([][]BlockType, len(f.blocks))
|
blocks := make([][]BlockType, len(f.blocks))
|
||||||
for i, blockCol := range f.blocks {
|
for i, blockCol := range f.blocks {
|
||||||
blocks[i] = make([]BlockType, len(blockCol))
|
blocks[i] = make([]BlockType, len(blockCol))
|
||||||
copy(blocks[i], blockCol[:])
|
copy(blocks[i], blockCol[:])
|
||||||
}
|
}
|
||||||
drawBlocks(r, images, blocks, x, y)
|
drawBlocks(r, blocks, x, y)
|
||||||
}
|
}
|
||||||
|
@ -39,7 +39,7 @@ func textWidth(str string) int {
|
|||||||
return charWidth * len(str)
|
return charWidth * len(str)
|
||||||
}
|
}
|
||||||
|
|
||||||
func drawText(rt *ebiten.Image, images *Images, str string, ox, oy, scale int, c color.Color) {
|
func drawText(rt *ebiten.Image, str string, ox, oy, scale int, c color.Color) {
|
||||||
parts := []ebiten.ImagePart{}
|
parts := []ebiten.ImagePart{}
|
||||||
|
|
||||||
locationX, locationY := 0, 0
|
locationX, locationY := 0, 0
|
||||||
@ -75,7 +75,7 @@ func drawText(rt *ebiten.Image, images *Images, str string, ox, oy, scale int, c
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func drawTextWithShadow(rt *ebiten.Image, images *Images, str string, x, y, scale int, clr color.Color) {
|
func drawTextWithShadow(rt *ebiten.Image, str string, x, y, scale int, clr color.Color) {
|
||||||
drawText(rt, images, str, x+1, y+1, scale, color.NRGBA{0, 0, 0, 0x80})
|
drawText(rt, str, x+1, y+1, scale, color.NRGBA{0, 0, 0, 0x80})
|
||||||
drawText(rt, images, str, x, y, scale, clr)
|
drawText(rt, str, x, y, scale, clr)
|
||||||
}
|
}
|
||||||
|
@ -36,7 +36,6 @@ type Game struct {
|
|||||||
once sync.Once
|
once sync.Once
|
||||||
sceneManager *SceneManager
|
sceneManager *SceneManager
|
||||||
input *Input
|
input *Input
|
||||||
images *Images
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGame() *Game {
|
func NewGame() *Game {
|
||||||
@ -53,6 +52,6 @@ func (game *Game) Update(r *ebiten.Image) error {
|
|||||||
SceneManager: game.sceneManager,
|
SceneManager: game.sceneManager,
|
||||||
Input: game.input,
|
Input: game.input,
|
||||||
})
|
})
|
||||||
game.sceneManager.Draw(r, game.images)
|
game.sceneManager.Draw(r)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -114,7 +114,7 @@ func (s *GameScene) Update(state *GameState) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *GameScene) Draw(r *ebiten.Image, images *Images) {
|
func (s *GameScene) Draw(r *ebiten.Image) {
|
||||||
r.Fill(color.White)
|
r.Fill(color.White)
|
||||||
|
|
||||||
field := imageEmpty
|
field := imageEmpty
|
||||||
@ -126,9 +126,9 @@ func (s *GameScene) Draw(r *ebiten.Image, images *Images) {
|
|||||||
ColorM: ebiten.ScaleColor(0.0, 0.0, 0.0, 0.5),
|
ColorM: ebiten.ScaleColor(0.0, 0.0, 0.0, 0.5),
|
||||||
})
|
})
|
||||||
|
|
||||||
s.field.Draw(r, images, 20, 20)
|
s.field.Draw(r, 20, 20)
|
||||||
|
|
||||||
if s.currentPiece != nil {
|
if s.currentPiece != nil {
|
||||||
s.currentPiece.Draw(r, images, 20, 20, s.currentPieceX, s.currentPieceY, s.currentPieceAngle)
|
s.currentPiece.Draw(r, 20, 20, s.currentPieceX, s.currentPieceY, s.currentPieceAngle)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,134 +0,0 @@
|
|||||||
// Copyright 2014 Hajime Hoshi
|
|
||||||
//
|
|
||||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
||||||
// you may not use this file except in compliance with the License.
|
|
||||||
// You may obtain a copy of the License at
|
|
||||||
//
|
|
||||||
// http://www.apache.org/licenses/LICENSE-2.0
|
|
||||||
//
|
|
||||||
// Unless required by applicable law or agreed to in writing, software
|
|
||||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
||||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
||||||
// See the License for the specific language governing permissions and
|
|
||||||
// limitations under the License.
|
|
||||||
|
|
||||||
package blocks
|
|
||||||
|
|
||||||
import (
|
|
||||||
"github.com/hajimehoshi/ebiten"
|
|
||||||
"image"
|
|
||||||
_ "image/png"
|
|
||||||
"os"
|
|
||||||
"sync"
|
|
||||||
)
|
|
||||||
|
|
||||||
type namePath struct {
|
|
||||||
name string
|
|
||||||
path string
|
|
||||||
}
|
|
||||||
|
|
||||||
type nameSize struct {
|
|
||||||
name string
|
|
||||||
size Size
|
|
||||||
}
|
|
||||||
|
|
||||||
type Images struct {
|
|
||||||
imagePaths chan namePath
|
|
||||||
renderTargetSizes chan nameSize
|
|
||||||
images map[string]*ebiten.Image
|
|
||||||
renderTargets map[string]*ebiten.Image
|
|
||||||
sync.RWMutex
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewImages() *Images {
|
|
||||||
images := &Images{
|
|
||||||
imagePaths: make(chan namePath),
|
|
||||||
renderTargetSizes: make(chan nameSize),
|
|
||||||
images: map[string]*ebiten.Image{},
|
|
||||||
renderTargets: map[string]*ebiten.Image{},
|
|
||||||
}
|
|
||||||
go func() {
|
|
||||||
for {
|
|
||||||
images.loopMain()
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
return images
|
|
||||||
}
|
|
||||||
|
|
||||||
func loadImage(path string) (image.Image, error) {
|
|
||||||
file, err := os.Open(path)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
img, _, err := image.Decode(file)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return img, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *Images) loopMain() {
|
|
||||||
select {
|
|
||||||
case p := <-i.imagePaths:
|
|
||||||
name := p.name
|
|
||||||
path := p.path
|
|
||||||
go func() {
|
|
||||||
img, err := loadImage(path)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
id, err := ebiten.NewImageFromImage(img, ebiten.FilterNearest)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
i.Lock()
|
|
||||||
defer i.Unlock()
|
|
||||||
i.images[name] = id
|
|
||||||
}()
|
|
||||||
case s := <-i.renderTargetSizes:
|
|
||||||
name := s.name
|
|
||||||
size := s.size
|
|
||||||
go func() {
|
|
||||||
id, err := ebiten.NewImage(size.Width, size.Height, ebiten.FilterNearest)
|
|
||||||
if err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
i.Lock()
|
|
||||||
defer i.Unlock()
|
|
||||||
i.renderTargets[name] = id
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *Images) RequestImage(name string, path string) {
|
|
||||||
i.imagePaths <- namePath{name, path}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *Images) RequestRenderTarget(name string, size Size) {
|
|
||||||
i.renderTargetSizes <- nameSize{name, size}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *Images) Has(name string) bool {
|
|
||||||
i.RLock()
|
|
||||||
defer i.RUnlock()
|
|
||||||
_, ok := i.images[name]
|
|
||||||
if ok {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
_, ok = i.renderTargets[name]
|
|
||||||
return ok
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *Images) GetImage(name string) *ebiten.Image {
|
|
||||||
i.RLock()
|
|
||||||
defer i.RUnlock()
|
|
||||||
return i.images[name]
|
|
||||||
}
|
|
||||||
|
|
||||||
func (i *Images) GetRenderTarget(name string) *ebiten.Image {
|
|
||||||
i.RLock()
|
|
||||||
defer i.RUnlock()
|
|
||||||
return i.renderTargets[name]
|
|
||||||
}
|
|
@ -144,7 +144,7 @@ const blockHeight = 10
|
|||||||
const fieldBlockNumX = 10
|
const fieldBlockNumX = 10
|
||||||
const fieldBlockNumY = 20
|
const fieldBlockNumY = 20
|
||||||
|
|
||||||
func drawBlocks(r *ebiten.Image, images *Images, blocks [][]BlockType, x, y int) {
|
func drawBlocks(r *ebiten.Image, blocks [][]BlockType, x, y int) {
|
||||||
parts := []ebiten.ImagePart{}
|
parts := []ebiten.ImagePart{}
|
||||||
for i, blockCol := range blocks {
|
for i, blockCol := range blocks {
|
||||||
for j, block := range blockCol {
|
for j, block := range blockCol {
|
||||||
@ -222,7 +222,7 @@ func (p *Piece) AbsorbInto(field *Field, x, y int, angle Angle) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *Piece) Draw(r *ebiten.Image, images *Images, fieldX, fieldY int, pieceX, pieceY int, angle Angle) {
|
func (p *Piece) Draw(r *ebiten.Image, fieldX, fieldY int, pieceX, pieceY 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 {
|
||||||
@ -236,5 +236,5 @@ func (p *Piece) Draw(r *ebiten.Image, images *Images, fieldX, fieldY int, pieceX
|
|||||||
|
|
||||||
x := fieldX + pieceX*blockWidth
|
x := fieldX + pieceX*blockWidth
|
||||||
y := fieldY + pieceY*blockHeight
|
y := fieldY + pieceY*blockHeight
|
||||||
drawBlocks(r, images, blocks, x, y)
|
drawBlocks(r, blocks, x, y)
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@ func init() {
|
|||||||
|
|
||||||
type Scene interface {
|
type Scene interface {
|
||||||
Update(state *GameState)
|
Update(state *GameState)
|
||||||
Draw(r *ebiten.Image, images *Images)
|
Draw(r *ebiten.Image)
|
||||||
}
|
}
|
||||||
|
|
||||||
const transitionMaxCount = 20
|
const transitionMaxCount = 20
|
||||||
@ -68,16 +68,16 @@ func (s *SceneManager) Update(state *GameState) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SceneManager) Draw(r *ebiten.Image, images *Images) {
|
func (s *SceneManager) Draw(r *ebiten.Image) {
|
||||||
if s.transitionCount == -1 {
|
if s.transitionCount == -1 {
|
||||||
s.current.Draw(r, images)
|
s.current.Draw(r)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
transitionFrom.Clear()
|
transitionFrom.Clear()
|
||||||
s.current.Draw(transitionFrom, images)
|
s.current.Draw(transitionFrom)
|
||||||
|
|
||||||
transitionTo.Clear()
|
transitionTo.Clear()
|
||||||
s.next.Draw(transitionTo, images)
|
s.next.Draw(transitionTo)
|
||||||
|
|
||||||
r.DrawImage(transitionFrom, nil)
|
r.DrawImage(transitionFrom, nil)
|
||||||
|
|
||||||
|
@ -46,14 +46,14 @@ func (s *TitleScene) Update(state *GameState) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *TitleScene) Draw(r *ebiten.Image, images *Images) {
|
func (s *TitleScene) Draw(r *ebiten.Image) {
|
||||||
drawTitleBackground(r, s.count)
|
drawTitleBackground(r, s.count)
|
||||||
drawLogo(r, images, "BLOCKS")
|
drawLogo(r, "BLOCKS")
|
||||||
|
|
||||||
message := "PRESS SPACE TO START"
|
message := "PRESS SPACE TO START"
|
||||||
x := (ScreenWidth - textWidth(message)) / 2
|
x := (ScreenWidth - textWidth(message)) / 2
|
||||||
y := ScreenHeight - 48
|
y := ScreenHeight - 48
|
||||||
drawTextWithShadow(r, images, message, x, y, 1, color.NRGBA{0x80, 0, 0, 0xff})
|
drawTextWithShadow(r, message, x, y, 1, color.NRGBA{0x80, 0, 0, 0xff})
|
||||||
}
|
}
|
||||||
|
|
||||||
func drawTitleBackground(r *ebiten.Image, c int) {
|
func drawTitleBackground(r *ebiten.Image, c int) {
|
||||||
@ -81,10 +81,10 @@ func drawTitleBackground(r *ebiten.Image, c int) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func drawLogo(r *ebiten.Image, images *Images, str string) {
|
func drawLogo(r *ebiten.Image, str string) {
|
||||||
scale := 4
|
scale := 4
|
||||||
textWidth := textWidth(str) * scale
|
textWidth := textWidth(str) * scale
|
||||||
x := (ScreenWidth - textWidth) / 2
|
x := (ScreenWidth - textWidth) / 2
|
||||||
y := 32
|
y := 32
|
||||||
drawTextWithShadow(r, images, str, x, y, scale, color.NRGBA{0x00, 0x00, 0x80, 0xff})
|
drawTextWithShadow(r, str, x, y, scale, color.NRGBA{0x00, 0x00, 0x80, 0xff})
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user