mirror of
https://github.com/koloideal/SortLab.git
synced 2026-06-10 10:25:30 +03:00
skelet
This commit is contained in:
+56
@@ -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<float>(array_.getSize());
|
||||
float maxVal = static_cast<float>(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();
|
||||
}
|
||||
|
||||
@@ -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<float>(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);
|
||||
}
|
||||
|
||||
+3
-15
@@ -1,19 +1,7 @@
|
||||
#include <SFML/Graphics.hpp>
|
||||
#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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user