1
0
Fork 0

World: Re-use RectangleShapes for rendering

sf::RectangleShapes were created brand new each frame, which takes a lot of time.
Instead, keep an array of them available and update them instead of creating new ones.

This also allows a nice optimization of only updating them if needed, by checking their
previous value.

Update the available number of rects when the window resizes.
This commit is contained in:
trotFunky 2024-01-29 00:25:57 +00:00
parent c875adfcbb
commit 10d0dee35d
3 changed files with 23 additions and 8 deletions

View file

@ -30,12 +30,13 @@ public:
std::vector<BlockType> worldMap = {});
int getW() const;
int getH() const;
void resizeWindow(const sf::FloatRect& resizedView);
inline BlockType getBlock(int x, int y) const;
inline BlockType getBlock(float x, float y) const;
void setBlock(BlockType block, int x, int y, int width = 1, int height = 1);
void render(sf::RenderWindow&) const;
void render(sf::RenderWindow&);
/**
* Move the world one step forward.
@ -47,13 +48,14 @@ public:
private:
int w;
int h;
std::vector<sf::RectangleShape> renderColumns;
std::vector<BlockType> map;
sf::Color groundColor;
sf::Color ceilingColor;
void fillColumn(sf::RenderWindow&, unsigned int column, float scale,
sf::Color fillColor = Colors::Wall) const;
sf::Color fillColor = Colors::Wall);
/**
* Cast a ray from a given position and return its distance to the origin.
* @param originX Ray X origin, strictly positive