Slices a provided image into tiles and then shuffles them. You need to restore initial image.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

126 lines
2.5 KiB

#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) :
current_state(STATE::PLAY),
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 (current_state)
{
case STATE::PLAY:
onGameState(event);
break;
case STATE::WIN:
onWinState(event);
break;
}
}
}
void Application::onGameState(sf::Event& event)
{
switch (event.type)
{
case sf::Event::Closed:
render_window.close();
break;
case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::Z)
board.onSelectionMode();
else
board.tryProcessDirection(getDirection(event.key.code));
break;
default:
break;
}
if (board.isWinCondition())
{
current_state = STATE::WIN;
board.setCursorVisibility(false);
}
}
void Application::onWinState(sf::Event& event)
{
switch (event.type)
{
case sf::Event::Closed:
render_window.close();
break;
case sf::Event::KeyPressed:
if (event.key.code == sf::Keyboard::Z)
render_window.close();
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;
}
}