Skip to content

Commit

Permalink
Add moving sprite tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bhperry committed Aug 23, 2024
1 parent d78eb8c commit c61adc3
Show file tree
Hide file tree
Showing 2 changed files with 98 additions and 10 deletions.
22 changes: 12 additions & 10 deletions tools/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,15 @@ Information about the machines used to record benchmark stats

### Stats

| Machine | Pixel | Benchmark | Duration | Frames | FPS Avg | FPS Min | FPS Max | FPS Stdev |
|--------------------|--------|------------------------------|----------|--------|----------|----------|----------|-----------|
| bhperry-wsl | v2.2.0 | imdraw-moving | 30s | 2193 | 73.1 | 66 | 76 | 2.73 |
| bhperry-wsl | v2.2.0 | imdraw-static | 30.01s | 2344 | 78.1 | 70 | 80 | 1.67 |
| bhperry-wsl | v2.2.0 | sprite-static | 30.01s | 1509 | 50.29 | 47 | 52 | 1.15 |
| bhperry-wsl | v2.2.0 | sprite-static-batched | 30.01s | 5187 | 172.83 | 153 | 178 | 4.57 |
| bhperry-win10 | v2.2.0 | imdraw-moving | 30s | 1436 | 47.86 | 23 | 50 | 5.92 |
| bhperry-win10 | v2.2.0 | imdraw-static | 30.03s | 1570 | 52.28 | 51 | 53 | 0.53 |
| bhperry-win10 | v2.2.0 | sprite-static | 30.03s | 1242 | 41.36 | 40 | 42 | 0.6 |
| bhperry-win10 | v2.2.0 | sprite-static-batched | 30s | 40957 | 1365.23 | 1254 | 1383 | 23.39 |
| Machine | Pixel | Benchmark | Duration | Frames | FPS Avg | FPS Min | FPS Max | FPS Stdev |
|--------------------|--------|------------------------------|----------|--------|---------|---------|---------|-----------|
| bhperry-wsl | v2.2.0 | imdraw-moving | 30.01s | 2232 | 74.37 | 60 | 78 | 3.45 |
| bhperry-wsl | v2.2.0 | imdraw-static | 30.02s | 2334 | 77.75 | 73 | 80 | 1.2 |
| bhperry-wsl | v2.2.0 | sprite-moving | 30.03s | 1452 | 48.35 | 45 | 50 | 1.05 |
| bhperry-wsl | v2.2.0 | sprite-moving-batched | 30.01s | 4004 | 133.42 | 127 | 139 | 2.45 |
| bhperry-wsl | v2.2.0 | sprite-static | 30.02s | 1534 | 51.1 | 48 | 52 | 0.91 |
| bhperry-wsl | v2.2.0 | sprite-static-batched | 30s | 5293 | 176.43 | 163 | 179 | 2.99 |
| bhperry-win10 | v2.2.0 | imdraw-moving | 30s | 1436 | 47.86 | 23 | 50 | 5.92 |
| bhperry-win10 | v2.2.0 | imdraw-static | 30.03s | 1570 | 52.28 | 51 | 53 | 0.53 |
| bhperry-win10 | v2.2.0 | sprite-static | 30.03s | 1242 | 41.36 | 40 | 42 | 0.6 |
| bhperry-win10 | v2.2.0 | sprite-static-batched | 30s | 40957 | 1365.23 | 1254 | 1383 | 23.39 |
86 changes: 86 additions & 0 deletions tools/benchmark/sprite_bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ func init() {
logoPath = path.Join(basepath, logoPath)

Benchmarks.Add(
Config{
Name: "sprite-moving",
Description: "Columns of sprites moving in opposite directions",
New: newSpriteMoving,
Duration: 30 * time.Second,
},
Config{
Name: "sprite-moving-batched",
Description: "Columns of sprites moving in opposite directions with batched draw",
New: newSpriteMovingBatched,
Duration: 30 * time.Second,
},
Config{
Name: "sprite-static",
Description: "Draw a sprite to the window in a grid",
Expand Down Expand Up @@ -92,6 +104,58 @@ func (ss *spriteStatic) Step(win *opengl.Window) {
}
}

func newSpriteMoving(win *opengl.Window) (Benchmark, error) {
sprite, err := loadSprite(logoPath, logoFrame)
if err != nil {
return nil, err
}
bounds := win.Bounds()
width := bounds.W()
height := bounds.H()
rows, cols := 32, 32
benchmark := &spriteMoving{
sprite: sprite,
rows: rows,
cols: cols,
cell: gridCell(width, height, rows, cols),
}
return benchmark, nil
}

func newSpriteMovingBatched(win *opengl.Window) (Benchmark, error) {
benchmark, err := newSpriteMoving(win)
if err != nil {
return nil, err
}
sm := benchmark.(*spriteMoving)
sm.batch = pixel.NewBatch(&pixel.TrianglesData{}, sm.sprite.Picture())
return sm, nil
}

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

func (sm *spriteMoving) Step(win *opengl.Window) {
win.Clear(backgroundColor)
var target pixel.Target
if sm.batch != nil {
sm.batch.Clear()
target = sm.batch
} else {
target = win
}
spriteGridMoving(sm.sprite, target, sm.rows, sm.cols, sm.cell, sm.counter)
if sm.batch != nil {
sm.batch.Draw(win)
}
sm.counter += 1
}

func spriteGrid(sprite *pixel.Sprite, target pixel.Target, rows, cols int, cell pixel.Vec) {
spriteBounds := sprite.Frame().Bounds()
spriteWidth := spriteBounds.W()
Expand All @@ -106,6 +170,28 @@ func spriteGrid(sprite *pixel.Sprite, target pixel.Target, rows, cols int, cell
}
}

func spriteGridMoving(sprite *pixel.Sprite, target pixel.Target, rows, cols int, cell pixel.Vec, counter int) {
spriteBounds := sprite.Frame().Bounds()
spriteWidth := spriteBounds.W()
spriteHeight := spriteBounds.H()
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++ {
yOffset := -cell.Y
delta := float64(counter % int(cell.Y))
if i%2 == 0 {
yOffset += delta
} else {
yOffset -= delta
}

for j := 0; j < rows+2; j++ {
pos := pixel.V(float64(i)*cell.X, (float64(j)*cell.Y)+yOffset).Add(offset)
sprite.Draw(target, matrix.Moved(pos))
}
}
}

func loadSprite(file string, frame pixel.Rect) (sprite *pixel.Sprite, err error) {
image, err := loadPng(file)
if err != nil {
Expand Down

0 comments on commit c61adc3

Please sign in to comment.