Michael
macro
-----
The macro statement lets you define a keyword that represents other programming statements. You can use parameters in a macro's definition by putting numbers inside curly brackets. Use '{1}' for the first parameter, '{2}' for the second parameter, '{3}' for the third parameter, and so on. Use an 'end' statement to terminate a macro's definition.
For example, the following macro defines 'setscreencolors' as a keyword that can be used to set the background color, playfield color, and score color:
macro setscreencolors
COLUBK={1}
COLUPF={2}
scorecolor={3}
end
callmacro
---------
The callmacro statement lets you call a macro after you've defined it. To pass parameters to the macro, follow the name of the macro with the variable names or values you want the macro to use. Put a space before each parameter, but don't include commas.
For example, the following statement calls the 'setscreencolors' macro that was defined above:
callmacro setscreencolors $02 $46 $1C
When the batari Basic compiler sees this statement in your program, it replaces the 'callmacro' statement with the macro's code, inserting the parameters in the order they were listed, as follows:
COLUBK=$02 COLUPF=$46 scorecolor=$1C
Combining macro, def, and callmacro
-----------------------------------
Although 'macro' essentially lets you create a new instruction of your own, it doesn't look much like a new instruction with the 'callmacro' keyword in front of it. But you can use 'def' to make a keyword that represents the 'callmacro' statement. For example:
def scolors=callmacro setscreencolors scolors $02 $46 $1C
Now it looks more like a new instruction!
More Examples of Using Macros
-----------------------------
The following example contains a few more macros that could be useful:
macro setplayercolors
COLUP0={1}
COLUP1={2}
end
def pcolors=callmacro setplayercolors
macro setplayer0xy
player0x={1}
player0y={2}
end
def p0xy=callmacro setplayer0xy
macro setplayer1xy
player1x={1}
player1y={2}
end
def p1xy=callmacro setplayer1xy
player0:
% 00010000
% 00101000
%01000100
%10000010
%01000100
% 00101000
% 00010000
end
p0xy 20 40
player1:
%11111111
%10000001
%10000001
%10000001
%10000001
%10000001
%11111111
end
p1xy 50 30
loop
pcolors $0E $C6
drawscreen
goto loop
You can even create macros that contain if-then statements, for-next statements, and other complex code, or call a macro from inside another macro. For example:
macro movesprite
if joy0left then {1}x={1}x-1
if joy0right then {1}x={1}x+1
if joy0up then {1}y={1}y-1
if joy0down then {1}y={1}y+1
end
callmacro movesprite player0
In this example, 'callmacro movesprite' will get replaced by the four if-then statements of the 'movesprite' macro, and '{1}' will get replaced by the name of the sprite, so player0 will get moved around by joystick0. If you change it to 'callmacro movesprite missile1', it will move missile1 around instead of player0.
As you can see, macros can help make it easier for you to write your program code. However, macros could also make it harder for someone else to understand your program. For example, if someone sees 'p0xy 20 40' in your program, first they would need to look through your code to find where you defined 'p0xy' to mean 'callmacro setplayer0xy', and then they would need to look further to find where you defined the code for the 'setplayer0xy' macro. On the other hand, that's not much different than seeing a statement like 'gosub moose_tracks' and then having to look for the 'moose_tracks' subroutine to see what it does.
Attached Files
Edited by SeaGtGruff, Sat Sep 18, 2010 1:02 AM.















