Some Tips using VectorDrawing lib.

Share your advanced PureBasic knowledge/code with the community.
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Some Tips using VectorDrawing lib.

Post by RASHAD »

Removed
See the last post for better tech.

- Load progress
- Busy
- Animate In Out
Last edited by RASHAD on Tue Nov 14, 2017 4:56 am, edited 3 times in total.
Egypt my love
davido
Addict
Addict
Posts: 1890
Joined: Fri Nov 09, 2012 11:04 pm
Location: Uttoxeter, UK

Re: Some Tips using VectorDrawing lib.

Post by davido »

@RASHAD,
More very nice examples. :D
Thank you very much for sharing.
DE AA EB
User avatar
Andre
PureBasic Team
PureBasic Team
Posts: 2071
Joined: Fri Apr 25, 2003 6:14 pm
Location: Germany (Saxony, Deutscheinsiedel)
Contact:

Re: Some Tips using VectorDrawing lib.

Post by Andre »

Very nice, thank you! :mrgreen:
Bye,
...André
(PureBasicTeam::Docs & Support - PureArea.net | Order:: PureBasic | PureVisionXP)
User avatar
dobro
Enthusiast
Enthusiast
Posts: 766
Joined: Sun Oct 31, 2004 10:54 am
Location: France
Contact:

Re: Some Tips using VectorDrawing lib.

Post by dobro »

Another Path (no Vector)

Code: Select all





UseGIFImageDecoder()



Enumeration
		#win
		#bouton
		#Gadget_Text    
		#gadget_g
		#image
EndEnumeration
Restore Sign:
Declare playanimation(*hgif)


; on precharge le Gif se trouvant sur le Net


AnimatedGIF.s = GetTemporaryDirectory() + "_test.gif"

If Not FileSize(AnimatedGIF) > 0
		InitNetwork()
		ReceiveHTTPFile("https://icons8.com/preloaders/preloaders/301/Windows8%20loader.gif", AnimatedGIF)
EndIf

If AnimatedGIF
		UseGIFImageDecoder()
		hGIF = LoadImage(#PB_Any, AnimatedGIF)
		
		If IsImage(hGIF)
				nWidth = ImageWidth(hGIF) : nHeight = ImageHeight(hGIF)
		ENdif
Endif
Global Thread



If OpenWindow(#win, 0, 0, 200, 300, "Signe d'attente.", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
		ButtonGadget  (#bouton, 10, 10, 200, 20, "Cliquez moi")
		TextGadget(#Gadget_Text, 100, 50, 100, 35, "")
		
		ImageGadget(#Gadget_g, 50, 150, nWidth, nHeight, #Null)
		Thread=CreateThread(@PlayAnimation(), hGIF)
		HideGadget(#Gadget_g, #True) ; cache le gadget
		
		
		Repeat
				Event = WaitWindowEvent()
				Select Event
						Case #PB_Event_Gadget
						Select EventGadget()
								Case #bouton
								HideGadget(#Gadget_g, #False); montre le gadget
								
								; boucle de traitement
								For i=1 to 50000
										compteur_sign=compteur_sign+1
										if compteur_sign=5:compteur_sign=1:Restore Sign :Endif
										Read.s sig.s
										SetGadgetText(#Gadget_Text, sig.s)   
										WindowEvent()
								Next i
								SetGadgetText(#Gadget_Text, "")   
								HideGadget(#Gadget_g, #True) ; cache le gadget
						EndSelect
				EndSelect
		Until Event = #PB_Event_CloseWindow
		If  IsThread(Thread)
				KillThread(Thread)
		Endif
		
		FreeImage(hGIF)
		
EndIf

; ********* Zone Procedures *******************
Procedure PlayAnimation(*hGIF)
;JHPJHP
		FrameCount = ImageFrameCount(*hGIF)
		
		Repeat
				For rtnCount = 0 To FrameCount - 1
						SetImageFrame(*hGIF, rtnCount)
						SetGadgetState(#Gadget_g, ImageID(*hGIF)) : Delay(40)
				Next
		ForEver
EndProcedure

DataSection
		sign:
		Data.s "|o---|"
		Data.s "|-o--|"
		Data.s "|--o-|"
		Data.s "|---o|"
		Data.s "|--o-|"
EndDataSection







Gif here :
https://icons8.com/preloaders/en/free

et bien voilaaaa !!
Image Image

ou bien :
Image Image

ça aussi c'est cool :

Image Image

:)

peut meme faire en 3d
Image Image

du texte:
Image


Image Image



Image

Image
Image
Windows 98/7/10 - PB 5.42
■ sites : http://michel.dobro.free.fr/
User avatar
Kwai chang caine
Always Here
Always Here
Posts: 5357
Joined: Sun Nov 05, 2006 11:42 pm
Location: Lyon - France

Re: Some Tips using VectorDrawing lib.

Post by Kwai chang caine »

Thanks RASHAD for sharing this usefull examples 8)
ImageThe happiness is a road...
Not a destination
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: Some Tips using VectorDrawing lib.

Post by RASHAD »

Thanks guys

Code: Select all

Global bColor.q
;x = x0 + r*Cos(t)
;y = y0 + r*Sin(t)

Procedure createIMG()
  If CreateImage(0, 120, 120)
    StartVectorDrawing(ImageVectorOutput(0))
      VectorSourceColor(bColor)
      FillVectorOutput()
      alpha = 255    
      For angle = 30 To 361 Step 30
        VectorSourceColor(RGBA(255,0,0,alpha))   
        x = 60 + 50*Cos(Radian(angle))
        y = 60 + 50*Sin(Radian(angle))
        AddPathCircle(x,y, 8)
        FillPath()
        alpha - 15
      Next
    StopVectorDrawing()    
  EndIf
  
EndProcedure
 
LoadFont(0, "Georgia", 8, #PB_Font_Bold)
  
Procedure runCB(par)
  Repeat    
      alpha = 255
      For angle = 361 To 0 Step -15
        If IsGadget(0)
          StartVectorDrawing(CanvasVectorOutput(0))
            DrawVectorImage(ImageID(0), 255)
            MovePathCursor(60, 60)     
            AddPathCircle(60, 60, 60, 15, angle, #PB_Path_Connected)
            VectorSourceColor(bColor)
            FillPath()        
            VectorSourceColor(RGBA(0,0,255,alpha))
            MovePathCursor(40, 50)
            VectorFont(FontID(0), 18)
            DrawVectorText("Busy")                 
            alpha - 15 
          StopVectorDrawing()
          Delay(50)
        EndIf 
      Next    
  ForEver
EndProcedure

  If OpenWindow(0, 0, 0, 400, 400, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    SetWindowColor(0,$E4E4E5)
    bColor = GetWindowColor(0)
    If bColor < 0
      StartDrawing(WindowOutput(0))
        bColor = Point(100,100)
      StopDrawing()
    EndIf
    bColor = bColor | $FF000000
    createIMG()
    CanvasGadget(0, 150, 150, 120, 120)
    thread = CreateThread(@runCB(),35)
    Repeat
      Select WaitWindowEvent()
        Case #PB_Event_CloseWindow
          Quit = 1          

      EndSelect          
    Until Quit = 1
  EndIf
Last edited by RASHAD on Mon Nov 13, 2017 9:18 pm, edited 2 times in total.
Egypt my love
oO0XX0Oo
User
User
Posts: 78
Joined: Thu Aug 10, 2017 7:35 am

Re: Some Tips using VectorDrawing lib.

Post by oO0XX0Oo »

Very cool, thanks for all the examples! :D
User avatar
Kukulkan
Addict
Addict
Posts: 1352
Joined: Mon Jun 06, 2005 2:35 pm
Location: germany
Contact:

Re: Some Tips using VectorDrawing lib.

Post by Kukulkan »

Nice!

But isn't it a problem to draw from threads? In my experience, every threaded access to any gadget created in the main thread lead to strange behavior up to crashes. While it seem to work somehow on Windows, on Linux and Mac it was a guarantee for crashes.

Is the Canvas thread-save in this way?
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: Some Tips using VectorDrawing lib.

Post by RASHAD »

Thanks guys
Previous post updated
- Graphics modified little bit
- Added check for existing of the CanvasGadget

@Kukulkan
It should be very safe now
It is just drawing to a gadget created in the main loop(that is all)
Using thread is the best way to avoid stop while moving or resizing the main window and leave the main window free from affected by the required delay time
Egypt my love
User avatar
skywalk
Addict
Addict
Posts: 4004
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Some Tips using VectorDrawing lib.

Post by skywalk »

Hi RASHAD, nice examples.
I do see random crashes within runCB() when closing the window.
Or
Moving the window and then closing it.

The runCB() drawing functions have nowhere to paint if the window closes before the thread ends.
Even adding
Case #PB_Event_CloseWindow
Quit = 1
WaitThread(thread, 6000)
does not always work.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: Some Tips using VectorDrawing lib.

Post by RASHAD »

Hi skywalk
Thanks
That is why I added check for existing of the CanvasGadget() in the last example and it should be added to every example in the first post
Please check the last example again
Waiting for your results
Egypt my love
User avatar
skywalk
Addict
Addict
Posts: 4004
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Some Tips using VectorDrawing lib.

Post by skywalk »

Yes, the error happens with your last example.
Maybe IsGadget(0) is not updated when the app quits?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: Some Tips using VectorDrawing lib.

Post by RASHAD »

I can not replicate the error

Try

Code: Select all

    Repeat
      Select WaitWindowEvent(10)
        Case #PB_Event_CloseWindow
          KillThread(thread)
          Quit = 1          

      EndSelect          
    Until Quit = 1
Or try other different timeout

Or try next snippet to avoid KillThread(thread)

Code: Select all

Global bColor.q,qFlag
Procedure createIMG()
  If CreateImage(0, 120, 120)
    StartVectorDrawing(ImageVectorOutput(0))
      VectorSourceColor(bColor)
      FillVectorOutput()
      alpha = 255    
      For angle = 30 To 361 Step 30
        VectorSourceColor(RGBA(255,0,0,alpha))   
        x = 60 + 50*Cos(Radian(angle))
        y = 60 + 50*Sin(Radian(angle))
        AddPathCircle(x,y, 8)
        FillPath()
        alpha - 15
      Next
    StopVectorDrawing()    
  EndIf
  
EndProcedure
 
LoadFont(0, "Georgia", 8, #PB_Font_Bold)
  
Procedure runCB(par)
  Repeat 
      alpha = 255
      For angle = 361 To 0 Step -15
        If qFlag = 1
          Break
        EndIf   
        StartVectorDrawing(CanvasVectorOutput(0))
          DrawVectorImage(ImageID(0), 255)
          MovePathCursor(60, 60)     
          AddPathCircle(60, 60, 60, 15, angle, #PB_Path_Connected)
          VectorSourceColor(bColor)
          FillPath()        
          VectorSourceColor(RGBA(0,0,255,alpha))
          MovePathCursor(40, 50)
          VectorFont(FontID(0), 18)
          DrawVectorText("Busy")                 
          alpha - 15 
        StopVectorDrawing()
        Delay(50)
      Next    
  ForEver
EndProcedure

  If OpenWindow(0, 0, 0, 400, 400, "VectorDrawing", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
    SetWindowColor(0,$E4E4E5)
    bColor = GetWindowColor(0)
    If bColor < 0
      StartDrawing(WindowOutput(0))
        bColor = Point(100,100)
      StopDrawing()
    EndIf
    bColor = bColor | $FF000000
    createIMG()
    CanvasGadget(0, 140, 120, 120, 120)
    thread = CreateThread(@runCB(),35)
    Repeat
      Select WaitWindowEvent(1)
        Case #PB_Event_CloseWindow
          qFlag = 1
          Delay(100)
          Quit = 1
          
      EndSelect          
    Until Quit = 1
  EndIf
Egypt my love
User avatar
skywalk
Addict
Addict
Posts: 4004
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Some Tips using VectorDrawing lib.

Post by skywalk »

I had tried waitthread() where you put killthread() and that still had errors.
Monitoring qFlag in the thread works.
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4664
Joined: Sun Apr 12, 2009 6:27 am

Re: Some Tips using VectorDrawing lib.

Post by RASHAD »

@skywalk thanks
The problem is we must use the thread to get the proper functionality
Glad to know that qFlag works
Thanks again
Egypt my love
Post Reply