• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Crusable77

Tile map errors

9 posts in this topic

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
0

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

does 

std::getline(m_MapFile, line);

 get the spaces in the text file?

Edited by Crusable
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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.

0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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

0

Share this post


Link to post
Share on other sites
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
0

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0