-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
127 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
package benchmark | ||
|
||
import ( | ||
"math" | ||
"time" | ||
|
||
"github.com/gopxl/pixel/v2" | ||
"github.com/gopxl/pixel/v2/backends/opengl" | ||
"github.com/gopxl/pixel/v2/ext/imdraw" | ||
) | ||
|
||
var ( | ||
backgroundColor = pixel.RGB(0, 0, 0) | ||
) | ||
|
||
func init() { | ||
Benchmarks.Add( | ||
Config{ | ||
Name: "tri-static", | ||
Description: "Stationary RGB triangles in a grid", | ||
Benchmark: &staticTriangles{}, | ||
Duration: 30 * time.Second, | ||
}, | ||
) | ||
Benchmarks.Add( | ||
Config{ | ||
Name: "tri-moving", | ||
Description: "Columns of RGB triangles moving in opposite directions", | ||
Benchmark: &movingTriangles{}, | ||
Duration: 30 * time.Second, | ||
}, | ||
) | ||
} | ||
|
||
type staticTriangles struct { | ||
imd *imdraw.IMDraw | ||
rows, cols int | ||
width, height, cellSize float64 | ||
} | ||
|
||
func (st *staticTriangles) Init(win *opengl.Window) { | ||
bounds := win.Bounds() | ||
st.width = bounds.W() | ||
st.height = bounds.H() | ||
st.cellSize = 32 | ||
st.rows, st.cols = grid(st.width, st.height, st.cellSize) | ||
st.imd = tri(st.cellSize) | ||
} | ||
|
||
func (st *staticTriangles) Step(win *opengl.Window) { | ||
win.Clear(backgroundColor) | ||
|
||
for i := 0; i < st.cols; i++ { | ||
for j := 0; j < st.rows; j++ { | ||
pos := pixel.V(float64(i)*st.cellSize, float64(j)*st.cellSize) | ||
win.SetMatrix(pixel.IM.Moved(pos)) | ||
st.imd.Draw(win) | ||
} | ||
} | ||
} | ||
|
||
type movingTriangles struct { | ||
imd *imdraw.IMDraw | ||
rows, cols int | ||
width, height, cellSize float64 | ||
counter int | ||
} | ||
|
||
func (mt *movingTriangles) Init(win *opengl.Window) { | ||
bounds := win.Bounds() | ||
mt.width = bounds.W() | ||
mt.height = bounds.H() | ||
mt.cellSize = 32 | ||
mt.rows, mt.cols = grid(mt.width, mt.height, mt.cellSize) | ||
mt.imd = tri(mt.cellSize) | ||
} | ||
|
||
func (mt *movingTriangles) Step(win *opengl.Window) { | ||
win.Clear(backgroundColor) | ||
|
||
for i := 0; i < mt.cols; i++ { | ||
offset := -mt.cellSize | ||
delta := float64(mt.counter % int(mt.cellSize)) | ||
if i%2 == 0 { | ||
offset += delta | ||
} else { | ||
offset -= delta | ||
} | ||
|
||
for j := 0; j < mt.rows+2; j++ { | ||
pos := pixel.V(float64(i)*mt.cellSize, float64(j)*mt.cellSize) | ||
pos.Y += offset | ||
matrix := pixel.IM.Moved(pos) | ||
if i%2 == 1 { | ||
matrix = matrix.Rotated(pos.Add(pixel.V(mt.cellSize/2, mt.cellSize/2)), math.Pi) | ||
} | ||
win.SetMatrix(matrix) | ||
mt.imd.Draw(win) | ||
} | ||
} | ||
|
||
mt.counter++ | ||
} | ||
|
||
func tri(size float64) *imdraw.IMDraw { | ||
imd := imdraw.New(nil) | ||
imd.Color = pixel.RGB(1, 0, 0) | ||
imd.Push(pixel.V(0, 0)) | ||
imd.Color = pixel.RGB(0, 1, 0) | ||
imd.Push(pixel.V(size, 0)) | ||
imd.Color = pixel.RGB(0, 0, 1) | ||
imd.Push(pixel.V(size/2, size)) | ||
imd.Polygon(0) | ||
return imd | ||
} | ||
|
||
func grid(width, height, cellSize float64) (int, int) { | ||
cols := width / cellSize | ||
rows := height / cellSize | ||
if rows != float64(int(rows)) { | ||
rows += 1 | ||
} | ||
if cols != float64(int(cols)) { | ||
cols += 1 | ||
} | ||
return int(rows), int(cols) | ||
} |