Page 2 of 3

Re: Clearing a CanvasGadget

Posted: Thu Sep 19, 2019 8:49 am
by #NULL
You have to comment out the FillMemory() part to make the Box() part visible.

Re: Clearing a CanvasGadget

Posted: Thu Sep 19, 2019 8:58 am
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?

Re: Clearing a CanvasGadget

Posted: Thu Sep 19, 2019 8:58 am
by BarryG
@#NULL: Got it! Thanks. Now it works.

Re: Clearing a CanvasGadget

Posted: Thu Sep 19, 2019 9:18 am
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.

Re: Clearing a CanvasGadget

Posted: Thu Sep 19, 2019 9:31 am
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

Re: Clearing a CanvasGadget

Posted: Thu Sep 19, 2019 9:38 am
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

Re: Clearing a CanvasGadget

Posted: Thu Sep 19, 2019 9:57 am
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!

Re: Clearing a CanvasGadget

Posted: Thu Sep 19, 2019 10:05 am
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.

Re: Clearing a CanvasGadget

Posted: Thu Sep 19, 2019 10:25 am
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.

Re: Clearing a CanvasGadget

Posted: Thu Sep 19, 2019 10:29 am
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()



Re: Clearing a CanvasGadget

Posted: Thu Sep 19, 2019 10:41 am
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

Re: Clearing a CanvasGadget

Posted: Thu Sep 19, 2019 11:04 am
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


Re: Clearing a CanvasGadget

Posted: Thu Sep 19, 2019 1:52 pm
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?

Re: Clearing a CanvasGadget

Posted: Thu Sep 19, 2019 3:12 pm
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:

Re: Clearing a CanvasGadget

Posted: Thu Sep 19, 2019 3:25 pm
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?