News:

Building a 3D Ray Tracer  By stevmjon

Main Menu

PlayBASICFX / PBFX Vm2 Translation (WIP)

Started by kevin, September 08, 2008, 12:13:35 PM

Previous topic - Next topic

kevin


PBFX V1.76 Beta #7 - Passing Individual UDT's into functions

     Still working on moving the new functionality, namely passing individual  UDT buffers into functions.. 
 




Type Vector2D
x#,y#
EndType



; container
Dim MyArray(10) as Vector2D

; alloc an instance
MyArray(10) = new Vector2d

; init the fioelds in this instance
MyArray(10).x=100
MyArray(10).y=200


TestUDT_1(MyArray(10))

sync
Waitkey



; Pass single bank into function
Function TestUDT_1(Me as Vector2d)
print me.x
print me.y
EndFunction



kevin


PBFX V1.76 Beta #8 - Optional Parameters in User defined functions & Psubs

   Took a bit of messing around, but this seems to be working now..

   


Function TestOptional_Void()
EndFunction


Function FunctionTestOptional_1(a=45.456,b=123)
print "--------------"
print str$(a)+" "+str$(b)
EndFunction

Function FunctionTestOptional_2(a#=45.456,b#=123.567)
print "--------------"
print str$(a#)+" "+str$(b#)
EndFunction

Function FunctionTestOptional_3(a$="Hello",b$="World")
print "--------------"
print a$+"  "+b$
EndFunction


; optional integers
print "Optional Integers"
FunctionTestOptional_1(10,20)
FunctionTestOptional_1(10)
FunctionTestOptional_1()

print "Optional Floats"
FunctionTestOptional_2(10,20)
FunctionTestOptional_2(10)
FunctionTestOptional_2()

print "Optional Strings"
FunctionTestOptional_3("Dude","Wobble")
FunctionTestOptional_3("Dude")
FunctionTestOptional_3()





Psub PsubTestOptional_1(a=45,b=123)
print "--------------"
print str$(a)+", "+str$(b)
EndPsub

Psub PsubTestOptional_2(a#=45.456,b#=123.567)
print "--------------"
print str$(a#)+", "+str$(b#)
EndPsub

Psub PsubTestOptional_3(a$="Hello",b$="World")
print a$+" "+b$
EndPsub


; optional integers
print "Optional Integers"
PsubTestOptional_1(10,20)
PsubTestOptional_1(10)
PsubTestOptional_1()

print "Optional Floats"
PsubTestOptional_2(10,20)
PsubTestOptional_2(10)
PsubTestOptional_2()

print "Optional Strings"
PsubTestOptional_3("Dude","Wobble")
PsubTestOptional_3("Dude")
PsubTestOptional_3()


Sync
waitkey



kevin


PBFX V1.76 Beta #9 - Exporting Individual UDT's into functions

     Working on the exporting structures from functions.. The first is returning UDT buffers, so you can passing the buffer in/out.   Not to be confusing with Array passing !




Type Vector2D
x#,y#
EndType


Dim Points(10) as vector2D

For lp=1 to 10
Points(lp)=NewPoint(222+lp,333+lp*10)
print str$(Points(lp))+"  "+Str$(Points(lp).x)+"  "+Str$(Points(lp).y)

next

Sync
waitkey
waitnokey



Function NewPoint(x#,y#)
Me = new Vector2D
Me.x = x#
Me.y = y#
EndFunction  Me as Vector2D







kevin


PBFX V1.76 Beta #9 - Array Assignments

     Got the first part of array assignments working in VM2... 




Dim This(1)
Dim That(100)

For lp=1 to 100
That(lp)=lp+1000
next

    ; Make's a copy of THAT() array and stores it in THIS()
This()=That()

Sync
Waitkey



kevin

#64
 PBFX V1.76 Beta #9b - Return Arrays From Functions

 
   Just got this working, so far in only supports Integer arrays, but the frame work is in place to return all array types.  Just need to test them..


   Integer Arrays



Dim This(1)

; create new array and assign it to the THIS() array
This()= NewArray(20,1000)

; display it's contents
For lp=1 to GetArrayElements(This())
print This(lp)
next

Sync
Waitkey


Function NewArray(Size,RandomRange)
Dim Me(Size)
For lp=0 to Size
me(Lp)=rnd(RandomRange)
next
EndFunction Me() as integer








   Float Arrays




Dim This#(1)

; create new array and assign it to the THIS() array
This#()= NewArray(20,1000)

; display it's contents
For lp=1 to GetArrayElements(This#())
print This#(lp)
next

Sync
Waitkey


Function NewArray(Size,RandomRange)
Dim Me#(Size)
For lp=0 to Size
me#(Lp)=rnd(RandomRange)
next
EndFunction Me() as float





   String Arrays



name$="Bill,Kev,Test,Dude"

Dim This$(1)

; create new array and assign it to the THIS() array
This$()= NewArray(20,name$)

; display it's contents
For lp=1 to GetArrayElements(This$())
print This$(lp)
next

Sync
Waitkey


Function NewArray(Size,Name$)
Dim NamesList$(100)
Count=SplitToArray(Name$,",",NamesLIst$(),0)

Dim Me$(Size)
For lp=0 to Size
me$(Lp)=NamesList$(rnd(Count))
next
EndFunction Me() as string




   Typed Arrays




Type Vector2D
X#,y#
EndType


Type Stuff
name$
pos as vector2d
speed as vector2d
EndType


Dim This(0) as Stuff



; create new array and assign it to the THIS() array
name$="Bill,Dude,PLayBasic,PBFX"

This()= NewArray(20,name$)

; display it's contents
For lp=1 to GetArrayElements(This())
print This(lp)
print This(lp).Name$
print  This(lp).pos.x
print  This(lp).pos.y
next

Sync
Waitkey


Function NewArray(Size,Name$)
Dim NamesList$(100)
Count=SplitToArray(Name$,",",NamesLIst$(),0)

Dim Me(Size) as stuff
For lp=0 to Size
me(Lp).name=NamesList$(rnd(Count))
me(Lp).pos.x=100+lp
me(Lp).pos.y=200+lp
next
EndFunction Me() as stuff





   Typed Variable

   Typed variables are basically typed arrays with 1 element.  So you can mix and match assignments, don't confuse this with passing a UDT element..






Type Vector2D
X#,y#
EndType


Type Stuff
name$
pos as vector2d
speed as vector2d
EndType


Dim This as Stuff

; create new typed VARIABLE (array) and assign it to the THIS() array
name$="Bill,Dude,PLayBasic,PBFX"

This()= NewArray(20,name$)

; display it's contents
print This
print This.Name$
print  This.pos.x
print  This.pos.y

Sync
Waitkey


Function NewArray(Size,Name$)
Dim NamesList$(100)
Count=SplitToArray(Name$,",",NamesLIst$(),0)

Dim Me as stuff
me.name=NamesList$(rnd(Count-1))
me.pos.x=100+lp
me.pos.y=200+lp

EndFunction Me.stuff





     This version returns a Typed Variable, back into a Type Array.  Here we can see that it is in fact a 1d array with 1 element..




Type Vector2D
X#,y#
EndType


Type Stuff
name$
pos as vector2d
speed as vector2d
EndType


Dim This(10) as Stuff

; create new typed VARIABLE (array) and assign it to the THIS() array
name$="Bill,Dude,PLayBasic,PBFX"

This()= NewArray(20,name$)

; display it's contents
for lp=0 to GetArrayElements(This())
print This(lp)
print This(lp).Name$
print  This(lp).pos.x
print  This(lp).pos.y
next

Sync
Waitkey


Function NewArray(Size,Name$)
Dim NamesList$(100)
Count=SplitToArray(Name$,",",NamesLIst$(),0)

Dim Me as stuff
me.name=NamesList$(rnd(Count-1))
me.pos.x=100+lp
me.pos.y=200+lp

EndFunction Me.stuff





kevin

#65
 PBFX V1.76 Beta #9b - Return Arrays From PSubs

 
   Testing the array exporting to work with PSUBS.


   Integer Arrays




Dim This(1)

; create new array and assign it to the THIS() array
This()= NewArray(20,1000)

; display it's contents
For lp=1 to GetArrayElements(This())
print This(lp)
next

Sync
Waitkey


Psub NewArray(Size,RandomRange)
Dim Me(Size)
For lp=0 to Size
me(Lp)=rnd(RandomRange)
next
EndPsub Me() as integer






 Float Arrays



Dim This#(1)

; create new array and assign it to the THIS() array
This#()= NewArray(20,1000)

; display it's contents
For lp=1 to GetArrayElements(This#())
print This#(lp)
next

Sync
Waitkey


Psub NewArray(Size,RandomRange)
Dim Me#(Size)
For lp=0 to Size
me#(Lp)=rnd#(RandomRange)
next
EndPsub Me() as float





String Arrays





name$="Bill,Kev,Test,Dude"

Dim This$(1)

; create new array and assign it to the THIS() array
This$()= NewArray(20,name$)

; display it's contents
For lp=1 to GetArrayElements(This$())
print This$(lp)
next

Sync
Waitkey

Psub NewArray(Size,Name$)
Dim NamesList$(100)
Count=SplitToArray(Name$,",",NamesLIst$(),0)

Dim Me$(Size)
For lp=0 to Size
me$(Lp)=NamesList$(rnd(Count-1))
next
EndPsub Me() as string





    Typed Arrays




Type Vector2D
X#,y#
EndType


Type Stuff
name$
pos as vector2d
speed as vector2d
EndType


Dim This(0) as Stuff



; create new array and assign it to the THIS() array
name$="Bill,Dude,PLayBasic,PBFX"

This()= NewArray(20,name$)

; display it's contents
For lp=1 to GetArrayElements(This())
print This(lp)
print This(lp).Name$
print  This(lp).pos.x
print  This(lp).pos.y
next

Sync
Waitkey


Psub NewArray(Size,Name$)
Dim NamesList$(100)
Count=SplitToArray(Name$,",",NamesLIst$(),0)

Dim Me(Size) as stuff
For lp=0 to Size
me(Lp).name=NamesList$(rnd(Count-1))
me(Lp).pos.x=100+lp
me(Lp).pos.y=200+lp
next
EndPsub Me() as stuff






 Typed Variables






Type Vector2D
X#,y#
EndType


Type Stuff
name$
pos as vector2d
speed as vector2d
EndType


Dim This(0) as Stuff



; create new array and assign it to the THIS() array
name$="Bill,Dude,PLayBasic,PBFX"

This()= NewArray(20,name$)

; display it's contents
For lp=1 to GetArrayElements(This())
print This(lp)
print This(lp).Name$
print  This(lp).pos.x
print  This(lp).pos.y
next

Sync
Waitkey


Psub NewArray(Size,Name$)
Dim NamesList$(100)
Count=SplitToArray(Name$,",",NamesLIst$(),0)

Dim Me(Size) as stuff
For lp=0 to Size
me(Lp).name=NamesList$(rnd(Count-1))
me(Lp).pos.x=100+lp
me(Lp).pos.y=200+lp
next
EndPsub Me() as stuff













Type Vector2D
X#,y#
EndType


Type Stuff
name$
pos as vector2d
speed as vector2d
EndType


Dim This(10) as Stuff

; create new typed VARIABLE (array) and assign it to the THIS() array
name$="Bill,Dude,PLayBasic,PBFX"

This()= NewArray(20,name$)

; display it's contents
for lp=0 to GetArrayElements(This())
print This(lp)
print This(lp).Name$
print  This(lp).pos.x
print  This(lp).pos.y
next

Sync
Waitkey


Function NewArray(Size,Name$)
Dim NamesList$(100)
Count=SplitToArray(Name$,",",NamesLIst$(),0)

Dim Me as stuff
me.name=NamesList$(rnd(Count-1))
me.pos.x=100+lp
me.pos.y=200+lp

EndFunction Me.stuff




kevin


PBFX V1.76 Beta #10 - Returning Typed Lists Functions/PSubs

 



Type Vector2D
X#,y#
EndType


Type Stuff
name$
pos as vector2d
speed as vector2d
EndType


Dim This as Stuff list

; create new array and assign it to the THIS() array
name$="Bill,Dude,PLayBasic,PBFX"

This()= NewArray(20,name$)

; display it's contents
For each This()
print This
print This.Name$
print  This.pos.x
print  This.pos.y
next

Sync
Waitkey


Function NewArray(Size,Name$)
Dim NamesList$(100)
Count=SplitToArray(Name$,",",NamesLIst$(),0)

Dim Me as Stuff List
For lp=0 to Size
me = new Stuff
me.name=NamesList$(rnd(Count-1))
me.pos.x=100+lp
me.pos.y=200+lp
next
EndFunction Me as stuff list



kevin


PBFX V1.76 Beta #12 - FunctionExist +FunctionIndex

  Been working towards getting the dynamic function calling working, first I need to set up the helper commands/functions.. 




print FunctionExist("COoL")
; print FunctionExist("Cool2")


createimage 1,100,100
print GetImageWidth(1)


x#=mod(x#,iw)
; x#=mod(x#-1.2,GetImageWidth(1))

Sync
waitkey


Function Cool()


EndFunction





    While the first few bits are working, after a few modifications, it would appear that those mod's make other functions misbehave..



kevin


PBFX V1.76 Beta #12b - FunctionExist +FunctionIndex

   These seem to be working now..



print FunctionExist("COoL")
print FunctionIndex("Cool")

print FunctionExist("SomeUnknownFunction")
print FunctionIndex("SomeUnknownFunction")

Sync
waitkey

Function BeforeCool()
EndFunction

Function Cool()
EndFunction

Function AfterCool()
EndFunction




   The next and final port of call will the implementing the CallFunction operation.




kevin


PBFX V1.76 Beta #12c - CallFunction


     After a bit a wrestling Call Function command springs to life.    It's not fully implemented as yet, currently it only supports  calling 'User Defined Functions' -  The Vm2 implementation works a bit differently, but has the same limitations as before. Namely while you can call a function by Name or by index,  you can't call a function an retrieve data back from it.   


     See Example #1

kevin

#70
 CallFunction Performance (PB vs PBFX)

    With the addition of the CallFunction operation, I wanted to compare the performance between  PB/Vm1 and PBFX/VM2.    While VM2 is faster than VM1,  we shouldn't simply assume it's everything is faster, just because it's running on it.  They're very different beasts under the hood, so it's not that unusual for things to actually run a bit slower in VM2.

    Now interestingly it just so happens that the CallFunction  operations was indeed slower.  About twice as slow actually in PBFXV1.76 beta12, than in PBV1.64l beta2.   I had a feeling it would be,  in particular when calling a function by name.   For two reasons, the searching routine was a linear search and secondly because PBFX has a lot more bound functions to search through.   So it just has more work to do.  

    So after a bit jiggling with the searcher the standard test (code bellow) in PBFX V1.76 beta 13 code runs this about 40% faster than PB V1.64.   In fact both methods are faster, although I doubt calling the function by index is much faster in VM2 than it is Vm1,  the test is quicker because Vm2 can just run the entire loop and function call quicker.
 



  Print "Dynamic Function Calling"
Dim FunctionNames$(3)
Dim FunctionIndexes(3)

FunctionNames$(0)="Test0"
FunctionNames$(1)="Test1"
FunctionNames$(2)="Test2"
FunctionNames$(3)="Test3"


FunctionIndexes(0)=FunctionIndex("Test0")
FunctionIndexes(1)=FunctionIndex("Test1")
FunctionIndexes(2)=FunctionIndex("Test2")
FunctionIndexes(3)=FunctionIndex("Test3")

MaxTests=10000

Do
Cls 0

frames++

t=timer()
for lp=0 to MaxTests
  CallFunction FunctionNames$(lp &3)
  next
tt1#+=(timer()-t)
print tt1#/frames
print "tests:"+STR$(lp)
 
t=timer()
for lp=0 to MaxTests
  CallFunction FunctionIndexes(lp &3)
  next
tt2#+=(timer()-t)
print tt2#/frames
print "tests:"+STR$(lp)


Sync
loop




Function Test0()
EndFunction

Function Test1()
EndFunction

Function Test2()
EndFunction

Function Test3()
EndFunction






kevin

#71
  PBFX V1.76 Beta #13 - Psub support CallFunction

  As you can see Vm2 version can now call PSUBS.   The Vm2 version has an extra feature when calling Psubs, which is the ability to recast  integers/floats to strings and vice versa.  

PlayBASIC Code: [Select]
   Test1(100,200)

; Passing in Integers
CallFunction "Test1",111,222

; Passing in floats
CallFunction "Test1",111.99,222.99

; Passing in strings. These get converted to the parameters type.
CallFunction "Test1","111.99","222.99"

ink $00ff00


Test2(100,200)

; Passing in Integers
CallFunction "Test2",111,222

; Passing in floats
CallFunction "Test2",111.99,222.99

; Passing in strings. These get converted to the parameters type.
CallFunction "Test2","111.99","222.99"


ink $ff0000

Test3("100","200")

; Passing in Integers
CallFunction "Test3",55,662

; Passing in floats
CallFunction "Test3",111.99,222.99

; Passing in strings. These get converted to the parameters type.
CallFunction "Test3","$ff","%1111"


Sync
waitkey



Psub Test1(a,b)
print "--------------"
print a
print b
EndPsub

Psub Test2(a#,b#)
print "--------------"
print a#
print b#
EndPsub

Psub Test3(a$,b$)
print "--------------"
print a$
print b$
EndPsub







Bench mark


PlayBASIC Code: [Select]
   Print "Dynamic Psub Calling With Parameter"

Dim FunctionNames$(3)
Dim FunctionIndexes(3)

FunctionNames$(0)="Test0"
FunctionNames$(1)="Test1"
FunctionNames$(2)="Test2"
FunctionNames$(3)="Test3"

FunctionIndexes(0)=FunctionIndex("Test0")
FunctionIndexes(1)=FunctionIndex("Test1")
FunctionIndexes(2)=FunctionIndex("Test2")
FunctionIndexes(3)=FunctionIndex("Test3")

MaxTests=10000

Do
Cls 0

frames++

t=timer()
for lp=0 to MaxTests
CallFunction FunctionNames$(lp &3),lp
next
tt1#+=(timer()-t)
print tt1#/frames
print "tests:"+STR$(lp)

t=timer()
for lp=0 to MaxTests
CallFunction FunctionIndexes(lp &3),lp
next
tt2#+=(timer()-t)
print tt2#/frames
print "tests:"+STR$(lp)


Sync
loop




Psub Test0(a)
EndPsub

Psub Test1(a)
EndPsub

Psub Test2(a)
EndPsub

Psub Test3(a)
EndPsub




kevin

#72
 PBFX V1.76 Beta #13 - Rounding It Off

    With this revision we're basically at that point where FX has caught up all the compiler changes that found in  PB1.64k.    


ExitFunction

  Now supports returning arrays

PlayBASIC Code: [Select]
   Dim Array(1)
Dim Array#(1)
Dim Array$(1)

Type Pos
x,y,z
EndType

; Yeah=1
Global State=true

for State=0 to 1
print "Test:State="+str$(State)
print Stuff_int()
print Stuff_int2()
print Stuff_float()
print Stuff_float2()
print Stuff_Str()
print Stuff_Str2()
next

Sync
waitkey
waitnokey

cls 0
for State=0 to 1
print "Test:State="+str$(State)
Array()=Stuff_intarray()
print GetArrayElements(Array())
Array$()=Stuff_Strarray()
print GetArrayElements(Array$())
#break
next

Sync
waitkey
waitnokey


Function Stuff_INtArray()
if State
Dim TestArray(100)
exitfunction TestArray()
else
Dim TestArray(200)
endif
EndFunction TestArray()



Function Stuff_FltArray()
if State
Dim TestArray#(100)
exitfunction TestArray#()
else
Dim TestArray#(200)
endif
EndFunction TestArray() as float



Function Stuff_StrArray()
if State
Dim TestArray$(102)
exitfunction TestArray$()
else
Dim TestArray$(202)
endif
EndFunction TestArray() as string




Function Stuff_INt()
a=1
Yeah=2
if State
exitfunction a
endif
EndFunction Yeah



Function Stuff_INt2()
a=1
Yeah=2
if State
exitfunction a
endif
EndFunction Yeah as integer




Function Stuff_Float()
a#=1
Yeah#=2
if State
exitfunction a#
endif
EndFunction Yeah#

Function Stuff_Float2()
a#=1
Yeah#=2
if State
exitfunction a#
endif
EndFunction Yeah as float


Function Stuff_Str()
a$="1$"
Yeah$="2$"
if State
exitfunction a$
endif
EndFunction Yeah$


Function Stuff_Str2()
a$="1$"
Yeah$="2$"
if State
exitfunction a$
endif
EndFunction Yeah as string







Vsync in Windowed modes

  This allows the window to vsync'd, bring it in line with full screen modes.    

PlayBASIC Code: [Select]
   img= MakeGridPatternImg(32,4,32,4,10,rgb(120,130,140),rgb(150,150,150),rgb(210,200,200))


//

StartTime=Timer()
Do

TileIMage img,Xpos,0,false

TimePast=Timer()-StartTime

Xpos-=1

if spacekey()
ScreenVsync 1-getScreenVsync()
flushkeys()
endif

Text 0,0,"Vsync:"+Str$(GetScreenVsync())

Sync
loop



Function MakeGridPatternImg(GridWidth,GridWidthStepX,GridHeight,GridHeightStepY,Segs, BG_RGB, SubDiv_RGB, HighLight_RGB)
oldRgb=Getink()
oldSurface=GetSurface()
w =GridWidth *Segs
h =GridHeight *Segs

ThisIMage=NewImage(w,h)
RenderToImage thisimage
Cls BG_RGB

ink SubDiv_RGB
lockbuffer
For Ylp=0 to H-1 step GridWidthStepX
For Xlp=0 to w-1 step GridHeightStepY
line 0,ylp,w,ylp
line xlp,0,xlp,h
next
next
unlockbuffer

ink HighLight_RGB
lockbuffer
For Ylp=0 to H-1 step GridHeight
For Xlp=0 to w-1 step GridWidth
line 0,ylp,w,ylp
line xlp,0,xlp,h
next
next
unlockbuffer

; restore old surface and ink colour
RenderToImage OldSurface
ink oldrgb
EndFunction ThisImage