Main Menu

PlayBasic V1.64k WIP Gallery

Started by kevin, October 19, 2009, 09:25:13 AM

Previous topic - Next topic

kevin

#45
  PlayBASIC V1.64k  Beta 21 - Optional Parameter For User Defined Functions

    I doubt this comes as any great surprise,  as it's a bit of no brainer that I'd be adding optional parameter support to User Defined Functions (& Psubs) next.      Adding this one requires a little more tinkering than the internal stuff.  Mainly because the pre-parser has to decipher the input parameters (and what they mean), beyond that,  it's just matter tweaking up the user function matching to generate the optional parameters in the function call.    

    So far the first stage (the parsing) is basically working,  ie

PlayBASIC Code: [Select]
      MyFunction(100,200,300)


Sync

waitkey



; Declare A,B and C as an optional that defaults to 1
Function MyFunction(a,b,c=1)

print a
print b
print c

EndFunction








kevin

#46
  PlayBASIC V1.64k  Beta 21 - Optional Parameter For User Defined Functions (Continued)

  Well, we can just about tick another feature off the to do list.   The current version has optional integer parameter support up and running for user defined  functions.     Currently the parser only supports the explicit syntax in the function header, rather than the 'As Datatype"  method.   Bit it's at least working..        
 
  To declare a parameter as being optional, this parameter uses the equals sign to declare this parameters default value(or string) in the function header.  

   ie.
PlayBASIC Code: [Select]
Function IncreaseVariable(ThisVariable, IncreaseAmount = 1)
Result =ThisVariable +IncreaseAmount
EndFunction Result




  In the example,  this user defined function is basically the equivalent of the INC operation.   If the user calls this function and omits the second param, then PB uses the functions default value for this parameter (which is 1).   So calling it with only one parameter, will add one to the input variable.   but if you use two params, then it's use what value you passed in for IncreaseAmount


  Limitations,

     * optional parameters can only be simple types. (Integers, floats and strings)
     * optional parameters must be the last parameters.  So you can't currently mix optional parameters in between fixed parameters.   So this   Function Dostuff(A , B=10 , C)  is not supported.


  Sample,

PlayBASIC Code: [Select]
      print Add()            ; Use all the optionals 
print Add(10) ; u8se last 2 optionals
print Add(10,20) ; use last optional
print Add(10,20,30) ; no optional parameters, so all parameters are manually sent

Sync
WaitKey


Function Add(a=50,b=100,c=200)
A+=b+c
EndFunction a





Outputs,

Quote
350
310
230
60




 Samples

    Had  few issues adding float + string support, but the follow examples of what's currently running in the latest build of beta 21
 
PlayBASIC Code: [Select]
      print TestStrings("Hello")

Sync
WaitKey


Function TestStrings(a$,b$="World")
result$=a$+" "+b$
EndFunction Result$





   Outputs Hello World



   Here's a version testing floats.  


PlayBASIC Code: [Select]
      print TestFloats("Flt")
print TestFloats2("Flt")

Sync
WaitKey


Function TestFloats(a$,b#=123.456)
result$=a$+"="+str$(b#)
EndFunction Result$


; Notice the mismatched assignment in the cast (B# = integer constant)
Function TestFloats2(a$,b#=123)
result$=a$+"="+str$(b#)
EndFunction Result$




  outputs
Quote
Flt=123.456
Flt=123.0




    The pre-parser had to be updated to handle both of these cases... but they're working ok now..





kevin

#47
  PlayBASIC V1.64k  Beta 22 - Optional Parameters In User Defined Functions/Command Calls

     This addresses an omission in the previous beta, where functions that didn't return a result couldn't use optional parameters,  which has since been addressed.   The only thing missing now (off the top of my head) is Psub support for optional parameters.  


PlayBASIC Code: [Select]
      Dude 10,20,30
Dude 10,20

a=MyFunction(100,200)

Sync
WaitKey


Function Dude(a,b,c=55)
print "DUDE"
print a
print b
print c
EndFunction


; Declare A,B and C as an optional that defaults to 1
Function MyFunction(a,b,c=234,f$="Yeah",g=true)
print "My Function"
print a
print b
print c
print f$
aa=a+b+c
EndFunction aa







 Psub Support


PlayBASIC Code: [Select]
      SubDude 10,20
SubDude 10,20,30
SubDude 10,20

a=MyFunction(100,200)

Sync
WaitKey


Function Dude(a,b,c=55)
print "DUDE"
print a
print b
print c
EndFunction



Psub SubDude(a,b,c=55)
print "DUDE as a SUB"
print a
print b
print c
EndPsub



; Declare A,B and C as an optional that defaults to 1
Function MyFunction(a,b,c=234,f$="Yeah",g=true)
print "My Function"
print a
print b
print c
print f$
aa=a+b+c
EndFunction aa










   

kevin

#48
  PlayBASIC V1.64k  Beta 22 - SortArray (string)
 
   Dropped this old chestnut back in.  While it works, it's not particularly quick.


PlayBASIC Code: [Select]
   Size=10
Dim name$(Size)
for lp=0 to size
name$(lp)=ReadData$()
next


SortArray name$(),0,size

for lp=0 to size
print name$(lp)
next
Sync
waitkey

Data "zack","wolf","kev","andy","michelle","dogs","tess","c64","zynaps","dudes","Alpha"




kevin

#49
  PlayBASIC V1.64k  Beta 24 - More As DataType Support in Function/Psub Declarations
 
    Since we've added support for the As DataType convention in function declarations for  Variables/Pointers/Types it stands to reason that some people will want to declare Arrays in such a fashion also.   While this is a little problematic given how the prototyping pass works in PB,  it should be doable. But the solution just isn't pretty.

 Sample,

PlayBASIC Code: [Select]
   Dim Stuff(100)
RandomizeIntegerArray(Stuff(), 100)

Sync
waitkey

Function RandomizeIntegerArray(Me() as integer, Max as integer)
For lp =0 to Getarrayelements(me())
Me(lp) =rnd(Max)
next
EndFunction





 Continued

     Due to running into a few catch 22's lately in how the pre-pass #1 prototyping and Pass#2 used to work, I've ended up biting the bullet and making the prepass that little bit smarter.  Turned out to be fairly straight forward as virtually all the code have been previously written, it just need to be unified better.    Which just means that the pass#2 doesn't need to bother about the parameters anymore, so expression solver won't have a fit with the extra info in the function declaration.


PlayBASIC Code: [Select]
   Dim Stuff(100)
RandomizeIntegerArray(Stuff(), 100)

Dim Stuff#(100)
RandomizeFloatArray(Stuff#(), 100)


Dim Stuff$(100)
RandomizeStringArray(Stuff$(), 100)

Sync
waitkey


Function RandomizeIntegerArray(Me() as integer, Max as integer)
For lp =0 to Getarrayelements(me())
Me(lp) =rnd(Max)
next
EndFunction


Function RandomizeFloatArray(Me() as float, Max as integer)
For lp =0 to Getarrayelements(me#())
Me#(lp) =rnd(Max)
next
EndFunction


Function RandomizeStringArray(Me() as string, Max as integer)
For lp =0 to Getarrayelements(me$())
Me$(lp) =str$(rnd(Max))
next
EndFunction









 Lists

  Updated the Function parameter parser to support the As UDT LIST convention.   So you can declare the parameter as a Typed LIST the same way as it's created.    The function doesn't really need to known it's a LIST, it's more for the user.    Internally a typed variable and typed list are the same structure underneath.  So they're interchangeable, it's just doesn't make list management enabled.    Moreover, a typed variable is really a 1D typed array.  


 List Declaration Example.

PlayBASIC Code: [Select]
   Type Pos
iValue
fValue#
EndType

Dim Numbers1 as pos list
Dim Numbers2 as pos list

AddValuesToLIst_LH(Numbers1(),10,2000)
AddValuesToLIst_LH(Numbers2(),10,2000)

print getlistsize(Numbers1())
print "Size:"+Str$(getlistsize(Numbers1()))
for each numbers1()
print numbers1.ivalue
next

print "Numbers #2"
print "Size:"+Str$(getlistsize(Numbers2()))
for each numbers2()
print numbers2.ivalue
next

Sync
waitkey

; Long hand declarations using AS DataType LIST
Function AddValuesToLIst_LH(Me as pos list, Count as integer, Max as integer)
For lp =0 to Count-1
me = new pos
Me.ivalue =rnd(Max)
next
EndFunction


; short hand declarations, same as above!
Function AddValuesToLIst_SH(Me.pos, Count, Max)
For lp =0 to Count-1
me = new pos
Me.ivalue =rnd(Max)
next
EndFunction








   

kevin

#50
PlayBASIC V1.64k  Beta 24b - Optional Parameter Tweaks

   Optional parameters can now use either literal constants or language constants.    Although, you can't use User Defined constants at this time.  


PlayBASIC Code: [Select]
   TestOptionalStringConstant("Testing")
TestOptionalStringConstant()

Sync
waitkey

Function TestOptionalStringConstant(Yeah$=PlayBasic$)
print Yeah$
EndFunction




kevin

#51
PlayBASIC V1.64k  Revision 2 - Beta 2 - Function Prototype Tweaks

    For the next few days/week I'll be working on a tweaking up revision #2 of the V1.64K package.  Namely trying to address some issues that should been discovered during the beta testing, but weren't..  So here we are again..

     Anything not solved during this testing phase, won't be looked at until the next major update 1.64L


Function Prototyping


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






 

kevin

#52
PlayBASIC V1.64k  Revision 2 - Beta 3 - Final Clean Up

    Well, given there hasn't been a single bit of user feedback, it looks like Beta 3 will be the release version.   Apart from correcting a bug with constant floats as optional parameters, this one has a few tiny changes.  Namely this version adds the ability to enable Vsync in windowed modes.

   A couple of points though, if you enable it, then the refresh rate will be whatever the users desktop refresh rate is.   So don't assume it'll be the same as yours, and secondly, the users machine might not be able to vsync.. ie card doesn't support it or  user has disabled it in the driver..



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








micky4fun

Hi Kevin

QuoteNamely this version adds the ability to enable Vsync in windowed modes
now that sounds good to me , did try a few things in window mode in previous PB versions and seem to be quite glitchy ,
so this will be a great help i think

mick :)

kevin

#54
   yes & no.   It's nice to have a vsync'd window..  But, this comes with a cost.  

stevmjon

to kev

thanks for vsync in window mode.

i avoided using vsync in my game because i wanted full screen & window to run the same. i experimented for a while to figure out the smoothest comprimise, but now i can run both in vsync. i am very happy.

was it difficult to impliment this into playbasic? could you have done this in older versions?

what is the 'cost' of having vsync in window mode?  my computer shows no difference in speed in fullsceen to window mode (i mean calc speed, not fps).

  thanks stevmjon
It's easy to start a program, but harder to finish it...

I think that means i am getting old and get side tracked too easy.

kevin

#56
 Read Here (login required)