Hello, I'm new to SFML and also to C++.
This question is similar to previous one but I also stuck with this problem.
#include <SFML\Graphics.hpp>
#include <iostream>
class Block
{
public:
Block();
~Block();
void Allocate(sf::Vector2f position, sf::Vector2f size);
sf::VertexArray GetShape() { return shape; }
bool CheckAlive() { return alive; }
private:
sf::VertexArray shape;
bool alive;
};
Block::Block()
{
}
Block::~Block()
{
}
void Block::Allocate(sf::Vector2f position, sf::Vector2f size)
{
shape = sf::VertexArray(sf::Quads, 4);
shape[0].position = position;
shape[1].position = { position.x + size.x, position.y };
shape[2].position = { position.x + size.x, position.y + size.y };
shape[3].position = { position.x, position.y + size.y};
shape[0].color = sf::Color::Red;
shape[1].color = sf::Color::Blue;
shape[2].color = sf::Color::Green;
shape[3].color = sf::Color::Yellow;
alive = true;
}
int main()
{
sf::RenderWindow window(sf::VideoMode(400, 400), "TEST");
Block block[100];
for (int i = 0; i < 10; i++)
for (int j = 0; j < 10; j++)
block[j + 10 * i].Allocate(sf::Vector2f(40.f * j, 40.f * i), sf::Vector2f(40.f, 40.f));
sf::VertexArray bunchOfShapes(sf::Quads, 0);
sf::Clock clock;
float frameTime = 0.f;
float timer = 0.f;
int fps = 0;
while (window.isOpen())
{
frameTime = clock.restart().asSeconds();
sf::Event e;
while (window.pollEvent(e))
{
if (e.type == sf::Event::Closed)
window.close();
}
timer += frameTime;
fps++;
if (timer >= 1.f)
{
std::cout << fps << std::endl;
fps = 0;
timer = 0.f;
}
window.clear();
// draw every shapes 100 times.
for (int i = 0; i < 100; i++)
if (block[i].CheckAlive())
window.draw(block[i].GetShape());
// draw one big shape.
/*for (int i = 0; i < 100; i++)
if (block[i].CheckAlive())
for (int j = 0; j < block[i].GetShape().getVertexCount(); j++)
bunchOfShapes.append(block[i].GetShape()[j]);
window.draw(bunchOfShapes);*/
window.display();
}
}
I expected drawing one time is faster but turned to be much worse. Each of those blocks has a state and it is to be changed frequently in game, so this one big shape has to be updated in every frame. This code is definitely wrong but I can't figure out.