From 7cdd2068be50d5a03f4d1eb33179e2b32daa8520 Mon Sep 17 00:00:00 2001 From: kolo Date: Mon, 2 Mar 2026 15:22:47 +0300 Subject: [PATCH] skelet --- CMakeLists.txt | 19 +++++++++++++++- include/App.hpp | 17 ++++++++++++++ include/Array.hpp | 22 +++++++++++++++++++ src/App.cpp | 56 +++++++++++++++++++++++++++++++++++++++++++++++ src/Array.cpp | 30 +++++++++++++++++++++++++ src/main.cpp | 18 +++------------ 6 files changed, 146 insertions(+), 16 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 60c276f..890e907 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,12 +8,29 @@ include(FetchContent) FetchContent_Declare( SFML GIT_REPOSITORY https://github.com/SFML/SFML.git - GIT_TAG 2.6.x + GIT_TAG 2.6.x + GIT_SHALLOW ON + EXCLUDE_FROM_ALL + SYSTEM ) FetchContent_MakeAvailable(SFML) add_executable(SortLab src/main.cpp + src/App.cpp + src/Array.cpp ) +target_include_directories(SortLab PRIVATE ${CMAKE_SOURCE_DIR}/include) + target_link_libraries(SortLab PRIVATE sfml-graphics sfml-window sfml-system) + + +if(WIN32) + add_custom_command(TARGET SortLab POST_BUILD + COMMAND ${CMAKE_COMMAND} -E copy_if_different + $ + $ + COMMAND_EXPAND_LISTS + ) +endif() diff --git a/include/App.hpp b/include/App.hpp index e69de29..7ea5cda 100644 --- a/include/App.hpp +++ b/include/App.hpp @@ -0,0 +1,17 @@ +#pragma once +#include +#include "Array.hpp" + +class App { +public: + App(); + void run(); + +private: + void handleEvents(); + void update(float dt); + void render(); + + sf::RenderWindow window_; + Array array_; +}; diff --git a/include/Array.hpp b/include/Array.hpp index e69de29..cbe91ca 100644 --- a/include/Array.hpp +++ b/include/Array.hpp @@ -0,0 +1,22 @@ +#pragma once +#include + +class Array { +public: + enum class State { NORMAL, COMPARE, SWAP, SORTED }; + + Array(int size); + void shuffle(); + + int getSize() const; + float getValue(int index) const; + State getState(int index) const; + + void setValue(int index, float value); + void setState(int index, State state); + void resetStates(); + +private: + std::vector data_; + std::vector states_; +}; diff --git a/src/App.cpp b/src/App.cpp index e69de29..feb83e0 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -0,0 +1,56 @@ +#include "App.hpp" + +App::App() : window_(sf::VideoMode(1280, 720), "SortLab"), array_(100) { + window_.setFramerateLimit(60); +} + +void App::run() { + sf::Clock clock; + while (window_.isOpen()) { + float dt = clock.restart().asSeconds(); + handleEvents(); + update(dt); + render(); + } +} + +void App::handleEvents() { + sf::Event event; + while (window_.pollEvent(event)) { + if (event.type == sf::Event::Closed) { + window_.close(); + } + if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::R) { + array_.shuffle(); + } + } +} + +void App::update(float dt) { + +} + +void App::render() { + window_.clear(sf::Color(18, 18, 22)); + + float width = window_.getSize().x / static_cast(array_.getSize()); + float maxVal = static_cast(array_.getSize()); + + for (int i = 0; i < array_.getSize(); ++i) { + float height = (array_.getValue(i) / maxVal) * (window_.getSize().y * 0.8f); + + sf::RectangleShape bar(sf::Vector2f(width - 1.0f, height)); + bar.setPosition(i * width, window_.getSize().y - height); + + switch (array_.getState(i)) { + case Array::State::NORMAL: bar.setFillColor(sf::Color(200, 200, 200)); break; + case Array::State::COMPARE: bar.setFillColor(sf::Color::Yellow); break; + case Array::State::SWAP: bar.setFillColor(sf::Color::Red); break; + case Array::State::SORTED: bar.setFillColor(sf::Color::Green); break; + } + + window_.draw(bar); + } + + window_.display(); +} diff --git a/src/Array.cpp b/src/Array.cpp index e69de29..ecdd2d9 100644 --- a/src/Array.cpp +++ b/src/Array.cpp @@ -0,0 +1,30 @@ +#include "Array.hpp" + +Array::Array(int size) { + data_.resize(size); + states_.resize(size, State::NORMAL); + shuffle(); +} + +void Array::shuffle() { + for (int i = 0; i < data_.size(); ++i) { + data_[i] = static_cast(i + 1); + } + + std::random_device rd; + std::mt19937 g(rd()); + std::shuffle(data_.begin(), data_.end(), g); + + resetStates(); +} + +int Array::getSize() const { return data_.size(); } +float Array::getValue(int index) const { return data_[index]; } +Array::State Array::getState(int index) const { return states_[index]; } + +void Array::setValue(int index, float value) { data_[index] = value; } +void Array::setState(int index, State state) { states_[index] = state; } + +void Array::resetStates() { + std::fill(states_.begin(), states_.end(), State::NORMAL); +} diff --git a/src/main.cpp b/src/main.cpp index 9ded152..e86d8d1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,19 +1,7 @@ -#include +#include "App.hpp" int main() { - sf::RenderWindow window(sf::VideoMode(1280, 720), "SortLab"); - - while (window.isOpen()) { - sf::Event event{}; - while (window.pollEvent(event)) { - if (event.type == sf::Event::Closed) { - window.close(); - } - } - - window.clear(sf::Color(18, 18, 22)); - window.display(); - } - + App app; + app.run(); return 0; }