Here is a good tutorial: C++ Tile Engine from Scratch (http://www.dreamincode.net/forums/topic/230524-c-tile-engine-from-scratch-part-1/)Good? At least concerning the code, there are some really bad mistakes.
Already this short code contains plenty of mistakes
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|
#
Thanks for your comments though and the english is good btw. :)
Here is a good tutorial: C++ Tile Engine from Scratch (http://www.dreamincode.net/forums/topic/230524-c-tile-engine-from-scratch-part-1/)Good? At least concerning the code, there are some really bad mistakes.#include <SFML\Graphics.hpp>Already this short code contains plenty of mistakes:
Engine::Engine()
{
}
Engine::~Engine()
{
}
bool Engine::Init()
{
window = new sf::RenderWindow(sf::VideoMode(800, 600, 32), "RPG");
if(!window)
return false;
return true;
}
- 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.
That said, isn't it possible for RenderWindow to throw an exception?What would that change?