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:
Teo-CD 2024-01-29 00:25:54 +00:00
parent 940c1d706b
commit 029f753bbf
3 changed files with 23 additions and 8 deletions

View file

@ -27,6 +27,12 @@ int World::getH() const
return h; 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 BlockType World::getBlock(int x, int y) const
{ {
return map[x + w*y]; 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, 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; float columnHeight = static_cast<float>(window.getSize().y)*scale;
sf::RectangleShape pixelColumn(sf::Vector2f(1,columnHeight)); sf::RectangleShape& pixelColumn = renderColumns[column];
if (pixelColumn.getSize().y != columnHeight) {
pixelColumn.setSize({1, columnHeight});
pixelColumn.setPosition(static_cast<float>(column), pixelColumn.setPosition(static_cast<float>(column),
(static_cast<float>(window.getSize().y)-columnHeight)/2.0f); (static_cast<float>(window.getSize().y) - columnHeight) / 2.0f);
}
if (pixelColumn.getFillColor() != fillColor)
pixelColumn.setFillColor(fillColor); pixelColumn.setFillColor(fillColor);
window.draw(pixelColumn); 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 windowX = static_cast<float>(window.getSize().x);
float windowY = static_cast<float>(window.getSize().y); float windowY = static_cast<float>(window.getSize().y);

View file

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

View file

@ -15,10 +15,12 @@ int main()
World world(32,32); World world(32,32);
world.setBlock(BlockType::AIR,1,1,30,30); world.setBlock(BlockType::AIR,1,1,30,30);
world.setBlock(BlockType::WALL,4,4,2,2); world.setBlock(BlockType::WALL,4,4,2,2);
world.setBlock(BlockType::WALL, 4, 6, 1, 10);
world.player.move(2,2); world.player.move(2,2);
std::cout << world << std::endl; std::cout << world << std::endl;
sf::RenderWindow window(sf::VideoMode(1000,1000),"Da raycasting"); sf::RenderWindow window(sf::VideoMode(1000,1000),"Da raycasting");
world.resizeWindow(sf::FloatRect(0,0, 1000, 1000));
#ifdef IMGUI #ifdef IMGUI
if (!ImGui::SFML::Init(window)) { if (!ImGui::SFML::Init(window)) {
std::cout << "Failed to init Dear ImGui SFML" << std::endl; std::cout << "Failed to init Dear ImGui SFML" << std::endl;
@ -47,6 +49,7 @@ int main()
static_cast<float>(event.size.width), static_cast<float>(event.size.width),
static_cast<float>(event.size.height)); static_cast<float>(event.size.height));
window.setView(sf::View(newView)); window.setView(sf::View(newView));
world.resizeWindow(newView);
continue; continue;
} }
#ifdef IMGUI #ifdef IMGUI