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.


Messages - Th3Oth3r50

Pages: [1]
1
SFML projects / Re: Breakout Game
« on: May 31, 2012, 04:53:38 am »
Use SFML 2, it makes no sense to learn SFML 1.6 when its about to be outdated especially since the code for a game like this is well documented and fairly easy to grasp on the SFML side.

Learning C++ in that time frame as mentioned will be the more difficult part.

I would suggest you start by specifying exactly what you want to achieve - features etc and laying out how you plan to go about achieving those features then people can comment on those choices and help you with the code along the way so they don't feel they are doing your assignment for you and the planning will make it much easier.


2
General discussions / Re: Can someone make a tile system tutorial ?
« 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.  :)

3
General discussions / Re: Can someone make a tile system tutorial ?
« 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|
#

Pages: [1]
anything