-
Notifications
You must be signed in to change notification settings - Fork 0
Standard Function Guide
General purpose math, string, and timing functions.
These functions are used for general purpose operations, such as mathematics equations and string manipulation.
Abs(x) returns the absolute value of x.
ArrayMax(array) returns the index of the highest element of array.
Iterating elements 0..ArrayMax(array) will therefore visit every element inside the array.
ArrayMax is a special function in that array can be any type, so long as it is an array.
Asc(x) takes a single string parameter x, and returns the ASCII value of the first character.
This is the opposite of the chr$ function
Atn(x) returns the Arc Tangent value of x, in radians.
Atnd(x) returns the Arc Tangent value of x, in degrees.
Atn2(x, y) returns the Arc Tangent value of x, y, in radians.
Atn2(x, y) returns the Arc Tangent value of x, y, in degrees.
Beep() causes the computer to beep.
Important
Beep() does nothing in the current version of Basic4GLj
Chr$(x) takes a single integer parameter x, and returns a string character whose ASCII value is x.
Example:
Printr Chr$(72)+Chr$(101)+Chr$(108)+Chr$(108)+Chr$(111)
Cos(x) returns the Cosine of x, where x is measured in radians.
Cosd(x) returns the Cosine of x, where x is measured in degrees.
Exp(x) returns e raised to the power of x.
Exp is the inverse of Log.
Int(x) casts a real valued x to an integer.
Important
The rounding is slightly different to the implicit type cast when a real value is assigned to an integer.
Int(x) rounds x towards negative infinity, whereas implicit type casting always rounds towards 0.
Example:
dim a#, i1, i2: a# = -5.1
i1 = a#
i2 = Int(a#)
printr "i1 = " + i1
printr "i2 = " + i2
Left$(s,c) returns a string containing the first c characters of s.
s is a string value, c is an integer value.
For example, Left$("ABCDEFG", 3) returns ABC
LCase$ (x) returns x converted to lowercase.
Len(x) returns the length of the string x in characters.
Log(x) returns the natural logarithm of x.
Log is the inverse of Exp.
Mid$(s,i,c) returns a string containing c consecutive characters of string s, starting from the ith character.
For example, Mid$("ABCDEFG", 4, 3) returns "DEF".
PerformanceCounter() returns the number of milliseconds that have elapsed since the computer was turned on.
This function is very similar to TickCount(),
except PerformanceCounter() is accurate to 1 millisecond whereas TickCount() is only accurate to 10ms.
Therefore, I strongly recommend using PerformanceCounter() for any timing operations.
The old TickCount() function is retained only for backwards compatibility with existing Basic4GL programs.
Pow(x,y) returns x raised to the power of y.
Right$(s,c) returns a string containing the last c characters of s.
For example, Right$("ABCDEFG", 3) returns "EFG"
Rnd() returns a random integer value, between 0 and RND_MAX.
(RND_MAX = 32767, but could be different in future ports of Basic4GL to different platforms or operating systems.)
Important
The Basic4GL for Java port uses RND_MAX = 32767 for random behavior compatibility with previous versions of Basic4GL.
To return a random number between 0 and x-1 (inclusive), use:
Rnd() % x
To return a random number between 1 and x (inclusive), use:
Rnd() % x + 1
Sgn(x) returns:
-
1, ifxis greater than0 -
0, ifxequals0 -
-1, ifxis less than0
Sin(x) returns the Sine of x, where x is measured in radians.
Sind(x) returns the Sine of x, where x is measured in degrees.
Sqr(x) returns the square root of x.
(Actually the square root of the absolute value of x.)
Sqrt(x) is exactly the same as Sqr(x)
Str$(x) converts an integer value x into a string representation of x.
For example, Str$(-13.4) returns "-13.4".
Tan(x) returns the Tangent of x, where x is measured in radians.
Tand(x) returns the Tangent of x, where x is measured in degrees.
Tanh(x) returns the Hyperbolic Tangent of x, where x is measured in radians.
TickCount() returns the number of milliseconds that have elapsed since the computer was turned on.
Note
This function is only accurate to about 10ms. I strongly advise using PerformanceCounter() instead.
UCase$ (x) returns x converted to uppercase.
Val(x) converts a string x into a numeric value.
If x cannot be converted into a number, then Val(x) returns 0.
For example, Val("27.2") returns 27.2.
Val is the opposite of Str$.
Pauses execution for a number of milliseconds.
Format:
Sleep (milliseconds)
Note
The application is completely unresponsive while sleeping.
Therefore, Basic4GL will not sleep for more than 5000 milliseconds (5 seconds) at a time.
To sleep for more than 5 seconds, use a loop.
For example:
Dim i
For i = 1 to 60: Sleep (1000): Next
Will pause for 60 seconds, but still give the user the opportunity to break out of the program if he/she wishes.
This function is similar to Sleep, and indeed has the same format:
WaitTimer (milliseconds)
The difference is that WaitTimer waits until milliseconds milliseconds has elapsed from the previous WaitTimer call.
This difference is significant if WaitTimer is used inside an animation loop,
with other code that may take some time to execute (such as rendering a frame).
For example:
While true
Draw a frame
WaitTimer (100)
Wend
If Draw a frame were to take 40 milliseconds, then WaitTimer will pause for only 60 milliseconds,
ensuring that the loop is correctly iterated 10 times a second.
Even simple animations can potentially take up to the resync period of the monitor (anything from 1/100th to 1/50th of a second), if the user's graphics card is configured to wait for retrace before drawing.
SyncTimer returns true if you need to update the internal state of the application to catch up to the clock.
This can be used to force an animation to update internally so many times per second, regardless of a PC's rendering speed, and is intended to be used as follows:
While main-loop-condition
Render scene
While SyncTimer (delay)
Update state
Wend
For example, if delay was 10 milliseconds, then Update state will execute 100 times per second,
regardless of whether the computer is capable of rendering 20 or 100 frames per second.
Example:
dim x, y, a#, b#
while true
glClear (GL_DEPTH_BUFFER_BIT or GL_COLOR_BUFFER_BIT)
glLoadIdentity ()
glTranslatef (0, 0, -16)
glRotatef (a#, 0, 0, 1)
for y = -5 to 5: for x = -5 to 5
glPushMatrix ()
glTranslatef (x * 3, y * 3, 0)
glRotatef ((x + y) * 60 + b#, 1, 0, 0)
glBegin (GL_QUADS)
glColor3f (1, 0, 0): glVertex2f ( 1, 1)
glColor3f (0, 1, 0): glVertex2f (-1, 1)
glColor3f (0, 0, 1): glVertex2f (-1,-1)
glColor3f (1, 1, 1): glVertex2f ( 1,-1)
glEnd ()
glPopMatrix ()
Next: Next
SwapBuffers ()
while SyncTimer (10)
a# = a# + 0.9: b# = b# + 3.6
wend
wend
Basic4GL, Copyright (C) 2003-2007 Tom Mulgrew
Programmer's guide
26-Jul-2008 Tom Mulgrew
Documentation modified for Markdown formatting by Nathaniel Nielsen