SetBrushTransparency after RotateBrush problem

Discuss any general programming issues here
Post Reply
Mazze
Posts: 69
Joined: Thu May 06, 2010 8:08 pm

SetBrushTransparency after RotateBrush problem

Post by Mazze »

Doing SetBrushTransparency after RotateBrush causes some corruption:

Image

Code: Select all

@DISPLAY 1, {Color = #SILVER}

CreateBrush(1, 50, 50)
SelectBrush(1)
Line(0, 25, 50, 25, #RED,{ Thickness = 10})
Line(0, 0, 50, 50, #RED,{ Thickness = 10})
EndSelect

DisplayBrush(1, 0, 0)
RotateBrush(1, 45)
DisplayBrush(1, 50, 0)
;DeleteMask(1)
SetBrushTransparency(1, #BLACK)
DisplayBrush(1, 150,0)

WaitLeftMouse()
Am I doing something wrong or is this a bug?
User avatar
airsoftsoftwair
Posts: 5887
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: SetBrushTransparency after RotateBrush problem

Post by airsoftsoftwair »

That's not a bug. SetBrushTransparency() generates a completely new mask for the brush, taking all pixels into account (visible and invisible pixels). After RotateBrush() returns, invisible pixels may contain random colors. That's why you get the graphical glitches. To workaround this you need to create another brush with a static background color (i.e. black), draw the rotated brush onto it, and then call SetBrushTransparency() on this new brush. I agree that this is a little bit of overhead. Maybe I'll add an optional argument to SetBrushTransparency() that allows you to specify whether invisible pixels should be taken into account as well or not.
Mazze
Posts: 69
Joined: Thu May 06, 2010 8:08 pm

Re: SetBrushTransparency after RotateBrush problem

Post by Mazze »

Got it. Here's how my "make all pixels in background color transparent" is implemented in HollyPaint:

Code: Select all

Function Brush.p_DoTransparent()
    DebugPrint("***** Brush.p_DoTransparent")

    If Brush.have = True
        If GetAttribute(#BRUSH, Brush.brush, #ATTRHASMASK)
            ; rotation of a brush adds a mask where the unmasked
            ; bits have random values. We need a workaround
            ; to clean the brush.
            Local width = GetAttribute(#BRUSH, Brush.brush, #ATTRWIDTH)
            Local height = GetAttribute(#BRUSH, Brush.brush, #ATTRHEIGHT)
            Local newbrush = CreateBrush(Nil, width, height, Canvas.backcolor)
            SelectBrush(newbrush)
            DisplayBrush(Brush.brush, 0, 0)
            EndSelect
            FreeBrush(Brush.brush)
            Brush.brush = newbrush
        EndIf
        SetBrushTransparency(Brush.brush, Canvas.backcolor)
    Else
        mui.Request("HollyPaint", "No brush available to set transparency.", "Ok")
    EndIf
EndFunction
Post Reply