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

Author Topic: Loading tile maps  (Read 3067 times)

0 Members and 1 Guest are viewing this topic.

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Loading tile maps
« on: August 11, 2013, 06:08:17 pm »
Hello

Here's my code which I've been following from the CodingmadeEasy tutorials series on SFML 2.0:


#include<SFML/Graphics.hpp>
#include<fstream>
#include<iostream>
#include<cctype>
using namespace std;
 
int main()
{
    std::ifstream openfile("Map.txt");
 
    sf::Vector2i map[100][100];
    sf::Vector2i loadCounter(0, 0);
    sf::Vector2i mapSize;
    sf::Texture tileTexture;
    sf::Sprite tiles;
 
    if(openfile.is_open())
    {
        std::string tileLocation;
        openfile >> tileLocation;
        std::cout << tileLocation << std::endl;
        tileTexture.loadFromFile(tileLocation);
        tiles.setTexture(tileTexture);
        while(!openfile.eof())
        {
            std::string tile;
            openfile >> tile;
            char x = tile[0], y = tile[2];
            //std::cout << "x: " << x << "y: " << y << std::endl;
            if(!isdigit(x) || !isdigit(y))
                map[loadCounter.x][loadCounter.y] = sf::Vector2i(-1, -1);
            else
                map[loadCounter.x][loadCounter.y] = sf::Vector2i(x - '0', y - '0');
 
            std::cout << map[loadCounter.x][loadCounter.y].x << "," << map[loadCounter.x][loadCounter.y].y << " ";
             
            if(openfile.peek() == '\n')
            {
                loadCounter.x = 0;
                loadCounter.y++;
                std::cout << std::endl;
            }
            else
                loadCounter.x++;
        }
        mapSize = loadCounter;
        mapSize.y++;
    }
 
    sf::RenderWindow Window(sf::VideoMode(640, 480, 32), "Loading Tile Maps[Easy]");
 
    while(Window.isOpen())
    {
        sf::Event Event;
        while(Window.pollEvent(Event))
        {
            switch (Event.type)
            {
            case sf::Event::Closed:
                Window.close();
                break;
            }
        }
 
        Window.clear();
 
        for(int i = 0; i < mapSize.x; i++)
        {
            for(int j = 0; j < mapSize.y; j++)
            {
                if(map[i][j].x != -1 && map[i][j].y != -1)
                {
                    tiles.setPosition(i * 32, j * 32);
                    tiles.setTextureRect(sf::IntRect(map[i][j].x * 32, map[i][j].y * 32, 32, 32));
                    Window.draw(tiles);
                }
            }
        }
 
        Window.display();
    }
 
    return 0;
}
 

His output is:



But my output is this:



I've made sure that Map.txt is the project directory and so is Map.png.

Here are they:

Map.txt
---------------------------------------------------------------------------------------------------------------------------------------
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
x,x x,x x,x x,x 0,1 1,0 1,1 x,x x,x x,x
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0
---------------------------------------------------------------------------------------------------------------------------------------

Map.png



It would be helpful if I am told the error I am commiting.

Thanks

Ixrec

  • Hero Member
  • *****
  • Posts: 1241
    • View Profile
    • Email
Re: Loading tile maps
« Reply #1 on: August 11, 2013, 09:06:03 pm »
http://www.sfml-dev.org/tutorials/2.1/graphics-sprite.php#the-white-square-problem

In this specific case, it looks like you're trying to read the string "Map.png" from the file "Map.txt", but the Map.txt you've posted doesn't contain the string "Map.png". Did you leave part of it out?

Also, you should probably do error checking on the loadFromFile call:

if( !tileTexture.loadFromFile(tileLocation) )
    //output an error message of some kind or simply exit the program
 
« Last Edit: August 11, 2013, 09:13:57 pm by Ixrec »

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Re: Loading tile maps
« Reply #2 on: August 12, 2013, 12:05:23 pm »
@Ixrec

http://www.sfml-dev.org/tutorials/2.1/graphics-sprite.php#the-white-square-problem

In this specific case, it looks like you're trying to read the string "Map.png" from the file "Map.txt", but the Map.txt you've posted doesn't contain the string "Map.png". Did you leave part of it out?

Also, you should probably do error checking on the loadFromFile call:

if( !tileTexture.loadFromFile(tileLocation) )
    //output an error message of some kind or simply exit the program
 

Thanks! As you said I forgot(or rather was unaware) that the string "Map.png" is absent from "Map.txt"

Now everything is working as expected.

Thanks!

lockandstrike

  • Newbie
  • *
  • Posts: 32
    • View Profile
Re: Loading tile maps
« Reply #3 on: August 12, 2013, 12:19:50 pm »
I've followed some of his tutorials so I can tell you one thing you are doing wrong your'e using the easy way. Always go for the Intermediate or the Hard way because they are harder but more effective. Anyway here as code that should work:

#include <SFML/Graphics.hpp>
#include <iostream>
#include <cctype>
#include <fstream>
#include <vector>
#include <string>

int main()
{
    std::ifstream openfile("Map.txt");

    sf::Texture tileTexture;
    sf::Sprite tiles;

    std::vector<std::vector<sf::Vector2i> > map;
    std::vector<sf::Vector2i> tempMap;

    while(openfile.is_open()){
       
        std::string tileLocation;
        openfile >> tileLocation;

        if(!tileTexture.loadFromFile(tileLocation)){
            return EXIT_FAILURE;
        }
        tiles.setTexture(tileTexture);

        while(!openfile.eof()){

            std::string tile;
            openfile >> tile;

            char x = tile[0], y = tile [2];
            if(!isdigit(x) || !isdigit(y)){
                tempMap.push_back(sf::Vector2i(-1,-1));    
            }
            else{
                tempMap.push_back(sf::Vector2i(x - '0', y - '0'));
            }

            if (openfile.peek() == '/n'){
                map.push_back(tempMap);
                temMap.clear();
            }
        }
       
        map.push_back(tempMap);
    }

    sf::RenderWindow Window (sf::VideoMode(800,600,32),"SFML");

    while(!Window.isOpen()){

        sf::Event event;

        if(event.type == sf::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(sf::IntRect(map[i][j].x * 32, map[i][j].y * 32, 32, 32));
                    Window.draw(tiles);
                }
            }
        }

        Window.display();
       
    }
}
 

And this how your Map.txt file should be:

Map.png
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
x,x x,x x,x x,x 0,1 1,0 1,1 x,x x,x x,x
x,x x,x x,x x,x x,x x,x x,x x,x x,x x,x
0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0 0,0

This one is the one I use but it's not the most effective. If you want the most effective of them all watch codingmadeeasy tutorial on Loading Tile Maps[Hard];

The Illusionist Mirage

  • Full Member
  • ***
  • Posts: 115
  • My Life = Linux, C++ & Computers
    • View Profile
    • fleptic
    • Email
Re: Loading tile maps
« Reply #4 on: August 12, 2013, 12:35:45 pm »
Quote
@ lockandstrike

Thanks, I'll then focus on the intermediate and hard ones.