Results 1 to 5 of 5

Thread: vb 2010 Mapeditor

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    vb 2010 Mapeditor

    Hello,

    I have a few problems with making a map editor, if someone knows how to make one please reply and ill give you my msn

    well actually this problem: its not making the grid invisible if you de-deselect the option "Grid On" because the rect's have already been drawn. So I need to know how you can delete them.
    Code:
        Private Sub GridOnToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GridOnToolStripMenuItem.Click
    
            If GridOnToolStripMenuItem.Checked = True Then
                GridOnToolStripMenuItem.Checked = False
                GRID_ON = False
            Else
                GridOnToolStripMenuItem.Checked = True
                GRID_ON = True
            End If
    
            UpdateMapEditorGraphics()
    
        End Sub
    Code:
        Public Sub UpdateMapEditorGraphics()
    
            If GRID_ON = True Then
    
                For x = 0 To MAX_GRID_X
                    For y = 0 To MAX_GRID_Y
                        sRect = New Rectangle(x * TILE_SIZE_X, y * TILE_SIZE_Y, TILE_SIZE_X, TILE_SIZE_Y)
                        g.DrawRectangle(Pens.Black, sRect)
    
                    Next
                Next
            Else
    'GRAPHICS SHOULD BE MADE INVISIBLE HERE'
            End If
    
        End Sub

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: vb 2010 Mapeditor

    When using GDI+ to draw on controls, you draw only on the Paint event. The drawing then gets done every time the control is repainted. You store the data that describes the drawing in member variables and you read those variables in the Paint event handler. If you want to control whether the grid gets drawn or not, you would use a Boolean variable for that. Whenever you want to show or hide the grid, you simply toggle the variable and force a repaint, which you do by calling Refresh on the control.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3
    Fanatic Member ThomasJohnsen's Avatar
    Join Date
    Jul 2010
    Location
    Denmark
    Posts
    528

    Re: vb 2010 Mapeditor

    Like Mr. McIlhinney said you need to draw under the Paint event.

    Say you declare some classes for a Map and Elements within a map:
    vb Code:
    1. 'This class represents a single square on a map
    2.     Public Class MapElement
    3.  
    4.         Public ElementColor As Color
    5.  
    6.         'more info about elements of a map.
    7.  
    8.         Public Sub New(ByVal sColor As Color)
    9.             ElementColor = sColor
    10.         End Sub
    11.  
    12.     End Class
    13.  
    14.     'This class represents an entire map made up of MapElements
    15.     Public Class Map
    16.  
    17.         'An array of elements within the map
    18.         'The array can be resized to any size with ReDim
    19.         Private vMapElements(,) As MapElement
    20.  
    21.         Public Sub New(ByVal sSizeX As Integer, ByVal sSizeY As Integer)
    22.             ReDim vMapElements(sSizeX, sSizeY)
    23.  
    24.             For i As Integer = 0 To sSizeX - 1
    25.                 For j As Integer = 0 To sSizeY - 1
    26.                     vMapElements(i, j) = Nothing
    27.                 Next
    28.             Next
    29.         End Sub
    30.  
    31.         'Gets/sets the current element at (X, Y).
    32.         'Note that these may be Nothing with the current implementation
    33.         Default Public Property Element(ByVal X As Integer, ByVal Y As Integer) As MapElement
    34.             Get
    35.                 Return vMapElements(X, Y)
    36.             End Get
    37.             Set(ByVal value As MapElement)
    38.                 vMapElements(X, Y) = value
    39.             End Set
    40.         End Property
    41.  
    42.         Public ReadOnly Property MapSizeX As Integer
    43.             Get
    44.                 Return vMapElements.GetUpperBound(0)
    45.             End Get
    46.         End Property
    47.         Public ReadOnly Property MapSizeY As Integer
    48.             Get
    49.                 Return vMapElements.GetUpperBound(1)
    50.             End Get
    51.         End Property
    52.  
    53.         'Draws the current contents of the map into sGraphics using sOffset as
    54.         'the upper, left corner.
    55.         Public Sub RenderMap(ByVal sGraphics As Graphics, ByVal sOffset As Point, ByVal sSize As Size)
    56.  
    57.             Dim R As Rectangle = New Rectangle
    58.             Dim B As New SolidBrush(Color.AliceBlue)
    59.             Dim vLocation As Point = sOffset
    60.  
    61.             R.Size = sSize
    62.  
    63.             For i As Integer = 0 To Me.MapSizeX - 1
    64.                 For j As Integer = 0 To Me.MapSizeY - 1
    65.  
    66.                     R.Location = vLocation
    67.  
    68.                     'If the element is present then draw it.
    69.                     If vMapElements(i, j) IsNot Nothing Then
    70.                         B.Color = vMapElements(i, j).ElementColor
    71.                         sGraphics.FillRectangle(B, R)
    72.                     End If
    73.  
    74.                     'Update the current location
    75.                     vLocation.Y += sSize.Height
    76.  
    77.                 Next
    78.  
    79.                 'Update the current location
    80.                 vLocation.Y = sOffset.Y
    81.                 vLocation.X += sSize.Width
    82.             Next
    83.  
    84.         End Sub
    85.  
    86.     End Class

    declare an instance of this Map with:
    vb Code:
    1. 'Declare a new map of size 20 by 10 elements.
    2.     Private MyMap As New Map(20, 10)

    and place this (modified version of your grid routine) in the Paint event of your form:
    vb Code:
    1. Dim PtStart, PtEnd As Point
    2.         Dim LeftMargin As Integer = 8
    3.         Dim TopMargin As Integer = 32
    4.  
    5.         e.Graphics.Clear(Me.BackColor)
    6.  
    7.         If MyMap IsNot Nothing Then
    8.  
    9.             MyMap.RenderMap(e.Graphics, New Point(LeftMargin, TopMargin), New Size(TILE_SIZE_X, TILE_SIZE_Y))
    10.  
    11.             If GRID_ON = True Then
    12.  
    13.                 '1. Draw some vertical lines
    14.                 PtStart = New Point(LeftMargin, TopMargin)
    15.                 PtEnd = New Point(LeftMargin, TopMargin + MyMap.MapSizeY * TILE_SIZE_Y)
    16.  
    17.                 For x As Integer = 0 To MyMap.MapSizeX
    18.                     e.Graphics.DrawLine(Pens.Black, PtStart, PtEnd)
    19.                     PtStart.X += TILE_SIZE_X
    20.                     PtEnd.X += TILE_SIZE_X
    21.                 Next
    22.  
    23.                 '2. Draw some horizontal lines
    24.                 PtStart = New Point(LeftMargin, TopMargin)
    25.                 PtEnd = New Point(LeftMargin + MyMap.MapSizeX * TILE_SIZE_X, TopMargin)
    26.  
    27.                 For y As Integer = 0 To MyMap.MapSizeY
    28.                     e.Graphics.DrawLine(Pens.Black, PtStart, PtEnd)
    29.                     PtStart.Y += TILE_SIZE_Y
    30.                     PtEnd.Y += TILE_SIZE_Y
    31.                 Next
    32.  
    33.             End If
    34.  
    35.         End If

    you should be able to draw maps.

    Single elements within the map can be set with:
    vb Code:
    1. MyMap(0, 0) = New MapElement(Color.Green)
    2.         MyMap(0, 1) = New MapElement(Color.Yellow)
    3.         MyMap(19, 9) = New MapElement(Color.Red)
    4.         MyMap(10, 7) = New MapElement(Color.Plum)

    and the entire map can be redrawn at any time with a call to Me.Refresh.

    NOTE: This is a bare bones type example and many of the things can be made differently and more efficiently. But it should be easy enough to fiddle a bit around with and later modify to your needs.

    Regards
    Tom
    In truth, a mature man who uses hair-oil, unless medicinally , that man has probably got a quoggy spot in him somewhere. As a general rule, he can't amount to much in his totality. (Melville: Moby Dick)

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Jan 2011
    Posts
    118

    Re: vb 2010 Mapeditor

    thanks, worked fine

  5. #5
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: vb 2010 Mapeditor

    I'd make the Map class a control (by inheriting Control, or Panel, or PictureBox, or...) and put the painting in the OnPaint method override. This way you can just drop the Map onto any form you want instead of having to repeat the drawing code in each form's Paint event...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width