mirror of
https://github.com/koloideal/SortLab.git
synced 2026-06-10 10:25:30 +03:00
skelet
This commit is contained in:
@@ -9,11 +9,28 @@ FetchContent_Declare(
|
|||||||
SFML
|
SFML
|
||||||
GIT_REPOSITORY https://github.com/SFML/SFML.git
|
GIT_REPOSITORY https://github.com/SFML/SFML.git
|
||||||
GIT_TAG 2.6.x
|
GIT_TAG 2.6.x
|
||||||
|
GIT_SHALLOW ON
|
||||||
|
EXCLUDE_FROM_ALL
|
||||||
|
SYSTEM
|
||||||
)
|
)
|
||||||
FetchContent_MakeAvailable(SFML)
|
FetchContent_MakeAvailable(SFML)
|
||||||
|
|
||||||
add_executable(SortLab
|
add_executable(SortLab
|
||||||
src/main.cpp
|
src/main.cpp
|
||||||
|
src/App.cpp
|
||||||
|
src/Array.cpp
|
||||||
)
|
)
|
||||||
|
|
||||||
|
target_include_directories(SortLab PRIVATE ${CMAKE_SOURCE_DIR}/include)
|
||||||
|
|
||||||
target_link_libraries(SortLab PRIVATE sfml-graphics sfml-window sfml-system)
|
target_link_libraries(SortLab PRIVATE sfml-graphics sfml-window sfml-system)
|
||||||
|
|
||||||
|
|
||||||
|
if(WIN32)
|
||||||
|
add_custom_command(TARGET SortLab POST_BUILD
|
||||||
|
COMMAND ${CMAKE_COMMAND} -E copy_if_different
|
||||||
|
$<TARGET_RUNTIME_DLLS:SortLab>
|
||||||
|
$<TARGET_FILE_DIR:SortLab>
|
||||||
|
COMMAND_EXPAND_LISTS
|
||||||
|
)
|
||||||
|
endif()
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <SFML/Graphics.hpp>
|
||||||
|
#include "Array.hpp"
|
||||||
|
|
||||||
|
class App {
|
||||||
|
public:
|
||||||
|
App();
|
||||||
|
void run();
|
||||||
|
|
||||||
|
private:
|
||||||
|
void handleEvents();
|
||||||
|
void update(float dt);
|
||||||
|
void render();
|
||||||
|
|
||||||
|
sf::RenderWindow window_;
|
||||||
|
Array array_;
|
||||||
|
};
|
||||||
|
|||||||
@@ -0,0 +1,22 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class Array {
|
||||||
|
public:
|
||||||
|
enum class State { NORMAL, COMPARE, SWAP, SORTED };
|
||||||
|
|
||||||
|
Array(int size);
|
||||||
|
void shuffle();
|
||||||
|
|
||||||
|
int getSize() const;
|
||||||
|
float getValue(int index) const;
|
||||||
|
State getState(int index) const;
|
||||||
|
|
||||||
|
void setValue(int index, float value);
|
||||||
|
void setState(int index, State state);
|
||||||
|
void resetStates();
|
||||||
|
|
||||||
|
private:
|
||||||
|
std::vector<float> data_;
|
||||||
|
std::vector<State> states_;
|
||||||
|
};
|
||||||
|
|||||||
+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() {
|
int main() {
|
||||||
sf::RenderWindow window(sf::VideoMode(1280, 720), "SortLab");
|
App app;
|
||||||
|
app.run();
|
||||||
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();
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user