-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticleRenderer.hpp
More file actions
51 lines (41 loc) · 1.75 KB
/
ParticleRenderer.hpp
File metadata and controls
51 lines (41 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#pragma once
#include <SFML/Graphics.hpp>
#include <vector>
#include "Particle.hpp"
class ParticleRenderer {
private:
sf::Texture texture;
public:
sf::VertexArray vertices;
ParticleRenderer(const int PARTICLE_COUNT) : vertices(sf::Quads, PARTICLE_COUNT * 4) {
texture.loadFromFile("../assets/circle.png");
texture.setSmooth(true);
}
void build(const std::vector<Particle> &particles) {
const float textureSize = static_cast<float>(texture.getSize().x);
for (int i = 0; i < particles.size(); ++i) {
const auto &p = particles[i];
const float r = p.radius;
const std::size_t base = i*4;
// positions: a quad around particle centre
vertices[base + 0].position = p.position + sf::Vector2f(-r, -r);
vertices[base + 1].position = p.position + sf::Vector2f( r, -r);
vertices[base + 2].position = p.position + sf::Vector2f( r, r);
vertices[base + 3].position = p.position + sf::Vector2f(-r, r);
// full texture
vertices[base + 0].texCoords = {0.f, 0.f};
vertices[base + 1].texCoords = {textureSize, 0.f};
vertices[base + 2].texCoords = {textureSize, textureSize};
vertices[base + 3].texCoords = {0.f, textureSize};
// color per vertex
for (int k = 0; k < 4; ++k) {
vertices[base + k].color = p.color;
}
}
}
void draw(sf::RenderTarget &target) {
sf::RenderStates states;
states.texture = &texture;
target.draw(vertices, states);
}
};