Page 1 of 1

Tile Map data? How to??? PLEASE HELP!

Posted: Fri Mar 06, 2009 6:58 pm
by ibly31
I'm making a tilemap editor, and I was about to make it have multiple layers, when I decided to change the size to a 16*16 map, and Its REALLY screwing up. I wanted to be able to read from files, so I couldn't reference the map data publicly, so this wouldn't work:

Code: Select all

int levelData[16][16] = {asdf,adsf,asdf,asdf,etc,etc};
void drawLevel()
		for(int x = 0; x < 17; x++){
			for(int y = 0; y < 17; y++){
						int sprite = getNextFreeID();
						dbSprite(sprite,y*32,x*32,levelData[x][y]);
						dbShowSprite(sprite);
			}
		}
}
void eraseLevel(){
	for(int i = 1; i < 500; i++){
		if(dbSpriteExist(i)){
			dbDeleteSprite(i);
		}else{
			return;
		}
	}
}


So I am passing it in the drawLevel() function. It's a 16*16 map, and It's made up of ALL 1's.(I checked using the step debugger). Now, the code for the drawLevel() function is:

Code: Select all

void drawLevel(int (&arrayz)[16][16]){
		for(int x = 0; x < 17; x++){
			for(int y = 0; y < 17; y++){
						int sprite = getNextFreeID();
						dbSprite(sprite,y*32,x*32,arrayz[x][y]);
						dbShowSprite(sprite);
			}
		}
}
And I call it like: drawLevel(levelData);

Am I doing something wrong? It's like drawing 9 and a half rows of sprites, and when I move my selector to the edge(column 1 or column 16) it draws another cursor on the other side...

Re: Tile Map data? How to??? PLEASE HELP!

Posted: Fri Mar 06, 2009 9:53 pm
by Joeyotrevor
I don't know if this is your problem, but you are doing

Code: Select all

for(; x < 17;)...
when your array is size 16. Also with two dimensional arrays, the y comes before x, so it would be array[y][x].

Re: Tile Map data? How to??? PLEASE HELP!

Posted: Sat Mar 07, 2009 2:37 am
by Scoody
Joeyotrevor wrote:I don't know if this is your problem, but you are doing

Code: Select all

for(; x < 17;)...
when your array is size 16. Also with two dimensional arrays, the y comes before x, so it would be array[y][x].
It's accessing some "random" memory at the end, should be 16 instead, since it starts counting from 0.

Code: Select all

for(int x = 0; x < 16; x++)
  // do stuff
You're passing your array in a weird manner, how about this:

Code: Select all

void drawLevel(int arrayz[][16])

Re: Tile Map data? How to??? PLEASE HELP!

Posted: Sat Mar 07, 2009 12:59 pm
by MarauderIIC
This isn't related to your question, but it'll make your life easier. If you do:

Code: Select all

const size_t MAP_WIDTH = 16;
const size_t MAP_HEIGHT = 16;
int levelData[MAP_WIDTH][MAP_HEIGHT] ...;
...
      for(int x = 0; x < MAP_WIDTH; x++){
         for(int y = 0; y < MAP_HEIGHT; y++){
                  int sprite = getNextFreeID();
                  dbSprite(sprite,y*32,x*32,levelData[x][y]);
                  dbShowSprite(sprite);
         }
      }
}
And reuse the same constants (define them in a header if you have to) wherever you referenced map height/map width, you'll have an easier time changing the size in the future. This is generally good practice, as otherwise you're using Magic numbers.

Re: Tile Map data? How to??? PLEASE HELP!

Posted: Sat Mar 07, 2009 1:01 pm
by programmerinprogress
constants are great, they are simple yet so powerful ;)

They will also save you so much time and will make your code much easier to read.

Re: Tile Map data? How to??? PLEASE HELP!

Posted: Sat Mar 07, 2009 8:56 pm
by deryni21
just thinking maru mayvbe you could put some of this level drawing stuff into the code snippets forum? It seems to be a commonly asked question