Thanks for your reply. I still don't understand pointers, memories,,,etc. I gotta study hard.
In this case I'm bothered with discarding vertices of vanished entities.
Fairly improved by this way but still be defeated by drawing 100 times.
#include <SFML\Graphics.hpp>
#include <iostream>
#include <vector>
class Block
{
public:
Block();
~Block();
sf::Vertex* GetShape() { return shape; }
void Allocate(sf::Vector2f position, sf::Vector2f size);
bool CheckAlive() { return alive; }
int GetVertexCount() { return vertexCount; }
private:
sf::Vertex shape[8];
bool alive;
int vertexCount;
};
Block::Block()
{
}
Block::~Block()
{
}
void Block::Allocate(sf::Vector2f position, sf::Vector2f size)
{
shape[0] = sf::Vertex(position, sf::Color::Red);
shape[1] = sf::Vertex({ position.x + size.x, position.y }, sf::Color::Blue);
shape[2] = sf::Vertex({ position.x + size.x, position.y + size.y}, sf::Color::Green);
shape[3] = sf::Vertex({ position.x, position.y + size.y }, sf::Color::Yellow);
alive = true;
vertexCount = 4;
}
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::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 -= 1.f;
}
window.clear();
// draw every shapes 100 times.
/*for (int i = 0; i < 100; i++)
if (block[i].CheckAlive())
window.draw(block[i].GetShape(), block[i].GetVertexCount(), sf::Quads);*/
// draw one big shape.
std::vector<sf::Vertex> vertices(0);
int totalVertexCount = 0;
for (int i = 0; i < 100; i++)
if (block[i].CheckAlive())
for (int j = 0; j < block[i].GetVertexCount(); j++)
{
vertices.emplace_back(block[i].GetShape()[j]);
totalVertexCount++;
}
window.draw(&vertices[0], totalVertexCount, sf::Quads);
window.display();
}
}