BitBlt Masking

Windows specific questions.
Post Reply
myndgrrrrrl
Posts: 11
Joined: Feb 09, 2022 0:03

BitBlt Masking

Post by myndgrrrrrl »

Well, some time ago I had asked about how to make a mask with the windows api. I managed to make a sprite class, but it just leaves the sprites with a black background. Any idea what could have happened?

Code: Select all

#include "windows.bi"
#include "globals.bas"

Function CreateBitmapMask(hbmColour As HBITMAP, crTransparent As COLORREF) As HBITMAP
	Dim As HDC hdcMem, hdcMem2
	Dim As HBITMAP hbmMask
	Dim As BITMAP bm

	GetObject(hbmColour, SizeOf(BITMAP), @bm)
	hbmMask = CreateBitmap(bm.bmWidth, bm.bmHeight, 1, 1, NULL)

	hdcMem = CreateCompatibleDC(0)
	hdcMem2 = CreateCompatibleDC(0)

	SelectObject(hdcMem, hbmColour)
	SelectObject(hdcMem2, hbmMask)

	SetBkColor(hdcMem, crTransparent)

	BitBlt(hdcMem2, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem, 0, 0, SRCCOPY)

	BitBlt(hdcMem, 0, 0, bm.bmWidth, bm.bmHeight, hdcMem2, 0, 0, SRCINVERT)

	DeleteDC(hdcMem)
	DeleteDC(hdcMem2)

	Return hbmMask
End Function

Type Sprite

    Dim As HDC         hdcMem
    Dim As HBITMAP     hbmSPR
    Dim As HBITMAP     hbmMask
    Dim As HBITMAP     hbmOld

	Dim As BITMAP bm
			
    Declare Sub loadImage(ByVal pathname As String)
    Declare Sub cleanUpImage()
    Declare Sub drawImage(ByVal x As Integer, ByVal y As Integer, _
                            ByVal w As Integer, ByVal h As Integer)
End Type

Sub Sprite.loadImage(ByVal pathname As String)
    hdcMem = CreateCompatibleDC(Memhdc)

    hbmSPR = Cast(HBITMAP, LoadImageA(NULL, pathname,IMAGE_BITMAP,0,0,LR_DEFAULTSIZE Or LR_LOADFROMFILE))
	hbmMask = CreateBitmapMask(hbmSPR, RGBA(255,0,255,0))

    
End Sub

Sub Sprite.cleanUpImage()
    DeleteObject(hbmSPR)
    DeleteObject(hbmMask)
    DeleteDC(hdcMem)
End Sub

Sub Sprite.drawImage(ByVal x As Integer, ByVal y As Integer, _
                    ByVal w As Integer, ByVal h As Integer)
	hbmOld = Cast(HBITMAP, SelectObject(hdcMem,hbmSPR))
	BitBlt(Memhdc, x, y, w, h, hdcMem, 0, 0, SRCAND)
	SelectObject(hdcMem, hbmSPR)
	BitBlt(Memhdc, x, y, w, h, hdcMem, 0, 0, SRCPAINT)
    SelectObject(hdcMem,hbmOld)
End Sub
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: BitBlt Masking

Post by BasicCoder2 »

@myndgrrrrrl

Strange you haven't had a reply.

Maybe the number of active members is so low that there isn't anyone left that knows much about the Windows API.

viewtopic.php?t=13451

https://documentation.help/FreeBASIC/ExtLibwindows.html

A search of the forum only found this.
viewtopic.php?p=289697&hilit=bitBlt+masking#p289697

I tried to learn to use the windows API using c++ but it was so bloated and long winded I gave up. That is why I chose FreeBASIC as it came with its own image commands.
UEZ
Posts: 972
Joined: May 05, 2017 19:59
Location: Germany

Re: BitBlt Masking

Post by UEZ »

BasicCoder2 wrote: Apr 08, 2022 22:44 Strange you haven't had a reply.
Because the code is not runable. Where can I find "globals.bas"?
BasicCoder2
Posts: 3906
Joined: Jan 01, 2009 7:03
Location: Australia

Re: BitBlt Masking

Post by BasicCoder2 »

UEZ wrote: Apr 09, 2022 10:58
BasicCoder2 wrote: Apr 08, 2022 22:44 Strange you haven't had a reply.
Because the code is not runable. Where can I find "globals.bas"?
Very strange the source of globals.bas wasn't given, nor any demo code using the functions.

I wonder where myndgrrrrrl is getting his information from in the first place. With c++ I had a proper set of tutorials. Although they showed how to do various things they never gave an explanation as to where the writers of the tutorials got their information. So if something wasn't covered then I had no way of working it out myself. With the old machines you just wrote your own sprite masking code, no tutorial required, just knowledge of the hardware, algorithm and knowing how to program. Fortunately I can now leave the api to the experts who have wrapped it all up in simple graphic libraries or the language's command set.

The old Amiga computer had a multitasking system but I couldn't understand the API manual that came with it. No doubt meant for those with a degree in computer science. Instead I turned off the OS and used the hardware manual to program it using Assembler. So much faster! Not possible on the new machines.
adeyblue
Posts: 299
Joined: Nov 07, 2019 20:08

Re: BitBlt Masking

Post by adeyblue »

The easiest way to not draw a colour is TransparentBlt, just tell it what colour to not draw. Might need to add
-l msimg32
to the compile command.
If you do that into another bitmap when you load it, then you should be able to BitBlt SRCCOPY the tranparencied bitmap when you need it. I think it works like that.
myndgrrrrrl
Posts: 11
Joined: Feb 09, 2022 0:03

Re: BitBlt Masking

Post by myndgrrrrrl »

UEZ wrote: Apr 09, 2022 10:58
BasicCoder2 wrote: Apr 08, 2022 22:44 Strange you haven't had a reply.
Because the code is not runable. Where can I find "globals.bas"?
Sorry for that, here i the globals.bas

Code: Select all

 
Dim Shared DT As Integer = 60
Dim shared as Integer xres=800
Dim shared as Integer yres=600
Dim shared as Integer backgroundColour=bgr(0,0,0)
Dim shared As hwnd p
Dim shared As HBITMAP hbmOld 
Dim shared As hdc Memhdc,WorkingScreen,hdc
Dim shared As HBITMAP Membitmap
Dim shared As msg emsg
Dim shared As Long fps

myndgrrrrrl
Posts: 11
Joined: Feb 09, 2022 0:03

Re: BitBlt Masking

Post by myndgrrrrrl »

adeyblue wrote: Apr 09, 2022 21:23 The easiest way to not draw a colour is TransparentBlt, just tell it what colour to not draw. Might need to add
-l msimg32
to the compile command.
If you do that into another bitmap when you load it, then you should be able to BitBlt SRCCOPY the tranparencied bitmap when you need it. I think it works like that.
Can you give any kind of example for this?
UEZ
Posts: 972
Joined: May 05, 2017 19:59
Location: Germany

Re: BitBlt Masking

Post by UEZ »

Do you have a running example how it looks currently?
Post Reply