This commit is contained in:
2026-03-02 16:18:48 +03:00
parent d87484393d
commit 0a55fef17c
5 changed files with 75 additions and 53 deletions
+1 -1
View File
@@ -25,7 +25,7 @@ private:
UI ui_; UI ui_;
bool isPlaying_; bool isPlaying_;
float timeSinceLastStep_; float timeSinceLastStep_;
float stepDelay_; int stepsPerFrame_;
sf::SoundBuffer beepBuffer_; sf::SoundBuffer beepBuffer_;
sf::Sound beepSound_; sf::Sound beepSound_;
+3 -1
View File
@@ -7,7 +7,7 @@ class UI {
public: public:
UI(); 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); void draw(sf::RenderWindow& window);
private: private:
@@ -18,5 +18,7 @@ private:
sf::Text swapsText_; sf::Text swapsText_;
sf::Text speedText_; sf::Text speedText_;
sf::Text controlsText_; sf::Text controlsText_;
sf::RectangleShape leftBackground_;
sf::RectangleShape rightBackground_;
bool fontLoaded_; bool fontLoaded_;
}; };
+33 -31
View File
@@ -12,7 +12,7 @@ App::App()
, ui_() , ui_()
, isPlaying_(false) , isPlaying_(false)
, timeSinceLastStep_(0.0f) , timeSinceLastStep_(0.0f)
, stepDelay_(0.01f) , stepsPerFrame_(1)
, isSweeping_(false) , isSweeping_(false)
, sweepIndex_(0) , sweepIndex_(0)
, sweepTimer_(0.0f) , sweepTimer_(0.0f)
@@ -22,7 +22,7 @@ App::App()
window_.setFramerateLimit(60); window_.setFramerateLimit(60);
generateBeepSound(); generateBeepSound();
beepSound_.setBuffer(beepBuffer_); beepSound_.setBuffer(beepBuffer_);
beepSound_.setVolume(15.0f); beepSound_.setVolume(100.0f);
} }
void App::run() { void App::run() {
@@ -55,13 +55,13 @@ void App::handleEvents() {
break; break;
case sf::Keyboard::Up: case sf::Keyboard::Up:
stepDelay_ = (stepDelay_ > 0.001f) ? stepDelay_ * 0.5f : 0.001f; stepsPerFrame_ = (stepsPerFrame_ < 500) ? stepsPerFrame_ * 2 : 500;
if (stepDelay_ < 0.001f) stepDelay_ = 0.001f; if (stepsPerFrame_ < 1) stepsPerFrame_ = 1;
break; break;
case sf::Keyboard::Down: case sf::Keyboard::Down:
stepDelay_ = (stepDelay_ < 0.5f) ? stepDelay_ * 2.0f : 0.5f; stepsPerFrame_ = (stepsPerFrame_ > 1) ? stepsPerFrame_ / 2 : 1;
if (stepDelay_ > 0.5f) stepDelay_ = 0.5f; if (stepsPerFrame_ < 1) stepsPerFrame_ = 1;
break; break;
case sf::Keyboard::Num1: case sf::Keyboard::Num1:
@@ -95,6 +95,7 @@ void App::handleEvents() {
sweepIndex_ = 0; sweepIndex_ = 0;
lastComparisons_ = 0; lastComparisons_ = 0;
lastSwaps_ = 0; lastSwaps_ = 0;
stepsPerFrame_ = 1;
break; break;
default: default:
@@ -105,7 +106,7 @@ void App::handleEvents() {
} }
void App::update(float dt) { void App::update(float dt) {
ui_.update(*currentSorter_, isPlaying_, currentSorter_->isFinished(), stepDelay_, array_); ui_.update(*currentSorter_, isPlaying_, currentSorter_->isFinished(), stepsPerFrame_, array_);
if (isSweeping_) { if (isSweeping_) {
sweepTimer_ += dt; sweepTimer_ += dt;
@@ -128,40 +129,40 @@ void App::update(float dt) {
} }
if (isPlaying_ && !currentSorter_->isFinished()) { if (isPlaying_ && !currentSorter_->isFinished()) {
timeSinceLastStep_ += dt; size_t beforeComparisons = array_.getComparisons();
size_t beforeSwaps = array_.getSwaps();
if (timeSinceLastStep_ >= stepDelay_) { for (int step = 0; step < stepsPerFrame_; ++step) {
size_t beforeComparisons = array_.getComparisons(); if (currentSorter_->isFinished()) {
size_t beforeSwaps = array_.getSwaps(); break;
}
currentSorter_->step(array_); currentSorter_->step(array_);
}
size_t afterComparisons = array_.getComparisons(); size_t afterComparisons = array_.getComparisons();
size_t afterSwaps = array_.getSwaps(); size_t afterSwaps = array_.getSwaps();
if (afterComparisons > beforeComparisons || afterSwaps > beforeSwaps) { if (afterComparisons > beforeComparisons || afterSwaps > beforeSwaps) {
float avgPitch = 1.0f; float avgPitch = 1.0f;
for (int i = 0; i < array_.getSize(); ++i) { for (int i = 0; i < array_.getSize(); ++i) {
if (array_.getState(i) == Array::State::COMPARE || if (array_.getState(i) == Array::State::COMPARE ||
array_.getState(i) == Array::State::SWAP) { array_.getState(i) == Array::State::SWAP) {
float normalizedValue = array_.getValue(i) / static_cast<float>(array_.getSize()); float normalizedValue = array_.getValue(i) / static_cast<float>(array_.getSize());
avgPitch = 0.5f + normalizedValue * 1.5f; avgPitch = 0.5f + normalizedValue * 1.5f;
break; break;
}
} }
playBeep(avgPitch);
} }
timeSinceLastStep_ = 0.0f; playBeep(avgPitch);
}
if (currentSorter_->isFinished()) { if (currentSorter_->isFinished()) {
array_.resetStates(); array_.resetStates();
isSweeping_ = true; isSweeping_ = true;
sweepIndex_ = 0; sweepIndex_ = 0;
sweepTimer_ = 0.0f; sweepTimer_ = 0.0f;
}
} }
} }
} }
@@ -204,4 +205,5 @@ void App::switchSorter(std::unique_ptr<Sorter> newSorter) {
sweepIndex_ = 0; sweepIndex_ = 0;
lastComparisons_ = 0; lastComparisons_ = 0;
lastSwaps_ = 0; lastSwaps_ = 0;
stepsPerFrame_ = 1;
} }
+1 -1
View File
@@ -10,7 +10,7 @@ void App::generateBeepSound() {
std::vector<sf::Int16> samples(sampleCount); std::vector<sf::Int16> samples(sampleCount);
const float frequency = 440.0f; const float frequency = 440.0f;
const float amplitude = 3000.0f; const float amplitude = 30000.0f;
for (unsigned int i = 0; i < sampleCount; ++i) { for (unsigned int i = 0; i < sampleCount; ++i) {
float t = static_cast<float>(i) / sampleRate; float t = static_cast<float>(i) / sampleRate;
+34 -16
View File
@@ -10,38 +10,40 @@ UI::UI() : fontLoaded_(false) {
fontLoaded_ = true; fontLoaded_ = true;
algorithmText_.setFont(font_); algorithmText_.setFont(font_);
algorithmText_.setCharacterSize(24); algorithmText_.setCharacterSize(28);
algorithmText_.setFillColor(sf::Color::White); algorithmText_.setFillColor(sf::Color::White);
algorithmText_.setPosition(20.0f, 20.0f); algorithmText_.setPosition(15.0f, 15.0f);
stateText_.setFont(font_); stateText_.setFont(font_);
stateText_.setCharacterSize(20); stateText_.setCharacterSize(20);
stateText_.setFillColor(sf::Color::White); stateText_.setFillColor(sf::Color::White);
stateText_.setPosition(20.0f, 55.0f); stateText_.setPosition(15.0f, 50.0f);
comparisonsText_.setFont(font_); comparisonsText_.setFont(font_);
comparisonsText_.setCharacterSize(18); comparisonsText_.setCharacterSize(18);
comparisonsText_.setFillColor(sf::Color(200, 200, 200)); comparisonsText_.setFillColor(sf::Color(220, 220, 220));
comparisonsText_.setPosition(20.0f, 90.0f); comparisonsText_.setPosition(15.0f, 80.0f);
swapsText_.setFont(font_); swapsText_.setFont(font_);
swapsText_.setCharacterSize(18); swapsText_.setCharacterSize(18);
swapsText_.setFillColor(sf::Color(200, 200, 200)); swapsText_.setFillColor(sf::Color(220, 220, 220));
swapsText_.setPosition(20.0f, 115.0f); swapsText_.setPosition(15.0f, 105.0f);
speedText_.setFont(font_); speedText_.setFont(font_);
speedText_.setCharacterSize(18); speedText_.setCharacterSize(18);
speedText_.setFillColor(sf::Color(200, 200, 200)); speedText_.setFillColor(sf::Color(220, 220, 220));
speedText_.setPosition(20.0f, 140.0f); speedText_.setPosition(15.0f, 130.0f);
controlsText_.setFont(font_); controlsText_.setFont(font_);
controlsText_.setCharacterSize(14); controlsText_.setCharacterSize(15);
controlsText_.setFillColor(sf::Color(150, 150, 150)); controlsText_.setFillColor(sf::Color(200, 200, 200));
controlsText_.setPosition(20.0f, 175.0f);
controlsText_.setString("[1-5] Algorithms [Space] Play/Pause [Right] Step [Up/Down] Speed [R] Shuffle"); 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_) { if (!fontLoaded_) {
return; 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())); comparisonsText_.setString("Comparisons: " + std::to_string(array.getComparisons()));
swapsText_.setString("Swaps: " + std::to_string(array.getSwaps())); swapsText_.setString("Swaps: " + std::to_string(array.getSwaps()));
speedText_.setString("Speed: " + std::to_string(stepsPerFrame) + "x");
int delayMs = static_cast<int>(stepDelay * 1000.0f);
speedText_.setString("Delay: " + std::to_string(delayMs) + "ms");
} }
void UI::draw(sf::RenderWindow& window) { void UI::draw(sf::RenderWindow& window) {
@@ -73,6 +73,24 @@ void UI::draw(sf::RenderWindow& window) {
return; 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<float>(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(algorithmText_);
window.draw(stateText_); window.draw(stateText_);
window.draw(comparisonsText_); window.draw(comparisonsText_);