-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsfml_body_utils.cpp
100 lines (86 loc) · 2.53 KB
/
sfml_body_utils.cpp
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "body_utils.h"
#include <SFML/Graphics.hpp>
using namespace A6;
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
/**
* Just to visualize various sets of bodies created by the functions in
* body_utils.h.
*/
class Vis {
public:
Vis(Config config, int index)
: m_window(sf::VideoMode(800, 600), std::to_string(index))
{
ostringstream oss;
oss << "Config_" << index << ".png";
m_filename = oss.str();
for (Body& x : config.bBodies) {
x.setColour(Colour{0, 0, 255, 127});
m_bodies.push_back(x);
}
for (Body& x : config.aBodies) {
x.setColour(Colour{255, 0, 0, 127});
m_bodies.push_back(x);
}
}
void loop()
{
while (m_window.isOpen()) {
sf::Event event;
while (m_window.pollEvent(event)) {
if (event.type == sf::Event::Closed) m_window.close();
}
m_window.clear();
draw();
m_window.display();
/*
sf::Texture texture;
texture.create(m_window.getSize().x, m_window.getSize().y);
texture.update(m_window);
if (texture.copyToImage().saveToFile(m_filename))
{
std::cout << "screenshot saved to " << m_filename << std::endl;
}
*/
}
}
private:
void draw()
{
for (const Body& b : m_bodies) {
vector<CartVec> vertices = b.vertexPositions();
CartVec centre = b.centre();
CartVec lastV = vertices[0];
int nverts = vertices.size();
for (int i = 1; i < nverts; i++) {
sf::VertexArray triangle(sf::Triangles, 3);
triangle[0].position = sf::Vector2f(lastV.x, lastV.y);
lastV = vertices[i];
triangle[1].position = sf::Vector2f(lastV.x, lastV.y);
triangle[2].position = sf::Vector2f(centre.x, centre.y);
Colour colour = b.colour();
sf::Color c { colour.red, colour.green, colour.blue, colour.alpha };
sf::Color brightened = c + sf::Color { 20, 20, 20, 0 };
triangle[0].color = brightened;
triangle[1].color = c;
triangle[2].color = c;
m_window.draw(triangle);
}
}
}
sf::RenderWindow m_window;
string m_filename;
vector<Body> m_bodies;
};
int main()
{
vector<Config> configs = allConfigs();
for (int i=0; i < configs.size(); ++i) {
Vis v{configs[i], i};
v.loop();
}
return 0;
}