This repository has been archived by the owner on Oct 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathApplication.cpp
90 lines (70 loc) · 2.76 KB
/
Application.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
#include "Application.hpp"
Application::Application()
: m_window{sf::VideoMode{c_window_x, c_window_y}, "ball collision demo"},
m_balls{Math::random<size_t>(c_balls_min, c_balls_max)},
m_tree{{0, 0, c_window_x, c_window_y}}
{
for (auto &it : m_balls)
{
it.setPosition(Math::random<float>(0, c_window_x), Math::random<float>(0, c_window_y));
it.setDirection({Math::random<float>(-5.0f / 3.0f, 5.0f / 3.0f), Math::random<float>(-5.0f / 3.0f, 5.0f / 3.0f)});
it.setRadius(Math::random<float>(5, 10));
it.setSpeed(Math::random<float>(30, 60));
}
m_window.setFramerateLimit(60);
}
void Application::run()
{
while (m_window.isOpen())
{
sf::Event event;
while (m_window.pollEvent(event))
if (event.type == sf::Event::Closed)
m_window.close();
float deltaTime = m_clock.restart().asSeconds();
m_window.clear();
loop(deltaTime);
m_fpsCounter.push(1.0f / deltaTime);
drawFPS(m_fpsCounter.getAverage());
m_window.display();
}
}
void Application::loop(float deltaTime)
{
auto windowSize = m_window.getSize();
auto endIt = m_balls.end();
m_tree.clear();
for (auto &it : m_balls)
m_tree.insert(it);
m_tree.draw(m_window);
for (auto &lhs : m_balls)
{
auto &nearbyObjects = m_tree.getNearbyObjects(lhs.getGlobalBounds());
// Ищем коллизию среди объектов поблизости
auto collide = std::find_if(nearbyObjects.begin(), nearbyObjects.end(),
[&lhs](const auto &it)
{
auto &rhs = it.get();
if (&rhs <= &lhs) // нам нужны только следующие за lhs элементы
return false;
return Ball::hasCollide(lhs, rhs);
});
if (collide != nearbyObjects.end())
Ball::onCollide(lhs, collide->get());
// Отскакиваем от стен
if (lhs.getPosition().x < 0 || lhs.getPosition().x + lhs.getRadius() * 2 > windowSize.x)
lhs.setDirection({-lhs.getDirection().x, lhs.getDirection().y});
if (lhs.getPosition().y < 0 || lhs.getPosition().y + lhs.getRadius() * 2 > windowSize.y)
lhs.setDirection({lhs.getDirection().x, -lhs.getDirection().y});
}
for (auto &ball : m_balls)
ball.update(deltaTime);
for (const auto ball : m_balls)
m_window.draw(ball);
}
void Application::drawFPS(float fps)
{
char c[32];
std::snprintf(c, sizeof(c), "FPS: %f", fps);
m_window.setTitle(c);
}