Advertisement

Dynamic Multidimension Array

Started by October 19, 2000 08:33 PM
7 comments, last by minerjr 24 years, 3 months ago
Hi I am working on a game and want to make a structure of an map into a dynamic map. I am having a hard time trying to get the map to setup. I have a structre with 3 __int8 type variables in it and the map is 1001 by 1001. I know it is large. I need to make my game be able to change the map from 1001 by 1001 to 51 by 51. Here is my code.
    
// Map structe with infomation

struct MapInfo
{

	__int8 background ;
	__int8 foreground;
	__int8 walkable;
};

//Actual array of map

MapInfo **Map;

//Allocate size of map

Map = (MapInfo**)malloc(sizeof(MapInfo*) * 1001 );
      for(x = 0; x < 1001; x++)      
	  {
         Map[x] = (MapInfo*)malloc(sizeof(MapInfo) * 1001);      
	  }
    
Thx for any help you can offer.
What is an __int8? I''ve never heard of that.

"I can't work like this! Where are my fuzzy dice?"
"I can't work like this! Where are my fuzzy dice?"
Advertisement
Sounds a bit like an 8bit integer, which would be a char right? Unsigned or Signed is the question

-Chris Bennett of Dwarfsoft - Site:"The Philosophers' Stone of Programming Alchemy" - IOL
The future of RPGs - Thanks to all the goblins over in our little Game Design Corner niche
          
this is how i do mine..

    char **Data;Level.Data = new char*[Width];for(DWORD i=0; i < Width; i++){	Level.Data<i> = new char[Height];}    


it doesn''t look to be very different from yours except im using new instead of malloc
That looks like it should work, what exact problem are you having?

You just need to make 1001 a variable instead of a literal and it would be dynamic. Probably a couple of globals will do ya just fine.


And why 1001? (as opposed to the more common 1000?)

Are the back/foreground chars colors?

You can pack 7 more bits of tile types into the __int8 walkable;

something like __int8 Flags;
enum
{
walkable =0x00;
land =0x01; //water otherwise?
explored =0x02;
roaded =0x03;
mined =0x04;
//etc...
}

Edited by - Magmai Kai Holmlor on October 20, 2000 1:28:27 AM
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Thx for the feedback. The reason why the map is 1001 by 1001 is because my map editor is in VB and when i made a array 0 to 1000 it came out to the size of 1001 when i saved the map out. So I just increased the C++ map size.

The back foreground are the number of the forground and background tiles to be loaded. Walkable will be come the events layer of each layer. So I can have lots of events and collision detection for my map. My characters walk at one tile at a time so I only need simple collision detection. When i read one xy location on my map my game knowns what tile is to be loaded into the background and also the forground. Sprites will be contained an a array of structs that each will have it own xy value.

In my map there can be 256 background tiles, 256 foreground tiles and 256 or mabe make the walkable to 16 bit int and use them for all kinds of events.

Thx Magmai Kai Holmlor I will try that tonight. I am currenly in grade 12 and I am in school.

I am making a RPG like the old Final Fantasy/Dragon Warrior style game. I am useing 32 by 32 tiles and have a friend who is good at sprite graphics giving me a hand. I have a decent tile engine and want to add dynamic maps so that I can use small maps for town and large ones for the world.

Thx for the help and I will try everyones response.


Minerjr
Advertisement
If I have to do arrays of dynamic multidimensional sizes I use something like this:
    template <class T> class dynamic_array_2d{   dynamic_array_2d(DWORD w, DWORD h)   {      pData    = new T[w * h];      dwWidth  = w;      dwHeight = h;   }   ~dynamic_array_2d()   {      delete [] pData;   }   T & operator () (DWORD x, DWORD y) { return pData[y * dwWidth + x]; }protected:   DWORD dwWidth;   DWORD dwHeight;   T   * pData;};    


You don''t need to use templates, of course.

__int8 is a VC''ism for an 8 bit integer. You''ve also got __int16, __int32, __int64.

I can''t speak for minerjr but I like to use those types when I have a requirement for a data type that is a specific size. "int" in C/C++ has no size. It''s 32-bits on most modern systems but 16 on others and 64 on still others. If you use a type that explicitely says how big it is then you''re set forever. You just need to add a typedef if you''re using a non-VC compiler.

I''ve never liked using "char" for a 8-bit integer. Even if there was a guarantee on how big a char is (and there isn''t), to me a char is a character not a number.

-Mike
Cool, I''ll have to remember that.

"I can't work like this! Where are my fuzzy dice?"
"I can't work like this! Where are my fuzzy dice?"

This topic is closed to new replies.

Advertisement