|
2 | 2 | #include <SortingVisualizer/Algorithms/BubbleSort.hpp>
|
3 | 3 | #include <SortingVisualizer/Collection.hpp>
|
4 | 4 | #include <SortingVisualizer/Display.hpp>
|
| 5 | +#include <chrono> |
5 | 6 | #include <iostream>
|
| 7 | +#include <memory> |
| 8 | +#include <thread> |
| 9 | +#include <tuple> |
| 10 | + |
| 11 | +void sorter(std::tuple<Collection *, Display *> container) |
| 12 | +{ |
| 13 | + auto collectionPtr = std::get<0>(container); |
| 14 | + auto displayPtr = std::get<1>(container); |
| 15 | + |
| 16 | + if (collectionPtr == nullptr) throw "didn't expect nullptr for collection"; |
| 17 | + if (displayPtr == nullptr) throw "didn't expect nullptr for display"; |
| 18 | + |
| 19 | + auto collection = *collectionPtr; |
| 20 | + auto display = *displayPtr; |
| 21 | + |
| 22 | + auto bars = collection.contents(); |
| 23 | + bubbleSort(collection); |
| 24 | + |
| 25 | + for (auto &decision : collection.getDecisions()) |
| 26 | + { |
| 27 | + if (std::holds_alternative<Swap>(decision)) |
| 28 | + { |
| 29 | + auto swap = std::get<Swap>(decision); |
| 30 | + std::swap(bars[swap.leftIdx], bars[swap.rightIdx]); |
| 31 | + display.setBars(bars); |
| 32 | + } |
| 33 | + |
| 34 | + std::this_thread::sleep_for(std::chrono::milliseconds(16)); |
| 35 | + } |
| 36 | +} |
6 | 37 |
|
7 | 38 | int main()
|
8 | 39 | {
|
| 40 | + // seed RNG |
| 41 | + std::srand(std::time(0)); |
| 42 | + |
9 | 43 | sf::ContextSettings settings;
|
10 |
| - settings.antialiasingLevel = 16; |
| 44 | + settings.antialiasingLevel = 4; |
| 45 | + settings.majorVersion = 3; |
11 | 46 |
|
12 |
| - sf::RenderWindow window(sf::VideoMode(800, 600), "Sorting Visualizer", sf::Style::Default, settings); |
13 |
| - window.setActive(false); |
| 47 | + auto window = sf::RenderWindow(sf::VideoMode(800, 600), "Sorting Visualizer", sf::Style::Default, settings); |
14 | 48 | window.setVerticalSyncEnabled(true);
|
| 49 | + window.setFramerateLimit(60); |
15 | 50 |
|
16 |
| - Collection collection(10); |
17 |
| - Display display(window, collection); |
| 51 | + auto collection = Collection(100); |
| 52 | + collection.randomize(); |
| 53 | + |
| 54 | + auto display = Display(window, collection); |
| 55 | + |
| 56 | + auto dirty = true; |
| 57 | + |
| 58 | + sf::Thread sortAlgo(&sorter, std::make_tuple<Collection *, Display *>(&collection, &display)); |
| 59 | + sortAlgo.launch(); |
18 | 60 |
|
19 | 61 | while (window.isOpen())
|
20 | 62 | {
|
21 | 63 | sf::Event event;
|
22 | 64 | while (window.pollEvent(event))
|
23 | 65 | {
|
24 |
| - if (event.type == sf::Event::Closed) window.close(); |
25 | 66 | if (event.type == sf::Event::Resized)
|
26 | 67 | {
|
| 68 | + [[likely]] |
27 | 69 | // SFML views are useful in games, but for me, i'd prefer if i was dealing with everything
|
28 | 70 | // as if it were regular window coordinates
|
29 | 71 | window.setView(sf::View(sf::FloatRect(0.0f, 0.0f, event.size.width, event.size.height)));
|
| 72 | + |
| 73 | + // we modified the view, we want to guarantee a re-render |
| 74 | + dirty = true; |
| 75 | + } |
| 76 | + else if (event.type == sf::Event::LostFocus) |
| 77 | + { |
| 78 | + window.setFramerateLimit(12); |
| 79 | + } |
| 80 | + else if (event.type == sf::Event::GainedFocus) |
| 81 | + { |
| 82 | + window.setFramerateLimit(60); |
| 83 | + } |
| 84 | + else if (event.type == sf::Event::Closed) |
| 85 | + { |
| 86 | + [[unlikely]] window.close(); |
| 87 | + exit(0); |
30 | 88 | }
|
31 | 89 | }
|
32 | 90 |
|
33 | 91 | window.clear();
|
34 |
| - |
35 |
| - collection.randomize(); |
36 | 92 | display.draw();
|
37 |
| - |
38 | 93 | window.display();
|
39 | 94 | }
|
40 | 95 |
|
|
0 commit comments