It is currently Thu Dec 12, 2019 2:09 am

All times are UTC + 1 hour




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: PureBasic modules step-by-step by example (triangles!)
PostPosted: Wed Oct 02, 2019 4:08 am 
Offline
Addict
Addict

Joined: Sat Jun 30, 2007 8:04 pm
Posts: 3362
Analyzing issues with PureBasic and how to improve it

Lets start with something simple. We'll create a window and draw a triangle.

Code:
Define window.i

window=OpenWindow(#PB_Any,0,0,132,103,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)

StartDrawing(WindowOutput(window))
FrontColor(#Black)
LineXY(65,7,113,91)
LineXY(113,91,17,91)
LineXY(17,91,65,7)
StopDrawing()

Repeat
Until WaitWindowEvent()=#PB_Event_CloseWindow


Great. But there is a problem. The points are hard-coded. And worse, we've had to type them twice (one for the end of one line and again for the start of another. So now if we want to move a point we have to analyze it closely. We can also forget moving the points around.

Great! Variables to the rescue!

Code:
Define x1.l, y1.l, x2.l, y2.l, x3.l, y3.l

x1=65
y1=7
x2=113
y2=91
x3=17
y3=91

LineXY(x1,y1,x2,y2)
LineXY(x2,y2,x3,y3)
LineXY(x3,y3,x1,y1)


Now we have a bunch of variables that we can modify and the triangle line drawing will automatically update. But why have 6 variables when we only need one? This code can be further improved by using structures.

Code:
Structure Triangle
  x1.l
  y1.l
  x2.l
  y2.l
  x3.l
  y3.l
EndStructure

Define triangle.Triangle

triangle\x1=65
triangle\y1=7
triangle\x2=113
triangle\y2=91
triangle\x3=17
triangle\y3=91

LineXY(triangle\x1,triangle\y1,triangle\x2,triangle\y2)
LineXY(triangle\x2,triangle\y2,triangle\x3,triangle\y3)
LineXY(triangle\x3,triangle\y3,triangle\x1,triangle\y1)


This is better. But what if we want multiple triangles? We'll need a procedure to both allocate and free the memory for the triangles. We'll have it return the structure so we can manipulate it just as easily as before.

And while we're at it, lets make a procedure to draw it as well.

Code:
;-- Triangle

Structure Triangle
  x1.l
  y1.l
  x2.l
  y2.l
  x3.l
  y3.l
EndStructure

Procedure Triangle_new(x1.l, y1.l, x2.l, y2.l, x3.l, y3.l)
  Protected *triangle.Triangle=AllocateStructure(Triangle)
 
  *triangle\x1=x1
  *triangle\y1=y1
  *triangle\x2=x2
  *triangle\y2=y2
  *triangle\x3=x3
  *triangle\y3=y3
 
  ProcedureReturn *triangle
EndProcedure

Procedure Triangle_delete(*triangle.Triangle)
  FreeStructure(*triangle)
EndProcedure

Procedure Triangle_draw(*triangle.Triangle)
  LineXY(*triangle\x1,*triangle\y1,*triangle\x2,*triangle\y2)
  LineXY(*triangle\x2,*triangle\y2,*triangle\x3,*triangle\y3)
  LineXY(*triangle\x3,*triangle\y3,*triangle\x1,*triangle\y1)
EndProcedure

;--

Define window.i

window=OpenWindow(#PB_Any,0,0,132,103,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)

Define *triangle.Triangle

*triangle=Triangle_new(65,7, 113,91, 17,91)

StartDrawing(WindowOutput(window))
FrontColor(#Black)
Triangle_draw(*triangle)
StopDrawing()

Repeat
Until WaitWindowEvent()=#PB_Event_CloseWindow


It looks like we have a lot of procedures now that have to do with triangles like Triangle_new(), Triangle_delete(), and Triangle_draw(). Lets put them into a module. I've also created an additional procedure and added a button to the window for creating random triangles when pressed.

Code:
;-- Triangle
DeclareModule Triangle
  Structure Triangle
    x1.l
    y1.l
    x2.l
    y2.l
    x3.l
    y3.l
  EndStructure
 
  Declare new(x1.l, y1.l, x2.l, y2.l, x3.l, y3.l)
  Declare delete(*triangle.Triangle)
  Declare draw(*triangle.Triangle)
  Declare fromRandom(min.l, max.l)
EndDeclareModule

Module Triangle
  Procedure new(x1.l, y1.l, x2.l, y2.l, x3.l, y3.l)
    Protected *triangle.Triangle=AllocateStructure(Triangle)
   
    *triangle\x1=x1
    *triangle\y1=y1
    *triangle\x2=x2
    *triangle\y2=y2
    *triangle\x3=x3
    *triangle\y3=y3
   
    ProcedureReturn *triangle
  EndProcedure
 
  Procedure delete(*triangle.Triangle)
    FreeStructure(*triangle)
  EndProcedure
 
  Procedure draw(*triangle.Triangle)
    LineXY(*triangle\x1,*triangle\y1,*triangle\x2,*triangle\y2)
    LineXY(*triangle\x2,*triangle\y2,*triangle\x3,*triangle\y3)
    LineXY(*triangle\x3,*triangle\y3,*triangle\x1,*triangle\y1)
  EndProcedure
 
  Procedure fromRandom(min.l, max.l)
    ProcedureReturn new(Random(max,min),Random(max,min),Random(max,min),Random(max,min),Random(max,min),Random(max,min))
  EndProcedure
EndModule

;--

Procedure drawRandomTriangle(*oldTriangle, image)
  Protected *triangle.Triangle::Triangle
 
  Triangle::delete(*oldTriangle)
  *triangle=Triangle::fromRandom(10,92)
 
  FreeImage(image)
 
  DisableDebugger
  CreateImage(image,132,103,32,#PB_Image_Transparent)
  EnableDebugger
 
  StartDrawing(ImageOutput(image))
  DrawingMode(#PB_2DDrawing_AllChannels)
  FrontColor(RGBA(0,0,0,255))
  Triangle::draw(*triangle)
  StopDrawing()
EndProcedure

Define window.i
Define image.i
Define imageGadget.i
Define buttonGadget.i
Define event.i

window=OpenWindow(#PB_Any,0,0,132,136,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
image=CreateImage(#PB_Any,132,103,32,#PB_Image_Transparent)
imageGadget=ImageGadget(#PB_Any,0,0,132,103,ImageID(image))
buttonGadget=ButtonGadget(#PB_Any,5,109,122,23,"Random")

Define *triangle.Triangle::Triangle

*triangle=Triangle::new(65,7, 113,91, 17,91)

StartDrawing(ImageOutput(image))
DrawingMode(#PB_2DDrawing_AllChannels)
FrontColor(RGBA(0,0,0,255))
Triangle::draw(*triangle)
StopDrawing()

SetGadgetState(imageGadget,ImageID(image))

Repeat
  event=WaitWindowEvent()
 
  If event=#PB_Event_Gadget And EventGadget()=buttonGadget
    drawRandomTriangle(*triangle,image)
    SetGadgetState(imageGadget,ImageID(image))
  EndIf
Until event=#PB_Event_CloseWindow


You can see now how everything is self-contained in a module:

Code:
Triangle::new(x1.l, y1.l, x2.l, y2.l, x3.l, y3.l)
Triangle::delete(*triangle.Triangle)
Triangle::draw(*triangle.Triangle)
Triangle::fromRandom(min.l, max.l)


Last edited by Mistrel on Thu Oct 03, 2019 2:40 am, edited 1 time in total.

Top
 Profile  
Reply with quote  
 Post subject: Re: PureBasic modules step-by-step by example (triangles!)
PostPosted: Wed Oct 02, 2019 5:45 am 
Offline
Enthusiast
Enthusiast

Joined: Mon Nov 25, 2013 6:41 am
Posts: 794
It only worked for me.
Code:
;-- Triangle
DeclareModule Triangle
  Structure Triangle
    x1.l
    y1.l
    x2.l
    y2.l
    x3.l
    y3.l
  EndStructure
 
  Declare new(x1.l, y1.l, x2.l, y2.l, x3.l, y3.l)
  Declare delete(*triangle.Triangle)
  Declare draw(*triangle.Triangle)
  Declare fromRandom(min.l, max.l)
EndDeclareModule

Module Triangle
  Procedure new(x1.l, y1.l, x2.l, y2.l, x3.l, y3.l)
    Protected *triangle.Triangle=AllocateStructure(Triangle)
   
    *triangle\x1=x1
    *triangle\y1=y1
    *triangle\x2=x2
    *triangle\y2=y2
    *triangle\x3=x3
    *triangle\y3=y3
   
    ProcedureReturn *triangle
  EndProcedure
 
  Procedure delete(*triangle.Triangle)
    FreeStructure(*triangle)
  EndProcedure
 
  Procedure draw(*triangle.Triangle)
    LineXY(*triangle\x1,*triangle\y1,*triangle\x2,*triangle\y2)
    LineXY(*triangle\x2,*triangle\y2,*triangle\x3,*triangle\y3)
    LineXY(*triangle\x3,*triangle\y3,*triangle\x1,*triangle\y1)
  EndProcedure
 
  Procedure fromRandom(min.l, max.l)
    ProcedureReturn new(Random(max,min),Random(max,min),Random(max,min),Random(max,min),Random(max,min),Random(max,min))
  EndProcedure
EndModule

;--
 Global image=CreateImage(#PB_Any,132,103,32,#PB_Image_Transparent)

Procedure drawRandomTriangle(*oldTriangle, imageGadget)
  Protected *triangle.Triangle::Triangle
 
  Triangle::delete(*oldTriangle)
  *triangle=Triangle::fromRandom(10,92)
 
  ;DisableDebugger
  FreeImage(image)
  image = CreateImage(#PB_Any,132,103,32,#PB_Image_Transparent)
  ;EnableDebugger
 
  StartDrawing(ImageOutput(image))
  DrawingMode(#PB_2DDrawing_AllChannels)
  FrontColor(RGBA(0,0,0,255))
  Triangle::draw(*triangle)
  StopDrawing()
 
    SetGadgetState(imageGadget,ImageID(image))
 
  ProcedureReturn *triangle
EndProcedure

Define window.i
Define imageGadget.i
Define buttonGadget.i
Define event.i

window=OpenWindow(#PB_Any,0,0,132,136,"",#PB_Window_SystemMenu|#PB_Window_ScreenCentered)
image=CreateImage(#PB_Any,132,103,32,#PB_Image_Transparent)
imageGadget=ImageGadget(#PB_Any,0,0,132,103,ImageID(image))
buttonGadget=ButtonGadget(#PB_Any,5,109,122,23,"Random")

Define *triangle.Triangle::Triangle

*triangle=Triangle::new(65,7, 113,91, 17,91)

StartDrawing(ImageOutput(image))
DrawingMode(#PB_2DDrawing_AllChannels)
FrontColor(RGBA(0,0,0,255))
Triangle::draw(*triangle)
StopDrawing()

SetGadgetState(imageGadget,ImageID(image))

Repeat
  event=WaitWindowEvent()
 
  If event=#PB_Event_Gadget And EventGadget()=buttonGadget
    *triangle = drawRandomTriangle(*triangle,imageGadget)
  EndIf
Until event=#PB_Event_CloseWindow


Top
 Profile  
Reply with quote  
 Post subject: Re: PureBasic modules step-by-step by example (triangles!)
PostPosted: Wed Oct 02, 2019 6:19 am 
Offline
Addict
Addict

Joined: Sat Jun 30, 2007 8:04 pm
Posts: 3362
mestnyi wrote:
It only worked for me.


Some of the code samples were iterative and not meant to be copy/pasted as complete. The first, second-to-last, and last can be compiled as such but not the ones between.


Top
 Profile  
Reply with quote  
 Post subject: Re: PureBasic modules step-by-step by example (triangles!)
PostPosted: Wed Oct 02, 2019 8:29 am 
Offline
Addict
Addict

Joined: Thu Aug 30, 2007 11:54 pm
Posts: 1160
Location: right here
But mestnyi is right, you shouldn't reuse a PB_ANY-generated id as a static one. (IMA if the button is clicked a second time)


Top
 Profile  
Reply with quote  
 Post subject: Re: PureBasic modules step-by-step by example (triangles!)
PostPosted: Wed Oct 02, 2019 8:33 am 
Offline
Addict
Addict

Joined: Sat Jun 30, 2007 8:04 pm
Posts: 3362
mestnyi, which platform did it fail on? It worked for me on Windows.


Top
 Profile  
Reply with quote  
 Post subject: Re: PureBasic modules step-by-step by example (triangles!)
PostPosted: Wed Oct 02, 2019 1:43 pm 
Offline
Enthusiast
Enthusiast

Joined: Mon Nov 25, 2013 6:41 am
Posts: 794
Mistrel wrote:
mestnyi, which platform did it fail on? It worked for me on Windows.

on mac os x


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 13 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye