Results 1 to 6 of 6

Thread: Flicker Issues

  1. #1

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    48

    Flicker Issues

    I am experimenting with making a game focused on crafting. At the moment, i have a 100x100 tileset with four sprites. Background, character, tree, tree stump.

    It creates 8 trees and places them randomly on the form, move the character over the tree and press space to chop it down (change it to the stump image)

    But every time you move the whole screen flickers. It MIGHT be just my computer, but I have a feeling it's the redraw. Not sure how to lower flickering.

    Code:
    Imports System.Drawing
    
    Public Class Form1
    
        Dim G As Graphics
        Dim BBG As Graphics
        Dim sRect As Rectangle
        Dim dRect As Rectangle
        Dim bmap As Bitmap
        Dim BB As Bitmap
        Dim NumberOfTrees As Integer = 8
        Dim GuyY As Integer = 0
        Dim GuyX As Integer = 0
        Dim Trees As Integer
        Dim X2 As Integer = Nothing
        Dim Y2 As Integer = Nothing
        Dim XArray(NumberOfTrees - 1) As Integer
        Dim YArray(NumberOfTrees - 1) As Integer
        Dim Stumps(NumberOfTrees - 1) As Integer
    
        Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
    
            Select Case e.KeyCode
                Case Keys.W
                    GuyY = GuyY - 50
                Case Keys.A
                    GuyX = GuyX - 50
                Case Keys.S
                    GuyY = GuyY + 50
                Case Keys.D
                    GuyX = GuyX + 50
                Case Keys.Space
                    ChopTree()
            End Select
    
            DrawAll()
            DrawTrees()
    
        End Sub
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            bmap = New Bitmap(PictureBox1.Image)
            G = Me.CreateGraphics
            BB = New Bitmap(Me.Width, Me.Height)
    
            For Me.Trees = 0 To NumberOfTrees - 1
                Me.XArray(Me.Trees) = Int(Rnd() * 9) * 50
                Me.YArray(Me.Trees) = Int(Rnd() * 9) * 50
            Next
    
            DrawAll()
            DrawTrees()
    
        End Sub
    
        Private Sub ChopTree()
    
            For Me.Trees = 0 To NumberOfTrees - 1
                If GuyX = Me.XArray(Me.Trees) Then
                    If GuyY = Me.YArray(Me.Trees) Then
                        Me.Stumps(Me.Trees) = 1
                    End If
                End If
            Next
    
        End Sub
    
        Private Sub DrawAll()
    
            Dim X, Y As Integer
    
            For X = 0 To 9
                For Y = 0 To 9
                    sRect = New Rectangle(0, 0, 50, 50)
                    dRect = New Rectangle(X * 50, Y * 50, 50, 50)
                    G.DrawImage(bmap, dRect, sRect, GraphicsUnit.Pixel)
                Next
            Next
    
            For Me.Trees = 0 To NumberOfTrees - 1
    
                If Me.Stumps(Me.Trees) = 0 Then
                    sRect = New Rectangle(50, 0, 50, 50)
                    G.DrawImage(bmap, Me.XArray(Me.Trees), Me.YArray(Me.Trees), sRect, GraphicsUnit.Pixel)
                    G = (Graphics.FromImage(BB))
                    BBG = Me.CreateGraphics
                    BBG.DrawImage(BB, 0, 0, Me.Width, Me.Height)
                End If
    
                If Me.Stumps(Me.Trees) = 1 Then
                    sRect = New Rectangle(0, 50, 50, 50)
                    G.DrawImage(bmap, Me.XArray(Me.Trees), Me.YArray(Me.Trees), sRect, GraphicsUnit.Pixel)
                    G = (Graphics.FromImage(BB))
                    BBG = Me.CreateGraphics
                    BBG.DrawImage(BB, 0, 0, Me.Width, Me.Height)
                End If
            Next
    
            bmap.MakeTransparent(Color.Fuchsia)
            sRect = New Rectangle(50, 50, 50, 50)
            G.DrawImage(bmap, GuyX, GuyY, sRect, GraphicsUnit.Pixel)
    
            G = (Graphics.FromImage(BB))
            BBG = Me.CreateGraphics
            BBG.DrawImage(BB, 0, 0, Me.Width, Me.Height)
    
        End Sub
    
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    
            DrawAll()
            DrawTrees()
    
        End Sub
    
        Private Sub DrawTrees()
    
    
        End Sub
    
    End Class

  2. #2
    Elite Hacker Jacob Roman's Avatar
    Join Date
    Aug 2004
    Location
    Miami Beach, FL
    Posts
    5,349

    Re: Flicker Issues

    Try adding Me.Doublebuffer = True to the Form_Load statement. That should eliminate the flicker

  3. #3

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    48

    Re: Flicker Issues

    When I turn on double buffering, rather it be in the load, the drawall(), or anywhere, it makes the screen blank. It shows the graphics at first when the program loads, but then quickly disappears.

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Flicker Issues

    Dont call Me.CreateGraphics, do all drawing from your paint eventhandler and use the Graphics instance supplied to you by the e.Graphics property.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Flicker Issues

    Yea, do something like this, first, delete "Dim G as Graphic", then change your "DrawAll" and "DrawTrees" to take the following:

    Code:
    Private Sub DrawAll(ByRef G As Graphics)
    .....
    
    Private Sub DrawTrees(ByRef G As Graphics)
    .....
    Finally, change the Draw override to:
    Code:
        Private Sub Form1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Me.Paint
    
            DrawAll(e.Graphics)
            DrawTrees(e.Graphics)
    
        End Sub
    I'll warn you, it'll probably still be flickery if you get too fancy or too fast with it. GDI+ can only draw so fast. DoubleBuffer buys you some elbowroom in the mean time.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  6. #6

    Thread Starter
    Member
    Join Date
    May 2011
    Posts
    48

    Re: Flicker Issues

    I have started a new project to rewrite the code, leaving the original in tact to remind myself of how I did some things. I'm now drawing to a bitmap and setting a picture box's image to the bitmap. This has gotten rid of seeing the form draw each square, but it's not displaying the top leftmost "square"...

    Code:
    Imports System.Drawing
    
    Public Class Form1
    
        Dim BackBuffer As Bitmap
        Dim G As Graphics
        Dim SRect As Rectangle
        Dim DRect As Rectangle
        Dim SImage As Bitmap
    
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
            SImage = New Bitmap(picTemplate.Image)
            BackBuffer = New Bitmap(499, 499)
            G = picCanvas.CreateGraphics
    
        End Sub
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            For X = 0 To 9
                For Y = 0 To 9
                    SRect = New Rectangle(0, 0, 50, 50)
                    DRect = New Rectangle(X * 50, Y * 50, 50, 50)
                    G.DrawImage(SImage, DRect, SRect, GraphicsUnit.Pixel)
                    G = Graphics.FromImage(BackBuffer)
                Next
            Next
    
            picCanvas.Image = BackBuffer
    
        End Sub
    End Class
    Umm, any idea's on why?

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