| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
2d pointers, the beaten topic!
So I'm making a game, go figure, and I'm trying to construct a map class that holds tiles. Anyway, I make and int[][] so as to straightup assign tiles like:
int Tiles[][]={{1,1},{2,2},}; which I can't do with int**, so I convert Tiles[][] to int** and pass to a function like so: myMap.UpdateMap((int**)&Tiles,...); So after all is said and done, it compiles but gives me the following error when I try to run it: Unhandled exception at 0x00496e16 in CreateDevice.exe: 0xC0000005: Access violation reading location 0x00000000. Code:
class cMap
{
private:
const static int TILESIZE=64;
int width, height;
int columns, rows;
public:
cMap(){}
int **Tiles;
void UpdateMap(int** arr, int row, int col)
{
memcpy(&Tiles, &arr, sizeof(int)*width*height);
rows=row;
height=row*TILESIZE;
columns=col;
width=col*TILESIZE;
}
void Paint(LPDIRECT3DDEVICE9 g_pd3dDevice)
{
g_pd3dSprite->Begin(D3DXSPRITE_ALPHABLEND);
for (int x=0; x<columns; x++){
for (int y=0; y<rows; y++){
RECT SrcRect;
//it's right here that it gives me beef:
SrcRect.left=(Tiles[x][y])*TILESIZE;
SrcRect.right=SrcRect.left+TILESIZE;
SrcRect.top=0;
SrcRect.bottom=TILESIZE;
g_pd3dSprite->Draw(g_pd3dTexture,
&SrcRect,
NULL,
&D3DXVECTOR3( (FLOAT)x*TILESIZE, (FLOAT)y*TILESIZE, 0.0f), //position
D3DCOLOR(0xFFFFFFFFF));
}
}
g_pd3dSprite->End();
}
}myMap;
and in the main method: Code:
int arr[3][3]={{2,2,2},{2,2,2},{2,2,2}};
myMap.UpdateMap((int**)&arr, 3,3);
I'd keep trying on my own, but I've been trying for days and i'm dumb. Thanks in advance. |
|
#2
|
|||
|
|||
|
For starters you're not allocating any memory for your "int **Tiles;" member var before doing a memcpy on it. Secondly, when it comes to allocating a multidimensional array, all dimensions except the first must be constant so if the size of array is going to be dynamic you're going to need to come up with a different algorithm ... hope this helps a bit.
Code:
int nNum = 99; char (*pszArray)[10] = new char[nNum][10]; delete[] pszArray; |
|
#3
|
|||
|
|||
|
Code:
int arr[3][3]={{2,2,2},{2,2,2},{2,2,2}};
when passing an array don't use the & operator. correct: Code:
|
|
#4
|
|||
|
|||
|
Quote:
aha! there we go, thank you! Actually, I'm not going to even bother anymore.. I decided to use one of those flexible functions, or whatever they're called witht he function(int width, int height, ...) and reading every value after height as an entry in a 2d array. That way, i didn't have to allocate memory for 2 arrays and ditch the first, and I didn't have to bother with how to reference a 2d array. |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > 2d pointers, the beaten topic! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|