Clearing a CanvasGadget

Just starting out? Need help? Post your questions and find answers here.
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Clearing a CanvasGadget

Post by #NULL »

You have to comment out the FillMemory() part to make the Box() part visible.
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 »

#NULL wrote:You have to comment out the FillMemory() part to make the Box() part visible.
But the FillMemory part doesn't change the canvas to red.

What am I doing wrong?
BarryG
Addict
Addict
Posts: 4128
Joined: Thu Apr 18, 2019 8:17 am

Re: Clearing a CanvasGadget

Post by BarryG »

@#NULL: Got it! Thanks. Now it works.
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Clearing a CanvasGadget

Post by #NULL »

On my system DrawingBufferPixelFormat() returns #PB_PixelFormat_24Bits_RGB so maybe you can't write an alpha value that way(?) I Don't know how you can FillMemory with a 3-byte sequence.
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Clearing a CanvasGadget

Post by #NULL »

DIsable the Debugger if you test performance.
The following works, but I don't know why. The FillMemory should result in white instead of red as far as I understand.

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, $ff0000ff)
Next f
time1_end=ElapsedMilliseconds()


While WindowEvent() : Wend
Delay(2000) ; To see the red


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


StopDrawing()


MessageRequester("", 
                 "time1: "+Str(time1_end-time1_start) + #CRLF$ + 
                 "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
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Clearing a CanvasGadget

Post by Josh »

marcoagpinto wrote:What is wrong in my test code?
The output is only done with StopDrawing()

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()
StopDrawing()


Delay(2000) ; To see the red


StartDrawing(CanvasOutput(1))
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
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 »

Ahhhh...

I was stressed and didn't notice the StopDrawing only at the end.

@Fred:
Could you implement a function in PB to clear a canvas?
ClearCanvasGadget(gadget, colour)
?

Thank you!
User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Clearing a CanvasGadget

Post by Josh »

marcoagpinto wrote:@Fred:
Could you implement a function in PB to clear a canvas?
ClearCanvasGadget(gadget, colour)
This is already implemented and is called SetGadgetColor() :mrgreen:
Look at my posting from 17.01.2019 20:47 in this thread.
sorry for my bad english
wilbert
PureBasic Expert
PureBasic Expert
Posts: 3942
Joined: Sun Aug 08, 2004 5:21 am
Location: Netherlands

Re: Clearing a CanvasGadget

Post by wilbert »

Josh wrote:his is already implemented and is called SetGadgetColor() :mrgreen:
Look at my posting from 17.01.2019 20:47 in this thread.
I don't know about Windows but on macOS SetGadgetColor is rather slow for clearing the canvas.
FillMemory is fastest but what also is very fast on macOS is to create a background image you want and use

Code: Select all

SetGadgetAttribute(CanvasGadget, #PB_Canvas_Image, ImageID(MyBackgroundImage))
That also gives you the freedom to use some kind of pattern instead of a solid color.
Windows (x64)
Raspberry Pi OS (Arm64)
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 »

Josh wrote:
marcoagpinto wrote:@Fred:
Could you implement a function in PB to clear a canvas?
ClearCanvasGadget(gadget, colour)
This is already implemented and is called SetGadgetColor() :mrgreen:
Look at my posting from 17.01.2019 20:47 in this thread.
Well,

It doesn't work properly :cry:

Code: Select all

StartDrawing(CanvasOutput(1))
time2_start=ElapsedMilliseconds()
For f=1 To 1000
  DrawText(10,10,"TESTING",#Yellow,#White)
  SetGadgetColor(1,#PB_Gadget_BackColor,#Blue)
  SetGadgetColor(1,#PB_Gadget_FrontColor,#Blue)
  ResizeGadget(1, #PB_Ignore, #PB_Ignore, #PB_Ignore, #PB_Ignore)
Next f
time2_end=ElapsedMilliseconds()
StopDrawing()


User avatar
Josh
Addict
Addict
Posts: 1183
Joined: Sat Feb 13, 2010 3:45 pm

Re: Clearing a CanvasGadget

Post by Josh »

marcoagpinto wrote:It doesn't work properly :cry:
Don't use SetGadgetColor() inside StartDrawing/StopDrawing

Code: Select all

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



StartDrawing(CanvasOutput(1))
time1 = ElapsedMilliseconds()
For i = 1  To 1000
  Box(1,0,500,500,#Red)
Next
time1 = ElapsedMilliseconds() - time1
StopDrawing()


MessageRequester ("", "")


StartDrawing(CanvasOutput(1))
time2 = ElapsedMilliseconds()
For i = 1 To 1000
  FillMemory(DrawingBuffer(), DrawingBufferPitch() * OutputHeight(), #Red)
Next
time2 = ElapsedMilliseconds() - time2
StopDrawing()


MessageRequester ("", "")


SetGadgetColor (1, #PB_Gadget_BackColor, #Green)
time3 = ElapsedMilliseconds()
For i = 1 To 1000
  ResizeGadget (1, #PB_Ignore, #PB_Ignore, #PB_Ignore, #PB_Ignore)
Next
time3 = ElapsedMilliseconds() - time3


MessageRequester ("", "")


SetGadgetColor (1, #PB_Gadget_BackColor, #Yellow)
StartDrawing(CanvasOutput(1))
time4 = ElapsedMilliseconds()
For i = 1 To 1000
Next
time4 = ElapsedMilliseconds() - time4
StopDrawing()


Debug "time1: " + time1
Debug "time2: " + time2
Debug "time3: " + time3
Debug "time4: " + time4


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 »

Well,

I give up.

In my code I am drawing in the canvas and then want to clear it, but this last SetGadgetColour is not the fastest solution.

Code: Select all

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



time1 = ElapsedMilliseconds()
For i = 1  To 1000
  StartDrawing(CanvasOutput(1))
  DrawText(10,10,"TEST",#Yellow,#Blue)
  Box(1,0,500,500,#Red)
  StopDrawing()
Next
time1 = ElapsedMilliseconds() - time1



; MessageRequester ("", "")



time2 = ElapsedMilliseconds()
For i = 1 To 1000
  StartDrawing(CanvasOutput(1))
  DrawText(10,10,"TEST",#Yellow,#Blue)
  FillMemory(DrawingBuffer(), DrawingBufferPitch() * OutputHeight(), #Red)
  StopDrawing()  
Next
time2 = ElapsedMilliseconds() - time2



; MessageRequester ("", "")



time3 = ElapsedMilliseconds()
For i = 1 To 1000
  StartDrawing(CanvasOutput(1))
  DrawText(10,10,"TEST",#Yellow,#Blue)
  StopDrawing()
  SetGadgetColor (1, #PB_Gadget_BackColor, #Green)
Next
time3 = ElapsedMilliseconds() - time3



Debug "time1: " + time1
Debug "time2: " + time2
Debug "time3: " + time3


Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow

#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Clearing a CanvasGadget

Post by #NULL »

In my code I am drawing in the canvas and then want to clear it
Can you explain why you are doing this?
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 »

#NULL wrote:
In my code I am drawing in the canvas and then want to clear it
Can you explain why you are doing this?
Yes.

Imagine you have a background image and you have hundreds of insects moving over it (on the canvas gadget).

So, every time I increase a time unit (1 to 1000) I need to clear the canvas, draw the background and draw the new position of the insects which use alpha transparency.

:mrgreen:
#NULL
Addict
Addict
Posts: 1497
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: Clearing a CanvasGadget

Post by #NULL »

It sounded to me as if you would draw and then clear, both in a single drawing block, that's why I asked. But just clear and draw makes sense though :) . Any reason why you don't use a screen?
Post Reply