memory indexing

BlitzMax Forums/BlitzMax Beginners Area/memory indexing

cPix(Posted 2008) [#1]
Hi!

I've just started with blitzmax, i have alot of years behind me with development in C and the demoscene. What i have not figured out yet, is how to index a memory area.

This is a sample code of genereating linear perspective of a 2d tunnell.
void calclineartbl(int xsize, int ysize, char *dest)
{
	int	x, y, col;

    for (y = 0; y < ysize / 2; y++)
	{
        for (x = 0; x < xsize / 2; x++)
		{
            col = atan2((ysize / 2) - y, (xsize / 2) - x) * (128 / PI);
            dest[y * 320 + x] = col;
            dest[y * 320 + (xsize - x - 1)] = 127 - col;
            dest[(ysize - y - 1) * 320 +(xsize - x - 1)] = 128 + col;
            dest[(ysize - y - 1) * 320 + x] = 255 - col;
		}
	}
}

Does anyone know how to replicate this to blitzmax? Like, how to use a memory area like this

memory (which is allocated ) [someusefullstuff*x] = x + y;

Thank you in advance!

- pixlor


tonyg(Posted 2008) [#2]
You could use a TBANK but I would simply use a TYPE or an array as it'll be easier to maintain.


ImaginaryHuman(Posted 2008) [#3]
Look at how BlitzMax uses pointers.

You create a pointer like:

Local dest:Byte Ptr

Then to access the elements of memory that it points to, you dereference it with [], like:

dest[y*320+x]=col

which actually is very similar to what your C code says.

You can do:

dest[whateveroffset]=something

and also

something=dest[whateveroffset]

See also TBank for a chunk of contiguous memory, or use a one-dimensional array, but avoid 2 dimensional arrays if you want more efficient contiguous memory.