abg

Animated background for X11
git clone git://git.laack.co/abg.git
Log | Files | Refs | README | LICENSE

main.cpp (3713B)


      1 #include <raylib.h>
      2 #include <unistd.h>
      3 
      4 #include <cstdint>
      5 #include <cstdlib>
      6 #include <ctime>
      7 #include <iostream>
      8 #include <queue>
      9 #include <unordered_set>
     10 
     11 #include "../include/background.hpp"
     12 #include "../include/constants.hpp"
     13 #include "../include/graph.hpp"
     14 #include "../include/prim.hpp"
     15 #include "../vendor/argparse.hpp"
     16 
     17 int main(int argc, char** argv) {
     18     // reverse semver
     19     // software does get completed at some point, and for me that's at 1.0.0.
     20 
     21     argparse::ArgumentParser program("abg", "10.0.0");
     22 
     23     program.add_argument("--vertices")
     24         .help("number of vertices in the graph")
     25         .default_value(DEFAULT_VERTEX_COUNT)
     26         .scan<'i', std::size_t>();
     27 
     28     program.add_argument("--sleep", "-s")
     29         .help("amount of time to sleep between traversals")
     30         .default_value(DEFAULT_SLEEP_TIME)
     31         .scan<'g', float>();
     32 
     33     // edges != total number of unique edges in all cases where uniqueness is
     34     // defined by vertices this is because we allow multiple edges between two
     35     // vertices (though they aren't rendered differently) we don't allow
     36     // self-edges though.
     37 
     38     program.add_argument("--edges", "-e")
     39         .help("number of edges in the graph")
     40         .default_value(DEFAULT_EDGE_COUNT)
     41         .scan<'i', std::size_t>();
     42 
     43     try {
     44         program.parse_args(argc, argv);
     45     } catch (const std::exception& err) {
     46         std::cerr << err.what() << std::endl;
     47         std::cerr << program;
     48         std::exit(1);
     49     }
     50 
     51     std::size_t vertexCount = program.get<std::size_t>("--vertices");
     52     std::size_t edgeCount = program.get<std::size_t>("--edges");
     53     float sleepTime = program.get<float>("--sleep");
     54 
     55     if (sleepTime < 0) {
     56         std::cout << "Sleep time must be >= 0." << std::endl;
     57         return -1;
     58     }
     59     if (vertexCount == 0) {
     60         std::cout << "Vertex count must be >= 1" << std::endl;
     61         return -1;
     62     }
     63     if (edgeCount == 0) {
     64         std::cout << "Edge count must be >= 1" << std::endl;
     65         return -1;
     66     }
     67     if (edgeCount > 0 && vertexCount <= 1) {
     68         std::cout << "Not enough vertices to create any edges." << std::endl;
     69         return -1;
     70     }
     71 
     72     SetTraceLogLevel(LOG_ERROR);
     73 
     74     auto ss = getScreenSize();
     75 
     76     uint32_t xMax = ss[0];
     77     uint32_t yMax = ss[1];
     78 
     79     // would be nice to do this all in background.cpp, but raylib and x11 can't
     80     // both be imported by the same file because of some dependency chain thing
     81     // with Font.
     82 
     83     InitWindow(xMax, yMax, "abg");
     84     sendToBg("abg");
     85     while (!WindowShouldClose()) {
     86         Graph g = Graph(edgeCount, vertexCount, xMax, yMax);
     87         std::vector<double> minEdgeToVertex(vertexCount, -1);
     88 
     89         std::unordered_set<std::size_t> visitedIndices{};
     90         std::priority_queue<Edge, std::vector<Edge>, std::greater<Edge>>
     91             toVisit{};
     92         std::vector<Edge> edges = g.getEdgesOfVertexIdx(0);
     93 
     94         for (auto edge : edges) {
     95             toVisit.push(edge);
     96         }
     97 
     98         g.traverseVertexIdx(0);
     99         visitedIndices.insert(0);
    100 
    101         RenderTexture2D blankGraph = LoadRenderTexture(xMax, yMax);
    102         BeginTextureMode(blankGraph);
    103         ClearBackground(BLACK);
    104         g.render();
    105         EndTextureMode();
    106 
    107         while (!WindowShouldClose() && toVisit.size() != 0) {
    108             BeginTextureMode(blankGraph);
    109             g.renderUnrenderedTraversed();
    110             EndTextureMode();
    111 
    112             BeginDrawing();
    113             DrawTexture(blankGraph.texture, 0, 0, WHITE);
    114             EndDrawing();
    115 
    116             usleep((int)(sleepTime * 1000000));
    117 
    118             oneStepPrim(toVisit, visitedIndices, g, minEdgeToVertex);
    119         }
    120 
    121         UnloadRenderTexture(blankGraph);
    122     }
    123 
    124     CloseWindow();
    125 }