-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoidRenderer.cpp
More file actions
62 lines (49 loc) · 1.45 KB
/
BoidRenderer.cpp
File metadata and controls
62 lines (49 loc) · 1.45 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
52
53
54
55
56
57
58
59
60
61
62
#include "BoidRenderer.hpp"
#include "config.hpp"
#include <cmath>
#include <iostream>
#include <SFML/Graphics.hpp>
BoidRenderer::BoidRenderer(unsigned int const& width,
unsigned int const& height, const std::string& title)
: window(sf::VideoMode(width, height), title)
, boidShape(5)
{
// window.setFramerateLimit(200);
boidShape.setFillColor(sf::Color::White);
scaleX = width / (cfg.getXSpace()[1] - cfg.getXSpace()[0]);
scaleY = height / (cfg.getYSpace()[1] - cfg.getYSpace()[0]);
if (!font.loadFromFile("./font.ttf")) {
throw std::runtime_error("Failed to load font");
}
infoText.setFont(font);
infoText.setCharacterSize(14);
infoText.setFillColor(sf::Color::White);
infoText.setPosition(10.f, 10.f);
}
void BoidRenderer::render(const std::vector<Boid>& boids, const std::string& info)
{
window.clear();
for (const auto& b : boids) {
float windowX =
static_cast<float>((b.get_x() - cfg.getXSpace()[0]) * scaleX);
float windowY =
static_cast<float>((b.get_y() - cfg.getYSpace()[0]) * scaleY);
boidShape.setPosition(windowX, windowY);
window.draw(boidShape);
}
infoText.setString(info);
window.draw(infoText);
window.display();
}
bool BoidRenderer::isOpen() const
{
return window.isOpen();
}
void BoidRenderer::processEvents()
{
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
window.close();
}
}