// These "include" code from the C++ library and SFML too
#include "stdafx.h"
#include <iostream>
#include <SFML/Graphics.hpp>

void draw(sf::RenderWindow& w, int xpts[], int ypts[], int level) {
	sf::VertexArray lines(sf::LineStrip, 4);
	lines[0].position = sf::Vector2f(xpts[0], ypts[0]);
	lines[1].position = sf::Vector2f(xpts[1], ypts[1]);
	lines[2].position = sf::Vector2f(xpts[2], ypts[2]);
	lines[3].position = sf::Vector2f(xpts[0], ypts[0]);
	w.draw(lines);
	if (level > 1) {//recurse
		int x[] = { (xpts[0]+xpts[1])/2, (xpts[2]+xpts[1])/2,(xpts[0]+xpts[2])/2 };
		int y[] = { (ypts[0]+ypts[1])/2, (ypts[2]+ypts[1])/2,(ypts[0]+ypts[2])/2 };
		draw(w, x, y, --level);
	}
}

int main() {
	int n{ 1 }; //Fractal depth
	// Make a window that is 800 by 200 pixels with the title "Hello from SFML"
	unsigned wWidth = 800, wHeight = 600;
	sf::RenderWindow window(sf::VideoMode(wWidth, wHeight), "Hello from SFML");
	
	// set coordinates of initial vertices
	int x[] = { 100,wWidth - 100,wWidth / 2 };
	int y[] = { 100,100,wHeight - 100 };

	std::cout << "How deep goes your fractal?\n";
	std::cin >> n;
	// This "while" loop goes round and round- perhaps forever
	while (window.isOpen()) {
		// The next 6 lines of code detect if the window is closed
		// And then shuts down the program
		sf::Event event;
		while (window.pollEvent(event)) {
			if (event.type == sf::Event::Closed)
				// Someone closed the window- bye
				window.close();
		}
		// Clear everything from the last run of the while loop
		window.clear();
		// draw the scene
		draw(window, x, y, n);
		// Show everything we just drew
		window.display();
	}
}