Skip to main content
GameDev.net gamedev.net
🔒 Locked

2D Array Problem

Started by StormUK Dec 22, 2002 at 4:29 PM 4 replies 200+ views
Original Post
StormUK
StormUK
This is my full code:
  
#include <SDL/sdl.h>
#include <windows.h>

void DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B) {

	Uint32 color = SDL_MapRGB(screen->format, R, G, B);
	switch (screen->format->BytesPerPixel)
	{
	    case 1:
		{
	        Uint8 *bufp;
			bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
			*bufp = color;
		}
		break;
		case 2:
		{
	        Uint16 *bufp;
			bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
			*bufp = color;
		}
		break;
		case 3:
		{
	        Uint8 *bufp;
			bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
			if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
	        {
			bufp[0] = color;
			bufp[1] = color >> 8;
			bufp[2] = color >> 16;
			} else {
			bufp[2] = color;
			bufp[1] = color >> 8;
			bufp[0] = color >> 16;
			}
		}
		break;
		case 4:
		{
	        Uint32 *bufp;
			bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
			*bufp = color;
		}
		break;
	}
}

void Slock(SDL_Surface *screen) {
	if ( SDL_MUSTLOCK(screen) ) {
	    if ( SDL_LockSurface(screen) < 0 ) {
			return;
		}
	}
}

void Sulock(SDL_Surface *screen) {
	if ( SDL_MUSTLOCK(screen) ) {
		SDL_UnlockSurface(screen);
	}
}

void DrawLandscape(SDL_Surface *screen);




int main(int argc, char *argv[]) {

	SDL_Surface *screen;

	SDL_Init(SDL_INIT_VIDEO);
	atexit(SDL_Quit);

	screen = SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE|SDL_DOUBLEBUF);

	bool done=false;

	DrawLandscape(screen);

	while(!done) {
		SDL_Event event;
		while (SDL_PollEvent(&event)) {
			if ( event.type == SDL_QUIT ) {
				done=true;  
			}			
		}
	}

	return 0;
}

void DrawLandscape(SDL_Surface *screen) {
	
	int Landscape[640][480];
	
	int height=0;
	int tmp=0;
	int old1=199;
	int old2=200;
	int x;
	int y;
	
	for(x=0;x<640;x++){
		for(y=0;y<480;y++) {
			Landscape[x][y]=0;
		}
	}
	
	srand(GetTickCount());
	
	for(x=0;x<640;x++) {
		if(old2 >= old1){
			height=old1-(rand()%5);
			old2=old1-2;
			old1=height;
		} else {
			height=old1+(rand()%5);
			old2=old1+2;
			old1=height;
		}
		for(y=0;y<height;y++) {
			Landscape[x][y]=1;

		}
	}

	Slock(screen);
	for(x=0;x<640;x++){
		for(y=0;y<480;y++) {
			if(Landscape[x][y]==1){
				DrawPixel(screen,x,y,0,0,255);
			} else {
				DrawPixel(screen,x,y,0,255,0);
				
				
			}
		}
	}
	Sulock(screen);
	SDL_Flip(screen);


}
  
And this is the function with the array that i''m havin a problem with:
  
void DrawLandscape(SDL_Surface *screen) {
	
	int Landscape[640][480];
	
	int height=0;
	int tmp=0;
	int old1=199;
	int old2=200;
	int x;
	int y;
	
	for(x=0;x<640;x++){
		for(y=0;y<480;y++) {
			Landscape[x][y]=0;
		}
	}
	
	srand(GetTickCount());
	
	for(x=0;x<640;x++) {
		if(old2 >= old1){
			height=old1-(rand()%5);
			old2=old1-2;
			old1=height;
		} else {
			height=old1+(rand()%5);
			old2=old1+2;
			old1=height;
		}
		for(y=0;y<height;y++) {
			Landscape[x][y]=1;

		}
	}

	Slock(screen);
	for(x=0;x<640;x++){
		for(y=0;y<480;y++) {
			if(Landscape[x][y]==1){
				DrawPixel(screen,x,y,0,0,255);
			} else {
				DrawPixel(screen,x,y,0,255,0);
				
				
			}
		}
	}
	Sulock(screen);
	SDL_Flip(screen);


}
  
My problem is, the array "int Landscape[640][480];" . When i run the game, it crashs. But if i make the array "int Landscape[540][480];", it doesn''t crash. any ideas why, or any suggestions as how to fix it? Thanks.
Burning_Ice
Burning_Ice
hi

the problem is that this call tries to allocate about 1,2 MB of data on the stack (640*480*4 bytes), which is just to much to fit there, so it crashes.
For arrays of this size you''ll have to use int* Landscape = new int [640*480] and calculate the indices as [y*640+x] instead of [x][y] since you can''t have two-dimensional dynamic arrays.
and at the end of the program you have to call delete [] Landscape so you don''t get memory leaks.

hope that helps



Runicsoft -- home of my open source Function Parser and more
StormUK
StormUK
You Star!

Thanks, that works gr8!
:D
readme
readme
quote:
Original post by Burning_Ice
For arrays of this size you''ll have to use int* Landscape = new int [640*480] and calculate the indices as [y*640+x] instead of [x][y] since you can''t have two-dimensional dynamic arrays.



Whoa! Of course you can have two- and moredimensional dynamic arrays!
davepermen
davepermen
quote:
Original post by readme
[quote]Original post by Burning_Ice
For arrays of this size you''ll have to use int* Landscape = new int [640*480] and calculate the indices as [y*640+x] instead of [x][y] since you can''t have two-dimensional dynamic arrays.



Whoa! Of course you can have two- and moredimensional dynamic arrays!

actually, no, and its discussed several times in here during the last weeks. arrays are technically always 1d, so use them that way. at least in c/c++ thats the only way you can savely handle them..

"take a look around" - limp bizkit
www.google.com
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
readme
readme
quote:
actually, no, and its discussed several times in here during the last weeks. arrays are technically always 1d, so use them that way. at least in c/c++ thats the only way you can savely handle them...


well, since the search function doesn''t work could you give me a link to said thread?

Topic Locked

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

Sign in to reply to this topic.