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

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
ibly31
Chaos Rift Junior
Chaos Rift Junior
Posts: 312
Joined: Thu Feb 19, 2009 8:47 pm
Current Project: Like... seven different ones
Favorite Gaming Platforms: Xbox 360, Gamecube
Programming Language of Choice: C++, ObjC
Location: New Jersey.

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

Post 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...
Image
Twitter
Website/Tumblr
My Projects

The best thing about UDP jokes is that I don’t care if you get them or not.
User avatar
Joeyotrevor
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 62
Joined: Thu Jan 22, 2009 6:24 pm
Programming Language of Choice: C++

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

Post 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].

Code: Select all

eb 0c 48 65 6c 6c 6f 20 77 6f 72 6c 64 21 31 d2 8e c2 30 ff b3 0a bd 02 7c b9 0b 00 b8 00 13 cd 10 eb fe
Scoody
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 65
Joined: Fri Feb 06, 2009 2:07 pm

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

Post 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])
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

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

Post 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.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
User avatar
programmerinprogress
Chaos Rift Devotee
Chaos Rift Devotee
Posts: 632
Joined: Wed Oct 29, 2008 7:31 am
Current Project: some crazy stuff, i'll tell soon :-)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++!
Location: The UK
Contact:

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

Post 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.
---------------------------------------------------------------------------------------
I think I can program pretty well, it's my compiler that needs convincing!
---------------------------------------------------------------------------------------
And now a joke to lighten to mood :D

I wander what programming language anakin skywalker used to program C3-PO's AI back on tatooine? my guess is Jawa :P
User avatar
deryni21
Chaos Rift Regular
Chaos Rift Regular
Posts: 124
Joined: Wed Dec 24, 2008 9:55 pm

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

Post 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
Image
Post Reply