Solved

2 and a half D and Graphics Layers

Posted on 2002-06-18
7
219 Views
Last Modified: 2013-12-26
Hi,
   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
Jacky
0
Comment
Question by:luckie
  • 4
  • 3
7 Comments
 
LVL 18

Expert Comment

by:mdougan
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:

http://www.mvps.org/vb/

Download the CustomBlt.zip sample
0
 

Author Comment

by:luckie
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
Thanks
Jack
0
 
LVL 18

Expert Comment

by:mdougan
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.
0
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

Author Comment

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

Expert Comment

by:mdougan
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.
0
 
LVL 18

Accepted Solution

by:
mdougan earned 50 total points
ID: 7099519
luckie,

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

0
 

Author Comment

by:luckie
ID: 7274060
Great.Thanks
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

706 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now