diff --git a/tools/benchmark/sprite_bench.go b/tools/benchmark/sprite_bench.go index 02598af..6f3d56d 100644 --- a/tools/benchmark/sprite_bench.go +++ b/tools/benchmark/sprite_bench.go @@ -3,7 +3,6 @@ package benchmark import ( "image" "image/png" - "math" "os" "path" "path/filepath" @@ -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() { @@ -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 { @@ -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 { @@ -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)) } } }