Whats the best way to draw a square 2D Grid on the screen based on world position and scale (zoom)?
The current method:
gridSize = 32
worldPosX = 0
worldPosY = 0
zoom as float
zoomMultiplier as float
perPixelUnit = 1
centreX = screen width()*.5
centreY = screen height()*.5
sync on : sync rate 60
set text font "Tahoma" : set text size 13
do : cls rgb(64,64,64)
// update zoom
zoomMultiplier = clamp(zoomMultiplier+(mousemovez() * 0.001), -5,5.0)
zoom = perPixelUnit * (2 ^ zoomMultiplier)
// draw mouse coord
x = worldPosX + (mousex()-centreX) / zoom
y = worldPosY - (mousey()-centreY) / zoom
ink set alpha(rgb(192,192,192),150),0
text mousex()-12,mousey()+24,"x: "+str$(x,0)
text mousex()+24,mousey(),"y: "+str$(y,0)
// draw grid
width = centreX/zoom
hieght = centreY/zoom
// make sure the grid is rescaled when beyond a limit
// (this i will change for user defined sizes)
if size <> 0
if ((toX-fromX)/size) < 12
gridSize = gridSize / 2
endif
if ((toX-fromX)/size) > 64
gridSize = gridSize * 2
endif
endif
size = gridSize / 4
fromX = size * ((int(worldPosX-width)+size-1)/size)
toX = size * ((int(worldPosX+width)+size-1)/size)
fromY = size * ((int(worldPosY-hieght)+size-1)/size)
toY = size * ((int(worldPosY+hieght)+size-1)/size)
a2StartLineBatch ((toX-fromX)/size)+((toY-fromY)/size)+1
for n = (fromX-size) to (toX+size) step size
x = centreX+(n-worldPosX)*zoom
a2line x,0,x,screen height(),set alpha(rgb(0, 255, 0),25-(sgn(abs(n) %% gridSize)*10))
next
for n = (fromY-size) to (toY+size) step size
y = centreY-(n-worldPosY)*zoom
a2line 0,y,screen width(),y,set alpha(rgb(0, 255, 0),25-(sgn(abs(n) %% gridSize)*10))
next
y = centreY+worldPosY*zoom
a2line 0,y,screen width(),y,set alpha(rgb(255, 0, 0),100)
a2EndBatch
sync
loop
"Get in the Van!" - Van B