|
Post by Tom Mulgrew on Dec 27, 2007 2:50:53 GMT -5
Get it here: www.basic4gl.net/unofficial/B4GLFunctionTest.zipThis is a development version of Basic4GL, not a proper release. It's just far enough along that I thought people might be interested in playing with it. It's not stable though. There are ways to write programs that will crash this version every time they are run. And it doesn't support returning arrays or structures from functions yet. Anyway, the syntax is based loosely on QBasic. You declare a subroutine like this: sub MySub(param1, param2$, param3()) ... end sub Then you can call it like this: MySub(12, "Hello", myArray) For example, someone mentioned a print command that also takes a cursor position and a colour parameter: const COL_RED = 1 const COL_GREEN = 2 const COL_BLUE = 3
sub PrintAt(text$, x, y, col) locate x, y if col = COL_RED then color(255, 0, 0) elseif col = COL_GREEN then color(0, 255, 0) elseif col = COL_BLUE then color(0, 0, 255) endif print text$ end sub
printat("Hello", 10, 5, COL_RED) printat("What's", 30, 7, COL_BLUE) printat("Up?", 5, 20, COL_GREEN) You can also define functions that return values: function MyFunction(param1, param2$) ... return [result goes here] end function Then use them inside expressions, e.g: dim a: a = MyFunction(6, "Banana) + 4 For example: function rand(min, max) return (rnd() % (max - min + 1)) + min end function printr "Random numbers between 1 and 10" dim i for i = 1 to 10 printr rand(1, 10) next Local variables can be declared by DIMming them inside the sub or function. The variable is then only valid inside the sub/function, and will not conflict with any variables of the same name DIMmed outside that sub/function. For example: sub Line(char$, count) dim i for i = 1 to count: print char$: next printr end sub
sub Triangle(char$, count) dim i for i = 1 to count: Line(char$, i): next end sub
Triangle("*", 15) Anyway, it's not complete, but it's functional enough to be useful. I know a number of ways to crash it (like jumping in/out of functions using goto/gosub). Let me know if you find any more. Cheers, -Tom
|
|
|
Post by Nicky Peter Hollyoake on Dec 27, 2007 9:46:20 GMT -5
Const TEXT_RED = 0 Const TEXT_GREEN = 1 Const TEXT_BLUE = 2
Sub CLP(Col, X, Y, Text$) if Col = TEXT_RED Then Color(255, 0, 0)
Elseif Col = TEXT_GREEN Then Color(255, 0, 0)
Elseif Col = TEXT_BLUE Then Color(255, 0, 0) Endif
Locate X, Y: Print Text$ EndSub
CLP(TEXT_RED, 10, 10, "Nicky") I can finally do the thing I wanted  . I've never played with Function commands before so its gonna take me a time to get use to them ... EDIT: I'm getting use to the "Sub" abit but could someon helpme out with "Function" how it works, etc.
|
|
|
Post by Supermonkey on Dec 27, 2007 10:55:25 GMT -5
Excellent work, I'll test it out later.
|
|
|
Post by Nicky Peter Hollyoake on Dec 27, 2007 15:47:21 GMT -5
Sub BindSprites(Dim a()) EndSub
Dim Sprite(10)
BindSprites(Sprite) Are they away to get how many arrays are in the "Sprite" varible? So I could do something like this then ... Sub BindSprites(Dim a())
Dim i
For i 0 To Array_Max '... Next EndSub
Dim Sprite(10)
BindSprites(Sprite) 
|
|
|
Post by Tom Mulgrew on Dec 28, 2007 4:15:00 GMT -5
Not quite sure what you're trying to do with BindSprite and arrays, since you can only bind one sprite at a time... But having a way to return the number of elements in an array is a good idea (and something I've considered before). Functions are useful when you want to have a quick way to calculate or evaluate something. The important thing about a function is the "return" statement includes a value to return. For example: function Five() return 5 end function
dim a a = Five() print a So when "a = Five()" is executed, the function "Five" is called. The "return 5" statement returns the value 5, so "Five()" effectively evaluates to 5, and the statement "a = Five()" is the same as "a = 5". You can also have more complicated functions: function TimesTwo(value) return value * 2 end function
print TimesTwo(6) When "print TimesTwo(6)" executes, it calls "TimesTwo" with the "value" parameter set to 6. The "return value * 2" evaluates 6 * 2 and returns the result. So "TimesTwo(6)" is the same as "12" So "print TimesTwo(6)" is the same as "print 12" Eventually you'll be able to make functions that return structures and arrays as well. (But that's not quite working yet  )
|
|
|
Post by James :) (aka Madcow) on Dec 28, 2007 4:59:35 GMT -5
i added it to our growing list of archives on the wiki.
|
|
|
Post by Nicky Peter Hollyoake on Dec 28, 2007 7:22:34 GMT -5
Not quite sure what you're trying to do with BindSprite and arrays, since you can only bind one sprite at a time... But having a way to return the number of elements in an array is a good idea (and something I've considered before). Functions are useful when you want to have a quick way to calculate or evaluate something. The important thing about a function is the "return" statement includes a value to return. For example: function Five() return 5 end function
dim a a = Five() print a So when "a = Five()" is executed, the function "Five" is called. The "return 5" statement returns the value 5, so "Five()" effectively evaluates to 5, and the statement "a = Five()" is the same as "a = 5". You can also have more complicated functions: function TimesTwo(value) return value * 2 end function
print TimesTwo(6) When "print TimesTwo(6)" executes, it calls "TimesTwo" with the "value" parameter set to 6. The "return value * 2" evaluates 6 * 2 and returns the result. So "TimesTwo(6)" is the same as "12" So "print TimesTwo(6)" is the same as "print 12" Eventually you'll be able to make functions that return structures and arrays as well. (But that's not quite working yet  ) Thank you. BTW the "BindSprites" was just an exemple ecause I saw someone going on about it before but what I was thinking "Bindsprites" could have something like sprite, size & pos altogether. One more thing (which ain't much of a big deal at the moment), but you can make a box or something with an input, and let you add words (or your command names,which pretty much should be the ones you add anyways), then whatever you put in it turns red.
|
|
|
Post by DJLinux on Dec 28, 2007 10:44:32 GMT -5
Hello Tom good job so far can/will you support any simple event label/sub/function callable from plugins too?
Joshy
|
|
|
Post by Nicky Peter Hollyoake on Dec 28, 2007 18:37:07 GMT -5
Someone help me with this, don't know what I done wrong. ResizeText(50, 30) SetTextScroll(FALSE) TextMode(TEXT_OVERLAID)
'CLP (Color, locate, print all-in-1 command). Const RED = 0 Const GREEN = 1 Const BLUE = 2 Const LIGHTBLUE = 3
Sub CLP(Col, X, Y, Text$) if Col = RED Then Color(255, 0, 0)
Elseif Col = GREEN Then Color(0, 255, 0)
Elseif Col = BLUE Then Color(0, 0, 255)
Elseif Col = LIGHTBLUE Then Color(0, 255, 255) Endif
Locate X, Y: Print Text$ EndSub
'Draw line box command Sub DrawLineBox(SX, SY, X, Y) GLLoadidentity() GLTransLateF(X, Y - 0.00001, 0) GLScaleF(SX, SY, 0) GLBegin(GL_LINES)
GLVertex2F(1, 0): GLVertex2F(0, 0) GLVertex2F(0, 0): GLVertex2F(0, 1) GLVertex2F(1, 1): GLVertex2F(1, 0) GLVertex2F(1, 1): GLVertex2F(0, 1) GLEnd() EndSub
'2D Screen command Sub Setup2DScreen(Width, Height) GLMatrixMode(GL_PROJECTION) GLLoadidentity() GLOrTho(0, Width, Height, 0, 0, 1) GLMatrixMode(GL_MODELVIEW) EndSub '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Setup2DScreen(TextCols(), TextRows())
While TRUE GLClear(GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT): CLS
DrawLineBox(TextCols(), 1, 0, TextRows() - 1) CLP(BLUE, 0, TextRows(), "Message:")
DrawText() Swapbuffers() Wend For some reason I can NOT use these at the same time ... DrawLineBox(TextCols(), 1, 0, TextRows() - 1) CLP(BLUE, 0, TextRows(), "Message:") if I just use one of them it works, I don't get it. 
|
|
|
Post by Tom Mulgrew on Dec 28, 2007 20:33:29 GMT -5
Hello Tom good job so far can/will you support any simple event label/sub/function callable from plugins too? There's no support for that now, but it could be added. How simple? Would a label to "gosub" to be enough? Or does the plugin need to pass parameters to the BASIC program? -Tom
|
|
|
Post by Tom Mulgrew on Dec 28, 2007 20:42:43 GMT -5
For some reason I can NOT use these at the same time ... DrawLineBox(TextCols(), 1, 0, TextRows() - 1) CLP(BLUE, 0, TextRows(), "Message:") if I just use one of them it works, I don't get it.  That was a Basic4GL bug in the new function/sub parameter passing code. I'll upload a fixed version soon. UPDATE: I've uploaded the fixed version. Same link ( www.basic4gl.net/unofficial/B4GLFunctionTest.zip)
|
|
|
Post by DJLinux on Dec 29, 2007 13:19:46 GMT -5
...How simple? Would a label to "gosub" to be enough? Or does the plugin need to pass parameters to the BASIC program? -Tom Sub_From_Plugin_RegisterSub(&AnyEventSub) Sub AnyEventSub() ' any Basic4GL or Plugin code End Sub
or Sub_From_Plugin_RegisterLabel(&AnyEventLabel) AnyEventLabel: ' any Basic4GL or Plugin code return Should be a good and simple starting point. After any event the Basic4GL code can use any "normal" PlugIn function/sub to get the needed params Important is only that you push the curent VM code pointer on VM stack and jump to Label or call the right sub You know what i mean? and i wish you and all others a happy good new year in peace Joshy
|
|
|
Post by Tom Mulgrew on Dec 30, 2007 5:24:31 GMT -5
Sub_From_Plugin_RegisterSub(&AnyEventSub) Sub AnyEventSub() ' any Basic4GL or Plugin code End Sub
or Sub_From_Plugin_RegisterLabel(&AnyEventLabel) AnyEventLabel: ' any Basic4GL or Plugin code return Should be a good and simple starting point. After any event the Basic4GL code can use any "normal" PlugIn function/sub to get the needed params Important is only that you push the curent VM code pointer on VM stack and jump to Label or call the right sub You know what i mean? and i wish you and all others a happy good new year in peace Joshy Happy new year to you too. That sounds easy enough to do. It will take me a little while to finish the functions version first though. Cheers, -Tom
|
|
|
Post by Tom Mulgrew on Dec 30, 2007 5:36:18 GMT -5
I've uploaded a new version again. Same url: www.basic4gl.net/unofficial/B4GLFunctionTest.zipThis one supports returning structures and arrays from functions. To return a structure: struc SMyStruc dim a, b endstruc
function SMyStruc MyFunction() dim SMyStruc s s.a = 10 s.b = 15 return s end function
dim SMyStruc test test = MyFunction() To return an array: function MyFunction(count) () dim i, array(count) for i = 0 to count: array(i) = i: next return array end function
dim a(10) a = MyFunction(10) The () after the function definition means it returns an array. I couldn't find any example of how QBasic returns arrays (I'm not sure it even supports it), so I made up my own syntax, and this was the easiest for the compiler to compile. I've also added a function called "arraymax", which returns the maximum element of an array. So looping from 0 to arraymax(array) will visit every element in "array". For example: sub PrintArray(array#()) dim i for i = 0 to arraymax(array#): print array#(i); " ": next printr end sub
PrintArray(vec2(1,2)) PrintArray(vec3(3,4,5)) PrintArray(vec4(6,7,8,9)) Also, as far as I know, this version should be stable. So if you find a way to crash it, please let me know. Cheers, -Tom
|
|
|
Post by Empyrion Martyr on Dec 30, 2007 10:08:12 GMT -5
 yay finally we get some real action, that's what i'm talking about!  I think i'm not rushing by saying this is the final touch to making bagel a complete language and anything added after this feature is extra... (now why would we need classes and other complications people? don't try and make everything look like cpp!) Thanx Tom!
|
|