Skip to content

Commit

Permalink
Fixed sprite rows and cols
Browse files Browse the repository at this point in the history
  • Loading branch information
bhperry committed Aug 19, 2024
1 parent 2497e95 commit 5f2c39e
Showing 1 changed file with 27 additions and 20 deletions.
47 changes: 27 additions & 20 deletions tools/benchmark/sprite_bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package benchmark
import (
"image"
"image/png"
"math"
"os"
"path"
"path/filepath"
Expand All @@ -17,7 +16,7 @@ import (
var (
basepath string
logoPath = "logo/LOGOTYPE-HORIZONTAL-BLUE2.png"
logoFrame = pixel.R(100, 44, 232, 180)
logoFrame = pixel.R(98, 44, 234, 180)
)

func init() {
Expand All @@ -42,11 +41,14 @@ func init() {
}

func newSingleSprite() Benchmark {
return &singleSprite{}
return &singleSprite{rows: 32, cols: 32}
}

type singleSprite struct {
sprite *pixel.Sprite
sprite *pixel.Sprite
rows, cols int
width, height float64
cell pixel.Vec
}

func (ss *singleSprite) Init(win *opengl.Window) error {
Expand All @@ -55,21 +57,28 @@ func (ss *singleSprite) Init(win *opengl.Window) error {
return err
}
ss.sprite = sprite
bounds := win.Bounds()
ss.width = bounds.W()
ss.height = bounds.H()
ss.cell = gridCell(ss.width, ss.height, ss.rows, ss.cols)
return nil
}

func (ss *singleSprite) Step(win *opengl.Window) {
win.Clear(backgroundColor)
spriteGrid(ss.sprite, win, win.Bounds())
spriteGrid(ss.sprite, win, ss.rows, ss.cols, ss.cell)
}

func newBatchedSprite() Benchmark {
return &batchedSprite{}
return &batchedSprite{rows: 32, cols: 32}
}

type batchedSprite struct {
sprite *pixel.Sprite
batch *pixel.Batch
sprite *pixel.Sprite
batch *pixel.Batch
rows, cols int
width, height float64
cell pixel.Vec
}

func (bs *batchedSprite) Init(win *opengl.Window) error {
Expand All @@ -79,32 +88,30 @@ func (bs *batchedSprite) Init(win *opengl.Window) error {
}
bs.sprite = sprite
bs.batch = pixel.NewBatch(&pixel.TrianglesData{}, sprite.Picture())
bounds := win.Bounds()
bs.width = bounds.W()
bs.height = bounds.H()
bs.cell = gridCell(bs.width, bs.height, bs.rows, bs.cols)
return nil
}

func (bs *batchedSprite) Step(win *opengl.Window) {
win.Clear(backgroundColor)
bs.batch.Clear()
spriteGrid(bs.sprite, bs.batch, win.Bounds())
spriteGrid(bs.sprite, bs.batch, bs.rows, bs.cols, bs.cell)
bs.batch.Draw(win)
}

func spriteGrid(sprite *pixel.Sprite, target pixel.Target, bounds pixel.Rect) {
func spriteGrid(sprite *pixel.Sprite, target pixel.Target, rows, cols int, cell pixel.Vec) {
spriteBounds := sprite.Frame().Bounds()
spriteWidth := spriteBounds.W()
spriteHeight := spriteBounds.H()

heightRatio := bounds.H() / spriteHeight
cols := int(math.Ceil(bounds.W() / spriteWidth))
rows := int(math.Ceil(heightRatio))

alignTop := (heightRatio - float64(rows)) * spriteHeight
offset := pixel.V(spriteWidth/2, spriteHeight/2+alignTop)

matrix := pixel.IM.ScaledXY(pixel.ZV, pixel.V(cell.X/spriteWidth, cell.Y/spriteHeight))
offset := pixel.V(cell.X/2, cell.Y/2)
for i := 0; i < cols; i++ {
for j := 0; j < rows; j++ {
pos := pixel.V(float64(i)*spriteWidth, float64(j)*spriteHeight).Add(offset)
sprite.Draw(target, pixel.IM.Moved(pos))
pos := pixel.V(float64(i)*cell.X, float64(j)*cell.Y).Add(offset)
sprite.Draw(target, matrix.Moved(pos))
}
}
}
Expand Down

0 comments on commit 5f2c39e

Please sign in to comment.