So im making a tilemap and rendering it to the scene . But it got really weird and mess up
I cant find what is wrong with it . Help!
This is the main code ( run in main.cpp)
private:
//display properties
int nTileWidth;
const static int nTileVisY = 16;
const static int nTileVisX = 16;
//level properties
const static int nLevelWidth = 32;
const static int nLevelHeigth = 32;
std::string sLevel;
std::fstream stream;
//camera
View camera;
//the tilemap
TileMap tilemap;
//the draw map
int defMap[nLevelWidth * nLevelHeigth];
int drawMap[(nTileVisX+1) * (nTileVisY+1)];
void Create() override
{
std::cout << "Create 1 \n";
stream.open("map.txt");
//the level
sLevel += "................................";
sLevel += "................................";
sLevel += "................................";
sLevel += "................................";
sLevel += "................................";
sLevel += ".......#######.........#######..";
sLevel += "......##..............##........";
sLevel += "################################";
sLevel += "................................";
sLevel += "................................";
sLevel += "................................";
sLevel += "................................";
sLevel += "................................";
sLevel += "................................";
sLevel += "................................";
sLevel += "................................";
sLevel += "................................";
sLevel += "................................";
sLevel += "................................";
sLevel += "................................";
sLevel += "................................";
sLevel += ".......#######.........#######..";
sLevel += "......##..............##........";
sLevel += "################################";
sLevel += "................................";
sLevel += "................................";
sLevel += "................................";
sLevel += "................................";
sLevel += "................................";
sLevel += ".......#######.........#######..";
sLevel += "......##..............##........";
sLevel += "################################";
//calculate the tile width
nTileWidth = ((int)VideoMode::getDesktopMode().height - 100.f) / nTileVisY;
//resize the window
window->create(VideoMode(nTileWidth * nTileVisX, nTileWidth * nTileVisY), "Platformer2", Style::Close | Style::Titlebar);
//resize the camera
camera.reset(FloatRect(0.f, 0.f, nTileWidth * nTileVisX, nTileVisY * nTileWidth));
window->setView(camera);
//open the tileset
if (!tilemap.setTileSet("tileset.png"))
{
std::cout << "Cannot find tileset.png \n";
}
//set the size of tilemap
tilemap.setSize(nLevelWidth, nLevelHeigth);
//create the defMap
for (int i = 0; i < nLevelWidth * nLevelHeigth; i++)
{
if (sLevel[i] == '.')
defMap[i] = 2;
else if (sLevel[i] == '#')
defMap[i] = 1;
}
tilemap.create(Vector2u(nTileWidth, nTileWidth), defMap, nLevelWidth, nLevelHeigth, Vector2u(16, 16), Vector2f(0.f, 0.f));
}
void Draw() override
{
//clamp the camera to the map
//bottom bound
if (camera.getCenter().y - camera.getSize().y / 2.f > nLevelHeigth * nTileWidth - nTileVisY * nTileWidth)
camera.setCenter(camera.getCenter().x, (int)(nLevelHeigth * nTileWidth - nTileVisY * nTileWidth + camera.getSize().y / 2.f));
//top bound
else if (camera.getCenter().y - camera.getSize().y / 2.f < 0.f)
camera.setCenter(camera.getCenter().x, (int)(camera.getSize().y / 2.f));
//left bound
if (camera.getCenter().x - camera.getSize().x / 2.f < 0.f)
camera.setCenter((int)(camera.getSize().x / 2.f), camera.getCenter().y);
//rigth bound
else if (camera.getCenter().x - camera.getSize().x / 2.f > nLevelWidth * nTileWidth - nTileVisX * nTileWidth)
camera.setCenter((int)(nLevelWidth * nTileWidth - nTileVisX * nTileWidth + camera.getSize().x / 2.f), camera.getCenter().y);
//calculate the top left tile that is visible
int fOffsetX = camera.getCenter().x / nTileWidth - (float)nTileVisX / 2.f;
int fOffsetY = camera.getCenter().y / nTileWidth - (float)nTileVisY / 2.f;
//create the drawMap
for (int x = 0; x < nTileVisX; x++)
{
for (int y = 0; y <= nTileVisY; y++)
{
drawMap[(x + y * nTileVisX)] = defMap[((x + (int)fOffsetX) + ((y + (int)fOffsetY) * nLevelWidth))];
//std::cout << (x + y * nTileVisX) << " " << ((x + (int)fOffsetX) + ((y + (int)fOffsetY) * nLevelWidth)) << "\n";
}
}
/*Time t = sf::seconds(100000.f);
sf::sleep(t);*/
tilemap.create(Vector2u(nTileWidth, nTileWidth), drawMap, nTileVisX + 1 , nTileVisY + 1, Vector2u(16, 16), Vector2f(fOffsetX, fOffsetY));
if (Keyboard::isKeyPressed(Keyboard::E))
{
for (int y = 0; y <= nTileVisY; y++)
{
for (int x = 0 ; x <= nTileVisX; x++)
{
if (drawMap[x + y*nTileVisX] == 1)
std::cout << "#";
else
{
std::cout << ".";
}
}
std::cout << "\n";
}
std::cout << "End \n";
Time t = sf::seconds(0.5f);
sf::sleep(t);
}
window->setView(camera);
window->clear();
window->draw(tilemap);
window->display();
tilemap.clear();
}
THis is the tilemap code from sfml tilemap tutorial
class TileMap : public sf::Drawable
{
public:
bool setTileSet(const std::string& tileset)
{
// load the tileset texture
if (!m_tileset.loadFromFile(tileset))
return false;
return true;
}
void setSize(int width, int heigth)
{
// resize the vertex array to fit the level size
m_vertices.setPrimitiveType(sf::Quads);
m_vertices.resize(width * heigth * 4);
m_width = width;
m_heigth = heigth;
}
bool create(sf::Vector2u tileSize, const int* tiles, unsigned int width, unsigned int height, Vector2u texSize, Vector2f startPos)
{
// populate the vertex array, with one quad per tile
for (int i = 0; i < width; ++i)
for (int j = 0; j < height; ++j)
{
// get the current tile number
int tileNumber = tiles[i + j * width];
// find its position in the tileset texture
//int TileOffsetX = tileNumber % (m_tileset.getSize().x / tileSize.x);
// get a pointer to the current tile's quad
sf::Vertex* quad = &m_vertices[(i + j * width) * 4];
// define its 4 corners
quad[0].position = sf::Vector2f((i + startPos.x) * tileSize.x, (j + startPos.y) * tileSize.y);
quad[1].position = sf::Vector2f((i + 1 + startPos.x) * tileSize.x, (j + startPos.y) * tileSize.y);
quad[2].position = sf::Vector2f((i + 1 + startPos.x) * tileSize.x, (j + 1 + startPos.y) * tileSize.y);
quad[3].position = sf::Vector2f((i + startPos.x) * tileSize.x, (j + 1 + startPos.y) * tileSize.y);
// define its 4 texture coordinates
quad[0].texCoords = sf::Vector2f(tileNumber * texSize.x, 0 * texSize.y);
quad[1].texCoords = sf::Vector2f((tileNumber + 1) * texSize.x, 0 * texSize.y);
quad[2].texCoords = sf::Vector2f((tileNumber + 1) * texSize.x, 1 * texSize.y);
quad[3].texCoords = sf::Vector2f(tileNumber * texSize.x, 1 * texSize.y);
}
return true;
}
const Vertex &getVertex(int i)
{
return m_vertices[i];
}
const Texture &getTexture()
{
return m_tileset;
}
void clear()
{
m_vertices.clear();
m_vertices.setPrimitiveType(sf::Quads);
m_vertices.resize(m_width * m_heigth * 4);
}
private:
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const
{
// apply the tileset texture
states.texture = &m_tileset;
// draw the vertex array
target.draw(m_vertices, states);
}
int m_width , m_heigth;
sf::VertexArray m_vertices;
sf::Texture m_tileset;
};