diff options
author | zedarider <ymherklotz@gmail.com> | 2016-12-07 17:44:30 +0000 |
---|---|---|
committer | zedarider <ymherklotz@gmail.com> | 2016-12-07 17:44:30 +0000 |
commit | dce13d7a63ecf8e2c0ceb4df5a13eee4f0849350 (patch) | |
tree | fb302eb5c409cf4d986adf57ce54ae43bb1f7b54 | |
parent | 1c8f5fa033cf11504b6368a71d05d1d4eb1e83fd (diff) | |
download | A-star-algorithm-dce13d7a63ecf8e2c0ceb4df5a13eee4f0849350.tar.gz A-star-algorithm-dce13d7a63ecf8e2c0ceb4df5a13eee4f0849350.zip |
adding comments
-rwxr-xr-x | bin/main | bin | 272848 -> 272776 bytes | |||
-rw-r--r-- | include/TileMap.hpp | 11 | ||||
-rw-r--r-- | src/TileMap.cpp | 13 | ||||
-rw-r--r-- | src/main.cpp | 31 |
4 files changed, 45 insertions, 10 deletions
Binary files differ diff --git a/include/TileMap.hpp b/include/TileMap.hpp index e461c52..e47bb22 100644 --- a/include/TileMap.hpp +++ b/include/TileMap.hpp @@ -1,3 +1,6 @@ +// Tile map class that creates vertices to form a grid full of +// squares and adds texture to it. + #ifndef TILEMAP_HPP #define TILEMAP_HPP @@ -10,11 +13,19 @@ public: TileMap(); ~TileMap(); + // loads the text file and then creates the vertex array and the assigns + // the texture to the vertex array. It also therefore defines the size + // of the grid. bool load(const std::string& txt_file, const sf::Vector2f& txt_size, const sf::Vector2f& tile_size, const int *tiles, unsigned int width, unsigned int height); private: + // private function draw that overwrites the draw function in the + // Drawable function. This draws all the vertices to the window. virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const; + // vertex array that contains all the vertices. sf::VertexArray m_vertices; + // texture that will be loaded onto the vertices. This will be associated + // to the vertices. sf::Texture m_texture; }; diff --git a/src/TileMap.cpp b/src/TileMap.cpp index f309b2f..21222bf 100644 --- a/src/TileMap.cpp +++ b/src/TileMap.cpp @@ -7,27 +7,31 @@ TileMap::~TileMap() { } bool TileMap::load(const std::string& txt_file, const sf::Vector2f& txt_size, const sf::Vector2f& tile_size, const int *tiles, unsigned int width, unsigned int height) { + // load texture from the file if(!m_texture.loadFromFile(txt_file)) return false; + // set the sizes and type of the vertex array. m_vertices.setPrimitiveType(sf::Quads); + // multiply it by 4 because a square has 4 vertices. m_vertices.resize(width * height * 4); + // go through the array and make vertices. for(unsigned int i = 0; i < width; ++i) for(unsigned int j = 0; j < height; ++j) { // get the colour of the tile int tile_number = tiles[i + j * width]; - // get pointer to vertex of current quad + // get pointer to vertex of current quad. sf::Vertex *quad = &m_vertices[(i + j * width) * 4]; - // define the quads + // define the quads. quad[0].position = sf::Vector2f(i * tile_size.x, j * tile_size.y); quad[1].position = sf::Vector2f((i + 1) * tile_size.x, j * tile_size.y); quad[2].position = sf::Vector2f((i + 1) * tile_size.x, (j + 1) * tile_size.y); quad[3].position = sf::Vector2f(i * tile_size.x, (j + 1) * tile_size.y); - // define texture + // define texture coordinates. quad[0].texCoords = sf::Vector2f(tile_number * txt_size.x, 0); quad[1].texCoords = sf::Vector2f((tile_number + 1) * txt_size.x, 0); quad[2].texCoords = sf::Vector2f((tile_number + 1) * txt_size.x, txt_size.y); @@ -37,8 +41,11 @@ bool TileMap::load(const std::string& txt_file, const sf::Vector2f& txt_size, co } void TileMap::draw(sf::RenderTarget& target, sf::RenderStates states) const { + // get the transform and set it to the states. states.transform = getTransform(); + // assign it the texture as well. states.texture = &m_texture; + // draw the vertices with the transforms and textures. target.draw(m_vertices, states); } diff --git a/src/main.cpp b/src/main.cpp index 7ca4243..89f9f10 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,9 @@ /* * - * description: + * description: Displays the A* Algorithm on a grid. * - * author: Yann Herklotz <ymherklotz@gmail.com> - * date created: DD-MM-YYYY + * author: Yann Herklotz <ymherklotz@gmail.com> + * date created: DD-MM-YYYY * */ @@ -20,44 +20,61 @@ 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); - tiles[(int)(mouse.x / tile_size) + - cols * (int)(mouse.y / tile_size)] = 1; + // 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); - tiles[(int)(mouse.x / tile_size) + - cols * (int)(mouse.y / tile_size)] = 2; + // 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(); } |