Files
BlitzExtensions/BlitzPointer/Blitz/Example05.bb
T

58 lines
1.7 KiB
BlitzBasic
Raw Normal View History

2015-05-11 23:10:15 +02:00
; ---------------------------------------------------------------------------- ;
2015-05-17 11:49:46 +02:00
; Example 5 - Calling Functions (Advanced)
2015-05-11 23:10:15 +02:00
; ---------------------------------------------------------------------------- ;
; License: Creative Commons Attribution 2.0
; Author: Michael Fabian Dirks<michael.dirks@realitybends.de>
; Prerequisite: Example 4
2015-05-17 11:49:46 +02:00
; In Blitz, there are only three real types: Int, Float and String - anything
; else is a pointer that can be represented using Int. That is why almost all
; Blitz functions return an Int.
; Using this knowledge we can try (and succeed) passing a Type Object into a
; function - using nothing more than the Int() function.
2015-05-11 23:10:15 +02:00
Include "Example_Shared.bb"
ExampleInit()
2015-05-17 11:49:46 +02:00
; We'll have to define a Type that can be passed to our function, any Type will
; do fine.
Type MyType
Field Test
Field Name$
2015-05-11 23:10:15 +02:00
End Type
2015-05-17 11:49:46 +02:00
; The function can be defined like any other Blitz function.
Global fpMyTypeFunc = 0
Function MyTypeFunc(This.MyType)
If fpMyTypeFunc = 0 Then
fpMyTypeFunc = BlitzPointer_GetFunctionPointer()
2015-05-11 23:10:15 +02:00
Return
EndIf
2015-05-17 11:49:46 +02:00
If This = Null Then Return
2015-05-11 23:10:15 +02:00
2015-05-17 11:49:46 +02:00
; Display the content of the Type Object.
Text 0, Handle(This) * 15, This\Test + This\Name
2015-05-11 23:10:15 +02:00
End Function
2015-05-17 11:49:46 +02:00
MyTypeFunc(Null)
2015-05-11 23:10:15 +02:00
2015-05-17 11:49:46 +02:00
; And create two testing objects.
Local MT1.MyType = New MyType
MT1\Test = 1
MT1\Name = "First Object"
2015-05-11 23:10:15 +02:00
2015-05-17 11:49:46 +02:00
Local MT2.MyType = New MyType
MT2\Name = "Second Object"
2015-05-11 23:10:15 +02:00
While Not KeyHit(1)
2015-05-17 11:49:46 +02:00
ExampleUpdate()
2015-05-11 23:10:15 +02:00
2015-05-17 11:49:46 +02:00
; Now in order to pass a Type Object to a function, we have to get a pointer
; from it. Thankfully, Blitz has this already built in: Int().
If KeyDown(2) Then BlitzPointer_CallFunctionVI fpMyTypeFunc, Int(MT1)
If KeyDown(3) Then BlitzPointer_CallFunctionVI fpMyTypeFunc, Int(MT2)
2015-05-11 23:10:15 +02:00
ExampleLoop()
Wend
2015-05-17 11:49:46 +02:00
End
2015-05-11 23:10:15 +02:00
;~IDEal Editor Parameters:
;~C#Blitz3D