Re: LF: Arrays


[Prev][Next][Index][Thread]

Re: LF: Arrays



At 03:45 PM 6/19/97 -0700, you wrote:
>Can anyone show me (in a code snippet) how to create an array and store
numbers into it? 
>I want to use one to store a game map, but I don't know how. Any help
would be much 
>appreciated.

There are a couple different ways to "create an array" one is to define it
at the bottom of your program (This will work the best for game maps,
unless you have your program set up a little differently) the other, more
complex way of doing this is to create a handle and play with that, but
this probably won't work for what you are trying to do unless you want to
create a temporary copy of th map to be modified and leave the original alone.

So, assuming that you want to use the first method, al you have to do to
create your map is us dc.* statements to define it at the bottom of your
program.  For an example, let us take a level from Dstar, the levels are
16x9 (they can be of any size, this just happens to be the board size for
DStar). The is how the first level looks at the bottom of the program:

level_1:
 dc.b	1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
 dc.b	1,3,1,1,0,0,0,0,0,0,0,0,2,1,4,1
 dc.b	1,0,0,0,0,0,0,0,0,0,0,2,0,1,1,1
 dc.b	1,0,0,0,0,0,0,0,1,1,2,0,0,0,0,1
 dc.b	1,0,0,0,1,1,0,2,0,0,0,0,0,0,1,1
 dc.b	1,1,0,2,0,0,2,0,0,0,0,0,2,0,0,1
 dc.b	1,0,2,0,0,0,0,0,0,2,1,1,0,2,0,1
 dc.b	1,0,0,0,0,0,1,1,2,0,0,0,0,0,2,1
 dc.b	1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1

A 0 stands for an empty space, a 1 stands for a wall, a 2 for a clear ball,
a 3 for a filled in ball, and a four for the block.

The routine to get an element of this array at d0,d1 looks like this:

get_element:
 lea		level_1(pc),a0      ;load starting address of array into a0
 mulu.w	#16,d1			;multiply d1 (y coordinate) by 16 because the level is 16
elements wide, this would change depending on the width of your array

 add.w		d1,a0			;add that to a0
 add.w		d0,a0			;then add the x-coord to a0 so you now have the address of
the piece you want in a0
 clr.l		d0			;clear d0, just in case as we are working with bytes
 move.b	(a0),d0		;put element in d0
 rts

The routine to put an element into the array is very similar:

put_element:				;this will put the element in d2 (a byte) into the array at
the coordinates (d0,d1)
 lea		level_1(pc),a0	;load level starting address	
 mulu.w	#16,d1			;multiply "y" by 16
 add.w		d1,a0			;get offset
 add.w		d0,a0			;get offset
 move.b	d2,(a0)		;put d2 into the correct position in the array
 rts

The second method is also pretty simple, but unnecesary unless you plan on
having a temporary copy of you map, if you do find this necesary, I would
be happy to explain.

>------------------------
>Also, can anyone tell me how to if you can store the arrays in an external
file and call 
>them in the program?
>------------------------

This gets a bit more complex, it is much easier to do it the other way.
There are two ways that I can think of that would work if you wanted to do
this. One would be to store the arrays as actual TI Variables (as Plane
Jump does) another would be to put them in a library and have routines in
that library that when called, will put the desired level into an area in
memory (possibly a handle) and/or give you the address of the level. Again,
for simplicities sake, I will leave it at that, but if you desire more, I
would be more than happy to help out.


	-Andrew


References: