aboutsummaryrefslogtreecommitdiffstats
path: root/src/main.cpp.window
blob: e9bb016f1292b0b54847f7ef69cfcd2e5a5cd4d4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 *
 * description:		Displays the A* Algorithm on a grid.
 *
 * author:          Yann Herklotz <ymherklotz@gmail.com>
 * date created:    DD-MM-YYYY
 *
 */

#include "tilemap.hpp"
#include "astar.hpp"

#include <SFML/Graphics.hpp>

#include <iostream>
#include <cstdlib>

using namespace std;

int main(int argc, char *argv[]) {
	cout << "executing " << argv[0] << endl;
	cout << "arguments given: " << argc - 1 << endl;

	// create a render window of size 800x600 with a title.
	sf::RenderWindow window(sf::VideoMode(800, 600), "A* Algorithm");

	// print out the window size.
	cout << "window size: " << window.getSize().x << ", " << window.getSize().y << endl;

	// set the constants int the file that define the grid to be displayed.
	const int tile_size = 21;
	const int rows = 41;
	const int cols = 34;

	// print out that information.
	cout << "tile size: " << tile_size << "px, rows: " << rows << ", cols: " <<
		cols << endl;

	// create the array of the right size using the constants.
	int tiles[cols * rows];

	// assign zeros to the tiles.
	for(int i = 0; i < cols * rows; ++i)
		tiles[i] = 0;

	// create a tile map that will be used to display the array.
	TileMap map;

	// event loop that runs the window.
	while(window.isOpen()) {
		// create an event.
		sf::Event event;
		// check if an event has been triggered.
		while(window.pollEvent(event))
			// if the event is the window closing, close the window.
			if(event.type == sf::Event::Closed)
				window.close();

		// check if mouse buttons are pressed.
		if(sf::Mouse::isButtonPressed(sf::Mouse::Left)) {
			// get the position of the mouse.
			sf::Vector2i mouse = sf::Mouse::getPosition(window);
			// set the tile colour to 1.
			tiles[(int)(mouse.x / tile_size) + cols * (int)(mouse.y / tile_size)] = 1;
		} else if(sf::Mouse::isButtonPressed(sf::Mouse::Right)) {
			sf::Vector2i mouse = sf::Mouse::getPosition(window);
			// set the tile colour to 2.
			tiles[(int)(mouse.x / tile_size) + cols * (int)(mouse.y / tile_size)] = 2;
		}

		// update tile map with the correct array.
		map.load("res/GridTileTexture3.png", sf::Vector2f(200, 200), sf::Vector2f(tile_size, tile_size), tiles, cols, rows);

		// clear the screen.
		window.clear(sf::Color(47, 47, 47));
		// draw the map onto the screen.
		window.draw(map);
		// display the window.
		window.display();
	}

	return 0;
}