// G. Hagopian making ball bound around inside a box

#define _USE_MATH_DEFINES  //for M_PI

#include <SFML/Graphics.hpp>
#include <random>
#include <iostream>
#include <cmath>
//#include <ctime>
using namespace std;

mt19937 mt_rand(time(0));  //seed the random number generator on the global scope

#define w_width 600
#define w_height 400
#define maxPoints 10000

unsigned int frameCounter{ 0 };  // static

void update(sf::CircleShape& ball, sf::Vector2f& ballVector,sf::Vertex v[]) {
	if (ball.getPosition().y > w_height-8 || ball.getPosition().y < 4) {
		ballVector.y *= -1;
	}
	if (ball.getPosition().x > w_width - 8 || ball.getPosition().x < 4) {
		ballVector.x *= -1;
	}
	ball.move(ballVector);
	v[frameCounter] = sf::Vector2f(ball.getPosition().x, ball.getPosition().y);
	++frameCounter;
}

int main() {
	// Make a window that is 800 by 200 pixels
	// And has the title "Hello from SFML"
	sf::RenderWindow window(sf::VideoMode(w_width, w_height), "Ball Bounce");
	window.setFramerateLimit(80);
	unsigned int keyPresses{ 0 };

	//Pick a random point on the interior
	sf::Vector2f rand_pt(mt_rand() % w_width, mt_rand() % w_height);
	sf::CircleShape ball(8);
	ball.setOrigin(4, 4);
	ball.setPosition(rand_pt.x, rand_pt.y);
	ball.setFillColor(sf::Color::Red);

	sf::Vector2f ballVector(1, 2);
	
	sf::Vertex vertices[maxPoints];
	vertices[0] = sf::Vector2f(ball.getPosition());
	++frameCounter;


	// This "while" loop goes round and round- perhaps forever
	while (window.isOpen()) {
		// The next 5 lines detects and responds to a request to close the window
		sf::Event event;
		while (window.pollEvent(event)) {
			if (event.type == sf::Event::Closed)
				window.close();
		}
		// Clear everything from the last run of the while loop
		window.clear();

		// Draw our circle and point
		window.draw(ball);
		
		//update
		update(ball, ballVector, vertices);

		//draw trail
		window.draw(vertices, frameCounter, sf::LineStrip);
		// Show everything we just drew
		window.display();
	}// This is the end of the "while" loop
}