You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
2.0 KiB
C++

#include <SFML/Window/Event.hpp>
#include <SFML/System/Time.hpp>
#include "application.h"
// Hardcoded for now
const sf::Time TIME_PER_SECOND = sf::seconds(1.f / 60.f);
Application::Application(unsigned int window_width, unsigned int window_height) :
render_window({window_width, window_height}, "Sliding Puzzle")
{}
bool Application::init(const std::string &path, int splitting)
{
render_window.setFramerateLimit(60);
return board.init(path, splitting, render_window);
}
void Application::run()
{
// Main game cycle
render_window.display();
while (render_window.isOpen())
{
processInput();
draw();
}
}
void Application::draw()
{
render_window.clear();
board.draw(render_window);
render_window.display();
}
void Application::processInput()
{
sf::Event event;
while (render_window.pollEvent(event))
{
switch (event.type)
{
case sf::Event::Closed:
render_window.close();
break;
case sf::Event::KeyPressed:
// If already won, do nothing
if (board.isWinCondition())
return;
// Go to selection mode
if (event.key.code == sf::Keyboard::Z)
board.onSelectionMode();
else // or just move cursor to next tile
board.moveSelection(getDirection(event.key.code));
break;
default:
break;
}
}
}
DIRECTION Application::getDirection(sf::Keyboard::Key &key) const
{
switch (key)
{
case sf::Keyboard::A:
case sf::Keyboard::Left:
case sf::Keyboard::Num4:
return DIRECTION::LEFT;
case sf::Keyboard::W:
case sf::Keyboard::Up:
case sf::Keyboard::Num8:
return DIRECTION::UP;
case sf::Keyboard::D:
case sf::Keyboard::Right:
case sf::Keyboard::Num6:
return DIRECTION::RIGHT;
case sf::Keyboard::S:
case sf::Keyboard::Down:
case sf::Keyboard::Num2:
return DIRECTION::DOWN;
default:
return DIRECTION::NONE;
}
}