Results 1 to 7 of 7

Thread: GDI+ flashing

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    GDI+ flashing

    Hello,

    I have a problem with GDI+. I have recently made a 2005 version of a snake game using GDI+. Every thing is working fine but unfortunately when playing the game i get a lot of flashing of the snake and the food it eats. I have tried double buffering and set style but neither one of these have worked. So i am looking for a way to stop this flashing. Is there anyway to stop it?

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

    Re: GDI+ flashing

    Are you refreshing the entire form when you make changes? If so then you should try only invalidating the areas that have changed. The smaller the area you repaint the faster it happens and the less likely the UI will flash as a result. Follow the Simple Drawing link in my signature to see how and why. Pay particular attention to post #17, which the first post directs you to.
    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

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: GDI+ flashing

    ok i tried playing around with the picturebox1.invalidate but i cant seem to get it to work at all. i don't know if it is cause i am not invalidating at the right time but i will post my snakemove code. could you tell me where would the best place to invalidate be or how to do it. i understand how invalidating works and how to invalidate but i don't know how to implement it with what i have now.

    Code:
        Private Sub movesnake()
            Dim g As Graphics
            g = PictureBox1.CreateGraphics
            For i = (length - 1) To 1 Step (-1)
                snake(i).x = snake(i - 1).x 'sets the snake.x to the snake.x in front of it
                snake(i).y = snake(i - 1).y 'sets the snake.y  to the snake.y in front of it
                snake(i).facing = snake(i - 1).facing 'sets the snake.facing to the snake.facing in front of it
                g.FillRectangle(Brushes.Red, snake(i).x, snake(i).y, 10, 10) ' paints the snakepart
                'Dim area As New Rectangle(snake(i).x, snake(i).y, 10, 10)
                'area.Inflate(1, 1)
                'PictureBox1.Invalidate(area)
                'PictureBox1.Update()
            Next i
    
    
    
            If snake(0).facing = 1 Then         'Moves left
                snake(0).x = snake(0).x - 10
                'Dim area As New Rectangle(snake(0).x, snake(0).y, 10, 10)
                'area.Inflate(1, 1)
                'PictureBox1.Invalidate(area)
                'PictureBox1.Update()
            ElseIf snake(0).facing = 2 Then     'Moves up
                snake(0).y = snake(0).y - 10
                'Dim area As New Rectangle(snake(0).x, snake(0).y, 10, 10)
                'area.Inflate(1, 1)
                'PictureBox1.Invalidate(area)
                'PictureBox1.Update()
            ElseIf snake(0).facing = 3 Then     'Moves right
                snake(0).x = snake(0).x + 10
                'Dim area As New Rectangle(snake(0).x, snake(0).y, 10, 10)
                'area.Inflate(1, 1)
                'PictureBox1.Invalidate(area)
                'PictureBox1.Update()
            ElseIf snake(0).facing = 4 Then     'Moves down
                snake(0).y = snake(0).y + 10
                'Dim area As New Rectangle(snake(0).x, snake(0).y, 10, 10)
                'area.Inflate(1, 1)
                'PictureBox1.Invalidate(area)
                'PictureBox1.Update()
            End If
            'Draw the head and redraw the food because i refreshed the picturebox1
            g.FillRectangle(Brushes.Red, snake(0).x, snake(0).y, 10, 10)
            g.FillRectangle(Brushes.Green, food.x, food.y, 10, 10)
        End Sub

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

    Re: GDI+ flashing

    You should absolutely NOT be calling CreateGraphics. Do ALL your drawing in the Paint event handler of the control being drawn on, where a Graphics object is provided for you. Follow the Simple Drawing link in my signature for a demonstration of how and an explanation of why.
    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

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: GDI+ flashing

    Ok I have got it to work but i need to figure out where the snake part was previously so i can invalidate that area to because when i only invalidate the new area that was drawn i leave a line of red boxes behind. this is the code for my move snake body sub i need to determine where the snake part was and were it is now and then i need to invalidate that and then update. how would i do that?

    Code:
    For i = (length - 1) To 1 Step (-1)
                'Dim areabodypre As New Rectangle(snake(i).x, snake(i).y, 10, 10)
    
                'PictureBox1.Invalidate(areabodypre)
    
    
                snake(i).x = snake(i - 1).x 'sets the snake.x to the snake.x in front of it
                snake(i).y = snake(i - 1).y 'sets the snake.y  to the snake.y in front of it
                snake(i).facing = snake(i - 1).facing 'sets the snake.facing to the snake.facing in front of it
                'g.FillRectangle(Brushes.Red, snake(i).x, snake(i).y, 10, 10) ' paints the snakepart
    
                Dim areabody As New Rectangle(snake(i).x, snake(i).y, 10, 10)
                'areabody.Inflate(10, 10)
                paintbody = True
                PictureBox1.Invalidate(areabody)
                PictureBox1.Update()
                paintbody = False
            Next i

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

    Re: GDI+ flashing

    If you can calculate where it is now then you can calculate where it was, because the calculation will be exactly the same as the last time you calculated where it is. Basically, you do NOT want to move the object and then do the two calculations to find out where it was and where it is. What you should be doing is working out where it is now, invalidating that area, moving it, working out where it is now, invalidating that area, forcing the control to repaint. That way you never have to actually know where the object was, just where it is now.
    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

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: GDI+ flashing

    i will play around with it a little but so far i haven't had any luck.

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