L6C/main.cpp
2024-12-04 15:45:56 -07:00

101 lines
3.1 KiB
C++

#include <SFML/Graphics.hpp>
using namespace sf;
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char** argv) {
string filename;
if (argc >= 2) {
filename = argv[1];
} else {
cout << "Please enter a maze filename to load: ";
cin >> filename;
}
cout << "Loading maze file " << filename << endl;
ifstream mazeFile(filename);
if (mazeFile.fail()) {
std::cout << "Failed to open specified file path " << filename
<< ", does it exist?" << std::endl;
return 1;
}
int rows, cols;
mazeFile >> rows >> cols;
mazeFile.get(); // move cursor past newline to actual grid characters
char** grid = new char*[rows];
pair<int, int> start(-1, -1);
for (int row = 0; row < rows; row++) {
grid[row] = new char[cols];
for (int col = 0; col < cols; col++) {
grid[row][col] = mazeFile.get();
if (grid[row][col] == 'S') start = make_pair(row, col);
}
mazeFile.get(); // advance past newline
}
// create a window
RenderWindow window( VideoMode(15*cols, 15*rows), "Maze Drawer" );
// create an event object once to store future events
Event event;
// while the window is open
while( window.isOpen() ) {
// clear any existing contents
window.clear();
/////////////////////////////////////
// BEGIN DRAWING HERE
// place any draw commands here to display in the window
for (int row = 0; row < rows; row++) {
for (int col = 0; col < cols; col++) {
sf::RectangleShape rectangle;
rectangle.setSize(Vector2f(15, 15));
rectangle.setPosition(col * 15, row * 15);
switch (grid[row][col]) {
case 'S':
rectangle.setFillColor(Color::Green);
break;
case 'E':
rectangle.setFillColor(Color::Red);
break;
case '#':
rectangle.setFillColor(Color::Black);
break;
case '.':
rectangle.setFillColor(Color::White);
break;
}
window.draw(rectangle);
}
}
// END DRAWING HERE
/////////////////////////////////////
// display the current contents of the window
window.display();
/////////////////////////////////////
// BEGIN EVENT HANDLING HERE
// check if any events happened since the last time checked
while( window.pollEvent(event) ) {
// if event type corresponds to pressing window X
if(event.type == Event::Closed) {
// tell the window to close
window.close();
}
// check addition event types to handle additional events
}
// END EVENT HANDLING HERE
/////////////////////////////////////
}
return 0;
}