PDA

Click to See Complete Forum and Search --> : Snapping To Grid...


Jun 3rd, 2000, 11:32 AM
I'm making a 2D level editor for a 3D game engine. The z positions are set by textboxes.

I implemented a grid today, which was fairly easy to do. Now I want to add a toggled "snap-to-grid" feature. I tried for many hours to figure this out but I dont get it. Can someone help me out?

Here's my current source code for grid lines in a picture box:
--------------------------------------------------------
Private Function CreateCheckeredBrush(ByVal hDC As Long, ByVal lColor1 As Long, ByVal lColor2 As Long) As Long

Dim X As Long
Dim Y As Long
Dim lRet As Long
Dim hBitmapDC As Long
Dim hBitmap As Long
Dim hOldBitmap As Long

'Convert System Colors if needed
If lColor1 < 0 Then
lColor1 = GetSysColor(lColor1 And &HFF&)
End If
If lColor2 < 0 Then
lColor2 = GetSysColor(lColor2 And &HFF&)
End If

'Create a new DC and Bitmap to draw the Brush
hBitmapDC = CreateCompatibleDC(hDC)
hBitmap = CreateCompatibleBitmap(hDC, 8, 8)
'Select the Bitmap into the DC for drawing
hOldBitmap = SelectObject(hBitmapDC, hBitmap)

'Draw the Brush's Bitmap (Checkerboard)
For Y = 0 To 6 Step 2
For X = 0 To 6 Step 2
lRet = SetPixelV(hBitmapDC, X, Y, lColor1)
lRet = SetPixelV(hBitmapDC, X + 1, Y, lColor2)
lRet = SetPixelV(hBitmapDC, X, Y + 1, lColor2)
lRet = SetPixelV(hBitmapDC, X + 1, Y + 1, lColor1)
Next X
Next Y

'Get the bitmap back out of the DC
hBitmap = SelectObject(hBitmapDC, hOldBitmap)

'Create the Brush from the bitmap
CreateCheckeredBrush = CreatePatternBrush(hBitmap)

'Delete the DC and Bitmap to free memory
lRet = DeleteDC(hBitmapDC)
lRet = DeleteObject(hBitmap)

End Function

Sub ShowGrid()

'Create a Checkered Brush (Dark and Light Grey)...
hBrush = CreateCheckeredBrush(picBoard.hDC, &H808080, &HC0C0C0)
'...and Select it into the PictureBox
hOldBrush = SelectObject(picBoard.hDC, hBrush)

iWidth = picBoard.ScaleWidth
iHeight = picBoard.ScaleHeight

'Draw the gridlines using the checkered pattern brush.
For fX = 0 To iWidth Step mfScale
lRet = PatBlt(picBoard.hDC, Int(fX), 0, 1, iHeight, PATCOPY)
Next
For fY = 0 To iHeight Step mfScale
lRet = PatBlt(picBoard.hDC, 0, Int(fY), iWidth, 1, PATCOPY)
Next

End Sub
-----------------------------------------------------------

-WebKing

P.S.- if anyone has any good ideas about programming a level editor in VB like Qoole or Worldcraft, then let me know. That's going to be my next project.

Aaron Young
Jun 3rd, 2000, 12:37 PM
All you need to do is alter the X/Y Coords by rounding them to the nearest Grid position, i.e.

In a Form with a Picturebox and a Checkbox:Private bInit As Boolean

Private Sub Picture1_MouseMove(Button As Integer, Shift As Integer, X As Single, Y As Single)
Static nX As Long
Static nY As Long

If chkSnap2Grid Then
X = Int(X / 32) * 32
Y = Int(Y / 32) * 32
End If

With Picture1
.ScaleMode = vbPixels
.DrawMode = vbXorPen
If bInit Then
Picture1.Line (nX, nY)-Step(32, 32), vbWhite, BF
End If
bInit = True
Picture1.Line (X, Y)-Step(32, 32), vbWhite, BF
End With
nX = X
nY = Y
End Sub

Private Sub Picture1_Paint()
Dim X As Long
Dim Y As Long

bInit = False
With Picture1
.Cls
.DrawMode = vbCopyPen
.ScaleMode = vbPixels
For Y = 0 To Int(.ScaleHeight / 32)
For X = 0 To Int(.ScaleWidth / 32)
Picture1.Line (X * 32, Y * 32)-Step(32, 32), , B
Next
Next
End With
End Sub
- Aaron.

Jun 3rd, 2000, 09:53 PM
WOW!!!
I didnt really need this, but I had to try it, you REALLY deserve guru status....
wow.....

Jun 4th, 2000, 09:09 AM
Thanks man! I can't believe I didn't think of that one.