Masking in BitBlt

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

Masking in BitBlt

Post by myndgrrrrrl »

Hello again, can someone explain to me how I can make a mask by a color (red, in this case) in the blt bit? I tried in many ways and I couldn't
dodicat
Posts: 7967
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Masking in BitBlt

Post by dodicat »

What kind of mask?

Code: Select all

#include "windows.bi"

Const xres=800
Const yres=600
Const backgroundColour=bgr(255,100,0)

Sub setfontsize(h As hdc,size As Long,style As zstring Ptr)
    SelectObject(h,CreateFont(size,0,0,0,400,0,0,0,DEFAULT_CHARSET,OUT_OUTLINE_PRECIS,CLIP_DEFAULT_PRECIS,ANTIALIASED_QUALITY,VARIABLE_PITCH,style)) 
End Sub

Sub setfontcolours(h As hdc,text As Ulong,background As Ulong)
    SetTextColor(h,text) 
    SetBkColor(h,background)
End Sub

Sub text(h As hdc,x As Long,y As Long,s As String)
    Var l=Len(s)
    textouta(h,x,y,s,L)
End Sub

Sub ClearScreen(h As hdc)
    Var colour=BackgroundColour
    SetDCBrushColor(h,colour)
    SetDCPenColor(h,colour)
    rectangle(h,0,0,xres,yres)
End Sub

Sub hidecursor()
    
    Dim As handle consoleHandle
    Dim As CONSOLE_CURSOR_INFO info
    consolehandle = GetStdHandle(STD_OUTPUT_HANDLE)
    info.dwSize = 100
    info.bVisible = FALSE 
    SetConsoleCursorInfo(consoleHandle, @info)
End Sub


Sub mask(h As hdc)
    SetDCBrushColor(h,bgr(200,0,0))
    SetDCPenColor(h,bgr(200,0,0))
    Var x=.3*xres,y=.5*yres,r=200
    ellipse(h,(x-r),(y-r/2),(x+r),(y+r/2))
    x=.7*xres:y=.5*yres:r=200
    ellipse(h,(x-r),(y-r/2),(x+r),(y+r/2))
    SetDCBrushColor(h,bgr(0,0,0))
    SetDCPenColor(h,bgr(200,100,0))
    x=.3*xres:y=.5*yres:r=30
    ellipse(h,(x-r/2),(y-r),(x+r/2),(y+r))
    x=.7*xres:y=.5*yres:r=30
    ellipse(h,(x-r/2),(y-r),(x+r/2),(y+r))
End Sub


Function Regulate(Byval MyFps As Long,Byref fps As Long) As Long 'optional
    Static As Double timervalue,_lastsleeptime,t3,frames
    frames+=1
    If (Timer-t3)>=1 Then t3=Timer:fps=frames:frames=0
    Var sleeptime=_lastsleeptime+((1/myfps)-Timer+timervalue)*1000
    If sleeptime<1 Then sleeptime=1
    _lastsleeptime=sleeptime
    timervalue=Timer
    Return sleeptime
End Function

Dim As hdc Memhdc,WorkingScreen,hdc
Dim As HBITMAP Membitmap
Dim As msg emsg
Dim As Long fps

Dim As hwnd p=getconsolewindow()
setwindowpos(p, HWND_TOPMOST, 100, 100, 810, 630,SWP_SHOWWINDOW)
WorkingScreen=GetDC(p)
Memhdc = CreateCompatibleDC(WorkingScreen)
Membitmap = CreateCompatibleBitmap(WorkingScreen, xres, yres)

SelectObject(Memhdc, Membitmap)
SelectObject(Memhdc,GetStockObject(DC_BRUSH))
SelectObject(Memhdc,GetStockObject(DC_PEN))

setfontsize(Memhdc,45,"comic sans ms")
setfontcolours(Memhdc,bgr(0,0,200),BackgroundColour)
'some console instructions
Var sysMenu = GetSystemMenu(p, False)
DeleteMenu(sysMenu, SC_CLOSE, MF_BYCOMMAND)    'cannot close console
DeleteMenu(sysMenu, SC_MINIMIZE, MF_BYCOMMAND) 'To prevent user from minimizing console window
DeleteMenu(sysMenu, SC_MAXIMIZE, MF_BYCOMMAND)'To prevent user from maximizing console window
DeleteMenu(sysMenu, SC_SIZE, MF_BYCOMMAND)    'non resizable console
hidecursor()
ShowScrollBar(p, SB_BOTH, FALSE)

While true
    
    'graphics loop
    clearscreen(Memhdc)
    text(Memhdc,10,10,"A Pussy Cat Mask?")
    text(Memhdc,10,500,"Press <escape> to finish")
    mask(Memhdc)
    BitBlt(WorkingScreen, 0, 0, xres, yres,Memhdc, 0, 0,SRCCOPY)
    Sleep regulate(60,fps)
    If GetAsyncKeyState(&h1B) Then ' escape key
        DeleteObject(Membitmap)
        DeleteDC    (Memhdc)
        End
    End If
Wend


 
aurelVZAB
Posts: 666
Joined: Jul 02, 2008 14:55
Contact:

Re: Masking in BitBlt

Post by aurelVZAB »

He mean on bitmap mask...
well from my experience you don't need it
i have example from another compiler using

'update sprite
TransparentBlt (hDCmem, sx, sy, 200, 440, hdcbmp2, 0, 0, 200, 440, 0)
BitBlt(hDC, 0, 0, w, h, hdcMem, 0, 0, SRCCOPY)


also you need to blit sprites and back images (if you use it ) under WM_PAINT message
like this :

CASE WM_PAINT
BitBlt(hDC, 0, 0, w, h, hdcMem, 0, 0, SRCCOPY)


if you wish i can upload somewhere code + executables
aurelVZAB
Posts: 666
Joined: Jul 02, 2008 14:55
Contact:

Re: Masking in BitBlt

Post by aurelVZAB »

so in general
Load your image into DC memory buffer
select object of HDC
TransparentBlt your image to backbuffer called hdcMem
and under WM_Paint just blit from backBufer to screen buffer or main HDC
UEZ
Posts: 966
Joined: May 05, 2017 19:59
Location: Germany

Re: Masking in BitBlt

Post by UEZ »

myndgrrrrrl wrote: Feb 13, 2022 0:25 I tried in many ways and I couldn't
Can we see your try?
Post Reply