Advertisement

Newbie array question

Started by February 15, 2000 04:50 AM
9 comments, last by elasticMan 25 years ago
I have the following array which draws tiled bitmaps. At the moment it uses 9 different tiles drawn from a single bitmap. However, I want to call on more than 9 and so need to go into double figures. How would I go about setting up the array to accomodate more than the 9 I am using at the moment. Each number in the array follows directly on from the last so therefore I cannot type in a 12 for example. Any help would be cool Thanks char *world[30] = { "111111111111111130004111111111", "111111111111111300041111111111", "111111111111113000411111111111", "111111111111130004111111111111", "111111111111300041111111111111", "111111111113000411111113211111", "111111111130000211111115021111", "211111111300000021111111502113", "021111113000450002111111150230", "002111130004115000211111115000", "000211300041111500021111113000", "900023000411111150002111130008", "190000004111111115000211300081", "100000000302222777773000000001", "100000000332222777773000000001", "100000000002222333333000000001", "100000666666666666666666600001", "100000800000000000000000800001", "100000800000000000000000800001", "100000000000000000000000000001", "111111111111111111111111111111", };
To find out what the next symbol is after the char ''9''
char test = 10;
printf("10 = %c", testchar);
it might be any symbol; some are tricky(13 might give trouble)
Advertisement
Or you could use a 2 dimensional array on unsigned chars or ints or something and then instead of storing your data as a string of chars you can store it as comma seperated numbers.
Ok, so how do I set up the pointer to the new 2 dimensional array of type int?

BTW, is this a good way to do map scrolling using tiles.
I mean it is quite a big map of 64 pixel tiles, can I expect to have problems scrolling smoothly?

Thanks fellas
The array can be done like this:

unsigned char world[3][3] = {
{18,10,23},
{9,0,255},
{0,100,32}};

I would suggest that you do the map in a 8bit bitmap instead, that way you can use any painting program to draw your maps . Then you just load in the map like any other image and use it to extract the tiles at any position of the map.

It''s a good way of doing your maps as long as you remember that the whole map doesn''t have to be rendered. Only the tiles visible on the screen should be processed.
I had a problem like that once, and what I did was too make an external file...
I just put any characters in there so it owuld look like this:

xxxccqqq
xxccqqqx
xccccqqq
...

Just read the file in to the array. Your program doesn''t care if there''s an 3 or a ''q''.

Apart from being easier, it''s better to seperate data and code... That way you can change your bitmap thingy without the need to recompile...


- Sleepwalker
- Sleepwalker
Advertisement
Thanks for your help everyone!
Just a quick question on how I would read an external
file into the array like sleepwalker suggests, and what kind of file would the external be?
The file sleepwalker suggests is a simple textfile. This can easily be read with fstream or something like that.

It would be easier to read such a file but I still recommend using a bitmap with palette, that way you have a graphical view of the map when working with it. It would be really easy to see difference between water and grass if you use blue pixels for the water and green for the grass.

Of course if you have lots of tiles you probably will need to write your own map editor but that is a later problem.

Yeah, I''d like to do it the way you suggest but I am trying to modify someone else''s file and if I set up the array like you say I get the following error:

error C2440: ''initializing'' : cannot convert from ''const int'' to ''unsigned char *''
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

I''m too newbie to work this out but I think it''s because I am using a pointer in the array.
pseudo code:

unsigned char *world[3][3] = {
{18,10,23},
{9,0,255},
{0,100,32}};

Maybe I could post you the code if you have a minute?
With that last code snippet, you don''t want the ''*''... just declare the array as

unsigned char world[3][3] = {
{18,10,23},
{9,0,255},
{0,100,32}};

notice, that only creates a world with a total of 9 tiles. to bring it more in line with the original map, here''s the declaration:


unsigned char world[30][30] = {
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,0,0,0,4,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,0,0,0,4,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,3,0,0,0,4,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,3,0,0,0,4,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,1,3,0,0,0,4,1,1,1,1,1,1,1,1,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,1,3,0,0,0,4,1,1,1,1,1,1,1,3,2,1,1,1,1,1},
{1,1,1,1,1,1,1,1,1,1,3,0,0,0,0,2,1,1,1,1,1,1,1,5,0,2,1,1,1,1},
{2,1,1,1,1,1,1,1,1,3,0,0,0,0,0,0,2,1,1,1,1,1,1,1,5,0,2,1,1,3},
{0,2,1,1,1,1,1,1,3,0,0,0,4,5,0,0,0,2,1,1,1,1,1,1,1,5,0,2,3,0},
{0,0,2,1,1,1,1,3,0,0,0,4,1,1,5,0,0,0,2,1,1,1,1,1,1,1,5,0,0,0},
{0,0,0,2,1,1,3,0,0,0,4,1,1,1,1,5,0,0,0,2,1,1,1,1,1,1,3,0,0,0},
{9,0,0,0,2,3,0,0,0,4,1,1,1,1,1,1,5,0,0,0,2,1,1,1,1,3,0,0,0,8},
{1,9,0,0,0,0,0,0,4,1,1,1,1,1,1,1,1,5,0,0,0,2,1,1,3,0,0,0,8,1},
{1,0,0,0,0,0,0,0,0,3,0,2,2,2,2,7,7,7,7,7,3,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,3,3,2,2,2,2,7,7,7,7,7,3,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,2,2,2,2,3,3,3,3,3,3,0,0,0,0,0,0,0,0,1},
{1,0,0,0,0,0,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,0,0,0,0,1},
{1,0,0,0,0,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,0,0,0,0,1},
{1,0,0,0,0,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,8,0,0,0,0,1},
{1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1},
{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1}
};


And yes, I did actually type all that out. god. i''m bored. Anyway, the numbers between the bracets now can range from 0 to 255. if you want REALLY huge variety of tiles, use unsigned int world[30][30] {...}; giving you 4 billion or so available tile numbers.

Note that after this change, you''ll have to alter how the code uses the map array. but, you''d have to do that anyway.
good luck.



*oof*
*oof*

This topic is closed to new replies.

Advertisement