Ifstream loading number higher than 9?

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
PaperDuckyFTW
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 76
Joined: Sat Apr 03, 2010 5:08 am
Programming Language of Choice: C++

Ifstream loading number higher than 9?

Post 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
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Ifstream loading number higher than 9?

Post by short »

for(int z=0; z<10; z++)
what? why the 10
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Ifstream loading number higher than 9?

Post 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.
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
User avatar
M_D_K
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1087
Joined: Tue Oct 28, 2008 10:33 am
Favorite Gaming Platforms: PC
Programming Language of Choice: C/++
Location: UK

Re: Ifstream loading number higher than 9?

Post 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?
Gyro Sheen wrote:you pour their inventory onto my life
IRC wrote: <sparda> The routine had a stack overflow, sorry.
<sparda> Apparently the stack was full of shit.
qpHalcy0n
Respected Programmer
Respected Programmer
Posts: 387
Joined: Fri Dec 19, 2008 3:33 pm
Location: Dallas
Contact:

Re: Ifstream loading number higher than 9?

Post 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 ;)
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: Ifstream loading number higher than 9?

Post 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.
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
PaperDuckyFTW
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 76
Joined: Sat Apr 03, 2010 5:08 am
Programming Language of Choice: C++

Re: Ifstream loading number higher than 9?

Post 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.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: Ifstream loading number higher than 9?

Post 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 :|
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
PaperDuckyFTW
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 76
Joined: Sat Apr 03, 2010 5:08 am
Programming Language of Choice: C++

Re: Ifstream loading number higher than 9?

Post 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.
User avatar
dandymcgee
ES Beta Backer
ES Beta Backer
Posts: 4709
Joined: Tue Apr 29, 2008 3:24 pm
Current Project: https://github.com/dbechrd/RicoTech
Favorite Gaming Platforms: NES, Sega Genesis, PS2, PC
Programming Language of Choice: C
Location: San Francisco
Contact:

Re: Ifstream loading number higher than 9?

Post 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.
Falco Girgis wrote:It is imperative that I can broadcast my narcissistic commit strings to the Twitter! Tweet Tweet, bitches! :twisted:
User avatar
MadPumpkin
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 484
Joined: Fri Feb 13, 2009 4:48 pm
Current Project: Octopia
Favorite Gaming Platforms: PS1-3, Genesis, Dreamcast, SNES, PC
Programming Language of Choice: C/++,Java,Py,LUA,XML
Location: C:\\United States of America\Utah\West Valley City\Neighborhood\House\Computer Desk

Re: Ifstream loading number higher than 9?

Post 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.
While Jesus equipped with angels, the Devil's equipped with cops
For God so loved the world that he blessed the thugs with rock
Image
Image
Image
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Ifstream loading number higher than 9?

Post 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:
PaperDuckyFTW
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 76
Joined: Sat Apr 03, 2010 5:08 am
Programming Language of Choice: C++

Re: Ifstream loading number higher than 9?

Post 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
User avatar
k1net1k
Chaos Rift Maniac
Chaos Rift Maniac
Posts: 563
Joined: Sun Nov 07, 2010 2:58 pm
Contact:

Re: Ifstream loading number higher than 9?

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