Indirection and Tilemapping
Reply | Flag | Categorize
Xeda Elnara 19 Feb 2013 13:07
Xeda Elnara




In preparation for TI-Concours, I picked up my TI-89t and tried to make a few programs with it. Among other things, I made a tilemap engine, so I thought I would share the code and explain the parts.

First, let's take a look at how indirection works. If you have an argument that requires a variable name, you can indirectly refer to it with a string. For example, you can draw a picture on the graph screen named sprite0 like this:

RclPic #string,0,0

In that example, 'string' had the variable name and we used the indirection symbol '#' to tell the program to use the string as the variable name. This will do the same thing:
RclPic sprite0,0,0

If you are familiar with the string() command, then you are probably coming up with all sorts of ideas already. For example, say you had a bunch of sprites named sprite0,sprite1,sprite2,… and you wanted to draw a sprite based on an input number:

RclPic #string,0,0

Or to optimise this a little:
RclPic #("sprite"&string(num)),0,0

Now if 'num' holds the sprite number to draw, it will draw the appropriate sprite. Using this, we can make a tilemap engine that uses a matrix to hold the tile numbers and uses 8x8 tiles:
For a,1,8     ;8 tiles tall
For b,1,8     ;8 tiles wide
RclPic #("sprite"&string(tilemap[a,b])),shift(a,3),shift(b,3)

tilemap[a,b] is used to access each matrix element and shift(a,3) basically multiplies the value of a by 8. A more memory efficient way of storing tilemaps, though, is to use strings. For example, if you start your sprite index at sprite1 instead of sprite0, you can convert your matrix to 1-byte char() strings and then add them all together. So the conversion code:
For a,1,8
For b,1,8

Then to draw your tilemap, you can use the inverse function of char()— ord():
For a,1,8
For b,1,8
RclPic #("sprite"&string(ord(mid(str0,c,1)))),shift(a,3),shift(b,3)

Or probably better:
For a,0,63
RclPic #("sprite"&string(ord(mid(str0,a+1,1)))),8*int(a/8),8*mod(a,8)
Attached is a screenshot taken using CalcCapture. I tested it and unfortunately, the screenshot is about half the speed that it actually runs at, so keep that in mind!
This program is a little different from the code that is posted in that it uses another level of indirection. It takes two inputs for the name of the tileset and the name of the tilemap, so it is meant to be a general tilemap engine.

Attachment: TileMapEx0.GIF

Make a reply:

To use the style buttons, click on the one you want, then press Ctrl+V.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Noncommercial 2.5 License.