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.
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.