Skip to main content
gamedev.net
🔒 Locked

Map array probems?

Started by _damN_ 6 replies 1.4k views
Original Post
_damN_
_damN_
Hi there! Im having trouble with my map array and have a few questions. This might be a bit tricky to explain so please bare with me! ;-) Basically i have a map array like so... #define MAP_WIDTH 16 #define MAP_HEIGHT 16 m_iIsoMap[MAP_WIDTH][MAP_HEIGHT]; I want MAP_WIDTH and MAP_HEIGHT to be able to hold the dimensions of the map. Is there a way for me to use a different kind of subscript, like int's? So the code would look something like this: int m_iWidth; int m_iHeight; m_iIsoMap[m_iWidth][m_iHeight]; But when i do it like that i get alot of errors because the arent constants. I wanto be able to do it that way because i wanto be able to just call a function simmilar to "SetMapSize(int iWidth,int iHeight) to set the maps size. So basically i just wanto be able to change the size of the map without having to use a pre-determined size. Can anybody think of a way i can do this, or a simmilar way. Any advice or ideas are welcome... I hope it makes sense ;-/ Thanks!
Dolf
Dolf
If you want to resize it, you could create a map on the heap with the "new" operator.

Or you could create a vector of vectors.

something like this:

// A 2d array on the heap, don't forget to delete it ^_^YourMapType* m_pMap = new YourMapType[m_iWidth][m_iHeight];// the vector versionstd::vector< std::vector< YourMapType > > m_map;


I hope that helped Mister _damN_.
fastcall22
fastcall22
Quote:
Original post by Dolf
*** Source Snippet Removed ***


Just a minor correction/expansion on what Dolf said:

// Allocateint* mMapArray = new int[ mWidth * mHeight ];// UsagemMapArray[ y * mWidth + x ] = 0; // Make sure y * mWidth + x is inside the array!// Deallocatedelete[] mMapArray;


// Allocate:int** mMapArray = new int*[ mHeight ];for ( int i = 0; i < mHeight; ++i )	mMapArray = new int[ mWidth ];	// UsagemMapArray[y][x] = 0; // Make sure y and x are inside the array!// Deallocatefor ( int i = 0; i < mHeight; ++i )	delete[] mMapArray;delete[] mMapArray;


// Allocateusing std::vector;vector<vector<int>> mMapArray( mHeight, vector<int>( mWidth, 0 ) );// UsagemMapArray[y][x] = 0; // vector will check bound for you !// Deallocate// vector will deallocate for you!
Dolf
Dolf
Haha yes ofcourse sorry about that with the 2d array on the heap.

Something like this can add in the asserts, and you can add functionality to your map.

#include "stdio.h"#include "assert.h"template< typename T >class MapArray{public:	MapArray( T width, T height )	:	m_width( width ),	m_height( height )	{		assert( width > 0 );		assert( height > 0 );		m_elements = new T[ width * height ];	}	~MapArray()	{		delete m_elements;	}	// should be replaced with operator overloading? :P	T Get( int x, int y ) const	{		assert( x >= 0 && x < m_width );		assert( y >= 0 && y < m_height );		return m_elements[ x + y * m_width ];	}	// should be replaced with operator overloading? :P	void Set( int x, int y, T& value )	{		assert( x >= 0 && x < m_width );		assert( y >= 0 && y < m_height );		m_elements[ y * m_width + x ] = value;	}	int GetWidth()	const	{ return m_width; }	int GetHeight() const	{ return m_height; }private:	T* m_elements;	int m_width;	int m_height;};int main() {	MapArray<int> map( 10, 10 );	// fill the map with K	int k = 0;	for ( int i = 0; i < map.GetWidth(); ++i )	{		for ( int j = 0; j < map.GetHeight(); ++j )		{			map.Set( i, j, k );			k++;		}	}	// print out the entire map, could make this a member of the class	for ( int i = 0; i < map.GetWidth(); ++i )	{		for ( int j = 0; j < map.GetHeight(); ++j )		{			printf( "X: %i, Y: %i, Value: %i \n", i, j, map.Get( i, j ) );		}	}	getchar();	return 1;} 


[Edited by - Dolf on March 19, 2009 4:09:47 PM]
fastcall22
fastcall22
Right. With subscript operators, would look something like this:

	const Type& operator() ( int x, int y ) const	{			assert( CheckBounds( x, y ) );				return mData[ y * mWidth + x ];	}			Type& operator() ( int x, int y )	{		assert( CheckBounds( x, y ) );		return mData[ y * mWidth + x ];	}


Subscript operators come in pairs
_damN_
_damN_
Thanks guys, i learned alot from this. did also some more research on dynamic arrays etc. i got a couple of ideas how to implement it.

MANY THANKS! -_-
Zahlman
Zahlman
Quote:
Original post by _damN_
Hi there!

Im having trouble with my map array and have a few questions.
This might be a bit tricky to explain so please bare with me! ;-)


I know you can't see me via the GDNet forum, but I'd rather bear with you. :)

Quote:
Basically i have a map array like so...

#define MAP_WIDTH 16
#define MAP_HEIGHT 16


Don't use #define to create constants. Use real constants, e.g. 'const int MAP_WIDTH = 16;'.

Quote:
Is there a way for me to use a different kind of subscript, like int's?


16 is an integer value. :) But since what you want to do is set the size of the array at runtime: don't build it yourself as suggested here. Instead, use boost::multi_array.
Dolf
Dolf
Quote:
Original post by Zahlman
don't build it yourself as suggested here. Instead, use boost::multi_array.


Depends Zahlman, he mentioned he learned something.

If you are working on a project and you want to be as productive as possible and it's your call whether to include a particular library or not, you might want use it.

If you are learning C++, you might not.


Ofcourse it's good that you mention that there is an alternative, the more choice the better.

Cheers,

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.