Go Premium for a chance to win a PS4. Enter to Win


2 and a half D and Graphics Layers

Posted on 2002-06-18
Medium Priority
Last Modified: 2013-12-26
   first of all, let me introduce 2 and a half D, it is a technique that
draws 3D graphics on 2D spaces. I'm using VB and ActiveX controls as
sprites. However,
on the form bitmap, there are a lot of components fixed in in the bitmap,
such as the megalift (do not take it seriously if you don't know what that
is) , rackings and beams (in a warehouse literally). How do I make those
objects go in front or behind of the sprites? I know that involves graphics
layers. How do i manipulate layers in VB in order to achieve this effect? (Z-order won't work because you can't bring the 'megalift' to front without bringing the whole bitmap so)Thanks
Question by:luckie
  • 4
  • 3
LVL 18

Expert Comment

ID: 7092751
If you want to work with graphical elements in different controls, then probably your only choice would be to use Image controls, and have transparent gifs for your sprites and/or elements in the foreground that you may want sprites to move behind.  Then, I think, you can layer the image controls with zorder to get the effects you want.

Other than that, the approach that is most often used is custom "bitblt"ing - which is to say, copying selected portions of different pictures into the display picture.  The bitblt Windows API can allow you to create a transparency mask so that you would bitblt your sprite on top of a background picture, and then you could also bitblt some foreground element on top of the sprite.

You can get a pretty cool example of the transparent bitblt code from Karl Peterson's website:


Download the CustomBlt.zip sample

Author Comment

ID: 7094428
Not that kind... I mean there are some components embedded in the background images, how do I make sprites go behind those components, such as the beam (it's like Z-ordering) or something like that
LVL 18

Expert Comment

ID: 7094842
That's what I meant about some "foreground" elements.  Anything that the sprites are going to go behind has to be in a separate layer from the "background".

How you do the layering is up to you.  You can either do it using images with transparent backgrounds (though something tells me I've tried this and it doesn't really work), or you bitblt the different layers to a picture control such that you bitblt first the background, then your sprite, then your foreground element.

This sort of animation is classical Disney style.
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 7094884
I understand now, but the 'foreground' elements are actually part of the background bitmap? How do I resolve that?
LVL 18

Expert Comment

ID: 7095503
With a drawing package like Paintbrush or Photoshop I'm afraid.  What you will want to do is copy the background picture, color over everything in the picture that is not part of the foreground element in one specific color (the specific color doesn't matter as long as that color isn't found in the foreground element.  I usually pick something obvious like bright pink.  Then, in the transparent bitblt routine. You pick the color to make transparent, and that will be your pink color.  When the image gets drawn on the display bitmap, only the non-transparent color bits will get drawn.
LVL 18

Accepted Solution

mdougan earned 200 total points
ID: 7099519

If you download the Customblt.zip from the Karl Peterson site that I listed, then edit the code under the form1 in the sample project and replace it with this code you can test what I'm talking about.  You will need to place two new pictureboxes on the form, Picture3 and Picture4 (you might want to set their visible properties to false).  

Don't worry about animation for now, but put a static picture of one of your sprites into the Picture property of Picture3 making sure to color the background to black (anything that is colored black will become transparent).  Make picture4 to be the same size as picture3 but don't put any picture into it.  Then, you can paste this code over the code that is already in the form and try it out:

Private Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)

Option Explicit

Private Sub Form_Load()
' all pictureboxes were loaded at design time.
' Picture 1 has a foreground element with a black background where there should be transparency
' Picture 2 holds the complex background wallpaper (tileable in this case)
' Picture 3 holds the sprite with a black background where there should be transparency
' Picture 4 has no picture, but it's the same size as Picture 3

   With Me
      .ScaleMode = vbPixels
      '.AutoRedraw = True
   End With
   ' Holds Foreground Image
   With Picture1
      .ScaleMode = vbPixels
      .AutoRedraw = True
   End With
   ' Holds Sprite Image
   With Picture3
      .ScaleMode = vbPixels
      .AutoRedraw = True
   End With
   ' Is used to restore the background after sprite moves.  It's the same exact size as Picture3
   With Picture4
      .ScaleMode = vbPixels
      .AutoRedraw = True
   End With
End Sub

Private Sub Form_Paint()
   Dim dX&
   Dim w&, h&
   ' Get dimensions of the foreground image
   w = Picture1.ScaleWidth
   h = Picture1.ScaleHeight
   ' Tiles a complex background image on the form
   Call TileBlt(Me.hWnd, Picture2.Picture.Handle)
    ' Preload the background restoring picturebox with the background behind the first sprite location
    Call TransBlt(Picture4.hdc, 0, 0, Picture3.ScaleWidth, Picture3.ScaleHeight, Me.hdc, 10, 10, vbBlack)
   ' Now progressively blit the sprite down from the upper left corner to the lower right corner
    For dX = 10 To 400 Step 10
            ' first restore the background at the sprites last position
            Call TransBlt(Me.hdc, dX - 10, dX - 10, Picture3.ScaleWidth, Picture3.ScaleHeight, Picture4.hdc, 0, 0, vbBlack)
            ' now, copy the background from the sprites new position to picture4 to save it for next restore
            Call TransBlt(Picture4.hdc, 0, 0, Picture3.ScaleWidth, Picture3.ScaleHeight, Me.hdc, dX, dX, vbBlack)
            ' now copy the sprite to it's new location
            Call TransBlt(Me.hdc, dX, dX, Picture3.ScaleWidth, Picture3.ScaleHeight, Picture3.hdc, 0, 0, vbBlack)
            ' now paint the foreground over the sprite
            Call TransBlt(Me.hdc, 200, 200, w, h, Picture1.hdc, 0, 0, vbBlack)
            ' now pause
            Sleep 80
    Next dX

End Sub


Author Comment

ID: 7274060

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

885 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question