Skip to content

Commit

Permalink
fixed imdraw rows and cols
Browse files Browse the repository at this point in the history
  • Loading branch information
bhperry committed Aug 19, 2024
1 parent cb52478 commit 2497e95
Showing 1 changed file with 27 additions and 36 deletions.
63 changes: 27 additions & 36 deletions tools/benchmark/imdraw_bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -55,52 +55,51 @@ 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)
}
}
}

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
}

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)
Expand All @@ -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))
}

0 comments on commit 2497e95

Please sign in to comment.