From 0a55fef17c7b67fdc1aa97e30be010233efa287a Mon Sep 17 00:00:00 2001 From: kolo Date: Mon, 2 Mar 2026 16:18:48 +0300 Subject: [PATCH] skelet --- include/App.hpp | 2 +- include/UI.hpp | 4 ++- src/App.cpp | 70 ++++++++++++++++++++++++----------------------- src/App_audio.cpp | 2 +- src/UI.cpp | 50 ++++++++++++++++++++++----------- 5 files changed, 75 insertions(+), 53 deletions(-) diff --git a/include/App.hpp b/include/App.hpp index db26d86..51303ed 100644 --- a/include/App.hpp +++ b/include/App.hpp @@ -25,7 +25,7 @@ private: UI ui_; bool isPlaying_; float timeSinceLastStep_; - float stepDelay_; + int stepsPerFrame_; sf::SoundBuffer beepBuffer_; sf::Sound beepSound_; diff --git a/include/UI.hpp b/include/UI.hpp index e4efea0..74cb136 100644 --- a/include/UI.hpp +++ b/include/UI.hpp @@ -7,7 +7,7 @@ class UI { public: UI(); - void update(const Sorter& sorter, bool isPlaying, bool isFinished, float stepDelay, const Array& array); + void update(const Sorter& sorter, bool isPlaying, bool isFinished, int stepsPerFrame, const Array& array); void draw(sf::RenderWindow& window); private: @@ -18,5 +18,7 @@ private: sf::Text swapsText_; sf::Text speedText_; sf::Text controlsText_; + sf::RectangleShape leftBackground_; + sf::RectangleShape rightBackground_; bool fontLoaded_; }; diff --git a/src/App.cpp b/src/App.cpp index 5a714ed..28db8c0 100644 --- a/src/App.cpp +++ b/src/App.cpp @@ -12,7 +12,7 @@ App::App() , ui_() , isPlaying_(false) , timeSinceLastStep_(0.0f) - , stepDelay_(0.01f) + , stepsPerFrame_(1) , isSweeping_(false) , sweepIndex_(0) , sweepTimer_(0.0f) @@ -22,7 +22,7 @@ App::App() window_.setFramerateLimit(60); generateBeepSound(); beepSound_.setBuffer(beepBuffer_); - beepSound_.setVolume(15.0f); + beepSound_.setVolume(100.0f); } void App::run() { @@ -55,13 +55,13 @@ void App::handleEvents() { break; case sf::Keyboard::Up: - stepDelay_ = (stepDelay_ > 0.001f) ? stepDelay_ * 0.5f : 0.001f; - if (stepDelay_ < 0.001f) stepDelay_ = 0.001f; + stepsPerFrame_ = (stepsPerFrame_ < 500) ? stepsPerFrame_ * 2 : 500; + if (stepsPerFrame_ < 1) stepsPerFrame_ = 1; break; case sf::Keyboard::Down: - stepDelay_ = (stepDelay_ < 0.5f) ? stepDelay_ * 2.0f : 0.5f; - if (stepDelay_ > 0.5f) stepDelay_ = 0.5f; + stepsPerFrame_ = (stepsPerFrame_ > 1) ? stepsPerFrame_ / 2 : 1; + if (stepsPerFrame_ < 1) stepsPerFrame_ = 1; break; case sf::Keyboard::Num1: @@ -95,6 +95,7 @@ void App::handleEvents() { sweepIndex_ = 0; lastComparisons_ = 0; lastSwaps_ = 0; + stepsPerFrame_ = 1; break; default: @@ -105,7 +106,7 @@ void App::handleEvents() { } void App::update(float dt) { - ui_.update(*currentSorter_, isPlaying_, currentSorter_->isFinished(), stepDelay_, array_); + ui_.update(*currentSorter_, isPlaying_, currentSorter_->isFinished(), stepsPerFrame_, array_); if (isSweeping_) { sweepTimer_ += dt; @@ -128,40 +129,40 @@ void App::update(float dt) { } if (isPlaying_ && !currentSorter_->isFinished()) { - timeSinceLastStep_ += dt; + size_t beforeComparisons = array_.getComparisons(); + size_t beforeSwaps = array_.getSwaps(); - if (timeSinceLastStep_ >= stepDelay_) { - size_t beforeComparisons = array_.getComparisons(); - size_t beforeSwaps = array_.getSwaps(); + for (int step = 0; step < stepsPerFrame_; ++step) { + if (currentSorter_->isFinished()) { + break; + } currentSorter_->step(array_); + } + + size_t afterComparisons = array_.getComparisons(); + size_t afterSwaps = array_.getSwaps(); + + if (afterComparisons > beforeComparisons || afterSwaps > beforeSwaps) { + float avgPitch = 1.0f; - size_t afterComparisons = array_.getComparisons(); - size_t afterSwaps = array_.getSwaps(); - - if (afterComparisons > beforeComparisons || afterSwaps > beforeSwaps) { - float avgPitch = 1.0f; - - for (int i = 0; i < array_.getSize(); ++i) { - if (array_.getState(i) == Array::State::COMPARE || - array_.getState(i) == Array::State::SWAP) { - float normalizedValue = array_.getValue(i) / static_cast(array_.getSize()); - avgPitch = 0.5f + normalizedValue * 1.5f; - break; - } + for (int i = 0; i < array_.getSize(); ++i) { + if (array_.getState(i) == Array::State::COMPARE || + array_.getState(i) == Array::State::SWAP) { + float normalizedValue = array_.getValue(i) / static_cast(array_.getSize()); + avgPitch = 0.5f + normalizedValue * 1.5f; + break; } - - playBeep(avgPitch); } - timeSinceLastStep_ = 0.0f; - - if (currentSorter_->isFinished()) { - array_.resetStates(); - isSweeping_ = true; - sweepIndex_ = 0; - sweepTimer_ = 0.0f; - } + playBeep(avgPitch); + } + + if (currentSorter_->isFinished()) { + array_.resetStates(); + isSweeping_ = true; + sweepIndex_ = 0; + sweepTimer_ = 0.0f; } } } @@ -204,4 +205,5 @@ void App::switchSorter(std::unique_ptr newSorter) { sweepIndex_ = 0; lastComparisons_ = 0; lastSwaps_ = 0; + stepsPerFrame_ = 1; } diff --git a/src/App_audio.cpp b/src/App_audio.cpp index c054824..affde8d 100644 --- a/src/App_audio.cpp +++ b/src/App_audio.cpp @@ -10,7 +10,7 @@ void App::generateBeepSound() { std::vector samples(sampleCount); const float frequency = 440.0f; - const float amplitude = 3000.0f; + const float amplitude = 30000.0f; for (unsigned int i = 0; i < sampleCount; ++i) { float t = static_cast(i) / sampleRate; diff --git a/src/UI.cpp b/src/UI.cpp index 702f7e3..070c3f3 100644 --- a/src/UI.cpp +++ b/src/UI.cpp @@ -10,38 +10,40 @@ UI::UI() : fontLoaded_(false) { fontLoaded_ = true; algorithmText_.setFont(font_); - algorithmText_.setCharacterSize(24); + algorithmText_.setCharacterSize(28); algorithmText_.setFillColor(sf::Color::White); - algorithmText_.setPosition(20.0f, 20.0f); + algorithmText_.setPosition(15.0f, 15.0f); stateText_.setFont(font_); stateText_.setCharacterSize(20); stateText_.setFillColor(sf::Color::White); - stateText_.setPosition(20.0f, 55.0f); + stateText_.setPosition(15.0f, 50.0f); comparisonsText_.setFont(font_); comparisonsText_.setCharacterSize(18); - comparisonsText_.setFillColor(sf::Color(200, 200, 200)); - comparisonsText_.setPosition(20.0f, 90.0f); + comparisonsText_.setFillColor(sf::Color(220, 220, 220)); + comparisonsText_.setPosition(15.0f, 80.0f); swapsText_.setFont(font_); swapsText_.setCharacterSize(18); - swapsText_.setFillColor(sf::Color(200, 200, 200)); - swapsText_.setPosition(20.0f, 115.0f); + swapsText_.setFillColor(sf::Color(220, 220, 220)); + swapsText_.setPosition(15.0f, 105.0f); speedText_.setFont(font_); speedText_.setCharacterSize(18); - speedText_.setFillColor(sf::Color(200, 200, 200)); - speedText_.setPosition(20.0f, 140.0f); + speedText_.setFillColor(sf::Color(220, 220, 220)); + speedText_.setPosition(15.0f, 130.0f); controlsText_.setFont(font_); - controlsText_.setCharacterSize(14); - controlsText_.setFillColor(sf::Color(150, 150, 150)); - controlsText_.setPosition(20.0f, 175.0f); + controlsText_.setCharacterSize(15); + controlsText_.setFillColor(sf::Color(200, 200, 200)); controlsText_.setString("[1-5] Algorithms [Space] Play/Pause [Right] Step [Up/Down] Speed [R] Shuffle"); + + leftBackground_.setFillColor(sf::Color(0, 0, 0, 180)); + rightBackground_.setFillColor(sf::Color(0, 0, 0, 180)); } -void UI::update(const Sorter& sorter, bool isPlaying, bool isFinished, float stepDelay, const Array& array) { +void UI::update(const Sorter& sorter, bool isPlaying, bool isFinished, int stepsPerFrame, const Array& array) { if (!fontLoaded_) { return; } @@ -63,9 +65,7 @@ void UI::update(const Sorter& sorter, bool isPlaying, bool isFinished, float ste comparisonsText_.setString("Comparisons: " + std::to_string(array.getComparisons())); swapsText_.setString("Swaps: " + std::to_string(array.getSwaps())); - - int delayMs = static_cast(stepDelay * 1000.0f); - speedText_.setString("Delay: " + std::to_string(delayMs) + "ms"); + speedText_.setString("Speed: " + std::to_string(stepsPerFrame) + "x"); } void UI::draw(sf::RenderWindow& window) { @@ -73,6 +73,24 @@ void UI::draw(sf::RenderWindow& window) { return; } + float leftWidth = 350.0f; + float leftHeight = 160.0f; + leftBackground_.setSize(sf::Vector2f(leftWidth, leftHeight)); + leftBackground_.setPosition(5.0f, 5.0f); + + sf::FloatRect controlsBounds = controlsText_.getLocalBounds(); + float rightWidth = controlsBounds.width + 20.0f; + float rightHeight = controlsBounds.height + 20.0f; + float windowWidth = static_cast(window.getSize().x); + + rightBackground_.setSize(sf::Vector2f(rightWidth, rightHeight)); + rightBackground_.setPosition(windowWidth - rightWidth - 5.0f, 5.0f); + + controlsText_.setPosition(windowWidth - controlsBounds.width - 15.0f, 15.0f); + + window.draw(leftBackground_); + window.draw(rightBackground_); + window.draw(algorithmText_); window.draw(stateText_); window.draw(comparisonsText_);