hi, im working on a tilemap right now, and even though it works properly it feels slow which is probably because i use the sfml functions and classes the wrong way:
So my tiles look like this:
class Tile
{
private:
const sf::Image* _source;
sf::IntRect _section; //! Section of the source that is drawn
public:
Tile(void)
:_source(nullptr),_section(sf::IntRect()) {}
Tile(const sf::Image* source,sf::IntRect section)
:_source(source),_section(section) {}
const sf::Image* source(void) const { return _source; }
const sf::IntRect& section(void) const { return _section; }
void setSection(sf::IntRect section) { _section = section; }
};
_section is just the part of the source image the tile is using.
First question:
Should i already use sf::Texture here for the image reference? im not manipulating the images anyway.
Now the render function:
void Engine::renderFrame()
{
int posx,posy;
window.clear();
for(unsigned int y = 0; y < level.getHeight(); ++y)
{
for(unsigned int x = 0; x < level.getWidth(); ++x)
{
const Tile& tile = level.getTile(x,y);
posx = (x * level.getTilesize());
posy = (y * level.getTilesize());
drawTile(tile,posx,posy);
}
}
window.display();
}
and my drawTile function:
void Engine::drawTile(const Tile& t,int posx,int posy)
{
if(t.source() != nullptr){
sf::Texture tex;
sf::Sprite spr;
tex.loadFromImage(*(t.source()),t.section());
spr.setTexture(tex);
spr.setPosition(posx,posy);
window.draw(spr);
}
}
This is my main concern:
i read that one should use as few textures as possible, now i always create a new one with every call, this should have a terrible performance.
How would one draw a tile based map like mine? Draw all the sprites to a single texture and draw that texture to the screen afterwards?
Thanks for any help