Page 1 of 3

Clearing a CanvasGadget

Posted: Thu Jan 17, 2019 11:23 am
by marcoagpinto
Currently I clear a CanvasGadget with the command BOX with #White.

Is there a faster/easier way in PB 5.70?

Thanks!

Re: Clearing a CanvasGadget

Posted: Thu Jan 17, 2019 12:15 pm
by Wolfram

Code: Select all

FillMemory(DrawingBuffer(), width * High * 4, color.l, #PB_Long)

Re: Clearing a CanvasGadget

Posted: Thu Jan 17, 2019 1:11 pm
by STARGĂ…TE
@Wolfram: This works only if, the DrawingBufferPixelFormat is 32 bit.
But the CanvasGadget Output is most likely 24bit.

I think Box() is a fast way.
If you use VectorOutput you can use FillVectorOutput()

Re: Clearing a CanvasGadget

Posted: Thu Jan 17, 2019 1:17 pm
by marcoagpinto
Thanks, guys, I will continue to use Box.

Re: Clearing a CanvasGadget

Posted: Thu Jan 17, 2019 1:18 pm
by Wolfram
If you use a 24 bit image you must use this:

Code: Select all

FillMemory(DrawingBuffer(), width * High * 3, color.l, #PB_Long)
the buffer size of the image is the width * High * bytePerPixel.
24Bit are 3 Byte.

Re: Clearing a CanvasGadget

Posted: Thu Jan 17, 2019 1:28 pm
by wilbert
Wolfram wrote:If you use a 24 bit image you must use this:
If you want to make the canvas white, you can use

Code: Select all

FillMemory(DrawingBuffer(), DrawingBufferPitch() * OutputHeight(), $FF)
for both 24 and 32 bit.
I haven't tested it on Windows but on MacOS FillMemory is significantly faster.

Re: Clearing a CanvasGadget

Posted: Thu Jan 17, 2019 2:39 pm
by #NULL
Another way might be to draw a prepared background image, but that's probably the slowest.

Re: Clearing a CanvasGadget

Posted: Thu Jan 17, 2019 3:30 pm
by srod
Yes even with a 24-bit, 3-byte per pixel buffer, there will usually be additional bytes added per row to bring the total to a multiple of 4 which is not the same as assuming 4-bytes per pixel.

So you either need to calculate the drawing buffer size manually or use Wilbert's method.

Re: Clearing a CanvasGadget

Posted: Thu Jan 17, 2019 3:32 pm
by Fred
DrawingBufferPitch() needs to be used in this case, some OS can include paddings per line

Re: Clearing a CanvasGadget

Posted: Thu Jan 17, 2019 5:05 pm
by netmaestro
+1 for Wilbert method, Fred sweated to make FillMemory fast & efficient, might as well use it.

Re: Clearing a CanvasGadget

Posted: Thu Jan 17, 2019 5:33 pm
by Fred
IIRC, it's only a memset() but thx :lol:

Re: Clearing a CanvasGadget

Posted: Thu Jan 17, 2019 7:47 pm
by Josh
Maybe a ResizeGadget() could be faster:

(Code only runs from V5.70 on.)

Code: Select all

OpenWindow   (0,  0,  0, 300, 300, "Test", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
CanvasGadget (0, 10, 10, 280, 280)

SetGadgetColor (0, #PB_Gadget_BackColor, #Red)

MessageRequester ("", "")

SetGadgetColor (0, #PB_Gadget_BackColor, #White)
ResizeGadget   (0, #PB_Ignore, #PB_Ignore, #PB_Ignore, #PB_Ignore)

Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

Re: Clearing a CanvasGadget

Posted: Thu Sep 19, 2019 8:31 am
by marcoagpinto
Hello!

Sorry for reopening the post.

The CanvasGadget doesn't turn to red.

What is wrong in my test code?:

Code: Select all

OpenWindow(0,0,0,600,600,"Test")
CanvasGadget(1,0,0,500,500)



StartDrawing(CanvasOutput(1))


time1_start=ElapsedMilliseconds()
For f=1 To 1000
  Box(1,0,500,500,#Red)
Next f
time1_end=ElapsedMilliseconds()


Delay(2000) ; To see the red


time2_start=ElapsedMilliseconds()
For f=1 To 1000
  FillMemory(DrawingBuffer(), DrawingBufferPitch() * OutputHeight(), #Red)
Next f
time2_end=ElapsedMilliseconds()


StopDrawing()



Debug "time1: "+Str(time1_end-time1_start)
Debug "time2: "+Str(time2_end-time2_start)


quit=0
Repeat
  event=WaitWindowEvent()
  
    ; User has pressed the close window gadget
    If event=#PB_Event_CloseWindow
      quit=1
    EndIf  
    
Until quit=1    
    

End

Re: Clearing a CanvasGadget

Posted: Thu Sep 19, 2019 8:44 am
by #NULL
#Red doesn't have an alpha value, so it's transparent red. Use this instead:

Code: Select all

Box(1,0,500,500,$ff0000ff)
I don't know about the FillMemory(). I tried ... , $ff0000ff, #PB_Long) but it doesn't work.

Re: Clearing a CanvasGadget

Posted: Thu Sep 19, 2019 8:47 am
by BarryG
I see no red box at all with the original code or NULL's alpha box version. I assume I'm supposed to?