Mail Archives: djgpp/1997/04/04/17:59:56
William Heymann wrote:
>
> I was wondering how do I make an multi-dimensional array that itilalizes
> at run time without knowing any of the parameters. What I am doing is I
> am using an array to keep track of objects on the screen so that my
> colision detection works. I am also using Allegro and would like to add
> in the function that allows the user to select what resolution to run
> the screen in. Since the array is screen resolution dependent I need a
> way to create the array at runtime using the the values I get from
> Allegro.
>
> So how do I make a dynamic two dimensional array in C++?
In C, you can just dynamically allocate a linear block of memory and
index it just like you would the screen memory. For example:
unsigned char *buffer;
buffer = calloc( sizeof(char) * SCREEN_WIDTH * SCREEN_HEIGHT );
...
buffer + x + SCREEN_WIDTH * y = pixel;
-or-
buffer[x + SCREEN_WIDTH * y] = pixel;
When you're done with it, be sure to free the buffer. I can't imagine
that you'd do things much differently iin C++.
--
---------------------------------------------------------------------
| John M. Aldrich, aka Fighteer I | fighteer AT cs DOT com |
| Plan: To find ANYONE willing to | http://www.cs.com/fighteer |
| play Descent 2 on DWANGO! | Tagline: <this space for rent> |
---------------------------------------------------------------------
- Raw text -