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
+36 -34
View File
@@ -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<float>(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<float>(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<Sorter> newSorter) {
sweepIndex_ = 0;
lastComparisons_ = 0;
lastSwaps_ = 0;
stepsPerFrame_ = 1;
}
+1 -1
View File
@@ -10,7 +10,7 @@ void App::generateBeepSound() {
std::vector<sf::Int16> 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<float>(i) / sampleRate;
+34 -16
View File
@@ -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<int>(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<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(stateText_);
window.draw(comparisonsText_);