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:
parent
c875adfcbb
commit
10d0dee35d
3 changed files with 23 additions and 8 deletions
22
World.cpp
22
World.cpp
|
@ -27,6 +27,12 @@ int World::getH() const
|
|||
return h;
|
||||
}
|
||||
|
||||
void World::resizeWindow(const sf::FloatRect& resizedView) {
|
||||
sf::RectangleShape defaultRectangle(sf::Vector2f(1,2));
|
||||
defaultRectangle.setFillColor(Colors::Wall);
|
||||
renderColumns.resize(static_cast<long>(resizedView.width), defaultRectangle);
|
||||
}
|
||||
|
||||
BlockType World::getBlock(int x, int y) const
|
||||
{
|
||||
return map[x + w*y];
|
||||
|
@ -212,18 +218,22 @@ float World::castRay(float originX, float originY, float orientation) const
|
|||
}
|
||||
|
||||
void World::fillColumn(sf::RenderWindow& window, unsigned int column,
|
||||
float scale, sf::Color fillColor) const
|
||||
float scale, sf::Color fillColor)
|
||||
{
|
||||
float columnHeight = static_cast<float>(window.getSize().y)*scale;
|
||||
sf::RectangleShape pixelColumn(sf::Vector2f(1,columnHeight));
|
||||
pixelColumn.setPosition(static_cast<float>(column),
|
||||
(static_cast<float>(window.getSize().y)-columnHeight)/2.0f);
|
||||
pixelColumn.setFillColor(fillColor);
|
||||
sf::RectangleShape& pixelColumn = renderColumns[column];
|
||||
if (pixelColumn.getSize().y != columnHeight) {
|
||||
pixelColumn.setSize({1, columnHeight});
|
||||
pixelColumn.setPosition(static_cast<float>(column),
|
||||
(static_cast<float>(window.getSize().y) - columnHeight) / 2.0f);
|
||||
}
|
||||
if (pixelColumn.getFillColor() != fillColor)
|
||||
pixelColumn.setFillColor(fillColor);
|
||||
|
||||
window.draw(pixelColumn);
|
||||
}
|
||||
|
||||
void World::render(sf::RenderWindow& window) const
|
||||
void World::render(sf::RenderWindow& window)
|
||||
{
|
||||
float windowX = static_cast<float>(window.getSize().x);
|
||||
float windowY = static_cast<float>(window.getSize().y);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue