Arrays in UDTs
PureGDK uses "structures", an advanced form of DarkBasic Professional's UDTs. Arrays in structures are natively supported by PureBasic. You can even have an structure array of arrays of structures!
Structure Family
Father.s
Mother.s
Children.s[2]
Surname.s
EndStructure
Define Family.Family
Family\Father = "Peter"
Family\Mother = "Sarah"
Family\Children[0] = "John"
Family\Children[1] = "Jane"
Family\Surname = "Smith"
Return a UDT from a function
PureBasic supports passing structures by address. This allows a single procedure to return multiple values and even entire structures!
;/ Pass a structure variable by address and output the result
Structure Struct
a.l
b.l
EndStructure
Define Var.Struct
Procedure ReturnStruct(*InputOutput.Struct)
*InputOutput\a=2
*InputOutput\b=4
EndProcedure
;/ Call the procedure
ReturnStruct(@Var.Struct)
;/ Output the new values
Debug Var\a
Debug Var\b
Global variable declarations no longer have to be 'executed' for initialization to happen
Compiler macros in PureBasic can be used to handle situations where globals and other variables have not yet been declared. For example, you can do an "If not yet declared then do this".
;/ Example using an if-defined compiler function
CompilerIf Defined(Var,#PB_Variable)
CompilerElse
Debug "Not yet defined!"
CompilerEndIf
Global Var
The ability to pass UDT data from an array to a function
PureBasic supports arrays as function parameters. You can also pass other data structures like lists and hash maps too!
;/ Pass a structure array to a procedure
Structure Struct
a.l
b.l
EndStructure
Dim Var.Struct(20)
;/ Fill structure array with random values
For i=0 To 19
Var(i)\a=Random(255)
Var(i)\b=Random(255)
Next i
Procedure DisplayStruct(Array InputArray.Struct(1), ArraySize)
For i=0 To ArraySize-1
Debug InputArray(i)\a
Debug InputArray(i)\b
Next i
EndProcedure
;/ Call the procedure
DisplayStruct(Var(),20)
Distinct "ElseIf" statement
The "ElseIf" keyword is natively supported by PureBasic.
Define N=2
If N=1
Debug 1
ElseIf N=2
Debug 2
Else
Debug 3
EndIf
Ability to do complex statements on 'ExitFunction'/'EndFunction'
PureBasic can return the value of complex expressions from functions with ease.
Procedure.f DegreeToRadian(Value.f)
ProcedureReturn Value.f*(#PI/180.0)
EndProcedure
Debug DegreeToRadian(20)
Optional parameters
Why write three functions when you only need one? What about default parameters? PureBasic can do all of this.
Procedure Example(a.l, b.l=0, c.l=0)
If a
;/ Do something
EndIf
If b
;/ Do something
EndIf
If c
;/ Do something
EndIf
EndProcedure
;/ Call each instance of the procedure in this example
Example(1)
Example(1,2)
Example(1,2,3)
Option explicit mode
Enable or disable the explicit mode with a single command. When enabled, all the variables which are not explicitly declared with Define, Global, Protected or Static are not accepted and the compiler will raise an error. This can be extremely useful for preventing typo bugs.
EnableExplicit
Define Var1
;/ Ok!
Var1=2
;/ Error!
Var2=3
Vectors and matrices are basic types
Never have to use a vector or matrix "ID" again! PureGDK supports vector and matrix datatypes as structures. This means that you can access any part of this data and they can be written to and read from disk.
;/ Declare variables with pre-defined structures
Define Vector2a.Vector2
Define Vector2b.Vector2
Define Vector2Result.Vector2
;/ Set vector data
Vector2a\x=3: Vector2b\x=7
Vector2a\y=5: Vector2b\y=5
;/ Add vectors together
dbAddVector2(@Vector2Result, @Vector2a, @Vector2b)
;/ Output results
Debug Vector2Result\x ; 3 + 7 = 10
Debug Vector2Result\y ; 5 + 5 = 10
Methods in UDTs (Advanced)
PureBasic supports storing methods within interfaces similar to classes in object-oriented programming.
;/ Now we will consider you want to access an external object
;/ (in a DLL for example) in PureBasic. First you need to declare its interface:
Interface MyObject
Move(x, y)
MoveF(x.f, y.f)
Destroy()
EndInterface
;/ CreateObject is the function which create your object, from the DLL.
;/ Create the first object..
Object1.MyObject=MyCreateObject()
;/ And the second one.
Object2.MyObject=MyCreateObject()
;/ Then just use the functions to act on the desired object
Object1\Move(10, 20)
Object1\Destroy()
Object2\MoveF(10.5, 20.1)
Object2\Destroy()
Pass by address/pass by reference
In PureBasic all arrays, lists, and hashmaps are passed by reference. Everything else, like in C, can be passed by address to achieve the same effect.
Inheriting Fields From Another Structure
Structures can also inherit fields from another structure by using the optional "Extends" keyword.
Structure PersonalDetails
FirstName.s
LastName.s
Home.s
EndStructure
Structure FullDetails Extends PersonalDetails
Address.s
Country.s
ZipCode.s
EndStructure
Define User.FullDetails
User\FirstName = "John"
User\LastName = "Smith"
User\Home = "A House"
User\Address = "A Street"
User\Country = "UK"
User\ZipCode = "12345"
Structure Unions
Structure unions are a way of conserving memory by forcing groups of variables within a structure to share the same memory address.
Structure UNIONSTRUCTURE
StructureUnion
One.l
Two.l
Three.l
EndStructureUnion
EndStructure
Linked Lists and Hash Maps
Linked Lists are similar to arrays in that they are able to refer to lots of data using one name. They are however different to arrays in that they don’t use an index to assign and retrieve data.
NewList Fruit.s()
AddElement(Fruit())
Fruit() = "Banana"
AddElement(Fruit())
Fruit() = "Apple"
Sorting Arrays And Linked Lists
Arrays and linked lists are great for storing all sorts of data and these data structures can easily be traversed to quickly retrieve that data. Sometimes though you may need to reorganize the data contained within an array or linked list, so it is sorted alphabetically or numerically.
Dim Fruit.s(3)
Fruit(0) = "Banana"
Fruit(1) = "Apple"
Fruit(2) = "Pear"
Fruit(3) = "Orange"
SortArray(Fruit(), 0)
Sorting structured arrays and linked lists
This is slightly more complicated as it uses a slightly more complicated sort command; "SortStructuredArray()".
Structure Weapon
Name.s
Range.l
EndStructure
Dim Weapons.Weapon(2)
Weapons(0)\Name = "Phased Plasma Rifle"
Weapons(0)\Range = 40
Weapons(1)\Name = "SVD-Dragunov Sniper Rifle"
Weapons(1)\Range = 3800
Weapons(2)\Name = "HK-MP5 Sub-Machine Gun"
Weapons(2)\Range = 300
SortStructuredArray(Weapons(), 0, OffsetOf(WEAPON\Range), #PB_Sort_Long)
For x.l = 0 To 2
Debug Weapons(x)\Name + " : " + Str(Weapons(x)\Range)
Next x
Visual Designer
The PureBasic Visual Designer (designer for graphical user interfaces) has been created for the PureBasic programming language and is of course developed in PureBasic. It will become more and more powerful and will support advanced features like dialogs, gadget editing from PureBasic source file etc.
Custom Gadgets
With PureBasic you're not limited to only the gadgets that come with the software. With all of the powerful tools at your disposal you can even create your own!
Native support for the Win32 API
With native support for the Windows Win32 API making advanced operating system calls has never been easier! Consider this example which lists all resolutions supported by the user's monitor that is of the same frequency and aspect ratio:
;/ Initialize PureBasic desktop library
ExamineDesktops()
;/ Iterate through supported display settings and display only select criteria
While EnumDisplaySettings_(0,i,@DevMode.DEVMODE)
If DesktopWidth(0)*1.0/DesktopHeight(0)=DevMode\dmPelsWidth*1.0/DevMode\dmPelsHeight
Debug Str(DevMode\dmPelsWidth)+" "+Str(DevMode\dmPelsHeight)+" "+Str(DevMode\dmDisplayFrequency)
EndIf
i+1
Wend