#include "SFML\Graphics.hpp"
#include "main.h"

Main::Main()
{
	settings = new sf::ContextSettings(16, 24, 2, 0, 8);
	clock = new sf::Clock;
	window = new sf::RenderWindow(sf::VideoMode(800, 600), "SFML works!", sf::Style::Close, *settings);
	mut = new sf::Mutex;
	drawThread = new sf::Thread(&Main::THREAD_Draw, this);
	evt = new sf::Event;

	shape1 = new sf::CircleShape(100);
	shape2 = new sf::CircleShape(100);

	shape1->setFillColor(sf::Color::Green);
	shape2->setFillColor(sf::Color::Red);
	shape1->setPosition(100, 0);
	shape2->setPosition(300, 0);

	time = 0;
	PlayMain();
}

void Main::PlayMain()
{

	while (window->isOpen())
	{
		time = (float)clock->getElapsedTime().asMicroseconds() / 800;
		clock->restart();

		while (window->pollEvent(*evt))
		{
			if (evt->type == sf::Event::Closed)
				window->close();
		}

		window->clear();
		window->setActive(false);

		drawThread->launch(); drawThread->wait();

		window->display();
	}
}

void Main::THREAD_Draw()
{
	mut->lock();
	window->draw(*shape1);
	window->draw(*shape2);
	mut->unlock();
}