Tile map errors

Started by
8 comments, last by Crusable77 10 years, 9 months ago

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;

Advertisement

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];

does


std::getline(m_MapFile, line);

get the spaces in the text file?

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


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

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?

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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,0 1,0 1,0 1,0 1,0 1,0
1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0
1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0
1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0
1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0
1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0
1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0
1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 1,0 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.

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.

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];

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. ;)

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.

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.

This topic is closed to new replies.

Advertisement