mirror of
https://github.com/hajimehoshi/ebiten.git
synced 2024-12-24 02:38:53 +01:00
Speed up: avoid allocating
This commit is contained in:
parent
ea98fefc4f
commit
9bec0cf1c6
@ -15,12 +15,12 @@
|
||||
package blocks
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/hajimehoshi/ebiten"
|
||||
"github.com/hajimehoshi/ebiten/ebitenutil"
|
||||
"image"
|
||||
"image/color"
|
||||
"math"
|
||||
"strings"
|
||||
)
|
||||
|
||||
var imageFont *ebiten.Image
|
||||
@ -41,11 +41,15 @@ func textWidth(str string) int {
|
||||
return charWidth * len(str)
|
||||
}
|
||||
|
||||
var fontImageParts = make([]ebiten.ImagePart, 0, 256)
|
||||
|
||||
func drawText(rt *ebiten.Image, str string, ox, oy, scale int, c color.Color) error {
|
||||
parts := make([]ebiten.ImagePart, len(strings.Replace(str, "\n", "", -1)))
|
||||
if cap(fontImageParts) < len(str) {
|
||||
return errors.New("str is too long")
|
||||
}
|
||||
parts := fontImageParts[:0]
|
||||
|
||||
locationX, locationY := 0, 0
|
||||
i := 0
|
||||
for _, c := range str {
|
||||
if c == '\n' {
|
||||
locationX = 0
|
||||
@ -55,9 +59,10 @@ func drawText(rt *ebiten.Image, str string, ox, oy, scale int, c color.Color) er
|
||||
code := int(c)
|
||||
x := (code % 16) * charWidth
|
||||
y := ((code - 32) / 16) * charHeight
|
||||
parts[i].Dst = image.Rect(locationX, locationY, locationX+charWidth, locationY+charHeight)
|
||||
parts[i].Src = image.Rect(x, y, x+charWidth, y+charHeight)
|
||||
i++
|
||||
parts = append(parts, ebiten.ImagePart{
|
||||
Dst: image.Rect(locationX, locationY, locationX+charWidth, locationY+charHeight),
|
||||
Src: image.Rect(x, y, x+charWidth, y+charHeight),
|
||||
})
|
||||
locationX += charWidth
|
||||
}
|
||||
|
||||
|
@ -143,17 +143,10 @@ const blockHeight = 10
|
||||
const fieldBlockNumX = 10
|
||||
const fieldBlockNumY = 20
|
||||
|
||||
var blocksImageParts = make([]ebiten.ImagePart, 0, fieldBlockNumX*fieldBlockNumY)
|
||||
|
||||
func drawBlocks(r *ebiten.Image, blocks [][]BlockType, x, y int, clr ebiten.ColorM) error {
|
||||
l := 0
|
||||
for _, blockCol := range blocks {
|
||||
for _, block := range blockCol {
|
||||
if block == BlockTypeNone {
|
||||
continue
|
||||
}
|
||||
l++
|
||||
}
|
||||
}
|
||||
parts := make([]ebiten.ImagePart, 0, l)
|
||||
parts := blocksImageParts[:0]
|
||||
for i, blockCol := range blocks {
|
||||
for j, block := range blockCol {
|
||||
if block == BlockTypeNone {
|
||||
|
@ -33,10 +33,14 @@ func init() {
|
||||
|
||||
type TitleScene struct {
|
||||
count int
|
||||
parts []ebiten.ImagePart
|
||||
}
|
||||
|
||||
func NewTitleScene() *TitleScene {
|
||||
return &TitleScene{}
|
||||
w, h := imageBackground.Size()
|
||||
return &TitleScene{
|
||||
parts: make([]ebiten.ImagePart, (ScreenHeight/h+2)*(ScreenWidth/w+2)),
|
||||
}
|
||||
}
|
||||
|
||||
func (s *TitleScene) Update(state *GameState) error {
|
||||
@ -48,7 +52,7 @@ func (s *TitleScene) Update(state *GameState) error {
|
||||
}
|
||||
|
||||
func (s *TitleScene) Draw(r *ebiten.Image) error {
|
||||
if err := drawTitleBackground(r, s.count); err != nil {
|
||||
if err := s.drawTitleBackground(r, s.count); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := drawLogo(r, "BLOCKS"); err != nil {
|
||||
@ -61,25 +65,24 @@ func (s *TitleScene) Draw(r *ebiten.Image) error {
|
||||
return drawTextWithShadow(r, message, x, y, 1, color.NRGBA{0x80, 0, 0, 0xff})
|
||||
}
|
||||
|
||||
func drawTitleBackground(r *ebiten.Image, c int) error {
|
||||
func (s *TitleScene) drawTitleBackground(r *ebiten.Image, c int) error {
|
||||
w, h := imageBackground.Size()
|
||||
dx := (-c / 4) % w
|
||||
dy := (c / 4) % h
|
||||
|
||||
parts := []ebiten.ImagePart{}
|
||||
index := 0
|
||||
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+w, dstY+h),
|
||||
Src: image.Rect(0, 0, w, h),
|
||||
})
|
||||
s.parts[index].Dst = image.Rect(dstX, dstY, dstX+w, dstY+h)
|
||||
s.parts[index].Src = image.Rect(0, 0, w, h)
|
||||
index++
|
||||
}
|
||||
}
|
||||
|
||||
return r.DrawImage(imageBackground, &ebiten.DrawImageOptions{
|
||||
Parts: parts,
|
||||
Parts: s.parts,
|
||||
})
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user