Jump to content

  • Log In with Google      Sign In   
  • Create Account


Tile map errors


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 Crusable   Members   -  Reputation: 592

Like
0Likes
Like

Posted 22 June 2013 - 05:15 PM

Hello, I am trying to make a tile map for the first time and it is not working. I am using SFML 2.0. 

 

TileMap::LoadTileMap:

void TileMap::loadTileMap(std::string fileName, sf::Image img){

	std::cout << "Creating a new tile map\n";

	m_MapFile.open(fileName, std::ios::in);

	if(!m_MapFile.is_open()) {
		
		std::cout << "Could not find map file\n";
		return;
	}else{

		std::cout << "Found the map file\n";

		Tile tempTile;
		sf::Texture tempTex;
		int tileX = 0; //current tile placment
		int tileY = 0; //current tile placement
		int tileNumX; //current tiles placment in the row
		int tileNumY; //current tiles row

		//while not the end of the map, get a line and set
		//tiles based on the map
		while(!m_MapFile.eof()){

			std::string line;
			std::getline(m_MapFile, line);

			for(int i = 0; i != line.size(); ++i){

				//a space seperates the tile chords
				if(line[i] == ' ') continue;
				//a comma seperates the x and y chords
				if(line[i] == ',') continue;

				if(line[i + 1] == ',') tileNumX = (int)line[i];
				if(line[i + 1] == ' ') tileNumY = (int)line[i];

				//if the current char is the y chord then the tile chords are complete, add to the tile map vector
				if(line[i + 1] == ' '){

					tempTex.loadFromImage(img, sf::IntRect(tileNumX * m_TILESIZE_X, 
                                                              tileNumY * m_TILESIZE_Y, m_TILESIZE_X, 
                                                              m_TILESIZE_Y));
					tempTile.setTile(sf::Vector2f((float)tileX, (float)tileY), tempTex);

					m_Map.push_back(tempTile);
				}
				++tileX;
			}

			++tileY;
		}
	}
}

The  error I get is in the command window and it repeats this for each tile:

Failed to create texture, its internal size is too high (4294965856x4294965840, maximum is 8192x8192)

 

thank you for any help.

 

EDIT: m_TILESIZE_X and m_TILESIZE_Y are static const unsigned int that are 32 ea;


Edited by Crusable, 22 June 2013 - 05:18 PM.

"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"

"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"

 

 


Sponsor:

#2 Servant of the Lord   Crossbones+   -  Reputation: 17189

Like
0Likes
Like

Posted 22 June 2013 - 09:25 PM

What is the value of 'tileNumX' and 'tileNumY' before you reach tempTex.loadFromImage()?

You never initialized those values when they were created, so it wouldn't be surprising that they have a random large number like 452555534. You then multiply them with m_TILESIZE_X and Y.

 

If either of these if() statements are false, then tileNumX and tileNumY are never set to anything.

if(line[i + 1] == ',') tileNumX = (int)line[i];
if(line[i + 1] == ' ') tileNumY = (int)line[i];

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#3 Crusable   Members   -  Reputation: 592

Like
0Likes
Like

Posted 22 June 2013 - 11:08 PM

does 

std::getline(m_MapFile, line);

 get the spaces in the text file?


Edited by Crusable, 22 June 2013 - 11:08 PM.

"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"

"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"

 

 


#4 BrentChua   Crossbones+   -  Reputation: 1066

Like
0Likes
Like

Posted 23 June 2013 - 12:32 AM

Yes it includes the spaces unless you specify it in the delimiter. 

 

 

 


if(line[i + 1] == ',') tileNumX = (int)line[i];
if(line[i + 1] == ' ') tileNumY = (int)line[i];

 

You're directly casting a char value to int. I think that would not work. You're getting the ascii value of that char. Take a look at this ascii table here: http://www.asciitable.com/. For example if your current char is '0', its integer decimal value is 48. What you need is a function something like atoi(). This will properly convert a char number to its actual decimal value.

 

Also since you're getting huge amount of values, it may be possible that you're skipping those if statements completely without having the tileNumX and tileNumY assigned; just like what Servant of the Lord said. Try assigning a default value before you get your next x,y pair.

std::string line;
std::getline(m_MapFile, line);

tileNumX = 0;
tileNumY = 0;
for(int i = 0; i != line.size(); ++i){
...

Can we also see how you're arranging your map file?


Edited by BrentChua, 23 June 2013 - 12:33 AM.


#5 Crusable   Members   -  Reputation: 592

Like
0Likes
Like

Posted 23 June 2013 - 12:48 AM

The map file I am using to test is this:

 

1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0
1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0
1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0
1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0
1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0
1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0
1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0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 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0
1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0
1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0
1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0
1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0
1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0
 
 
The comma splits the x and y chord of the tile and the space splits the tile. 1,0 is on the tile sheet row 0 tile 1, which is a red brick looking tile.

"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"

"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"

 

 


#6 BrentChua   Crossbones+   -  Reputation: 1066

Like
0Likes
Like

Posted 23 June 2013 - 01:11 AM

Debug your program. Add those default 0 values for your tilenumx and tilenumy. Put a breakpoint in tempTex.loadFromImage(...) and see those value just before it calls loadfromimage(). Let us know what those values are.



#7 Crusable   Members   -  Reputation: 592

Like
0Likes
Like

Posted 23 June 2013 - 01:39 AM

tileNumX = 0, tileNumY = 48

 

Edit: This is without changing 

if(line[i + 1] == ',') tileNumX = (int)line[i];
if(line[i + 1] == ' ') tileNumY = (int)line[i];

Edited by Crusable, 23 June 2013 - 01:51 AM.

"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"

"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"

 

 


#8 BrentChua   Crossbones+   -  Reputation: 1066

Like
0Likes
Like

Posted 23 June 2013 - 06:59 AM

Okay. Assuming that is the first pair of your map file, those values are clearly wrong. It should have been:

 

tileNumX = 1

tileNumY = 0

 

Use atoi() function. Also since tileNumX in your example is 0, i'm guessing it never went inside your if block that assigned the value for tileNumX. Check the content of your line string make sure it doesn't contain any special/weird characters. When it gets to the loadFromImage function make sure both values has been assigned.

 

I think your problem can be solved by simple debugging techniques using the visual studio debugger. Try a bit harder. ;)



#9 Servant of the Lord   Crossbones+   -  Reputation: 17189

Like
0Likes
Like

Posted 23 June 2013 - 12:45 PM

Here's some untested code I just wrote, illustrating how it might be done.
#include <fstream>
#include <sstream>
#include <algorithm> //For std::replace().

//Loads the entire file, newlines and all, and returns its contents as a string.
//Returns an empty string if the file doesn't exist.
std::string LoadFileAsString(const std::string &filepath)
{
	//Open the file.
	std::ifstream file(filepath.c_str());
	
	//If it failed to open, report the error in whatever way your program handles errors.
	if(!file)
	{
		std::cerr << "LoadFileAsString(): Failed to load '" << filepath << "'\n" << std::endl;
		return std::string();
	}
	
	//Read the entire filestream into the stringstream.
	std::ostringstream stringStream;
	stringStream << file.rdbuf();

	//Get the string from the stringstream.
	return stringStream.str();
}

//If you have C++11 enabled on your compiler, you can use std::stoi() instead of using this function.
int StringToInt(const std::string &str)
{
	int value;
	std::stringstream stringStream(str);
	stringStream >> value;

	return value;
}

void TileMap::SetTile(unsigned tileX, unsigned tileY, const sf::Image &tileset, unsigned tilesetLocX, unsigned tilesetLocY)
{
	if(tileX < m_MapWidth) return;
	if(tileY < m_MapHeight) return;
	
	//Get the subrect for the texture.
	sf::Texture texture;
	texture.loadFromImage(tileset, sf::IntRect((tileNumX * m_TILESIZE_X), (tileNumY * m_TILESIZE_Y),
											   m_TILESIZE_X, m_TILESIZE_Y));
	
	//Assign the texture to the tile.
	unsigned tileIndex = (tileY * m_MapHeight) + tileX;
	m_Map[tileIndex].setTexture(texture); 
}

bool TileMap::LoadTileMap(const std::string &filepath, const sf::Image &tileset)
{
	//Load the map.
	std::string fileBuffer = LoadFileAsString(filepath);
	if(fileBuffer.empty())
	{
		std::cerr << "TileMap::LoadTileMap(): Failed to load the map file. Filepath: '" << filepath << "'\n" << std::endl;
		return false;
	}
	
	//Make sure our tile array is the right size.
	m_Map.resize(m_MapWidth * m_MapHeight);
	
	//Push back an extra space for parsing convience, because we are using spaces to mark the end of each pair.
	fileBuffer.push_back(' ');
	
	//The left and right sides of pairs.
	std::string leftSide, rightSide;
	
	//Which tile we're currently on.
	unsigned tileIndex = 0;
	
	//Iterate over the entire string.
	bool reachedDivider = true;
	bool alreadyReachedComma = false;
	for(size_t i = 0; i < fileBuffer.end(); i++)
	{
		//std::isspace() detects spaces, tabs, newlines, etc... These type of characters are collectively known as 'whitespace'.
		if(std::isspace(fileBuffer[i]))
		{
			//If this is the first whitespace character we've reached since the last pair, then we process the pair.
			if(!reachedDivider)
			{
				//Convert the strings to integers. You can't just cast a char to an int - that only gives you the index of the char in the ASCII chart.
				unsigned tilesetLocX = StringToInt(leftSide);
				unsigned tilesetLocY = StringToInt(rightSide);
						
				//Convert our tile index to a 2D position. See this link for how to convert from 1D to 2D indices:
				//http://www.gamedev.net/topic/627686-2d-tileset-index-to-sub-rectangular-position-calculation/
				unsigned tileX = (tileIndex % m_MapWidth);
				unsigned tileY = (tileIndex / m_MapWidth);
				
				//Set the tile in the map, with the tileset.
				this->SetTile(tileX, tileY, tileset, tilesetLocX, tilesetLocY);
				
				//Move to the next tile.
				++tileIndex;
				
				//Reset the string buffers, so we can reuse them for the next pair.
				leftSide.clear();
				rightSide.clear();
			}
			
			//Because we just set 'beginNewPair' to true, even if it was already true, and then
			//just continue, we implicitely handle multiple spaces or newlines in a row.
			reachedDivider = true;
			alreadyReachedComma = false;
		}
		//This is the divider between the left side of the pair, and the right side.
		else if(fileBuffer[i] == ',')
		{
			alreadyReachedComma = true;
		}
		else if(std::isdigit(fileBuffer[i]))
		{
			//This is the left right side of the pair (the tile row in the tileset).
			if(alreadyReachedComma)
			{
				leftSide.push_back(fileBuffer[i]);
			}
			//This is the right right side of the pair (the tile column in the tileset).
			else
			{
				rightSide.push_back(fileBuffer[i]);
			}
			
			//Reset the divider marker, so we're prepared to hit the next divider.
			reachedDivider = false;
		}
		else
		{
			//Do nothing. Ignores invalid characters.
                        //The compiler will optimize out this empty 'else {}', it's only here for documentation.
		}
	}
}
Since I didn't bother compiling it, it might have a mistake or two of its own. If it does, I'll correct it.

Edited by Servant of the Lord, 23 June 2013 - 12:46 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.

[Fly with me on Twitter] [Google+] [My broken website]

All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.                                                                                                                                                       [Need free cloud storage? I personally like DropBox]

Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal


#10 Crusable   Members   -  Reputation: 592

Like
0Likes
Like

Posted 26 June 2013 - 02:50 PM

I have been having internet problems for the last few days. Anyways, Thanks for the comments, I decided to go with a sf::VertexArray of sf::quads, The code can be found here:

https://github.com/Crusable77/Square-Wars/blob/master/Square%20Wars/Square%20Wars/TileMap.cpp

https://github.com/Crusable77/Square-Wars/blob/master/Square%20Wars/Square%20Wars/TileMap.hpp

 

It seems to be working, thanks for the help.


Edited by Crusable, 26 June 2013 - 02:51 PM.

"C spilled his beer all over C++'s shirt. Outraged, C++ shouted, "Good god, man! Have you no class?"

"Your mother is so fat that the recursive function that was used to calculate her mass created a stack overflow"

 

 





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS