Page 1 of 1

Ifstream loading number higher than 9?

Posted: Thu May 26, 2011 10:36 am
by PaperDuckyFTW
Heya fellas
I've tried googling for a solution, but I haven't been successful. For a loooong looong time I have been using this format for loading a tile map:

Code: Select all

0 0 0 0 0 0 0
0 0 1 1 2 0 0
0 0 1 1 2 0 0
However, when I try and load a number which is higher then 9, when I try and render it, it just draws a black box where the tile should be. After some thinking, I think my program isn't loading a number higher then 9. Anyone know a solution, or a better map loading method/format?

Here are the loading and map rendering functions:
Rendering Function

Code: Select all

void Level::RenderMap()
{
	for(int x=0; x<mapW*2; x++)
	{
		for(int y=0; y<mapH*2; y++)
		{
			for(int z=0; z<10; z++)
			{
				if(tileArray[y][x] == z)
				{
					tiles->DrawSubImage(x*tileW - camera.x, y*tileH - camera.y, 32, 32, screen, z, 0);
				}					
			}	
		}
	}
}

Code: Select all

bool Level::LoadTiles()
{
	mapfile.open(leveldir, ios::in);

	if(!mapfile.is_open() )
	{
		cout << "Could not load " << leveldir <<"."<<endl;
		return false;
	}
	
	for( int x=0; x < mapW; x++ )
	{
		for(int y=0; y<mapH; y++)
		{
			mapfile >> tileArray[x][y];
		}
	}
	mapfile.close();
	return true;
}
I apologize in advance, its late at night and idk how good this post is lol. But I would be appreciated if someone could give me a pointer to a solution or a better map loading system. Thanks for your time

Re: Ifstream loading number higher than 9?

Posted: Thu May 26, 2011 12:00 pm
by short
for(int z=0; z<10; z++)
what? why the 10

Re: Ifstream loading number higher than 9?

Posted: Thu May 26, 2011 1:18 pm
by Ginto8
short wrote:
for(int z=0; z<10; z++)
what? why the 10
the layers, which is the issue. It looks like he tried to make more layers, then utterly failed because of this little line:

Code: Select all

if(tileArray[y][x] == z)
It shows that he fails to understand how layers work. What he needs instead is a 3D array, and to add the Z component as an index, and to add in Z component processing in the map file.

Re: Ifstream loading number higher than 9?

Posted: Thu May 26, 2011 1:48 pm
by M_D_K
Ginto8 wrote:
short wrote:
for(int z=0; z<10; z++)
what? why the 10
the layers, which is the issue. It looks like he tried to make more layers, then utterly failed because of this little line:

Code: Select all

if(tileArray[y][x] == z)
It shows that he fails to understand how layers work. What he needs instead is a 3D array, and to add the Z component as an index, and to add in Z component processing in the map file.
Could you be more of a dick?

Re: Ifstream loading number higher than 9?

Posted: Thu May 26, 2011 1:54 pm
by qpHalcy0n
Oh come now, he's only 14....he's got several prime douchebag years ahead of him.

...we're only scratching the surface ;)

Re: Ifstream loading number higher than 9?

Posted: Thu May 26, 2011 3:16 pm
by MadPumpkin
qpHalcy0n wrote:Oh come now, he's only 14....he's got several prime douchebag years ahead of him.

...we're only scratching the surface ;)
Eh don't worry, I'm a douche bag too. But I usually think ahead when I am, "Would it make me a dick to submit this?... Oh well took too long to type.*Submit*"

But on a more important note, all you really need to do is make this into a 3D array. instead of maparray[x][y] do, maparray[x][y][z]. z, being your layers if you're using OpenGL then whenever you do a texture switch, make sure you draw all of that same texture before moving to the next texture, that's what I do to speed up the texturing. Switching your texture is slow as hell in computing time. Rapes your render/draw time. Using multiple layers makes this process even easier :P

EDIT: Oh and to try to fix your problem however you're doing it (Not sure how, I'm in school and I don't have much time sorry). I do it by reading everything into the array and then stop and stop at a space and skip it. Instead of read an int then a space, then an int then space etc. I'm not sure how you're doing it but I gotta go sorry, maybe can help more when I get home.

Re: Ifstream loading number higher than 9?

Posted: Thu May 26, 2011 4:37 pm
by PaperDuckyFTW
Actually, the 'z' part has nothing to do with layers. Before I had something like this:

Code: Select all

if(map[y][x] == 0)
{
     draw tile
}
if(map[y][x] == 1)
{
     draw different tile
}
if(map[y][x] == 2)
{
     draw different tile again
}
What if I had over 100 tiles? Fuck putting them in individually, so I just put a loop where it changes the part of the tilesheet. I had the 10 there in the loop because for some reason, it won't load any number higher then 9. I already have a method for having different layers (even though i am going to rewrite it with the 3D array. My method is really bloated) But before that I was wanting to focus on tackling why the program doesn't read numbers higher then 9 from the file.

Re: Ifstream loading number higher than 9?

Posted: Thu May 26, 2011 5:49 pm
by Ginto8
M_D_K wrote:Could you be more of a dick?
Sorry, I wasn't trying to sound condescending. I was just trying to say is that it shows that he fails to understand what he's trying to do. I was also suggesting a method to make it work, but I guess no one cares about that :|

Re: Ifstream loading number higher than 9?

Posted: Thu May 26, 2011 6:52 pm
by PaperDuckyFTW
hey man thats cool, I didn't give enough information about the code so I understand why you assumed it was the layers :)

Thanks for your suggestion as well, I've known about 3D arrays for some time but I'll start working on implimenting it after I figure out the loading problem.

Re: Ifstream loading number higher than 9?

Posted: Thu May 26, 2011 7:49 pm
by dandymcgee
Are you sure it's not loading numbers higher than nine? I believe streams by default use space as a delimiter, so as long as you put one space between each number and you're reading it into an int it should work.

Re: Ifstream loading number higher than 9?

Posted: Thu May 26, 2011 8:28 pm
by MadPumpkin
I agree with DandyMcGee, but honestly without more information (MY<- thoughts:) what it looks like is that your tile sheet is a certain width, and when your draw function finds 10, instead of going 32 down THEN going to first tile, it just goes back to the beginning where your transparent or blank tile should be.

Code: Select all

THESE BEING TILES:
[00][01][02][03][04]
[05][06][07][08][09]
when I acces 05 I forget to go down to the next line to take that image and instead select 01
Don't get me wrong, I don't know if this is the issue, but I've had a chance to look at your code now, and without the rest I can't really see any other reason.

Re: Ifstream loading number higher than 9?

Posted: Thu May 26, 2011 8:48 pm
by Falco Girgis
qpHalcy0n wrote:Oh come now, he's only 14....he's got several prime douchebag years ahead of him.

...we're only scratching the surface ;)
I just laughed out loud. :lol:

Re: Ifstream loading number higher than 9?

Posted: Fri May 27, 2011 3:12 am
by PaperDuckyFTW
SOLVED

I really, truly am sorry guys, I want to partake in a socially frowned event commonly known as jumping off a bridge.
There was nothing wrong with the code. There was nothing wrong with any of the loading or the format of the map.

It was my rendering function. For some reason, it wasn't rendering it.
Thanks everyone for your help and I really cant apologize enough for wasting your time haha
Take care and mods; feel free to delete this topic

Re: Ifstream loading number higher than 9?

Posted: Fri May 27, 2011 4:32 am
by k1net1k
qpHalcy0n wrote:Oh come now, he's only 14....he's got several prime douchebag years ahead of him.

...we're only scratching the surface ;)
lol