Hello guys,
So I wanted to draw a tile map, making each tile a different object and then store them in vector, so I can go through it, and tile by tile draw them on the screen. The problem is that instead of a tile map, I get a clean white screen. I'm posting a code, could you help me?
That's my tile map class.
#pragma once
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include "tile.h"
#include <vector>
class TileMap
{
public:
std::string source;
std::vector <Tile> tileContainer;
TileMap(std::string mapSource)
{
source = mapSource;
}
void readMap()
{
std::fstream plik;
std::string stringId;
sf::Vector2f loadCounter(0, 0);
plik.open(source, std::ios::in || std::ios::out);
if (plik.good())
{
while (!plik.eof())
{
plik >> stringId;
int id = stoi(stringId);
if (id == 0)
{
Tile tile(id, "tiles.png", sf::Vector2f(32, 32));
tileContainer.push_back(tile);
}
else if (id == 1)
{
Tile tile(id, "tiles.png", sf::Vector2f(128, 32));
tileContainer.push_back(tile);
}
}
}
}
void drawMap(sf::RenderWindow &okno)
{
sf::Vector2f loadCounter(0, 0);
if (tileContainer.size() != 0)
{
for (int i = 0; i < tileContainer.size(); i++)
{
tileContainer.sprite.setPosition(loadCounter.x * 32, loadCounter.y * 32);
okno.draw(tileContainer.sprite);
std::cout << tileContainer.id;
loadCounter.x++;
if (loadCounter.x == 16)
{
loadCounter.x = 0;
loadCounter.y++;
if (loadCounter.y == 16)
{
loadCounter.y = 0;
loadCounter.x = 0;
}
}
}
}
}
};
and that's my tile class
#pragma once
#include <SFML/Graphics.hpp>
#include <iostream>
#include <vector>
#include <cmath>
#include <string>
class Tile
{
public:
float id;
sf::Texture texture;
sf::FloatRect bounds;
sf::Sprite sprite;
bool walkable;
Tile(float tileId, std::string tileSource,sf::Vector2f graphCords)
{
id = tileId;
texture.loadFromFile(tileSource);
sprite.setTexture(texture);
float x = graphCords.x;
float y = graphCords.y;
sprite.setTextureRect(sf::IntRect(x,y, 32, 32));
}
};