A5/main.cpp

120 lines
3.2 KiB
C++

/**
* @author Tyler Beckman (tyler_beckman@mines.edu)
* @brief A program to read polygon points and colors from a data file and
* display them utilizing the SFML library, validating all shapes at each point
* during processing.
* @version 1
* @date 2024-11-11
*/
#include "Coordinate.h"
#include "EquilateralTriangle.h"
#include "IsoscelesTriangle.h"
#include "Polygon.h"
#include "Rhombus.h"
#include "ScaleneTriangle.h"
#include <fstream>
#include <iostream>
#include <memory>
#include <cstdio>
#include <SFML/Graphics/Color.hpp>
int main(void) {
// Start file parsing logic
std::cout << "Please enter file path to read polygons from: ";
std::string filePath;
std::cin >> filePath;
std::ifstream file(filePath);
if (file.fail()) {
std::cout << "Failed to open specified file path " << filePath
<< ", does it exist?" << std::endl;
return 1;
}
std::vector<std::unique_ptr<APolygon>> polygonList;
std::unique_ptr<APolygon> currentPolygon;
char type;
double x1, y1, x2, y2, x3, y3, x4 = -1, y4 = -1;
int r, g, b;
while (true) {
file >> type >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;
if (type == 'R') {
file >> x4 >> y4;
}
file >> r >> g >> b;
if (file.fail()) {
break;
}
switch (type) {
case 'S':
currentPolygon = std::make_unique<ScaleneTriangle>();
break;
case 'I':
currentPolygon = std::make_unique<IsoscelesTriangle>();
break;
case 'E':
currentPolygon = std::make_unique<EquilateralTriangle>();
break;
case 'R':
currentPolygon = std::make_unique<Rhombus>();
break;
default:
std::cout << "polygon is invalid - \"" << type << " " << x1 << " " << y1
<< " " << x2 << " " << y2 << " " << x3 << " " << y3 << " "
<< x4 << " " << y4 << " " << r << " " << g << " " << b << "\""
<< std::endl;
continue;
}
currentPolygon->setCoordinate(0, Coordinate(x1, y1));
currentPolygon->setCoordinate(1, Coordinate(x2, y2));
currentPolygon->setCoordinate(2, Coordinate(x3, y3));
if (type == 'R') {
currentPolygon->setCoordinate(3, Coordinate(x4, y4));
}
currentPolygon->setColor(sf::Color(r, g, b));
if (!currentPolygon->validate()) {
std::cout << "polygon is invalid - \"" << type << " " << x1 << " " << y1
<< " " << x2 << " " << y2 << " " << x3 << " " << y3;
if (x4 != -1) {
std::cout << " " << x4 << " " << y4;
}
std::cout << " " << r << " " << g << " " << b << "\"" << std::endl;
} else {
polygonList.push_back(std::move(currentPolygon));
}
};
// Start SFML Rendering logic
sf::RenderWindow window(
sf::VideoMode(640, 640), "correct horse battery staple",
sf::Style::Titlebar | sf::Style::Close // Disable window resize
);
window.setVerticalSyncEnabled(true);
sf::Event event;
while (window.isOpen()) {
window.clear();
for (size_t i = 0; i < polygonList.size(); i++) {
polygonList.at(i)->draw(window);
}
window.display();
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
}
}