dcsimg
Results 1 to 2 of 2

Thread: [VB 2010] Simple Terrain Generator.

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2014
    Posts
    1

    [VB 2010] Simple Terrain Generator.

    Just a basic terrain generator i've been adding to on my weekends off. uses a perlin noise maker and allows you to add color to it. I was hoping to add more than just a perlin but so far it is beyond my abilities - I got lucky to stumble upon the perlin class from someone who was nice enough to post it(credit givin in the code).

    I've found it to be a good base that I can then load into paintshop to better tweak it.

    I did have a problem of not being able to have the height and width being different values.

    It's still a work in progress.....


    perlin noise mapping.zip - Moderator Note: This ZIP includes binaries(exe).
    Last edited by dday9; Jul 15th, 2014 at 09:10 AM.

  2. #2
    Sinecure devotee
    Join Date
    Aug 2013
    Location
    Southern Tier NY
    Posts
    5,338

    Re: [VB 2010] Simple Terrain Generator.

    Quote Originally Posted by seraniz View Post
    ...
    I did have a problem of not being able to have the height and width being different values.
    ...
    p.s. I see you mis-nested your Quotes, so the above quote wasn't yours originally, but was deuce1's. Oh well. I think he's long gone so won't benefit from these changes.
    I didn't have a problem with different height and width, but then again, I did fix an obvious error. x is associated with width, and y with height throughout the code, but in the RedrawStaticMap Sub, the values were reversed so you would get a array index out of bounds error if height and width were not equal.
    Code:
        Public Sub RedrawStaticMap()
            Dim rndNum As Random = New Random
            Dim c As Integer
    
            For y As Integer = 0 To pvtHeight - 1      'original code had pvtWidth here
                For x As Integer = 0 To pvtWidth - 1     'original code had pvtHeight here
                    c = rndNum.Next(255)
                    stat(x, y) = c
                Next
            Next
            Recalc()
        End Sub
    I don't know if you're using the code "as is", or pulling parts that you want from it, but code assigning the colors to the grayscale is pretty slow.
    There are better, but more complicated ways to do the update, but a couple of simple changes will speed up the existing code with little modification.

    The first change is the code is updating the form caption as each pixel is processed, to show progress. But updating the caption for every pixel update is totally unnecessary and a major slowdown.
    If you just move the update down so it is out of the inner loop, but still in the outer loop, it will update once per line of pixels, saving you over a hundred thousand updates, which makes the code much snappier.

    The second change is where he's using the grayscale value, 0 to 255, to update a string and search the panel for the control that has that string for a name and then use that controls backcolor to set the pixel color. Again, you have hundreds of thousand string modifications and control searches by name being done.
    It would be more appropriate to have a simple array lookup, indexed 0 to 255, to get the mapped color. But since his code is tied to using the labels and modifying their background color to define the map lookup, it would take a bit of update to keep the array synchronized to the label backcolors as they are modified.

    A simple alternative, is just to create an array or list of labels, and as he creates the labels, add them to the array (or list). Then in the loop, instead of creating the control name from the 0 to 255, and doing a Find, just use the 0 to 255 as an index into the array (or list) to get the backcolor of the label directly. This will speed things quite a bit more, so that rather than having to wait for quite a few seconds for the color version of the map to be created, it will be created in less than a second, most likely, for the default size of image being used.
    Below, I cut out some code, but left enough code in to give context to where the array was added, and populated. I did the list first, and it worked fine, and then I used an array just to test that it also worked fine. The array might technically be a little faster, and use a little less memory than a list. A list is good for flexibility, but in this case, since the number of labels is a known size, and won't change, you don't need flexibility so an array will work well.
    Code:
    Module Resources
    
        Dim cDialog As New ColorDialog()
        ' Public lblList As New List(Of Label)
        Public lblList(255) As Label
    
        Sub colorlabels256()
          '...   
            For z = 0 To 255
                collbl = New Label
                If z < 125 Then collbl.ForeColor = Color.White
                With collbl
                    .Parent = Form1.Panel5
                    .Name = "col" & lnum.ToString
                    .BackColor = Color.FromArgb(z, z, z)
    '...
                End With
                ' lblList.Add(collbl)
                lblList(z) = collbl
    '...
    The first speed up (moving the status update), and using the label array (array or list has the same syntax).
    Code:
        Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
            ' Dim colorlabel As Label
            Dim curcol As Integer
            Dim newcol As Color
    '...
            'loops through colormap, pixel by pixel, to get its color and finds its corresponding label, then changes the pixel color to the labels background color.
            For x = 0 To wx - 1
                For y = 0 To vy - 1
                    curcol = colormap.GetPixel(x, y).R
                    ' colorlabel = Panel5.Controls.Find("col" & curcol.ToString, True)(0)
                    ' newcol = colorlabel.BackColor
                    newcol = lblList(curcol).BackColor  'replace the two lines above with this one
                    colormap.SetPixel(x, y, newcol)
                    sizecount += 1
                Next
               'move status update out of the inner loop to greatly increase speed
                nform.Text = "Color Map " & cmapcount.ToString & "   |--->   " & sizecount.ToString & "   of   " & maxsize.ToString & "  Pixels"
            Next
    '...
    Last edited by passel; Jan 22nd, 2019 at 02:22 AM.

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width