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

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.


Topics - JohannesMerkt

Pages: [1]
1
General / Saving a tile map
« on: August 27, 2013, 04:04:10 pm »
Hello SFML Community,

I have created a class map. Now I want to save this map and therefor I wrote 2 functions within my class Map:

This is the function to open a file
bool Map::open(sf::String dir) // dir will be the directory were the file is but i dont use this for now
{
        delete [] ptiles; //clear the tileset from the old map
        ptiles = 0; //pointer ptiles set to 0 for savety reasons
        tilesetdir.clear(); // clear the old sf::String

        std::fstream in("maps/map2.map", std::ios::in | std::ios::binary);
        if(!in) return false;
        in.read( (char *) &tilesetdir,sizeof(sf::String));

        if(!tileset.loadFromFile(tilesetdir)) return false; //here i try to load the texture
        drawingsprite.setTexture(tileset); // and i set the texture

        in.read( (char *) &mapsize, sizeof(sf::Vector2i));

        ptiles = new textile[mapsize.x*mapsize.y];

        in.read( (char *) ptiles,sizeof(textile)*mapsize.x+mapsize.y);
        in.close();
        return true;
}

And this is the function to save the map:
bool Map::save(sf::String dir) // dir is directory were I want to save to but it is also not used for now
{
        std::fstream out("maps/map2.map", std::ios::out | std::ios::binary );
        if(!out) return false;
        out.write( (char *) &tilesetdir, sizeof(sf::String));
        out.write( (char *) &mapsize, sizeof(sf::Vector2i));
        out.write( (char *) ptiles, sizeof(textile)*mapsize.x*mapsize.y);
        out.flush();
        out.close();
        return true;
}

The data i want to save is:
textile * ptiles; //a pointer on an array of textiles (one dimension)
-----------------------------------------------------------------------------------------------------------------
//this is how the textile looks:
        struct textile
        {
                sf::Vector2<unsigned char> tilepos;
                unsigned char type;
        };
ptiles has the size[mapsize.x*mapsize.y]
sf::String tilesetdir; //a string of the directory of the tileset
sf::Vector2i mapsize; //a vector that stores the mapsize
the masize is used to know how big the array *ptiles has to be

when I try to use the functions it seems to save and when I try to open it it returns false
Now my question is what do i do wrong?
I tried several different ways but none worked properly

Pages: [1]