Loading tiles from a file

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

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: Loading tiles from a file

Post by PaperDuckyFTW »

Thoughout this issue, I have printed the array into the console to see if it was working correctly. Even though the file is completely filled with '2's, the output to the console just reads '0'. I can post a screenshot if its needed. At the moment, these are my loading and drawing functions. The loading function works perfectly fine, its just the drawing thats not working :\

Code: Select all

bool LoadLevel( const std::string& filename )
{
	std::ifstream file;
	file.open( filename.c_str(), ios::in );
	char temp;

	if(!file.is_open())
	{
		return false;
	}

	for( int x = 0; x < 40; x++ )
	{
		for( int y = 0; y < 30; y++ )
		{
			file >> map[y][x];
			//map[y][x] = temp;
			cout<<map[y][x];
		}
		
	}
	file.close();
	return true;
}

void draw_level()
{
	for( int x = 0; x < 40; x++ )
	{
		for( int y = 0; y < 30; y++ )
		{
			if( map[y][x] == 0 )
			{
				draw_surface( x * 32 - camera.x, y * 32 - camera.y, tiles, screen, &clip_tiles[0] ); //draw the first tile in the sprite sheet
			}
			if( map[y][x] == 2 )
			{
				draw_surface( x * 32 - camera.x, y * 32 - camera.y, tiles, screen, &clip_tiles[2] ); //draw the third sprite in the tilesheet
			}
		}
	}
}
Sorry this is a bit off topic, but how is your text based rpg coming along, dragoon? I remember ages ago watching a youtube vid of its development, and it helped motivate me as I was planning on making one myself when I was starting with c++. Haha i guess i owe you a thanks on that part, and best of luck with it. When I have the laod funciton read the file, it reads '50' for some reason...idk. When i have it read to the array like file<<map[y][x] instead of reading '50' it reads '0' as mentioned.
User avatar
ultimatedragoon69
Chaos Rift Regular
Chaos Rift Regular
Posts: 122
Joined: Tue Oct 28, 2008 1:57 pm
Current Project: Pangea's quest (text ~tile~ based rpg)
Favorite Gaming Platforms: Dreamcast, PC, playstation 1, Virtual Boy, Snes
Programming Language of Choice: c++
Contact:

Re: Loading tiles from a file

Post by ultimatedragoon69 »

I'm unsure why you have a char temp and your not using it. if it was me and i wanted to use the buffer variable you have i would do something like this->

Code: Select all

file >> temp;
map[y][x] = temp;
cout<<map[y][x];
Another thing i wanted to note on in your code is where your drawing the tile why you dont just use map[y][x] as &clip_tiles[map[y][x]]. If your text file is only integers you could also just load int's instead of char's. Would probably make things alot easier.

Code: Select all

bool LoadLevel( const std::string& filename )
{
   std::ifstream file;
   file.open( filename.c_str(), ios::in );
   char temp;

   if(!file.is_open())
   {
      return false;
   }

   for( int x = 0; x < 40; x++ )
   {
      for( int y = 0; y < 30; y++ )
      {
         file >> temp;
         map[y][x] = temp;
         cout<<map[y][x];
      }
      
   }
   file.close();
   return true;
}

void draw_level()
{
   for( int x = 0; x < 40; x++ )
   {
      for( int y = 0; y < 30; y++ )
      {
/*
         if( map[y][x] == 0 )
         {
            draw_surface( x * 32 - camera.x, y * 32 - camera.y, tiles, screen, &clip_tiles[0] ); //draw the first tile in the sprite sheet
         }
         if( map[y][x] == 2 )
         {
            draw_surface( x * 32 - camera.x, y * 32 - camera.y, tiles, screen, &clip_tiles[2] ); //draw the third sprite in the tilesheet
         }
*/
        draw_surface(x*32-camera.x, y*32-camera.y, tiles, screen,  &clip_tiles[map[y][x]] );
      }
   }
}
I'm not 100% sure why your reading only 0's could you show me the text file you are using?

Oh as far as Pangea's Quest i had to once again put in on the back burner because of real life stuff, I'm still working on it (even though its near stand still).
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: Loading tiles from a file

Post by PaperDuckyFTW »

i really have no ideae whats going on.
Here is part of my level file:
It doesnt matter if I have it filled with 0's, 2's or 1's, its just not reading anything.

Code: Select all

200000000000000000000000000002
222222000000000001122222222222
222222222222222222222222222222
222222222222222222222222222222
222222222222222222222222222222
222222222222222222222222222222
222222222222222222222222222222
222222222222222222222222222222
222222222222222222222222222222
222222222222222222222222222222
222222222222222222222222222222
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Loading tiles from a file

Post by K-Bal »

Put spaces between the numbers.
wearymemory
Chaos Rift Junior
Chaos Rift Junior
Posts: 209
Joined: Thu Feb 12, 2009 8:46 pm

Re: Loading tiles from a file

Post by wearymemory »

K-Bal wrote:Put spaces between the numbers.
That shouldn't matter if he's reading in chars.
OP, is your map variable a 2-dimensional array of chars? If so, then you should use character literals when comparing the values in your draw function.

Code: Select all

'0' == 48
If not, then the simplest solution would be to make your map variable a 2-dimensional array of chars, and make sure that all comparisons are done using character literals.
User avatar
xiphirx
Chaos Rift Junior
Chaos Rift Junior
Posts: 324
Joined: Mon Mar 22, 2010 3:15 pm
Current Project: ******** (Unkown for the time being)
Favorite Gaming Platforms: PC
Programming Language of Choice: C++
Contact:

Re: Loading tiles from a file

Post by xiphirx »

K-Bal wrote:Put spaces between the numbers.
Do this.

Then

Code: Select all

bool LoadLevel( const std::string& filename )
{
   std::ifstream file;
   file.open( filename.c_str(), ios::in );

   if(!file.is_open())
   {
      return false;
   }

   for( int x = 0; x < 40; x++ )
   {
      for( int y = 0; y < 30; y++ )
      {
         file >> map[x][y];
         cout<<map[x][y];
      }
      
   }
   file.close();
   return true;
}

void draw_level()
{
   for( int x = 0; x < 40; x++ )
   {
      for( int y = 0; y < 30; y++ )
      {
         if( map[x][y] == 0 )
         {
            draw_surface( x * 32 - camera.x, y * 32 - camera.y, tiles, screen, &clip_tiles[0] ); //draw the first tile in the sprite sheet
         }
         if( map[x][y] == 2 )
         {
            draw_surface( x * 32 - camera.x, y * 32 - camera.y, tiles, screen, &clip_tiles[2] ); //draw the third sprite in the tilesheet
         }
      }
   }
}
StarCraft II Zerg Strategy, open to all levels of players!

Looking for paid work :< Contact me if you are interested in creating a website, need a web design, or anything else you think I'm capable of :)
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Loading tiles from a file

Post by K-Bal »

wearymemory wrote:
K-Bal wrote:Put spaces between the numbers.
That shouldn't matter if he's reading in chars.
Yeah, but the white space is neccessary when using >>.
wearymemory
Chaos Rift Junior
Chaos Rift Junior
Posts: 209
Joined: Thu Feb 12, 2009 8:46 pm

Re: Loading tiles from a file

Post by wearymemory »

K-Bal wrote:
wearymemory wrote:
K-Bal wrote:Put spaces between the numbers.
That shouldn't matter if he's reading in chars.
Yeah, but the white space is neccessary when using >>.
This is true when reading strings, but I've tested the following code and this is its resulting output when using chars (as stated in my post you quoted):

main.cpp:

Code: Select all

#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char** argv) {
    ifstream fin("data.txt");
    char c;
    while (fin >> c) {
        cout << "Read from file: " << c << endl;
    }
    return 0;
}


data.txt:

Code: Select all

123
456
789
output:

Code: Select all

Read from file: 1
Read from file: 2
Read from file: 3
Read from file: 4
Read from file: 5
Read from file: 6
Read from file: 7
Read from file: 8
Read from file: 9
Press [Enter] to close the terminal ...
It shows that adding spaces between the numbers is unnecessary, even when using the >> operator on an ifstream instance. I believe that the OP's answer is dependent on the question I asked in my previous post.
K-Bal
ES Beta Backer
ES Beta Backer
Posts: 701
Joined: Sun Mar 15, 2009 3:21 pm
Location: Germany, Aachen
Contact:

Re: Loading tiles from a file

Post by K-Bal »

You are right.
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: Loading tiles from a file

Post by PaperDuckyFTW »

YESSSSSS!!!!!!!!!!!!!!!!!!!!

Okay i am estatic but also feel like smashing my face agsint a wall. Im soo terribly sorry for wasting all of your time. The problem wasnt loading or drawing the tiles. The problem was HOW the tiles were loaded. It was reading the file, but there wasnt any breaks in it:

for example, instead of it reading somethig like this:

Code: Select all

0 2 2 2 0
0 0 0 0 0
0 0 2 0 0
0 2 1 2 0


It was reading the file like this:

Code: Select all

02220000000020002120
So it wasnt drawing as a map, it was drawing as a single line.
Thankyou to everyone who helped, it has helped me learn more about c++ which i never knew before, plus it now means I can move onto a game entity or state machine.
Now to jsut figure out how to make it start reading another line, but im armed with lazyfoo's tutorial so i have an idea. Thanks again for all your help, and haah sorry it took so damn long. Take care yall



EDIT: Dont worry, I now know EXACTLY what was wrong, I was reading the file backwards, then drawing the tiles, reading the array backwards. Kinda confusing but it works perfectly fine now =]
Post Reply