Clearing a CanvasGadget
- marcoagpinto
- Addict
- Posts: 1045
- Joined: Sun Mar 10, 2013 3:01 pm
- Location: Portugal
- Contact:
Clearing a CanvasGadget
Currently I clear a CanvasGadget with the command BOX with #White.
Is there a faster/easier way in PB 5.70?
Thanks!
Is there a faster/easier way in PB 5.70?
Thanks!
Re: Clearing a CanvasGadget
Code: Select all
FillMemory(DrawingBuffer(), width * High * 4, color.l, #PB_Long)
macOS Catalina 10.15.7
Re: Clearing a CanvasGadget
@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()
But the CanvasGadget Output is most likely 24bit.
I think Box() is a fast way.
If you use VectorOutput you can use FillVectorOutput()
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and more ― Typeface - Sprite-based font include/module
Lizard - Script language for symbolic calculations and more ― Typeface - Sprite-based font include/module
- marcoagpinto
- Addict
- Posts: 1045
- Joined: Sun Mar 10, 2013 3:01 pm
- Location: Portugal
- Contact:
Re: Clearing a CanvasGadget
Thanks, guys, I will continue to use Box.
Re: Clearing a CanvasGadget
If you use a 24 bit image you must use this:
the buffer size of the image is the width * High * bytePerPixel.
24Bit are 3 Byte.
Code: Select all
FillMemory(DrawingBuffer(), width * High * 3, color.l, #PB_Long)
24Bit are 3 Byte.
macOS Catalina 10.15.7
Re: Clearing a CanvasGadget
If you want to make the canvas white, you can useWolfram wrote:If you use a 24 bit image you must use this:
Code: Select all
FillMemory(DrawingBuffer(), DrawingBufferPitch() * OutputHeight(), $FF)
I haven't tested it on Windows but on MacOS FillMemory is significantly faster.
Windows (x64)
Raspberry Pi OS (Arm64)
Raspberry Pi OS (Arm64)
Re: Clearing a CanvasGadget
Another way might be to draw a prepared background image, but that's probably the slowest.
Re: Clearing a CanvasGadget
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.
So you either need to calculate the drawing buffer size manually or use Wilbert's method.
I may look like a mule, but I'm not a complete ass.
Re: Clearing a CanvasGadget
DrawingBufferPitch() needs to be used in this case, some OS can include paddings per line
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
Re: Clearing a CanvasGadget
+1 for Wilbert method, Fred sweated to make FillMemory fast & efficient, might as well use it.
BERESHEIT
Re: Clearing a CanvasGadget
IIRC, it's only a memset() but thx 

Re: Clearing a CanvasGadget
Maybe a ResizeGadget() could be faster:
(Code only runs from V5.70 on.)
(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
sorry for my bad english
- marcoagpinto
- Addict
- Posts: 1045
- Joined: Sun Mar 10, 2013 3:01 pm
- Location: Portugal
- Contact:
Re: Clearing a CanvasGadget
Hello!
Sorry for reopening the post.
The CanvasGadget doesn't turn to red.
What is wrong in my test code?:
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
#Red doesn't have an alpha value, so it's transparent red. Use this instead:
I don't know about the FillMemory(). I tried ... , $ff0000ff, #PB_Long) but it doesn't work.
Code: Select all
Box(1,0,500,500,$ff0000ff)
Re: Clearing a CanvasGadget
I see no red box at all with the original code or NULL's alpha box version. I assume I'm supposed to?