|
-
Nov 28th, 2011, 02:21 PM
#1
Thread Starter
Member
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
-
Nov 30th, 2011, 05:59 PM
#2
Re: Flicker Issues
Try adding Me.Doublebuffer = True to the Form_Load statement. That should eliminate the flicker
-
Dec 2nd, 2011, 02:21 AM
#3
Thread Starter
Member
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.
-
Dec 2nd, 2011, 04:56 AM
#4
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.
-
Dec 5th, 2011, 10:35 AM
#5
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.
-
Dec 11th, 2011, 05:47 PM
#6
Thread Starter
Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|