Add demo to main, press different keys to generate using different algorithms

This commit is contained in:
twistdroach 2024-05-02 23:23:10 -04:00
parent 48917f4770
commit a796410fb4
3 changed files with 38 additions and 12 deletions

View File

@ -1,20 +1,19 @@
cmake_minimum_required(VERSION 3.15)
project(sfml_game)
project(maze_cpp)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
find_package(SFML 2.5 COMPONENTS system window graphics network audio REQUIRED)
#find_package(Catch2 3 REQUIRED)
find_package(Catch2 REQUIRED)
add_subdirectory(src)
add_subdirectory(demos)
add_executable(sfml_game main.cpp)
target_link_libraries(sfml_game PRIVATE sfml-system sfml-window sfml-graphics sfml-network sfml-audio maze)
add_executable(sfml_maze main.cpp)
target_link_libraries(sfml_maze PRIVATE sfml-system sfml-window sfml-graphics sfml-network sfml-audio maze)
# Add Catch2 test target
# Add Catch test target
enable_testing()
add_subdirectory(tests)

View File

@ -1,9 +1,13 @@
// main.cpp
#include <SFML/Graphics.hpp>
#include <cmath>
#include "Grid.h"
#include "Sidewinder.h"
#include "AldousBroder.h"
#include "Wilsons.h"
#include "HuntAndKill.h"
#include "RecursiveBacktracker.h"
#include "BinaryTree.h"
sf::Color interpolateColors(const sf::Color& c1, const sf::Color& c2, float t) {
auto r = static_cast<sf::Uint8>((1 - t) * c1.r + t * c2.r);
@ -92,9 +96,9 @@ void drawGridToTexture(sf::RenderTexture& render_texture, const DistanceGrid& gr
render_texture.display();
}
const int CELL_SIZE = 50;
const int WIDTH = 30;
const int HEIGHT = 30;
const int CELL_SIZE = 20;
const int WIDTH = 25;
const int HEIGHT = 25;
int main() {
@ -103,7 +107,9 @@ int main() {
grid.setStart(grid.randomCell());
sf::RenderWindow window(sf::VideoMode(WIDTH * CELL_SIZE, HEIGHT * CELL_SIZE), "Hello, SFML!");
while (window.isOpen()) {
while ( window.isOpen() &&
!sf::Keyboard::isKeyPressed(sf::Keyboard::Escape) &&
!sf::Keyboard::isKeyPressed(sf::Keyboard::Q)) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
@ -111,11 +117,31 @@ int main() {
}
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Space))
if (sf::Keyboard::isKeyPressed(sf::Keyboard::S))
{
grid = DistanceGrid(HEIGHT, WIDTH);
Sidewinder::on(grid);
grid.setStart(grid.randomCell());
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
grid = DistanceGrid(HEIGHT, WIDTH);
AldousBroder::on(grid);
grid.setStart(grid.randomCell());
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::W)) {
grid = DistanceGrid(HEIGHT, WIDTH);
Wilsons::on(grid);
grid.setStart(grid.randomCell());
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::H)) {
grid = DistanceGrid(HEIGHT, WIDTH);
HuntAndKill::on(grid);
grid.setStart(grid.randomCell());
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::R)) {
grid = DistanceGrid(HEIGHT, WIDTH);
RecursiveBacktracker::on(grid);
grid.setStart(grid.randomCell());
} else if (sf::Keyboard::isKeyPressed(sf::Keyboard::B)) {
grid = DistanceGrid(HEIGHT, WIDTH);
BinaryTree::on(grid);
grid.setStart(grid.randomCell());
}
window.clear(sf::Color::Blue);

View File

@ -7,6 +7,7 @@
#include <stdexcept>
#include <fstream>
#include <string>
#include <tuple>
static std::random_device rd;
static std::mt19937 gen(rd());