Skip to content

Commit

Permalink
Combine batched/un-batched sprite structs
Browse files Browse the repository at this point in the history
  • Loading branch information
bhperry committed Aug 23, 2024
1 parent 8b7335d commit d78eb8c
Showing 1 changed file with 19 additions and 33 deletions.
52 changes: 19 additions & 33 deletions tools/benchmark/sprite_bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func newSpriteStatic(win *opengl.Window) (Benchmark, error) {
width := bounds.W()
height := bounds.H()
rows, cols := 32, 32

benchmark := &spriteStatic{
sprite: sprite,
rows: rows,
Expand All @@ -59,51 +60,36 @@ func newSpriteStatic(win *opengl.Window) (Benchmark, error) {
return benchmark, nil
}

type spriteStatic struct {
sprite *pixel.Sprite
rows, cols int
cell pixel.Vec
}

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

func newSpriteStaticBatched(win *opengl.Window) (Benchmark, error) {
sprite, err := loadSprite(logoPath, logoFrame)
benchmark, err := newSpriteStatic(win)
if err != nil {
return nil, err
}

bounds := win.Bounds()
width := bounds.W()
height := bounds.H()
rows, cols := 32, 32

benchmark := &spriteStaticBatched{
sprite: sprite,
batch: pixel.NewBatch(&pixel.TrianglesData{}, sprite.Picture()),
rows: rows,
cols: cols,
cell: gridCell(width, height, rows, cols),
}

return benchmark, nil
ss := benchmark.(*spriteStatic)
ss.batch = pixel.NewBatch(&pixel.TrianglesData{}, ss.sprite.Picture())
return ss, nil
}

type spriteStaticBatched struct {
type spriteStatic struct {
sprite *pixel.Sprite
batch *pixel.Batch
rows, cols int
cell pixel.Vec
batch *pixel.Batch
}

func (ssb *spriteStaticBatched) Step(win *opengl.Window) {
func (ss *spriteStatic) Step(win *opengl.Window) {
win.Clear(backgroundColor)
ssb.batch.Clear()
spriteGrid(ssb.sprite, ssb.batch, ssb.rows, ssb.cols, ssb.cell)
ssb.batch.Draw(win)
var target pixel.Target
if ss.batch != nil {
ss.batch.Clear()
target = ss.batch
} else {
target = win
}
spriteGrid(ss.sprite, target, ss.rows, ss.cols, ss.cell)
if ss.batch != nil {
ss.batch.Draw(win)
}
}

func spriteGrid(sprite *pixel.Sprite, target pixel.Target, rows, cols int, cell pixel.Vec) {
Expand Down

0 comments on commit d78eb8c

Please sign in to comment.