Interfaces
DescriptionInterface <name> [Extends <name>] ... EndInterface
Interfaces are used to access Object Oriented modules, like COM (Component Object Model) or DirectX dynamic libraries (DLL). These kind of libraries are more and more common on Windows and the interfaces fill the gap to access it easily without any performance hit. It also put the basis for an Object Oriented programming in PureBasic, but requires some advanced knowledges to do it. Most of the standard Windows interfaces are already implemented in a resident file which allow a direct use of these objects.
The optionnal Extends parameter allows to extends another interface with new functions (Theses functions are commonly called 'methods' in Object Orientied (OO) languages like C++ or Java). All functions found in the extended interface will be available in the new interface and will be placed before the new functions. This is useful to do basic inheritance of objects.
SizeOf can be used with Interfaces to get the size of the interface and OffsetOf can be used to retrieve the index of the specified function.
The pseudotypes can be used for the functions parameters, but not for the returned value.
Note: The objects concepts and uses are mainly targeted for experienced programmers and it's not need to understand or even use them to create a professionnal software or game.Example: Basic Example of object call
; 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()Example: Example with 'Extends'
; Define a basic Cube interface object ; Interface Cube GetPosition() SetPosition(x) GetWidth() SetWidth(Width) EndInterface Interface ColoredCube Extends Cube GetColor() SetColor(Color) EndInterface Interface TexturedCube Extends Cube GetTexture() SetTexture(TextureID) EndInterface ; We now have 3 differents objects interfaces: ; ; - 'Cube' which has Get/SetPosition() and Get/SetWidth() functions ; - 'ColoredCube' which has Get/SetPosition(), Get/SetWidth() and Get/SetColor() functions ; - 'TexturedCube' which has Get/SetPosition(), Get/SetWidth() and Get/SetTexture() functions ;Example: Creating its own object in PureBasic (see advanced examples)