diff --git a/main.cpp b/main.cpp index 819b99c..a0d1ded 100644 --- a/main.cpp +++ b/main.cpp @@ -1,29 +1,82 @@ -// make sure sfml is installed: -// linux - sudo apt-get install libsfml-dev -// windows - manual dl from https://www.sfml-dev.org/download.php -#include +#include // cin, cout +#include // ifstream +#include // srand, rand +#include // time +#include // strcmp +#include // TODO: or use arrays for maximum performance? +#include "Display.h" +#include "Point.h" // TODO: check if there is a usable SFML or c++ class #include "Timing.h" -int main() +int main (int argc, char **argv) { - sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!"); - sf::CircleShape shape(100.f); - shape.setFillColor(sf::Color::Green); - - while (window.isOpen()) + // read program arguments + // -v mode with enabled visualization + // -f read random numbers from file + // TODO: care a little more <3 + std::string fileRnd = ""; + bool vis = false; + for (int i = 0; i < argc; ++i) { - sf::Event event; - while (window.pollEvent(event)) - { - if (event.type == sf::Event::Closed) - window.close(); - } - - window.clear(); - window.draw(shape); - window.display(); + if (strcmp(argv[i], "-v") == 0) vis = true; + else if (strcmp(argv[i], "-f") == 0) fileRnd = argv[i + 1]; } + int valCount = 10; + std::vector points; + if (fileRnd.empty()) // generate random numbers + { + std::cout << "generating random numbers..." << std::endl; + //srand(static_cast (time(0))); + srand(static_cast (0)); // fixed seed for testing + for (int i = 0; i < valCount; ++i) + { + float x = static_cast (rand()) / (static_cast (RAND_MAX / WIDTH)); + float y = static_cast (rand()) / (static_cast (RAND_MAX / HEIGHT)); + points.push_back(Point(x, y)); + } + } + else // read random numbers + { + std::cout << "reading random numbers from file: " << fileRnd << "..." << std::endl; + std::ifstream in(fileRnd.c_str()); + if (in.is_open()) + { + std::string line; + std::getline(in, line); // get first line for explizit valueCount + valCount = std::stoi(line); + for (int i = 0; i < valCount; ++i) + { + getline(in, line); + //getline(in, line, ','); // could also be used slightly different but includes newlines :/ + size_t pos = line.find(','); + if (pos != std::string::npos) + { + float x = std::stof(line.substr(0, pos)); + float y = std::stof(line.substr(pos + 1)); + points.push_back(Point(x, y)); + } + else + { + throw "invalid format!"; + } + } + } + } + + for (Point& pt : points) + { + std::cout << "pt: " << pt.x() << ", " << pt.y() << std::endl; + } + + if (vis) + { + Display display; + display.show(); + } + + std::cout << "any key to exit..."; + std::cin.get(); return 0; } \ No newline at end of file