Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Text file to tilemap, where to start?  (Read 1050 times)

0 Members and 1 Guest are viewing this topic.

fate950

  • Newbie
  • *
  • Posts: 4
    • View Profile
Text file to tilemap, where to start?
« on: October 23, 2022, 10:55:17 pm »
So, I want to draw a 2d tilemap based on my text file. I followed this https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php from the docs, but the issue here is that my text file isn't composed of numbers, rather they are characters (#,$, . ,@). So a my text file looks something like this.

#########
#.......$..........#
#...............@...#
#...$..............#
#..................#
#....$..........$..#


with # being the walls, $ the loot items,@ as the character, and . as the floor. How would I even do something like this without numbers?

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Text file to tilemap, where to start?
« Reply #1 on: October 24, 2022, 12:46:51 pm »
first let me say that this is not a good idea at all. your first line has 9 characters, and (because dots have a smaller size) your second line has 20. walls won't match in position, unless you are creating your tilemaps in a monospaced font..

but if you really want to do that, you'll have to convert each character to textCoord in the tileset (in the example from the link) instead of just using the tile number itself for the calculations.

i'd suggest you to use a tilemap creator, like Tiled, and export your map to numbers...
Visit my game site (and hopefully help funding it? )
Website | IndieDB

fate950

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Text file to tilemap, where to start?
« Reply #2 on: October 24, 2022, 08:17:01 pm »
No, these characters would be mapped to an individual texture.

I ended up following the VertexArray tutorial from the docs, with the addition that it reads from my .txt file to make my map https://www.sfml-dev.org/tutorials/2.5/graphics-vertex-array.php.

My issue at the moment is that, it only accepts my text file if it is composed of numbers https://imgur.com/a/ipgZP73, rather than the aforementioned characters (#,*, . , O) https://imgur.com/a/xUmnALQ

At this point I am only left with turning those numerical values of the textures into chars, so map.txt can be read properly

This is a code snippet of how I process the text file which I suspect is part of the problem
```
bool load(const std::string& tileset, sf::Vector2u tileSize) {
        if (!m_tileset.loadFromFile(tileset))
            return false;
        std::vector<std::vector<int> > tiles;
        std::ifstream stream("map.txt");
        stream.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // ignore first line
        for (std::string line; std::getline(stream, line);) {
            tiles.push_back(std::vector<int>());
            std::stringstream ss(line);
            std::copy(std::istream_iterator<int>(ss), std::istream_iterator<int>(), std::back_inserter(tiles.back()));
           ........
        }
         .....
(this is from the sfml vertex array tutorial that I followed)
// define its 4 corners
                quad[0].position = sf::Vector2f(i * tileSize.x, j * tileSize.y);
                quad[1].position = sf::Vector2f((i + 1) * tileSize.x, j * tileSize.y);
                quad[2].position = sf::Vector2f((i + 1) * tileSize.x, (j + 1) * tileSize.y);
                quad[3].position = sf::Vector2f(i * tileSize.x, (j + 1) * tileSize.y);

                // define its 4 texture coordinates
                quad[0].texCoords = sf::Vector2f(tu * tileSize.x, tv * tileSize.y);
                quad[1].texCoords = sf::Vector2f((tu + 1) * tileSize.x, tv * tileSize.y);
                quad[2].texCoords = sf::Vector2f((tu + 1) * tileSize.x, (tv + 1) * tileSize.y);
                quad[3].texCoords = sf::Vector2f(tu * tileSize.x, (tv + 1) * tileSize.y);

From what I've gathered, I need to somehow map these quad positions to the chars I want to use, which is where im at right now

Stauricus

  • Sr. Member
  • ****
  • Posts: 369
    • View Profile
    • A Mafia Graphic Novel
    • Email
Re: Text file to tilemap, where to start?
« Reply #3 on: October 24, 2022, 09:08:18 pm »
At this point I am only left with turning those numerical values of the textures into chars, so map.txt can be read properly

Yeah, you can do that or map the texture coordinates manually. but it will take a looot of time and may become messy. if you tile is 64x64 pixels, for example:

if (tile == "a"){
    quad[0].texCoords = sf::Vector2f(0, 0);
    quad[1].texCoords = sf::Vector2f(0, 64);
    quad[2].texCoords = sf::Vector2f(64, 64);
    quad[3].texCoords = sf::Vector2f(64, 0);
}
if (tile == "b"){
    quad[0].texCoords = sf::Vector2f(64, 64);
    quad[1].texCoords = sf::Vector2f(64, 128);
    quad[2].texCoords = sf::Vector2f(128, 128);
    quad[3].texCoords = sf::Vector2f(128, 64);
}

//etc

 
Visit my game site (and hopefully help funding it? )
Website | IndieDB

fate950

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Text file to tilemap, where to start?
« Reply #4 on: October 25, 2022, 07:10:45 pm »
unfortunately, that didn’t work for me, it results in an operator error. I greatly appreciate it though.

what I am trying to wrap my head around is assigning those 0s to chars, no matter what I try, it always results in a blank screen (I assume the txt file is not being read at all). At this point I’m very stumped.

kojack

  • Sr. Member
  • ****
  • Posts: 300
  • C++/C# game dev teacher.
    • View Profile
Re: Text file to tilemap, where to start?
« Reply #5 on: October 25, 2022, 08:18:15 pm »
What I would do is convert the characters to numbers as the file is loaded and use the rest of the code like normal. Something like:
int tile = 0;
switch (c)
{
        case '.':
                tile = 0;
                break;
        case '#':
                tile = 1;
                break;
        case '$':
                tile = 2;
                break;
        case '@':
                tile = 3;
                break;
}
where c is a char from the map. Do that for each character in the file and put the value of tile into the tiles vector.

fate950

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Text file to tilemap, where to start?
« Reply #6 on: October 25, 2022, 08:23:45 pm »
Oh, I see now! I take it this is done in the ifstream loop from my code snippet, right?

kojack

  • Sr. Member
  • ****
  • Posts: 300
  • C++/C# game dev teacher.
    • View Profile
Re: Text file to tilemap, where to start?
« Reply #7 on: October 25, 2022, 09:17:00 pm »
Yep, if you replaced the std::copy (which is doing a whole line at a time) with a loop to do one character at a time.

Here's a little sample I had to test it. It's not using the same kind of container (just an array, not vectors) but it shows reading the file and converting. (I wrote it before seeing your code above)
No error checking or safety stuff. :)

        const int g_width = 16;
        const int g_height = 8;
        char g_tiles[g_height][g_width];
       
        void loadMap()
        {
                std::fstream in("data/testmap.txt");
                std::string s;
                for (int y = 0; y < g_height; ++y)
                {
                        std::getline(in, s);
                        for (int x = 0; x < g_width; ++x)
                        {
                                int tile = 0;
                                switch (s[x])
                                {
                                case '.':
                                        tile = 0;
                                        break;
                                case '#':
                                        tile = 1;
                                        break;
                                case '$':
                                        tile = 2;
                                        break;
                                case '@':
                                        tile = 3;
                                        break;
                                }
                                g_tiles[y][x] = tile;
                        }
                }
        }
 

 

anything