#include "Game.h"
#include<iostream>
using namespace std;
const sf::Time TimePerFrame = sf::seconds(1.f / 60.f);
Game::Game()
: mWindow(sf::VideoMode(640, 480), "SFML Application"), mPlayer()
{
//sf::Vector2f pPlayer = mPlayer.getPosition();
mPlayer.setRadius(20.f);
mPlayer.setPosition(100.f, 100.f);
mPlayer.setFillColor(sf::Color::Cyan);
}
void Game::run()
{
sf::Clock clock;
sf::Time timeSinceLastUpdate = sf::Time::Zero;//Set time = 0
while (mWindow.isOpen())
{
processEvents();
timeSinceLastUpdate += clock.restart();
while (timeSinceLastUpdate > TimePerFrame)
{
timeSinceLastUpdate -= TimePerFrame;
processEvents();
update(timeSinceLastUpdate);
}
render();
std::cout << 1.0f / TimePerFrame.asSeconds() << std::endl;
clock.restart().asSeconds();
}
}
void Game::processEvents()
{
sf::Event event;
while (mWindow.pollEvent(event))
{
if (event.type == sf::Event::Closed)
mWindow.close();
if (sf::Mouse::isButtonPressed(sf::Mouse::Left))
{
//mPlayer.setPosition(sf::Mouse::getPosition(mWindow).x - 20,sf::Mouse::getPosition(mWindow).y - 20);
sf::Vector2i mousePos = sf::Mouse::getPosition(mWindow);
sf::Vector2f target = mWindow.mapPixelToCoords(mousePos);
std::cout << "MOUSE LEFT CLICK" << std::endl;
const float speed = 1.0f;
sf::Vector2f position = mPlayer.getPosition();
sf::Vector2f direction = target - position;
}
}
}
void Game::update(sf::Time timeSinceLastUpdate)
{
sf::Time dt = timeSinceLastUpdate;
//position += speed * thor::unitVector(direction) * dt.asSeconds();
}
void Game::render()
{
mWindow.clear();
mWindow.draw(mPlayer);
mWindow.display();
}