So, I just watched a video which explained how to write a tile map in a .txt file and then load it, using C++ code and the SFML API.
I'm trying to make a level editor, however, so I also want to know how to export it to another .txt file, not only to import it.
Here is the code I'm using.
#include <SFML/Graphics.hpp>
#include <fstream>
#include <cctype>
#include <string>
#include <vector>
#include <sstream>
using namespace sf;
void LoadMap(std::string mapFileName, RenderWindow& window)
{
std::ifstream LOADMAP(mapFileName);
Texture tileTexture;
Sprite tiles;
std::vector<std::vector<Vector2i>> map;
std::vector<Vector2i> tempMap;
if(LOADMAP.is_open())
{
std::string tileLocation;
LOADMAP >> tileLocation;
tileTexture.loadFromFile(tileLocation);
tiles.setTexture(tileTexture);
while(!LOADMAP.eof())
{
std::string str, value;
std::getline(LOADMAP, str);
std::stringstream stream(str);
while (std::getline(stream, value, '|'))
{
if (value.length() > 0)
{
std::string xx = value.substr(0, value.find('/')).c_str();
std::string yy = value.substr(value.find('/') + 1).c_str();
int i, j, x, y;
for (i = 0; i < xx.length(); i++)
{
if(!isdigit(xx[i])) { break; }
}
for (j = 0; j < yy.length(); j++)
{
if (!isdigit(yy[j])) { break; }
}
if (i == xx.length()) { x = atoi(xx.c_str()); }
else { x = -1; }
if (j == yy.length()) { y = atoi(yy.c_str()); }
else { y = -1; }
tempMap.push_back(Vector2i(x, y));
}
}
map.push_back(tempMap);
tempMap.clear();
}
}
while (window.isOpen())
{
Event event;
while (window.pollEvent(event))
{
if (event.type == Event::Closed) { window.close(); }
}
window.clear();
for (int i = 0; i < map.size(); i++)
{
for (int j = 0; j < map[i].size(); j++)
{
if (map[i][j].x != -1 && map[i][j].y != -1)
{
tiles.setPosition(j * 32, i * 32);
tiles.setTextureRect(IntRect(map[i][j].x * 32, map[i][j].y * 32, 32, 32));
window.draw(tiles);
}
}
}
window.display();
}
}
And here is my tile map.
tiles.png
-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1
-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1
-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1
-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1
-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1
-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1
-1/-1|-1/-1|-1/-1|-1/-1|00/01|01/00|01/01|-1/-1|-1/-1|-1/-1
-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1|-1/-1
00/00|00/00|00/00|00/00|00/00|00/00|00/00|00/00|00/00|00/00
"tiles.png" is the name of the tile set, and everything loads correctly. The tiles are 32x32 pixels. As for the formatting, "|" separates each tile and "-1" is a tile without a texture. As for 00/01 and such, it's for a tileset I'm using (2x2 tiles currently), so 00/01 is the second tile (01) of the first row (00).
So, is there anyone here who'd be willing to help me with this? I understand most of this code, but I can't wrap my mind around how to "reverse" the process.
I'd appreciate any help I can get.