Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VB6 Double Buffering Images and shapes 1

Status
Not open for further replies.

Tag666

Programmer
Apr 11, 2012
2
hi, I'm a probably classed as a vb newbie. All my games have lots and lots of flickering due to shapes on other shapes on images refreshing at 10ms. i read double buffering is the answer, but i don't know where to start.

Basic concept:
i have a galaxy (frame) filled with a repeating star background (images), random planets (shapes), on top of this i have a spaceship (shapes), you control the space ship with the arrow keys, but the spaceship stays center and the whole galaxy (frame) and everything in it moves around the spaceship.
The galaxy generator has multiple options for size, amount of planets and so on.

Problem:
the more detail i add, the more flickering occurs.
whats the simplest way of creating a double buffer for a huge galaxy eg(16000x11250) within a frame
keeping a high FPS (timers=10ms)?????

I've probably gone about this idea the wrong way,
but i think its best to address my flickering problem before it gets any more complex.

PS: i also hear that strongm is the man


thanks
 
Are you just moving an image or picture box around ?
If so, look for a tutorial on BITBLT - bit mapped block transfering - that will make a large improvement - the double buffering will be an improvemnt, but requires bitblt-ing as a base.
 
I have found that moving shapes and images around directly on a form can create a momemtary loss of picture at the point of movement. This appears as a flicker. This has nothing to do with how many frames per second you use.

However if you place objects in a borderless frame or form and only move the frame or form then it does not flicker or jitter when scrolled smoothly.

Only problem then is the outline of the frame will obscure any objects underneath.

To get around this you can place each object or image in a small new borderless FORM and apply an API routine to make the form the exact shape of the object. Make the form itself a color that is not included in your object and use this color as the mask color so the corners of the form become transparent.
Code:
'This code is needed in every form that you want to be borderless.
'make the backcolor of the form where you want it to be invisible= &h010101 (very slightly grey)

Option Explicit

Private Declare Function GetWindowLong Lib "User32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
Private Declare Function SetWindowLong Lib "User32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function SetLayeredWindowAttributes Lib "user32.dll" (ByVal hWnd As Long, ByVal crKey As Long, ByVal bAlpha As Byte, ByVal dwFlags As Long) As Long
Const LWA_COLORKEY = &H1
Const LWA_ALPHA = &H2&
Const GWL_EXSTYLE = (-20)
Const WS_EX_LAYERED = &H80000

Private Sub Form_Load()
Dim dwExStyle As Long
' Set up a transparent window
dwExStyle = GetWindowLong(hWnd, GWL_EXSTYLE)
dwExStyle = dwExStyle Or WS_EX_LAYERED
SetWindowLong hWnd, GWL_EXSTYLE, dwExStyle
BackColor = RGB(1, 1, 1)   ' this is what will be transparant
SetLayeredWindowAttributes hWnd, RGB(1, 1, 1), 0, LWA_COLORKEY
End Sub

If you have a lot of little forms you can put the declares once in a module and make them public

When repeatedly writing to a screen with a complex changing arrangement of objects, if you make the frame invisible, then write the new layout to the frame then make the frame visible, you get an instantaneous apparent simultaneous change in the position of the objects without any gap of flicker (as observed on any reaonably good video card)
You would think you would see a black gap but you don't because the outgoing picture seems to hang on in the videocard for some reason.
 
@jzr i looked into BITBLT after your suggestion, It seems quite complex, But when i got a simple BITBLT test i put together to work, it only slightly decreased the flickering. But I'm sure BITBLT will come in useful one day. thanks

@tedsmith Perfect! i made a test with lots of layers and shapes, it was very simple and with almost no flickering. Also this looks like it will be very easy to implement into my existing game. thanks

I think my problem is resolved now, but i will post here again probably next week when i have time to full implement tedsmiths's idea/solution.

Thanks again :)
 
>if you make the frame invisible, then write the new layout to the frame then make the frame visible

And thus doing a very, very primitive sort of double-buffering

>you don't because the outgoing picture seems to hang on in the videocard for some reason

It's to do with the way GDI optimises screen writes

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top