maze-cpp/demos/ColoredMaze.cpp

98 lines
2.7 KiB
C++

// Simple demo to color a maze based on distance from middle of the maze
#include "Grid.h"
#include "algorithms/Sidewinder.h"
#include "algorithms/BinaryTree.h"
#include "algorithms/AldousBroder.h"
#include "algorithms/Wilsons.h"
#include "algorithms/HuntAndKill.h"
#include "algorithms/RecursiveBacktracker.h"
#include "SfmlUtils.h"
#include <SFML/Graphics.hpp>
const int CELL_SIZE = 50;
const int WIDTH = 30;
const int HEIGHT = 30;
int main() {
DistanceGrid grid(HEIGHT, WIDTH);
Sidewinder::on(grid);
grid.setStart(grid.getCellRef(grid.getCols() / 2, grid.getRows() / 2));
sf::RenderWindow window(sf::VideoMode(WIDTH * CELL_SIZE, HEIGHT * CELL_SIZE), "s: Sidewinder b: BinaryTree a: AldousBroder w: Wilsons h: HuntAndKill, r: RecursiveBacktracker");
while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::B))
{
grid = DistanceGrid(HEIGHT, WIDTH);
BinaryTree::on(grid);
grid.setStart(grid.getCellRef(grid.getCols() / 2, grid.getRows() / 2));
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
grid = DistanceGrid(HEIGHT, WIDTH);
Sidewinder::on(grid);
grid.setStart(grid.getCellRef(grid.getCols() / 2, grid.getRows() / 2));
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::A))
{
grid = DistanceGrid(HEIGHT, WIDTH);
AldousBroder::on(grid);
auto start = grid.getCellRef(grid.getCols() / 2, grid.getRows() / 2);
grid.setStart(start);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::W))
{
grid = DistanceGrid(HEIGHT, WIDTH);
Wilsons::on(grid);
auto start = grid.getCellRef(grid.getCols() / 2, grid.getRows() / 2);
grid.setStart(start);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::H))
{
grid = DistanceGrid(HEIGHT, WIDTH);
HuntAndKill::on(grid);
auto start = grid.getCellRef(grid.getCols() / 2, grid.getRows() / 2);
grid.setStart(start);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::R))
{
grid = DistanceGrid(HEIGHT, WIDTH);
RecursiveBacktracker::on(grid);
auto start = grid.getCellRef(grid.getCols() / 2, grid.getRows() / 2);
grid.setStart(start);
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
window.close();
}
window.clear(sf::Color::Blue);
sf::RenderTexture render_texture;
utils::sfml::drawGridToTexture(render_texture, grid, CELL_SIZE);
const sf::Texture& texture = render_texture.getTexture();
sf::Sprite sprite(texture);
window.draw(sprite);
window.display();
}
return 0;
}