Clearing a CanvasGadget

Just starting out? Need help? Post your questions and find answers here.
User avatar
marcoagpinto
Addict
Addict
Posts: 1045
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Clearing a CanvasGadget

Post by marcoagpinto »

Currently I clear a CanvasGadget with the command BOX with #White.

Is there a faster/easier way in PB 5.70?

Thanks!
Wolfram
Enthusiast
Enthusiast
Posts: 604
Joined: Thu May 30, 2013 4:39 pm

Re: Clearing a CanvasGadget

Post by Wolfram »

Code: Select all

FillMemory(DrawingBuffer(), width * High * 4, color.l, #PB_Long)
macOS Catalina 10.15.7
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Clearing a CanvasGadget

Post 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()
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 moreTypeface - Sprite-based font include/module
User avatar
marcoagpinto
Addict
Addict
Posts: 1045
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: Clearing a CanvasGadget

Post by marcoagpinto »

Thanks, guys, I will continue to use Box.
Wolfram
Enthusiast
Enthusiast
Posts: 604
Joined: Thu May 30, 2013 4:39 pm

Re: Clearing a CanvasGadget

Post 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.
macOS Catalina 10.15.7
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Clearing a CanvasGadget

Post 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.
Windows (x64)
Raspberry Pi OS (Arm64)
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Clearing a CanvasGadget

Post by #NULL »

Another way might be to draw a prepared background image, but that's probably the slowest.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Re: Clearing a CanvasGadget

Post 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.
I may look like a mule, but I'm not a complete ass.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Clearing a CanvasGadget

Post by Fred »

DrawingBufferPitch() needs to be used in this case, some OS can include paddings per line
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Re: Clearing a CanvasGadget

Post by netmaestro »

+1 for Wilbert method, Fred sweated to make FillMemory fast & efficient, might as well use it.
BERESHEIT
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Clearing a CanvasGadget

Post by Fred »

IIRC, it's only a memset() but thx :lol:
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Clearing a CanvasGadget

Post 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
sorry for my bad english
User avatar
marcoagpinto
Addict
Addict
Posts: 1045
Joined: Sun Mar 10, 2013 3:01 pm
Location: Portugal
Contact:

Re: Clearing a CanvasGadget

Post 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
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Clearing a CanvasGadget

Post 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.
BarryG
Addict
Addict
Posts: 4128
Joined: Thu Apr 18, 2019 8:17 am

Re: Clearing a CanvasGadget

Post 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?
Post Reply