From 2497e95dbf333630058c84373e9e22f42bd3b0d0 Mon Sep 17 00:00:00 2001 From: Ben Perry Date: Sun, 18 Aug 2024 20:35:31 -0500 Subject: [PATCH] fixed imdraw rows and cols --- tools/benchmark/imdraw_bench.go | 63 ++++++++++++++------------------- 1 file changed, 27 insertions(+), 36 deletions(-) diff --git a/tools/benchmark/imdraw_bench.go b/tools/benchmark/imdraw_bench.go index 6055032..a502415 100644 --- a/tools/benchmark/imdraw_bench.go +++ b/tools/benchmark/imdraw_bench.go @@ -31,22 +31,22 @@ func init() { } func newStaticTriangles() Benchmark { - return &staticTriangles{} + return &staticTriangles{rows: 32, cols: 32} } type staticTriangles struct { - imd *imdraw.IMDraw - rows, cols int - width, height, cellSize float64 + imd *imdraw.IMDraw + rows, cols int + width, height float64 + cell pixel.Vec } func (st *staticTriangles) Init(win *opengl.Window) error { 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) + st.cell = gridCell(st.width, st.height, st.rows, st.cols) + st.imd = tri(st.cell) return nil } @@ -55,7 +55,7 @@ func (st *staticTriangles) Step(win *opengl.Window) { 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) + pos := pixel.V(float64(i)*st.cell.X, float64(j)*st.cell.Y) win.SetMatrix(pixel.IM.Moved(pos)) st.imd.Draw(win) } @@ -63,23 +63,23 @@ func (st *staticTriangles) Step(win *opengl.Window) { } func newMovingTriangles() Benchmark { - return &movingTriangles{} + return &movingTriangles{rows: 32, cols: 32} } type movingTriangles struct { - imd *imdraw.IMDraw - rows, cols int - width, height, cellSize float64 - counter int + imd *imdraw.IMDraw + rows, cols int + width, height float64 + cell pixel.Vec + counter int } func (mt *movingTriangles) Init(win *opengl.Window) error { 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) + mt.cell = gridCell(mt.width, mt.height, mt.rows, mt.cols) + mt.imd = tri(mt.cell) return nil } @@ -87,20 +87,19 @@ 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)) + yOffset := -mt.cell.Y + delta := float64(mt.counter % int(mt.cell.Y)) if i%2 == 0 { - offset += delta + yOffset += delta } else { - offset -= delta + yOffset -= delta } for j := 0; j < mt.rows+2; j++ { - pos := pixel.V(float64(i)*mt.cellSize, float64(j)*mt.cellSize) - pos.Y += offset + pos := pixel.V(float64(i)*mt.cell.X, (float64(j)*mt.cell.Y)+yOffset) matrix := pixel.IM.Moved(pos) if i%2 == 1 { - matrix = matrix.Rotated(pos.Add(pixel.V(mt.cellSize/2, mt.cellSize/2)), math.Pi) + matrix = matrix.Rotated(pos.Add(pixel.V(mt.cell.X/2, mt.cell.Y/2)), math.Pi) } win.SetMatrix(matrix) mt.imd.Draw(win) @@ -110,26 +109,18 @@ func (mt *movingTriangles) Step(win *opengl.Window) { mt.counter++ } -func tri(size float64) *imdraw.IMDraw { +func tri(cell pixel.Vec) *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.Push(pixel.V(cell.X, 0)) imd.Color = pixel.RGB(0, 0, 1) - imd.Push(pixel.V(size/2, size)) + imd.Push(pixel.V(cell.X/2, cell.Y)) 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) +func gridCell(width, height float64, rows, cols int) (cell pixel.Vec) { + return pixel.V(width/float64(cols), height/float64(rows)) }