TIP: Click on subject to list as thread! ANSI
echo: visual_basic
to: All
from: Murphy McCauley
date: 2004-07-04 06:27:00
subject: Re: Sprite graphics in VB

"Jure Sah"  wrote in message
news:gTzFc.6129$37.817322{at}news.siol.net...
> Hello,
>
> Umm I'm trying to create an application (actualy game) that displays
> pictures in a window.
>
> Now while there is no problem in displaying pictures with white
> backgrounds on a form with white background, displaying pictures with a
> white background ontop of another picture kind of overwrites the picture
> underneath with the white background.
>
> Is there any way to draw BMPs on a window with one of the colours set to
> transparent without well using the slow pixel by pixel
> program-controlled draw?
>
> If not, is there any OCX which will help me overcome that problem quickly?
>
> Thanks in advance.

Well, you could use DirectDraw.  If your needs aren't that heavy, though...
and you can assume Windows 98 and up (I think)... you can just use the
TransparentBlt() API function.

Here's some pertaining stuff out of my Imaging LibPack (available on my
site)...

Global Const MIN_LONG As Long = &H80000000

Declare Function TransparentBlt Lib "msimg32.dll" _
 (ByVal DestHDC As Long, ByVal DestX As Long, ByVal DestY As Long, ByVal
DestW As Long, ByVal DestH As Long, _
  ByVal SrcHDC As Long, ByVal SrcX As Long, ByVal SrcY As Long, ByVal SrcW
As Long, ByVal SrcH As Long, _
  ByVal TransparentColor As Long) As Long

Global Const MAGIC_PINK As Long = &HFF00FF

Function DrawSprite(ByVal DestHDC As Long, ByVal SourceHDC As Long, ByVal X
As Long, ByVal Y As Long, ByVal W As Long, ByVal H As Long, Optional ByVal
ClipWidth As Long, Optional ByVal ClipHeight As Long, Optional ByVal Wrap As
Boolean, Optional ByVal PinX As Long = MIN_LONG, Optional ByVal PinY As Long
= MIN_LONG)
    ' Draws a "sprite" from an HDC
    If PinX  MIN_LONG Then X = X - PinX Else X = X - (W \ 2)
    If PinY  MIN_LONG Then Y = Y - PinY Else Y = Y - (H \ 2)

    If ClipWidth And Wrap Then
        If X < 0 Then
            TransparentBltClipped DestHDC, X + ClipWidth, Y, W, H, SourceHDC
, 0, 0, MAGIC_PINK, ClipWidth, ClipHeight
        ElseIf X >= ClipWidth Then
            TransparentBltClipped DestHDC, X - ClipWidth, Y, W, H,
SourceHDC, 0, 0, MAGIC_PINK, ClipWidth, ClipHeight
        End If
    End If
    If ClipHeight And Wrap Then
        If Y < 0 Then
            TransparentBltClipped DestHDC, X, Y + ClipHeight, W, H,
SourceHDC, 0, 0, MAGIC_PINK, ClipWidth, ClipHeight
        ElseIf Y >= ClipHeight Then
            TransparentBltClipped DestHDC, X, Y - ClipHeight, W, H,
SourceHDC, 0, 0, MAGIC_PINK, ClipWidth, ClipHeight
        End If
    End If
    TransparentBltClipped DestHDC, X, Y, W, H, SourceHDC, 0, 0, MAGIC_PINK,
ClipWidth, ClipHeight
End Function

Function TransparentBltClipped _
 (ByVal DestHDC As Long, ByVal DestX As Long, ByVal DestY As Long, ByVal W
As Long, ByVal H As Long, _
  ByVal SrcHDC As Long, ByVal SrcX As Long, ByVal SrcY As Long, _
  ByVal TransparentColor As Long, Optional ByVal ClipWidth As Long, Optional
ByVal ClipHeight As Long) As Long
    If ClipWidth Then
        If DestX + W > ClipWidth Then
            W = ClipWidth - DestX
        ElseIf DestX < 0 Then
            SrcX = SrcX - DestX
            W = W + DestX
        End If
    End If
    If ClipHeight Then
        If DestY + H > ClipHeight Then
            H = ClipHeight - DestY
        ElseIf DestY < 0 Then
            SrcY = SrcY - DestY
            H = H + DestY
        End If
    End If

    TransparentBltClipped = TransparentBlt(DestHDC, DestX, DestY, W, H,
SrcHDC, SrcX, SrcY, W, H, TransparentColor)
End Function

Basically DrawSprite() is just a wrapper for TransparentBlt with the
following properties:
o The transparent color is assumed to be "magic pink".  This is fairly
common.  RGB 255, 0, 255.
o It has a "wrap" mode... if the sprite extends to the left/up of 0, it
draws that part on the right/bottom... if it extends to the right/below, it
draws it on the left/top.  You know what I mean.  In a lot of old games when
you go off the screen, you come out the other side.  This is for that.
o You can specify "pin" coordinates.  DrawSprite will center drawing on the
"pin" instead of on (0,0) like normal.  You can tell it the pin is (0,0) if
you want, or set some other arbitrary value.  The default is for the pin to
be in the middle of the sprite.

It could use a couple more little features... I'm not sure why I don't have
the transparent color as an optional default parameter, for instance.  And
it might as well be able to clip/wrap to an arbitrary position on the
left/top sides instead of being fixed at 0.  But whatever.  There it is if
you want it.


Murphy
www.ConstantThought.com
---
þ RIMEGate(tm)/RGXPost V1.14 at BBSWORLD * Info{at}bbsworld.com

---
 * RIMEGate(tm)V10.2áÿ* RelayNet(tm) NNTP Gateway * MoonDog BBS
 * RgateImp.MoonDog.BBS at 7/4/04 6:27:06 AM
* Origin: MoonDog BBS, Brooklyn,NY, 718 692-2498, 1:278/230 (1:278/230)
SEEN-BY: 633/267 270
@PATH: 278/230 10/345 106/1 2000 633/267

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.