chore: clang-format-ted codebase
This commit is contained in:
parent
697ba9f2f8
commit
fc74361bb3
|
|
@ -1,11 +1,12 @@
|
|||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <random>
|
||||
#include <cmath>
|
||||
|
||||
#include <SFML/Graphics.hpp>
|
||||
#include <cmath>
|
||||
#include <cstdlib>
|
||||
#include <random>
|
||||
#include <vector>
|
||||
|
||||
namespace v1 {
|
||||
struct Ball {
|
||||
sf::CircleShape shape;
|
||||
sf::Vector2f velocity;
|
||||
|
|
@ -20,7 +21,7 @@ struct Ball {
|
|||
};
|
||||
|
||||
class BallGame {
|
||||
private:
|
||||
private:
|
||||
// Create balls
|
||||
std::vector<Ball> balls;
|
||||
std::random_device rd;
|
||||
|
|
@ -30,8 +31,8 @@ private:
|
|||
std::uniform_int_distribution<int> colorDist;
|
||||
std::uniform_real_distribution<float> radiusDist;
|
||||
|
||||
public:
|
||||
BallGame::BallGame() {
|
||||
public:
|
||||
BallGame() {
|
||||
gen = std::mt19937(rd());
|
||||
posDist = std::uniform_real_distribution<float>(5.0f, 795.0f);
|
||||
velDist = std::uniform_real_distribution<float>(-200.0f, 200.0f);
|
||||
|
|
@ -50,17 +51,17 @@ public:
|
|||
}
|
||||
}
|
||||
|
||||
void updateBalls(const sf::Vector2u& windowSize, float deltaTime) {
|
||||
void updateBalls(const sf::Vector2u &windowSize, float deltaTime) {
|
||||
// Update positions
|
||||
for (auto& ball : balls) {
|
||||
for (auto &ball : balls) {
|
||||
ball.shape.move(ball.velocity * deltaTime);
|
||||
}
|
||||
|
||||
// Handle ball-to-ball collisions
|
||||
for (size_t i = 0; i < balls.size(); ++i) {
|
||||
for (size_t j = i + 1; j < balls.size(); ++j) {
|
||||
Ball& ball1 = balls[i];
|
||||
Ball& ball2 = balls[j];
|
||||
Ball &ball1 = balls[i];
|
||||
Ball &ball2 = balls[j];
|
||||
|
||||
sf::Vector2f pos1 = ball1.shape.getPosition();
|
||||
sf::Vector2f pos2 = ball2.shape.getPosition();
|
||||
|
|
@ -87,7 +88,8 @@ public:
|
|||
float velAlongNormal = relativeVel.x * normal.x + relativeVel.y * normal.y;
|
||||
|
||||
// Don't resolve if velocities are separating
|
||||
if (velAlongNormal > 0) continue;
|
||||
if (velAlongNormal > 0)
|
||||
continue;
|
||||
|
||||
// Apply collision response (elastic collision)
|
||||
float restitution = 0.0f; // Bounce factor (0 = no bounce, 1 = perfect bounce)
|
||||
|
|
@ -102,7 +104,7 @@ public:
|
|||
}
|
||||
|
||||
// Handle wall collisions
|
||||
for (auto& ball : balls) {
|
||||
for (auto &ball : balls) {
|
||||
sf::Vector2f pos = ball.shape.getPosition();
|
||||
float radius = ball.shape.getRadius();
|
||||
|
||||
|
|
@ -112,8 +114,7 @@ public:
|
|||
// Clamp position to prevent sticking
|
||||
if (pos.x - radius <= 0) {
|
||||
ball.shape.setPosition(sf::Vector2f(radius, pos.y));
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
ball.shape.setPosition(sf::Vector2f(windowSize.x - radius, pos.y));
|
||||
}
|
||||
}
|
||||
|
|
@ -123,18 +124,17 @@ public:
|
|||
// Clamp position to prevent sticking
|
||||
if (pos.y - radius <= 0) {
|
||||
ball.shape.setPosition(sf::Vector2f(pos.x, radius));
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
ball.shape.setPosition(sf::Vector2f(pos.x, windowSize.y - radius));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void drawBalls( sf::RenderWindow& window ) const
|
||||
{
|
||||
for (const auto& ball : balls) {
|
||||
void drawBalls(sf::RenderWindow &window) const {
|
||||
for (const auto &ball : balls) {
|
||||
window.draw(ball.shape);
|
||||
}
|
||||
}
|
||||
};
|
||||
} // namespace v1
|
||||
|
|
|
|||
|
|
@ -9,6 +9,13 @@
|
|||
#include <imgui.h>
|
||||
#include <print>
|
||||
|
||||
#include "Balls.hpp"
|
||||
namespace v1 {
|
||||
static BallGame sim;
|
||||
}
|
||||
namespace v2 {
|
||||
}
|
||||
|
||||
void configure(AppConfig &config) {
|
||||
config.window_title = "CHANGEME";
|
||||
config.frame_rate_limit = std::nullopt;
|
||||
|
|
@ -18,7 +25,6 @@ void configure(AppConfig &config) {
|
|||
void setup() {
|
||||
ImGui::GetIO().ConfigFlags |= (ImGuiConfigFlags_NavEnableKeyboard | ImGuiConfigFlags_NavEnableGamepad | ImGuiConfigFlags_DockingEnable);
|
||||
sf::View view{get_window().getView()};
|
||||
view.setCenter({0.f, 0.f});
|
||||
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>()}) {
|
||||
sf::View view{get_window().getView()};
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
void loop(double delta) {
|
||||
v1::sim.updateBalls(get_window().getSize(), delta);
|
||||
}
|
||||
|
||||
void draw_scene(sf::RenderTarget &target, sf::RenderStates const &states) {
|
||||
static sf::RectangleShape rect{{300, 300}};
|
||||
sf::RenderStates n_states{states};
|
||||
n_states.transform.translate({-150, -150});
|
||||
target.draw(rect, n_states);
|
||||
v1::sim.drawBalls(get_window());
|
||||
}
|
||||
|
||||
void draw_main_menu_bar() {
|
||||
|
|
|
|||
|
|
@ -10,8 +10,8 @@
|
|||
#include <optional>
|
||||
|
||||
struct AppConfig {
|
||||
sf::String window_title{ "UNNAMED WINDOW" };
|
||||
sf::VideoMode mode{ { 1280, 720 } };
|
||||
sf::String window_title{"UNNAMED WINDOW"};
|
||||
sf::VideoMode mode{{1280, 720}};
|
||||
sf::RenderStates default_render_states{sf::RenderStates::Default};
|
||||
bool vsync{false};
|
||||
std::optional<int> frame_rate_limit{std::nullopt};
|
||||
|
|
@ -19,7 +19,7 @@ struct AppConfig {
|
|||
|
||||
extern AppConfig const &get_application_configuration();
|
||||
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);
|
||||
|
||||
#endif // !LLCS_DEFS_H
|
||||
|
|
|
|||
20
src/main.cpp
20
src/main.cpp
|
|
@ -16,8 +16,9 @@ void initialize_window() {
|
|||
cfg = AppConfig();
|
||||
configure(cfg);
|
||||
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.setVerticalSyncEnabled(cfg.vsync);
|
||||
view = window.getDefaultView();
|
||||
}
|
||||
|
|
@ -39,8 +40,7 @@ void try_handle_input_event(sf::Event const &event) {
|
|||
event.is<sf::Event::TouchBegan>() || event.is<sf::Event::TouchEnded>() ||
|
||||
event.is<sf::Event::TouchMoved>() ||
|
||||
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) {
|
||||
handle_input_event(event);
|
||||
}
|
||||
|
|
@ -50,21 +50,21 @@ void try_handle_window_event(sf::Event const &event) {
|
|||
bool const is_window_event{
|
||||
event.is<sf::Event::FocusGained>() ||
|
||||
event.is<sf::Event::FocusLost>() ||
|
||||
event.is<sf::Event::Resized>()
|
||||
};
|
||||
event.is<sf::Event::Resized>()};
|
||||
if (is_window_event) {
|
||||
handle_window_event(event);
|
||||
}
|
||||
}
|
||||
|
||||
void poll_events() {
|
||||
while (std::optional const event{ window.pollEvent() })
|
||||
{
|
||||
while (std::optional const event{window.pollEvent()}) {
|
||||
ImGui::SFML::ProcessEvent(window, *event);
|
||||
try_handle_input_event(*event);
|
||||
try_handle_window_event(*event);
|
||||
|
||||
if (event->is<sf::Event::Closed>()) { window.close(); }
|
||||
if (event->is<sf::Event::Closed>()) {
|
||||
window.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -86,7 +86,7 @@ int main() {
|
|||
delta_time = delta_clock.restart();
|
||||
ImGui::SFML::Update(window, delta_time);
|
||||
loop(delta_time.asSeconds());
|
||||
if(ImGui::BeginMainMenuBar()) {
|
||||
if (ImGui::BeginMainMenuBar()) {
|
||||
draw_main_menu_bar();
|
||||
ImGui::EndMainMenuBar();
|
||||
}
|
||||
|
|
@ -110,7 +110,7 @@ sf::Time const &get_delta_time() {
|
|||
return delta_time;
|
||||
}
|
||||
|
||||
sf::RenderWindow const &get_window() {
|
||||
sf::RenderWindow &get_window() {
|
||||
return window;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue