This commit is contained in:
2026-03-02 15:48:49 +03:00
parent 7cdd2068be
commit d87484393d
21 changed files with 986 additions and 7 deletions
+156 -5
View File
@@ -1,7 +1,28 @@
#include "App.hpp"
#include "sorters/BubbleSorter.hpp"
#include "sorters/SelectionSorter.hpp"
#include "sorters/InsertionSorter.hpp"
#include "sorters/MergeSorter.hpp"
#include "sorters/QuickSorter.hpp"
App::App() : window_(sf::VideoMode(1280, 720), "SortLab"), array_(100) {
window_.setFramerateLimit(60);
App::App()
: window_(sf::VideoMode(1280, 720), "SortLab")
, array_(100)
, currentSorter_(std::make_unique<BubbleSorter>())
, ui_()
, isPlaying_(false)
, timeSinceLastStep_(0.0f)
, stepDelay_(0.01f)
, isSweeping_(false)
, sweepIndex_(0)
, sweepTimer_(0.0f)
, sweepDelay_(0.005f)
, lastComparisons_(0)
, lastSwaps_(0) {
window_.setFramerateLimit(60);
generateBeepSound();
beepSound_.setBuffer(beepBuffer_);
beepSound_.setVolume(15.0f);
}
void App::run() {
@@ -20,14 +41,129 @@ void App::handleEvents() {
if (event.type == sf::Event::Closed) {
window_.close();
}
if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::R) {
array_.shuffle();
if (event.type == sf::Event::KeyPressed) {
switch (event.key.code) {
case sf::Keyboard::Space:
isPlaying_ = !isPlaying_;
break;
case sf::Keyboard::Right:
if (!isPlaying_ && !currentSorter_->isFinished()) {
currentSorter_->step(array_);
}
break;
case sf::Keyboard::Up:
stepDelay_ = (stepDelay_ > 0.001f) ? stepDelay_ * 0.5f : 0.001f;
if (stepDelay_ < 0.001f) stepDelay_ = 0.001f;
break;
case sf::Keyboard::Down:
stepDelay_ = (stepDelay_ < 0.5f) ? stepDelay_ * 2.0f : 0.5f;
if (stepDelay_ > 0.5f) stepDelay_ = 0.5f;
break;
case sf::Keyboard::Num1:
switchSorter(std::make_unique<BubbleSorter>());
break;
case sf::Keyboard::Num2:
switchSorter(std::make_unique<SelectionSorter>());
break;
case sf::Keyboard::Num3:
switchSorter(std::make_unique<InsertionSorter>());
break;
case sf::Keyboard::Num4:
switchSorter(std::make_unique<MergeSorter>());
break;
case sf::Keyboard::Num5:
switchSorter(std::make_unique<QuickSorter>());
break;
case sf::Keyboard::R:
array_.shuffle();
array_.resetStates();
array_.resetCounters();
currentSorter_->reset();
isPlaying_ = false;
timeSinceLastStep_ = 0.0f;
isSweeping_ = false;
sweepIndex_ = 0;
lastComparisons_ = 0;
lastSwaps_ = 0;
break;
default:
break;
}
}
}
}
void App::update(float dt) {
ui_.update(*currentSorter_, isPlaying_, currentSorter_->isFinished(), stepDelay_, array_);
if (isSweeping_) {
sweepTimer_ += dt;
if (sweepTimer_ >= sweepDelay_) {
if (sweepIndex_ < array_.getSize()) {
array_.setState(sweepIndex_, Array::State::SORTED);
float normalizedValue = array_.getValue(sweepIndex_) / static_cast<float>(array_.getSize());
float pitch = 0.5f + normalizedValue * 1.5f;
playBeep(pitch);
sweepIndex_++;
sweepTimer_ = 0.0f;
} else {
isSweeping_ = false;
}
}
return;
}
if (isPlaying_ && !currentSorter_->isFinished()) {
timeSinceLastStep_ += dt;
if (timeSinceLastStep_ >= stepDelay_) {
size_t beforeComparisons = array_.getComparisons();
size_t beforeSwaps = array_.getSwaps();
currentSorter_->step(array_);
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;
}
}
playBeep(avgPitch);
}
timeSinceLastStep_ = 0.0f;
if (currentSorter_->isFinished()) {
array_.resetStates();
isSweeping_ = true;
sweepIndex_ = 0;
sweepTimer_ = 0.0f;
}
}
}
}
void App::render() {
@@ -52,5 +188,20 @@ void App::render() {
window_.draw(bar);
}
ui_.draw(window_);
window_.display();
}
void App::switchSorter(std::unique_ptr<Sorter> newSorter) {
currentSorter_ = std::move(newSorter);
array_.shuffle();
array_.resetStates();
array_.resetCounters();
isPlaying_ = false;
timeSinceLastStep_ = 0.0f;
isSweeping_ = false;
sweepIndex_ = 0;
lastComparisons_ = 0;
lastSwaps_ = 0;
}