Simple Source Code Examples
The File Types topic already described the types of files that make up a Tibbo BASIC/C project.
Here is a look at the contents of a Tibbo BASIC (.TBC) and Tibbo C (.TC) file.
include "global.tbh" 'This includes a BASIC header file
dim counter1 as byte 'This is a global variable
'Variables must be defined or declared before use
Const MAX_COUNTER=5 'BASIC constant
declare function increment(x as byte) as byte 'This is a declaration
'Procedures (subs and functions) must be defined or declared before use
'==================================================================
sub on_sys_init 'event handler for the ON_SYS_INIT event
counter1=0
end sub
sub on_sys_timer 'event handler for the ON_SYS_TIMER event
counter1=increment(counter1)
if counter1>=MAX_COUNTER then
counter1=0
pat.play("R-R-R",PL_PAT_CANINT) 'invoke method .play of the pat. object
end if
end sub
function increment(x as byte) as byte
increment=x+1
end function
#include "global.th" //This includes a C header file
unsigned char counter1; //This is a global variable
//Variables must be defined or declared before use
#define MAX_COUNTER 5 //There is no const statement in C, so use preprocessor
unsigned char increment(unsigned char x); //This is a declaration
//Procedures (subs and functions) must be defined or declared before use
//==================================================================
void on_sys_init(){ //event handler for the ON_SYS_INIT event
counter1=0;
}
void on_sys_timer(){ //event handler for the ON_SYS_TIMER event
counter1=increment(counter1);
if(counter1>=MAX_COUNTER){
counter1=0;
pat.play("R-R-R",PL_PAT_CANINT); //invoke method .play of the pat. object
}
}
unsigned char increment(unsigned char x){
return x+1;
}
At the very top of the source file you typically have header (.TPH and .TH) inclusions.
This is followed by global variable and constant declarations. Variables in Tibbo BASIC/C must be defined or declared before they are used.
Next you have procedure declarations. Again, procedures of Tibbo BASIC/C must be defined or declared before they are used.
The rest are procedure bodies. Some of these are event handlers, they are called in response to events. These do not require declaration (they are already declared in the platform files).
Other procedures are regular subs and functions of your program.
How do you tell if a procedure is an event handler or not? Names of event handlers are highlighted in green.
Additionally, all event handler names start with on_, which means on x happening, do....
Notice how procedure bodies are separated from each other by a horizontal line. This is described in Project Parser and Browser.