Passing a 2d array...

I'm trying to pass a 2d array into a function to modify it... But it doesn't really work.

here is where I call drawCar:
1
2
//inside main function
playerCar.drawCar(display.outputBuffer);

1
2
3
4
5
6
7
8
9
void Cars::drawCar(CHAR_INFO screen[][80]) {

	for(int Y=0; Y<3; Y++) {
		for(int X=0; X<3; X++) {
			screen[(int)y][(int)x]= carVisual[Y][X];
                     //this will not work as I expected..
		}
	}
}

Also here is where the outputBuffer is declared.. Maybe it will help.
1
2
3
4
5
class Display {
//some more stuff here
public:
CHAR_INFO outputBuffer[45][80];
//more stuff here 


I know I shouldn't use multidimensional arrays... because they're evil, as I noticed ... So a little help please? Also if you could recommend a easier way to do this please do.


what are 'y' and 'x' and why are you casting them to int?

Also, you're only changing screen[y][x], which doesn't really make sense unless you're changing y,x in that loop (which it doesn't look like you are.

You probably meant to do this:

 
screen[y+Y][x+X] = carVisual[Y][X];
y,x are from *this... they remember the position of the player. They're doubles so i had to cast them to ints. Also I use this loop because carVisual is an array of [3][3] of CHAR_INFO's so I need to put every CHAR_INFO into the screen buffer to show it on the screen. I'm actually looping through the carVisual rather than the screen, and putting whatever is there in the screen buffer at the [y][x] coordinates.
Last edited on
I'm actually looping through the carVisual rather than the screen, and putting whatever is there in the screen buffer at the [y][x] coordinates.


Right so your car is 3x3 but you're only drawing it to a 1x1 space.

In your loop you're putting all 9 carVisual elements at 'screen[y][x]'. Since y and x don't change in that loop, that means each iteration of that loop is erasing the output of the previous iteration.

If x,y are both zero, you're doing this:

1
2
3
4
screen[0][0] = carVisual[0][0];  // draw the upper-left corner of the car
screen[0][0] = carVisual[0][1];  // but now you just erased that corner and drew the top of the car instead
screen[0][0] = carVisual[0][2];  // and now you erased that and drew the upper-right corner there instead.
//.. etc 


You need to change the coords to where you're drawing each part of the car graphic:

 
screen[y+Y][x+X] = carVisual[Y][X];  // like this 


But beware that this may cause you to overflow your array. IE: If y == 44, then y+1 will overflow your array and cause memory corruption problems (trying to draw part of the car "off screen") So be sure to check for that.
Right so your car is 3x3 but you're only drawing it to a 1x1 space.

ups... I failed. Just saw that mistake :)

Thanks! You're pretty amazing seeing that. Kinda embarissing.. but now it makes sense :P
Topic archived. No new replies allowed.