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

Author Topic: Can someone make a tile system tutorial ?  (Read 15925 times)

0 Members and 1 Guest are viewing this topic.

OutlawLee

  • Jr. Member
  • **
  • Posts: 50
  • This is my personal text. Dont read it.
    • View Profile
    • Email
Can someone make a tile system tutorial ?
« on: May 12, 2012, 07:11:41 pm »
Hi guys, i was desperatly trying to learn sfml, and i learned a lot but i still cant create a good tile system engine that loads maps from xml files.
Will anyone be kind to make me one ?

Im using SFML 2.0 RC, since 1.6 seems bad

p.s. a tutorial section would be cool

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6275
  • Thor Developer
    • View Profile
    • Bromeon
Re: Can someone make a tile system tutorial ?
« Reply #1 on: May 12, 2012, 07:26:28 pm »
There have been many discussions about tilemaps here, maybe the posts will help you. Apart from that, you're requesting two different tasks: XML loading and creating a tilemap with SFML. You should learn them separately, it's unlikely that you directly find a tutorial explaining both, and even more unlikely that someone takes the time to write one just for you ;)

And please don't call everything "engine".
« Last Edit: May 12, 2012, 07:39:58 pm by Nexus »
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Can someone make a tile system tutorial ?
« Reply #2 on: May 13, 2012, 01:26:00 am »
Here is a good tutorial: C++ Tile Engine from Scratch

Loading XML files can be a little tricky. Here is the TinyXML tutorial: TinyXML Tutorial
« Last Edit: May 14, 2012, 09:14:29 pm by mateandmetal »
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6275
  • Thor Developer
    • View Profile
    • Bromeon
Re: Can someone make a tile system tutorial ?
« Reply #3 on: May 13, 2012, 09:52:44 am »
Here is a good tutorial: C++ Tile Engine from Scratch
Good? At least concerning the code, there are some really bad mistakes.
#include <SFML\Graphics.hpp>

Engine::Engine()
{

}

Engine::~Engine()
{

}

bool Engine::Init()
{
        window = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "RPG");

        if(!window)
                return false;

        return true;
}
Already this short code contains plenty of mistakes:
  • It should be #include <SFML/Graphics.hpp> with a slash for portable code.
  • Empty constructors and destructors needn't be defined, but the Init() method's functionality should be in the constructor.
  • new without std::nothrow never returns a nullpointer, thus Init() always returns true and the check is useless.
  • There is a memory leak, because the dynamically allocated memory is never freed. This mistake is repeated when the engine object is created. I don't know why he even uses pointers, there's no need to.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Can someone make a tile system tutorial ?
« Reply #4 on: May 14, 2012, 09:12:15 pm »
Already this short code contains plenty of mistakes

Agree. I will remove the word "Good" from my previous post  ;D
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Th3Oth3r50

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Can someone make a tile system tutorial ?
« Reply #5 on: May 17, 2012, 01:18:11 pm »
Why are you trying to load from a xml file?
I'm guessing you are using a map editor that outputs the map in this way.
I've found it's easy to load a simple plain text map in C++ using getline etc.

The load map function below is for a simple 2-d map however it will only load a certain map structure so you would have to find a map editor which creates a plain text map file and modify the code to load it.
I'm sure you could find map editor which will do this if you decide to do it this way.

I'm somewhat new to programming C++ and SFML so bear in mind the following code may not be the best way to do this or particularly correct though I hope it is, it works for me.

Code: [Select]
bool Map::loadMap(ImageManager* imageManager, std::string filename) {

    std::ifstream mapfile;
    mapfile.open (filename.c_str());

    if ( mapfile.is_open() == false ) {
        std::cout << "Unable to open map file: " << filename << "\n";
        return false;
    }

    mapfile.ignore(3000,'#'); // should skip any notes at the top and find start
    mapfile.ignore(1); // ignores # as above line stops before it
    int imagenumber,width,height,tilew,tileh,temp; // to hold the map info
    std::string line; // for storing each line of the map
    std::string tile; // for storing individual tiles parsed from the line
    std::string integer; // for storing the individual tile integers but as strings

    imageManager->addResourceDirectory("resources/map/terrain/"); // image manager like on SFML website

    mapfile >> imagenumber; // how mnany images to load
    mapfile.ignore(1); // ignore newline

    std::vector < std::string > tempVector; // to hold the filenames of the tile images

    for ( int i = 0; i < imagenumber; i++) {

        getline(mapfile, line); // get the currect line and store it in string line
        tempVector.push_back(line); // add image name to the vector
    }

    mapfile >> width; // store map width height etc
    mapfile >> height;
    mapfile >> tilew;
    mapfile >> tileh;
    mapfile.ignore(1);

    Tile temptile; // create an empty tile

// create map. Tile** m_MapTiles; - Member of class Map.
              //needs to be deleted later on - handled in Map class destructor;

// allocation of map
m_MapTiles = new Tile*[height];
for(int i = 0; i < height; i++)
m_MapTiles[i] = new Tile[width];

// initialization of map
for(int j = 0; j < height; j++)
for(int i = 0; i < width; i++)
m_MapTiles[j][i] = temptile;

    for ( int j = 0; j < height; ++j ) {

        getline(mapfile, line);
        std::stringstream ss1( line );

        for ( int i = 0; i < width; ++i ) {

            getline(ss1, tile, '|');
// in the string stream ss1 get the info in the string before the | and put it into string tile
            std::stringstream ss2( tile ); // put the info in tile into string stream ss2

            getline(ss2, integer, ':');
// in the string stream ss2 get the info in the string before the : and put it into string integer
            std::stringstream ss3( integer ); // put the info in integer into string stream ss3

            ss3 >> temp; // convert the string in ss3 to int and store it in the integer variable temp
            temptile.setObjectParams(temp,i*tilew,j*tileh,tilew,tileh ); // set the tiles parameters

            getline(ss2, integer); // get the info after the : in ss2 and put it into string integer
            std::stringstream ss4( integer ); // put the info in integer into string stream ss4

            ss4 >> temp; // convert the string in ss4 to int and store it in the integer variable temp

            temptile.setWalkable(temp); // set the temp tiles walkable value based on this value

            temptile.setTexture( imageManager->getResource( tempVector.at( temptile.getID() ) ) );
// getResource returns a reference to the texture given by the filename found in tempVector
// note set texture is a class Tile function and simply sets the sprite it contains to having this texture
            m_MapTiles[j][i] = temptile; // finally add it to the map
        }
    }

    mapfile.ignore(3000,'#'); // find end - somewhat unnecessary

    mapfile.close(); // make sure to close the map file

    return true;
}

Loads a map of this format

hash = start / finish, text before/after is ignored

structure:

hash
number of filenames following
filenames of images ...
    ...
MapW
MapH
TileW
TileH
0:1 1:0 2:0 3:1
0:1 1:0 2:0 3:1
0:1 1:0 2:0 3:1
0:1 1:0 2:0 3:1
hash

where 1:1 -> L:K
    L = tile type - corresponding to the images at the start 0=first etc
    K = walkable ( 0=no)

#
4
notile.png
grass.png
green.png
red.png
4
4
40
40
2:1|2:1|2:1|2:1|
2:1|1:0|1:0|1:0|
2:1|1:0|1:1|1:0|
2:1|1:0|1:0|1:0|
#

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Can someone make a tile system tutorial ?
« Reply #6 on: May 20, 2012, 05:10:01 am »
Th3Oth3r50:

You are loading 4 texture files for your tiles:

notile.png
grass.png
green.png
red.png


This is not recommended. Your graphics driver will hate you!  ;D
You should have 1 texture file (a tileset). You can store the coordinates of each tile in a std::vector <sf::IntRect>, so when you need to draw a tile you can get its coordinates and use a sprite.setTextureRect() to draw it on the window (or use sf::VertexArray instead)

And for the memory allocation... I donĀ“t know what your Tile structure is, but... Memory allocation is a slow operation.... Maybe you should call new only once...  new Tile[width * height] ... something like that?


(sorry for my bad english)  :)
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Th3Oth3r50

  • Newbie
  • *
  • Posts: 3
    • View Profile
Re: Can someone make a tile system tutorial ?
« Reply #7 on: May 21, 2012, 12:23:37 pm »

The memory allocation is creating an array Tile m_MapTiles[height][width] which stores my tiles instead of a vector - I prefer to do it this way. It is only called once when the map loads and is cleaned up when it is closed, I do not think it will impact performance at all during run time and is likely faster than a vector.

As for having a tileset it is more convenient for me to store and load them this way. Other than at loading I do not see any reason for significant performance differences when drawing etc as the resources are only loaded once.

I guess it is hard for you to see how it all works without the full code however the main reason for posting this code was so the OP could get ideas about how to parse text files or XML files using getline and stringstreams to convert the text.

Thanks for your comments though and the english is good btw.  :)

mateandmetal

  • Full Member
  • ***
  • Posts: 171
  • The bird is the word
    • View Profile
    • my blog
Re: Can someone make a tile system tutorial ?
« Reply #8 on: May 21, 2012, 03:44:27 pm »
Thanks for your comments though and the english is good btw.  :)

Ok... thanks!  ;D
- Mate (beverage) addict
- Heavy metal addict _lml
- SFML 2 addict
- My first (and free) game: BichingISH!

Serapth

  • Full Member
  • ***
  • Posts: 105
    • View Profile
Re: Can someone make a tile system tutorial ?
« Reply #9 on: June 04, 2012, 02:36:40 pm »
This is why you were invaluable vetting my tutorial, you are like a human C++ Lint. :)

That said, isn't it possible for RenderWindow to throw an exception?

Here is a good tutorial: C++ Tile Engine from Scratch
Good? At least concerning the code, there are some really bad mistakes.
#include <SFML\Graphics.hpp>

Engine::Engine()
{

}

Engine::~Engine()
{

}

bool Engine::Init()
{
        window = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "RPG");

        if(!window)
                return false;

        return true;
}
Already this short code contains plenty of mistakes:
  • It should be #include <SFML/Graphics.hpp> with a slash for portable code.
  • Empty constructors and destructors needn't be defined, but the Init() method's functionality should be in the constructor.
  • new without std::nothrow never returns a nullpointer, thus Init() always returns true and the check is useless.
  • There is a memory leak, because the dynamically allocated memory is never freed. This mistake is repeated when the engine object is created. I don't know why he even uses pointers, there's no need to.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6275
  • Thor Developer
    • View Profile
    • Bromeon
Re: Can someone make a tile system tutorial ?
« Reply #10 on: June 04, 2012, 05:41:33 pm »
That said, isn't it possible for RenderWindow to throw an exception?
What would that change?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

 

anything