chore: clang-format-ted codebase
This commit is contained in:
parent
697ba9f2f8
commit
fc74361bb3
230
src/Balls.hpp
230
src/Balls.hpp
|
|
@ -1,140 +1,140 @@
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <vector>
|
|
||||||
#include <random>
|
|
||||||
#include <cmath>
|
|
||||||
|
|
||||||
#include <SFML/Graphics.hpp>
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include <cmath>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <random>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
namespace v1 {
|
||||||
struct Ball {
|
struct Ball {
|
||||||
sf::CircleShape shape;
|
sf::CircleShape shape;
|
||||||
sf::Vector2f velocity;
|
sf::Vector2f velocity;
|
||||||
|
|
||||||
Ball(float x, float y, float radius, sf::Color color, float vx, float vy) {
|
Ball(float x, float y, float radius, sf::Color color, float vx, float vy) {
|
||||||
shape.setRadius(radius);
|
shape.setRadius(radius);
|
||||||
shape.setPosition(sf::Vector2f(x, y));
|
shape.setPosition(sf::Vector2f(x, y));
|
||||||
shape.setFillColor(color);
|
shape.setFillColor(color);
|
||||||
shape.setOrigin(sf::Vector2f(radius, radius)); // Center origin
|
shape.setOrigin(sf::Vector2f(radius, radius)); // Center origin
|
||||||
velocity = sf::Vector2f(vx, vy);
|
velocity = sf::Vector2f(vx, vy);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
class BallGame {
|
class BallGame {
|
||||||
private:
|
private:
|
||||||
// Create balls
|
// Create balls
|
||||||
std::vector<Ball> balls;
|
std::vector<Ball> balls;
|
||||||
std::random_device rd;
|
std::random_device rd;
|
||||||
std::mt19937 gen;
|
std::mt19937 gen;
|
||||||
std::uniform_real_distribution<float> posDist;
|
std::uniform_real_distribution<float> posDist;
|
||||||
std::uniform_real_distribution<float> velDist;
|
std::uniform_real_distribution<float> velDist;
|
||||||
std::uniform_int_distribution<int> colorDist;
|
std::uniform_int_distribution<int> colorDist;
|
||||||
std::uniform_real_distribution<float> radiusDist;
|
std::uniform_real_distribution<float> radiusDist;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
BallGame::BallGame() {
|
BallGame() {
|
||||||
gen = std::mt19937(rd());
|
gen = std::mt19937(rd());
|
||||||
posDist = std::uniform_real_distribution<float>(5.0f, 795.0f);
|
posDist = std::uniform_real_distribution<float>(5.0f, 795.0f);
|
||||||
velDist = std::uniform_real_distribution<float>(-200.0f, 200.0f);
|
velDist = std::uniform_real_distribution<float>(-200.0f, 200.0f);
|
||||||
colorDist = std::uniform_int_distribution<int>(0, 255);
|
colorDist = std::uniform_int_distribution<int>(0, 255);
|
||||||
radiusDist = std::uniform_real_distribution<float>(2.5f, 2.5f);
|
radiusDist = std::uniform_real_distribution<float>(2.5f, 2.5f);
|
||||||
|
|
||||||
// Generate random balls
|
// Generate random balls
|
||||||
for (int i = 0; i < 2500; ++i) {
|
for (int i = 0; i < 2500; ++i) {
|
||||||
sf::Color randomColor(colorDist(gen), colorDist(gen), colorDist(gen));
|
sf::Color randomColor(colorDist(gen), colorDist(gen), colorDist(gen));
|
||||||
balls.emplace_back(
|
balls.emplace_back(
|
||||||
posDist(gen), posDist(gen), // position
|
posDist(gen), posDist(gen), // position
|
||||||
radiusDist(gen), // radius
|
radiusDist(gen), // radius
|
||||||
randomColor, // color
|
randomColor, // color
|
||||||
velDist(gen), velDist(gen) // velocity
|
velDist(gen), velDist(gen) // velocity
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void updateBalls(const sf::Vector2u& windowSize, float deltaTime) {
|
void updateBalls(const sf::Vector2u &windowSize, float deltaTime) {
|
||||||
// Update positions
|
// Update positions
|
||||||
for (auto& ball : balls) {
|
for (auto &ball : balls) {
|
||||||
ball.shape.move(ball.velocity * deltaTime);
|
ball.shape.move(ball.velocity * deltaTime);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle ball-to-ball collisions
|
// Handle ball-to-ball collisions
|
||||||
for (size_t i = 0; i < balls.size(); ++i) {
|
for (size_t i = 0; i < balls.size(); ++i) {
|
||||||
for (size_t j = i + 1; j < balls.size(); ++j) {
|
for (size_t j = i + 1; j < balls.size(); ++j) {
|
||||||
Ball& ball1 = balls[i];
|
Ball &ball1 = balls[i];
|
||||||
Ball& ball2 = balls[j];
|
Ball &ball2 = balls[j];
|
||||||
|
|
||||||
sf::Vector2f pos1 = ball1.shape.getPosition();
|
sf::Vector2f pos1 = ball1.shape.getPosition();
|
||||||
sf::Vector2f pos2 = ball2.shape.getPosition();
|
sf::Vector2f pos2 = ball2.shape.getPosition();
|
||||||
float radius1 = ball1.shape.getRadius();
|
float radius1 = ball1.shape.getRadius();
|
||||||
float radius2 = ball2.shape.getRadius();
|
float radius2 = ball2.shape.getRadius();
|
||||||
|
|
||||||
// Calculate distance between centers
|
// Calculate distance between centers
|
||||||
sf::Vector2f delta = pos2 - pos1;
|
sf::Vector2f delta = pos2 - pos1;
|
||||||
float distance = std::sqrt(delta.x * delta.x + delta.y * delta.y);
|
float distance = std::sqrt(delta.x * delta.x + delta.y * delta.y);
|
||||||
float minDistance = radius1 + radius2;
|
float minDistance = radius1 + radius2;
|
||||||
|
|
||||||
if (distance < minDistance && distance > 0) {
|
if (distance < minDistance && distance > 0) {
|
||||||
// Normalize collision vector
|
// Normalize collision vector
|
||||||
sf::Vector2f normal = delta / distance;
|
sf::Vector2f normal = delta / distance;
|
||||||
|
|
||||||
// Separate balls to prevent overlap
|
// Separate balls to prevent overlap
|
||||||
float overlap = minDistance - distance;
|
float overlap = minDistance - distance;
|
||||||
sf::Vector2f separation = normal * (overlap * 0.5f);
|
sf::Vector2f separation = normal * (overlap * 0.5f);
|
||||||
ball1.shape.setPosition(pos1 - separation);
|
ball1.shape.setPosition(pos1 - separation);
|
||||||
ball2.shape.setPosition(pos2 + separation);
|
ball2.shape.setPosition(pos2 + separation);
|
||||||
|
|
||||||
// Calculate relative velocity
|
// Calculate relative velocity
|
||||||
sf::Vector2f relativeVel = ball2.velocity - ball1.velocity;
|
sf::Vector2f relativeVel = ball2.velocity - ball1.velocity;
|
||||||
float velAlongNormal = relativeVel.x * normal.x + relativeVel.y * normal.y;
|
float velAlongNormal = relativeVel.x * normal.x + relativeVel.y * normal.y;
|
||||||
|
|
||||||
// Don't resolve if velocities are separating
|
// Don't resolve if velocities are separating
|
||||||
if (velAlongNormal > 0) continue;
|
if (velAlongNormal > 0)
|
||||||
|
continue;
|
||||||
|
|
||||||
// Apply collision response (elastic collision)
|
// Apply collision response (elastic collision)
|
||||||
float restitution = 0.0f; // Bounce factor (0 = no bounce, 1 = perfect bounce)
|
float restitution = 0.0f; // Bounce factor (0 = no bounce, 1 = perfect bounce)
|
||||||
float impulse = -(1 + restitution) * velAlongNormal;
|
float impulse = -(1 + restitution) * velAlongNormal;
|
||||||
|
|
||||||
// Assume equal mass for simplicity
|
// Assume equal mass for simplicity
|
||||||
sf::Vector2f impulseVector = impulse * normal;
|
sf::Vector2f impulseVector = impulse * normal;
|
||||||
ball1.velocity -= impulseVector;
|
ball1.velocity -= impulseVector;
|
||||||
ball2.velocity += impulseVector;
|
ball2.velocity += impulseVector;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle wall collisions
|
// Handle wall collisions
|
||||||
for (auto& ball : balls) {
|
for (auto &ball : balls) {
|
||||||
sf::Vector2f pos = ball.shape.getPosition();
|
sf::Vector2f pos = ball.shape.getPosition();
|
||||||
float radius = ball.shape.getRadius();
|
float radius = ball.shape.getRadius();
|
||||||
|
|
||||||
// Bounce off walls
|
// Bounce off walls
|
||||||
if (pos.x - radius <= 0 || pos.x + radius >= windowSize.x) {
|
if (pos.x - radius <= 0 || pos.x + radius >= windowSize.x) {
|
||||||
ball.velocity.x = -ball.velocity.x;
|
ball.velocity.x = -ball.velocity.x;
|
||||||
// Clamp position to prevent sticking
|
// Clamp position to prevent sticking
|
||||||
if (pos.x - radius <= 0) {
|
if (pos.x - radius <= 0) {
|
||||||
ball.shape.setPosition(sf::Vector2f(radius, pos.y));
|
ball.shape.setPosition(sf::Vector2f(radius, pos.y));
|
||||||
}
|
} else {
|
||||||
else {
|
ball.shape.setPosition(sf::Vector2f(windowSize.x - radius, pos.y));
|
||||||
ball.shape.setPosition(sf::Vector2f(windowSize.x - radius, pos.y));
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (pos.y - radius <= 0 || pos.y + radius >= windowSize.y) {
|
if (pos.y - radius <= 0 || pos.y + radius >= windowSize.y) {
|
||||||
ball.velocity.y = -ball.velocity.y;
|
ball.velocity.y = -ball.velocity.y;
|
||||||
// Clamp position to prevent sticking
|
// Clamp position to prevent sticking
|
||||||
if (pos.y - radius <= 0) {
|
if (pos.y - radius <= 0) {
|
||||||
ball.shape.setPosition(sf::Vector2f(pos.x, radius));
|
ball.shape.setPosition(sf::Vector2f(pos.x, radius));
|
||||||
}
|
} else {
|
||||||
else {
|
ball.shape.setPosition(sf::Vector2f(pos.x, windowSize.y - radius));
|
||||||
ball.shape.setPosition(sf::Vector2f(pos.x, windowSize.y - radius));
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
void drawBalls( sf::RenderWindow& window ) const
|
void drawBalls(sf::RenderWindow &window) const {
|
||||||
{
|
for (const auto &ball : balls) {
|
||||||
for (const auto& ball : balls) {
|
window.draw(ball.shape);
|
||||||
window.draw(ball.shape);
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
};
|
} // namespace v1
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,13 @@
|
||||||
#include <imgui.h>
|
#include <imgui.h>
|
||||||
#include <print>
|
#include <print>
|
||||||
|
|
||||||
|
#include "Balls.hpp"
|
||||||
|
namespace v1 {
|
||||||
|
static BallGame sim;
|
||||||
|
}
|
||||||
|
namespace v2 {
|
||||||
|
}
|
||||||
|
|
||||||
void configure(AppConfig &config) {
|
void configure(AppConfig &config) {
|
||||||
config.window_title = "CHANGEME";
|
config.window_title = "CHANGEME";
|
||||||
config.frame_rate_limit = std::nullopt;
|
config.frame_rate_limit = std::nullopt;
|
||||||
|
|
@ -18,7 +25,6 @@ void configure(AppConfig &config) {
|
||||||
void setup() {
|
void setup() {
|
||||||
ImGui::GetIO().ConfigFlags |= (ImGuiConfigFlags_NavEnableKeyboard | ImGuiConfigFlags_NavEnableGamepad | ImGuiConfigFlags_DockingEnable);
|
ImGui::GetIO().ConfigFlags |= (ImGuiConfigFlags_NavEnableKeyboard | ImGuiConfigFlags_NavEnableGamepad | ImGuiConfigFlags_DockingEnable);
|
||||||
sf::View view{get_window().getView()};
|
sf::View view{get_window().getView()};
|
||||||
view.setCenter({0.f, 0.f});
|
|
||||||
set_render_view(view);
|
set_render_view(view);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -29,18 +35,17 @@ void handle_window_event(sf::Event const &event) {
|
||||||
if (sf::Event::Resized const *resized{event.getIf<sf::Event::Resized>()}) {
|
if (sf::Event::Resized const *resized{event.getIf<sf::Event::Resized>()}) {
|
||||||
sf::View view{get_window().getView()};
|
sf::View view{get_window().getView()};
|
||||||
view.setSize({static_cast<float>(resized->size.x), static_cast<float>(resized->size.y)});
|
view.setSize({static_cast<float>(resized->size.x), static_cast<float>(resized->size.y)});
|
||||||
|
view.setCenter({view.getSize().x / 2.f, view.getSize().y / 2.f});
|
||||||
set_render_view(view);
|
set_render_view(view);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop(double delta) {
|
void loop(double delta) {
|
||||||
|
v1::sim.updateBalls(get_window().getSize(), delta);
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw_scene(sf::RenderTarget &target, sf::RenderStates const &states) {
|
void draw_scene(sf::RenderTarget &target, sf::RenderStates const &states) {
|
||||||
static sf::RectangleShape rect{{300, 300}};
|
v1::sim.drawBalls(get_window());
|
||||||
sf::RenderStates n_states{states};
|
|
||||||
n_states.transform.translate({-150, -150});
|
|
||||||
target.draw(rect, n_states);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void draw_main_menu_bar() {
|
void draw_main_menu_bar() {
|
||||||
|
|
|
||||||
|
|
@ -10,8 +10,8 @@
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
|
||||||
struct AppConfig {
|
struct AppConfig {
|
||||||
sf::String window_title{ "UNNAMED WINDOW" };
|
sf::String window_title{"UNNAMED WINDOW"};
|
||||||
sf::VideoMode mode{ { 1280, 720 } };
|
sf::VideoMode mode{{1280, 720}};
|
||||||
sf::RenderStates default_render_states{sf::RenderStates::Default};
|
sf::RenderStates default_render_states{sf::RenderStates::Default};
|
||||||
bool vsync{false};
|
bool vsync{false};
|
||||||
std::optional<int> frame_rate_limit{std::nullopt};
|
std::optional<int> frame_rate_limit{std::nullopt};
|
||||||
|
|
@ -19,7 +19,7 @@ struct AppConfig {
|
||||||
|
|
||||||
extern AppConfig const &get_application_configuration();
|
extern AppConfig const &get_application_configuration();
|
||||||
extern sf::Time const &get_delta_time();
|
extern sf::Time const &get_delta_time();
|
||||||
extern sf::RenderWindow const &get_window();
|
extern sf::RenderWindow &get_window();
|
||||||
extern void set_render_view(sf::View const &view);
|
extern void set_render_view(sf::View const &view);
|
||||||
|
|
||||||
#endif // !LLCS_DEFS_H
|
#endif // !LLCS_DEFS_H
|
||||||
|
|
|
||||||
132
src/main.cpp
132
src/main.cpp
|
|
@ -13,59 +13,59 @@ static sf::View view{};
|
||||||
extern void configure(AppConfig &config);
|
extern void configure(AppConfig &config);
|
||||||
|
|
||||||
void initialize_window() {
|
void initialize_window() {
|
||||||
cfg = AppConfig();
|
cfg = AppConfig();
|
||||||
configure(cfg);
|
configure(cfg);
|
||||||
window.create(cfg.mode, cfg.window_title);
|
window.create(cfg.mode, cfg.window_title);
|
||||||
if (cfg.frame_rate_limit)
|
if (cfg.frame_rate_limit) {
|
||||||
window.setFramerateLimit(cfg.frame_rate_limit.value_or(0));
|
window.setFramerateLimit(cfg.frame_rate_limit.value_or(0));
|
||||||
window.setVerticalSyncEnabled(cfg.vsync);
|
}
|
||||||
view = window.getDefaultView();
|
window.setVerticalSyncEnabled(cfg.vsync);
|
||||||
|
view = window.getDefaultView();
|
||||||
}
|
}
|
||||||
|
|
||||||
void initialize_imgui() {
|
void initialize_imgui() {
|
||||||
if (!ImGui::SFML::Init(window))
|
if (!ImGui::SFML::Init(window))
|
||||||
exit(-1);
|
exit(-1);
|
||||||
}
|
}
|
||||||
|
|
||||||
extern void handle_input_event(sf::Event const &evt);
|
extern void handle_input_event(sf::Event const &evt);
|
||||||
extern void handle_window_event(sf::Event const &evt);
|
extern void handle_window_event(sf::Event const &evt);
|
||||||
|
|
||||||
void try_handle_input_event(sf::Event const &event) {
|
void try_handle_input_event(sf::Event const &event) {
|
||||||
bool const is_input_event{
|
bool const is_input_event{
|
||||||
event.is<sf::Event::JoystickButtonPressed>() || event.is<sf::Event::JoystickButtonReleased>() ||
|
event.is<sf::Event::JoystickButtonPressed>() || event.is<sf::Event::JoystickButtonReleased>() ||
|
||||||
event.is<sf::Event::JoystickMoved>() ||
|
event.is<sf::Event::JoystickMoved>() ||
|
||||||
event.is<sf::Event::JoystickConnected>() || event.is<sf::Event::JoystickDisconnected>() ||
|
event.is<sf::Event::JoystickConnected>() || event.is<sf::Event::JoystickDisconnected>() ||
|
||||||
event.is<sf::Event::KeyPressed>() || event.is<sf::Event::KeyReleased>() ||
|
event.is<sf::Event::KeyPressed>() || event.is<sf::Event::KeyReleased>() ||
|
||||||
event.is<sf::Event::TouchBegan>() || event.is<sf::Event::TouchEnded>() ||
|
event.is<sf::Event::TouchBegan>() || event.is<sf::Event::TouchEnded>() ||
|
||||||
event.is<sf::Event::TouchMoved>() ||
|
event.is<sf::Event::TouchMoved>() ||
|
||||||
event.is<sf::Event::MouseButtonPressed>() || event.is<sf::Event::MouseButtonReleased>() ||
|
event.is<sf::Event::MouseButtonPressed>() || event.is<sf::Event::MouseButtonReleased>() ||
|
||||||
event.is<sf::Event::MouseMoved>() || event.is<sf::Event::MouseMovedRaw>()
|
event.is<sf::Event::MouseMoved>() || event.is<sf::Event::MouseMovedRaw>()};
|
||||||
};
|
if (is_input_event) {
|
||||||
if (is_input_event) {
|
handle_input_event(event);
|
||||||
handle_input_event(event);
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void try_handle_window_event(sf::Event const &event) {
|
void try_handle_window_event(sf::Event const &event) {
|
||||||
bool const is_window_event{
|
bool const is_window_event{
|
||||||
event.is<sf::Event::FocusGained>() ||
|
event.is<sf::Event::FocusGained>() ||
|
||||||
event.is<sf::Event::FocusLost>() ||
|
event.is<sf::Event::FocusLost>() ||
|
||||||
event.is<sf::Event::Resized>()
|
event.is<sf::Event::Resized>()};
|
||||||
};
|
if (is_window_event) {
|
||||||
if (is_window_event) {
|
handle_window_event(event);
|
||||||
handle_window_event(event);
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void poll_events() {
|
void poll_events() {
|
||||||
while (std::optional const event{ window.pollEvent() })
|
while (std::optional const event{window.pollEvent()}) {
|
||||||
{
|
ImGui::SFML::ProcessEvent(window, *event);
|
||||||
ImGui::SFML::ProcessEvent(window, *event);
|
try_handle_input_event(*event);
|
||||||
try_handle_input_event(*event);
|
try_handle_window_event(*event);
|
||||||
try_handle_window_event(*event);
|
|
||||||
|
|
||||||
if (event->is<sf::Event::Closed>()) { window.close(); }
|
if (event->is<sf::Event::Closed>()) {
|
||||||
}
|
window.close();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
extern void setup(void);
|
extern void setup(void);
|
||||||
|
|
@ -76,44 +76,44 @@ extern void draw_gui(void);
|
||||||
extern void shutdown(void);
|
extern void shutdown(void);
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
initialize_window();
|
initialize_window();
|
||||||
initialize_imgui();
|
initialize_imgui();
|
||||||
setup();
|
setup();
|
||||||
while (window.isOpen()) {
|
while (window.isOpen()) {
|
||||||
// Event Polling
|
// Event Polling
|
||||||
poll_events();
|
poll_events();
|
||||||
// Update
|
// Update
|
||||||
delta_time = delta_clock.restart();
|
delta_time = delta_clock.restart();
|
||||||
ImGui::SFML::Update(window, delta_time);
|
ImGui::SFML::Update(window, delta_time);
|
||||||
loop(delta_time.asSeconds());
|
loop(delta_time.asSeconds());
|
||||||
if(ImGui::BeginMainMenuBar()) {
|
if (ImGui::BeginMainMenuBar()) {
|
||||||
draw_main_menu_bar();
|
draw_main_menu_bar();
|
||||||
ImGui::EndMainMenuBar();
|
ImGui::EndMainMenuBar();
|
||||||
}
|
}
|
||||||
draw_gui();
|
draw_gui();
|
||||||
// Render
|
// Render
|
||||||
window.clear();
|
window.clear();
|
||||||
draw_scene(window, cfg.default_render_states);
|
draw_scene(window, cfg.default_render_states);
|
||||||
ImGui::SFML::Render(window);
|
ImGui::SFML::Render(window);
|
||||||
window.display();
|
window.display();
|
||||||
}
|
}
|
||||||
shutdown();
|
shutdown();
|
||||||
ImGui::SFML::Shutdown();
|
ImGui::SFML::Shutdown();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
AppConfig const &get_application_configuration() {
|
AppConfig const &get_application_configuration() {
|
||||||
return cfg;
|
return cfg;
|
||||||
}
|
}
|
||||||
|
|
||||||
sf::Time const &get_delta_time() {
|
sf::Time const &get_delta_time() {
|
||||||
return delta_time;
|
return delta_time;
|
||||||
}
|
}
|
||||||
|
|
||||||
sf::RenderWindow const &get_window() {
|
sf::RenderWindow &get_window() {
|
||||||
return window;
|
return window;
|
||||||
}
|
}
|
||||||
|
|
||||||
void set_render_view(sf::View const &view) {
|
void set_render_view(sf::View const &view) {
|
||||||
window.setView(view);
|
window.setView(view);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue