Hi,
so I did a small code for do a sort of tilemap, that uses an std::vector of a class that inherits of sf::Sprite. So basically, a
std::vector<sf::Sprite> tilemap(i, sf::Sprite(sprite));
But I have to draw this tilemap in the constructor, where other operations are done. So I am basically doing this:
window.clear(sf::Color::Cyan);
std::vector<Tile> tilemap(i, Tile(tile));
for(int a = 0; a < tilemap.size(); a++)
{
tilemap[a].setPosition(a * xpos, ypos);
}
for(int a = 0; a < tilemap.size(); a++)
{
window.draw(tilemap[a]);
}
window.draw(hero);
window.display();
This is my till class, that inherits of sf::Sprite.
Tile::Tile(sf::Texture texture, int x, int y, int scalex, int scaley)
{
t = texture;
xpos = x; ypos = y; scx = scalex; scy = scaley;
this->setTexture(t);
this->setPosition(xpos, ypos);
this->setScale(scx, scy);
}
Tile::Tile()
{
}
So my question is, is it bad, to do things beetween window.clear and window.display, or that doesn't matter? Will that impact the framerate of the program?
Thanks
Ah, I have of course done a class for do the vector and all, TileArray:
#include "TileArray.h"
TileArray::TileArray(int I, int x, int y, Tile &tile, sf::RenderWindow &window)
{
i = I;
xpos = x; ypos = y;
std::vector<Tile> tilemap(i, Tile(tile));
for(int a = 0; a < tilemap.size(); a++)
{
tilemap[a].setPosition(a * xpos, ypos);
}
for(int a = 0; a < tilemap.size(); a++)
{
window.draw(tilemap[a]);
}
}
For who that are interested, here's my final code:
main.cpp:
#include <SFML/Graphics.hpp>
#include "Tile.h"
void TileArray(std::vector<Tile>& tilemap, int x, int y)
{
for(int a = 0; a < tilemap.size(); a++)
{
tilemap[a].setPosition(a * x, y);
}
}
int main()
{
int screenWidth = 1920; int screenHeight = 1080;
sf::RenderWindow window(sf::VideoMode(screenWidth, screenHeight), "Colider");
///Textures loading
sf::Texture ground_001;
ground_001.loadFromFile("Game_Textures/ground_001.png"); ///Walls
///Tiles / Tilemaps
Tile tile(ground_001, 0, 0, 7, 7);
std::vector<Tile> tilemap(28, tile);
TileArray(tilemap, 70, 870);
///
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
///Clear / Draw / Display
window.clear(sf::Color::Cyan);
for(int a = 0; a < tilemap.size(); a++)
{
window.draw(tilemap[a]);
}
window.display();
}
}
return 0;
}
Tile.h:
#ifndef TILE_H
#define TILE_H
#include <SFML/Graphics.hpp>
class Tile : public sf::Sprite
{
int xpos, ypos, scx, scy;
sf::Texture t;
public:
Tile(sf::Texture, int, int, int, int);
Tile();
};
#endif // TILE_H
Tile.cpp:
#include "Tile.h"
Tile::Tile(sf::Texture texture, int x, int y, int scalex, int scaley)
{
t = texture;
xpos = x; ypos = y; scx = scalex; scy = scaley;
this->setTexture(t);
this->setPosition(xpos, ypos);
this->setScale(scx, scy);
}
Tile::Tile()
{
}
As you can see, I removed the TileArray class, and I replaced it by a function.