I am very new to using the SFML library, and C++ in general. However I am trying to wrap my head around some SFML best practices in loading images and displaying sprites.
I have a txt file that contains what I want my level(s) to look like. I currently read in the lines and will iterate through each character to display tiles for my map.
My question is how would I go about loading and keeping track of my images and sprites in regards to SFML best practices?
what I am thinking is just creating an image and sprite for each tile type and then rendering it based on my map.
My other problem is actually drawing the tiles to the screen, right now I am looping though my map character by character to display each tile, it does this once each frame, which is not ideal as it lags everything out. How can I display my map and only redraw tiles that need to be redrawn? I.E a mob or player moves, only drawing the floor once etc?
Class Map
{
Public:
std::Vector<std::string> ReadMap()
{
std::string line;
std::vector<std::string> vec;
std::ifstream myfile("lvl/lvl1.txt");
if(myfile.is_open()){
while(getline(myfile, line)){ //this will keep the loop goign and grab a new line each iteration
vec.push_back(line);
}
myfile.close();
}
else{
std::cout << "Unable to open File!\n";
}
for(unsigned int i = 0; i < vec.size(); i++){ //iterate through vecotor and display line by line to make sure map is reading right
std::cout<<vec[i]<<std::endl;
}
return vec;
}
void DisplayMap()
{
int x,y;
for(unsigned int i = 0; i < levelMap.size(); i++){
for(unsigned int j = 0; j < levelMap[i].size() + 1; j++){
std::cout<<levelMap[i][j];
y = i * 32;
x = j * 32;
window.Draw(floorSprite); //Draw floors on every space first
if (levelMap[i][j] == '#'){
window.Draw(wallSprite);
}
}
}
}
private:
sf::Image floor;
sf::Image wall;
sf::Sprite Floor;
sf::Sprite Wall;
}
Thank you all for your input.