PDA

View Full Version : Multidimensional Arrays when using MFC?



10-09-2000, 02:57 PM
Hi! My problem is not really a OpenGL-Problem; it's more a basic MFC-programming-Problem.
I have to code an OpenGL-Example using MFC.
The data of the 3d-objects that i use should be in a 2-dimensional array.
(m objects with n vertices)
When i coded a test-program as a pure win32-application everything was alright; i was able to handle the array with standard c operations.
But these operations do not work when i use MFC. I have no access to the elements of the array. I thing i have to use an CArray-Object, but how can i create a 2-dimensional CArray?
Or is there another way to manage it?

(Sorry for my english, it's not my mother tongue)

DFrey
10-09-2000, 03:13 PM
Using MFC should not change the way c arrays work, so you shouldn't need to use CArray. On the otherhand, if you must use CArray as part of the program, then simply allocate n*m elements for an n by m array, and reference the elements as i*m+j, where 0<=i<n, and 0<=j<m. Or another approach is to use a CArray of CArrays.

[This message has been edited by DFrey (edited 10-10-2000).]

10-10-2000, 01:40 AM
That's it, CArray of CArrays. But how do i have to Code it? Can i access the elements then by coding CArrayname[n][m]?

DFrey
10-10-2000, 06:21 AM
To declare a n by m CArray of Object elements:
typedef CArray<CArray<Object, int>,int> ObjectArray;
ObjectArray myArray;

Since myArray is a CArray of CArray, each CArray has to have its size set first, typically in a constructor for example:

myArray.SetSize(n);
for(int i=0;i<n;i++) myArray[i].SetSize(m);

Then each element can be indexed by myArray[i][j] , where 0<=i<n and 0<=j<m

10-10-2000, 08:27 AM
Hmm, that may work.
Is it correct, that i have to include afxtempl.h ?
Is it possible to transfer the data in this way:
myArray={{1,2,3},{4,5,6},{7,8,9}}?
At present that does not seem work, and at over 1000 values an individual transfer is toilsome.

DFrey
10-10-2000, 09:12 AM
Is it correct, that i have to include afxtempl.h ?
Yes.

Is it possible to transfer the data in this way:
myArray={{1,2,3},{4,5,6},{7,8,9}}?
No that will not work. You will need to read in the data from a file, generate it procedurally, or hard code each element of it.


[This message has been edited by DFrey (edited 10-10-2000).]

10-10-2000, 03:36 PM
Ok , the array theoretically works (thanks to you); i can access the data from everywhere in my code.

But then i tried using this array in the function glBitmap(8, 13, 0,0, 2,0, 10,0, 0,0, myArray[loop1]); . This produced the following error when compiling the program: error C2664: ' glBitmap ': Conversion of the parameter 7 of ' class CArray<unsigned char, unsigned char > * ' in ' const unsigned char * ' not possible

Is there the possibility to convert it with the cast-operator, or can i handle it somehow different?

(Is this complicated data adminstration actually MFC or C++ conditioned? Pascal or pure C seemed to me in former times more simply.)

DFrey
10-10-2000, 04:03 PM
You can not cast a CArray to a simple C type array. If need to use something like glBitmap(8, 13, 0,0, 2,0, 10,0, 0,0, myArray[loop1]); then myArray can not be a CArray. Just use an ordinary array of GLubytes rather than a CArray of CArrays.

Eric
10-10-2000, 10:47 PM
You should consider writing your own 'dynamic array' routines...

That's what I did (I do not use MFC in all my programs so CArray wasn't a solution) and it is pretty easy to do...

Here is the header I am using in DynamicArrays.h (I am sure you'll figure out the source code easily !) :

//************************************************** ************************************************** *******//
//***** *****//
//***** Dynamic Arrays Classes Declaration *****//
//***** *****//
//************************************************** ************************************************** *******//

template <class ValueType> class C_Array1D
{

public:

bool m_bDeleteOnDestroy;
int m_iLo1,m_iHi1;
ValueType *m_pBuffer;

C_Array1D();
void Reset(void);
void Allocate(int iLo1,int iHi1);
void Clear(void);
void ClearPart(int iLo1,int iHi1);
void Delete(void);
void Set(ValueType v);
ValueType& operator()(int i1);
~C_Array1D();

};

template <class ValueType> class C_Array2D
{

public:

bool m_bDeleteOnDestroy;
int m_iLo1,m_iHi1;
int m_iLo2,m_iHi2;
ValueType *m_pBuffer;

C_Array2D();
void Reset(void);
void Allocate(int iLo1,int iHi1,int iLo2,int iHi2);
void Clear(void);
void ClearPart(int iLo1,int iHi1,int iLo2,int iHi2);
void Delete(void);
void Set(ValueType v);
ValueType& operator()(int i1,int i2);
C_Array1D<ValueType>& operator()(int i1);
~C_Array2D();

};

template <class ValueType> class C_Array3D
{

public:

bool m_bDeleteOnDestroy;
int m_iLo1,m_iHi1;
int m_iLo2,m_iHi2;
int m_iLo3,m_iHi3;
ValueType *m_pBuffer;

C_Array3D();
void Reset(void);
void Allocate(int iLo1,int iHi1,int iLo2,int iHi2,int iLo3,int iHi3);
void Clear(void);
void ClearPart(int iLo1,int iHi1,int iLo2,int iHi2,int iLo3,int iHi3);
void Delete(void);
void Set(ValueType v);
ValueType& operator()(int i1,int i2,int i3);
C_Array1D<ValueType>& operator()(int i1,int i2);
C_Array2D<ValueType>& operator()(int i1);
~C_Array3D();
};

/********************************************/
/***** Standard array types definitions *****/
/********************************************/

typedef C_Array1D <bool> C_bool1D;
typedef C_Array1D <char> C_char1D;
typedef C_Array1D <short> C_short1D;
typedef C_Array1D <int> C_int1D;
typedef C_Array1D <long> C_long1D;
typedef C_Array1D <float> C_float1D;
typedef C_Array1D <double> C_double1D;

typedef C_Array2D <bool> C_bool2D;
typedef C_Array2D <char> C_char2D;
typedef C_Array2D <short> C_short2D;
typedef C_Array2D <int> C_int2D;
typedef C_Array2D <long> C_long2D;
typedef C_Array2D <float> C_float2D;
typedef C_Array2D <double> C_double2D;

typedef C_Array3D <bool> C_bool3D;
typedef C_Array3D <char> C_char3D;
typedef C_Array3D <short> C_short3D;
typedef C_Array3D <int> C_int3D;
typedef C_Array3D <long> C_long3D;
typedef C_Array3D <float> C_float3D;
typedef C_Array3D <double> C_double3D;

Of course, we could extend it to C_ArrayND but I haven't had to use 4+ dimensions yet...

Regards.

Eric