Image Loading Performance Issue

Parsnips

New member
Joined
Jul 5, 2012
Messages
4
Programming Experience
1-3
Hi there!

I am toying around with game programming in VB.net, and was having performance issues with a sort of map generator I created. The procedure reads lines from map.txt, and depending on the number it reads, draws different images on the screen from tiles from a picturebox. When I run it, however, I get only about 6-7 FPS. Is there something wrong with my code? Should I be using a different method to achieve what I want? Thanks in advance!

Private Sub DrawMap()

objSR = New StreamReader("C:\map.txt")


For x = 0 To 15
For y = 0 To 15


source = New Rectangle(0, 0, 32, 32)
destination = New Rectangle(x * 32, y * 32, 32, 32)
G.DrawImage(images, destination, source, GraphicsUnit.Pixel)


read = objSR.ReadLine


If read = "1" Then


source = New Rectangle(96, 128, 32, 32)
destination = New Rectangle(x * 32, y * 32, 32, 32)
G.DrawImage(images, destination, source, GraphicsUnit.Pixel)


ElseIf read = "2" Then


source = New Rectangle(96, 96, 32, 32)
destination = New Rectangle(x * 32, y * 32, 32, 32)
G.DrawImage(images, destination, source, GraphicsUnit.Pixel)


End If

If grid = True Then G.DrawRectangle(Pens.Black, x * 32, y * 32, 32, 32)

Next

Next

objSR.Close()

End Sub
 
Well the most obvious problem is that you're combining multiple file readings with multiple drawings. Read the whole file into a suitable array or list before you start the graphics processing.
 
Yup. I clear the graphics every frame so that something moving along the screen doesn't leave "footprints". And then it's redrawn. How can I get around this?
 
Draw the map to a bitmap in memory - you only need to do this once. Then you can load the complete map at will in a single operation. This is a very simple (and old!) example but it will get you started ..

VB.NET:
Dim b As New Bitmap(100, 100)
        Dim g As Graphics = Graphics.FromImage(b)


        g.DrawEllipse(Pens.Red, New RectangleF(0, 0, b.Width, b.Height))
        g.Dispose()

        PictureBox1.Image = b  'now you can deploy the complete image using a single variable and without redrawing
 
That helped a lot! Thanks!

The only issue now is that if I paint the screen with the map image to clear the screen of the "footprints" then I get a flickering. I was hoping I would be able to load the map only once and then clear all of the previous moving images every frame instead. Using .Clear() is no good though because it covers the map. Is there a way to clear it away without painting the screen a whole colour?

Thanks again for your continuing help!
 
Turn OFF double buffering on your form. The flicker comes from the form redrawing entirely every time you make a change to any of the object. Normally double buffering is good because if prevents flickering from many controls redrawing, but in this case you do not want to redraw the whole form.

Also what you can do is draw your transparent sprite (via PNG or GIF format, or a transparent color) on top of your map bitmap, which resolves the map clearing issue.

    ' Get a Graphics object from the map's bitmap
    Dim gMap As Graphics = Graphics.FromImage(bmpMap)
    
    ' Make the color White transparent in the sprite bitmap
    bmpSprite.MakeTransparent(Color.White)
    
    ' Draw the sprite bitmap onto the map.
    gMap.DrawImage(bmpSprite, New Point(X, Y))
 
Not to resurrect an old thread but another thing to look at is the SuspendLayout() and ResumeLayout() methods. You can suspend the form's redrawing, make all your changes (load the next frame), then resume for a single redraw.
 

Latest posts

Back
Top