diff options
author | zedarider <ymherklotz@gmail.com> | 2016-12-13 22:25:23 +0000 |
---|---|---|
committer | zedarider <ymherklotz@gmail.com> | 2016-12-13 22:25:23 +0000 |
commit | 26b1a4317c6c8eceb78edac2ade26b7f344ace96 (patch) | |
tree | 4a5077088e941f8d4073aa93d432320dc2333213 | |
parent | 34c24c08e646113fa6a020099c7222b2059ffd38 (diff) | |
download | A-star-algorithm-optimising_queue.tar.gz A-star-algorithm-optimising_queue.zip |
adding bad attempt 1optimising_queue
-rwxr-xr-x | bin/main | bin | 416128 -> 35712 bytes | |||
-rw-r--r-- | include/astar.hpp | 48 | ||||
-rw-r--r-- | include/node.hpp | 44 | ||||
-rw-r--r-- | include/priority_queue.hpp | 196 | ||||
-rw-r--r-- | include/tilemap.hpp | 31 | ||||
-rw-r--r-- | src/astar.cpp | 166 | ||||
-rw-r--r-- | src/main.cpp | 113 | ||||
-rw-r--r-- | src/main.cpp.test | 27 | ||||
-rw-r--r-- | src/main.cpp.window | 83 | ||||
-rw-r--r-- | src/node.cpp | 37 | ||||
-rw-r--r-- | src/tilemap.cpp | 48 |
11 files changed, 53 insertions, 740 deletions
Binary files differ diff --git a/include/astar.hpp b/include/astar.hpp deleted file mode 100644 index a8225f3..0000000 --- a/include/astar.hpp +++ /dev/null @@ -1,48 +0,0 @@ -#ifndef ASTAR_HPP -#define ASTAR_HPP - -#include "priority_queue.hpp" -#include "node.hpp" - -#include <ostream> -#include <vector> - -// defines the number of nodes one can go to -// we don't need this as we will have the queues -#define NEIGHBOUR_NUM 8 - -// TODO add constructors and functions to calculate heuristics etc.. -class AStar { -public: - AStar(); - AStar(int *curr_graph, const unsigned int& width, const unsigned int& height); - - bool start_algorithm(int *curr_graph, const unsigned int& width, const unsigned int& height); - - void recreate_path(Node n); -private: - PriorityQueue<Node> open_set; - std::vector<Node> closed_set; - - int *graph; - int graph_width; - int graph_height; - int path_length; - - Node start_node, end_node; - - bool start_algorithm(); - - Node get_neighbour(Node& n_in, const int& neighbour_num); - - void calc_heuristic(Node& n); - void calc_f(Node& n); - - bool check_item_vec(const Node& n); - int get_index_vec(const Node& n); - void remove_from_vec(const Node& n); - - Node find_node(const unsigned int& x, const unsigned int& y); -}; - -#endif // ASTAR_HPP diff --git a/include/node.hpp b/include/node.hpp deleted file mode 100644 index f126fc4..0000000 --- a/include/node.hpp +++ /dev/null @@ -1,44 +0,0 @@ -#ifndef NODE_HPP -#define NODE_HPP - -#include <ostream> - -class AStar; - -class Node { - friend AStar; -public: - Node(); - - // overloading operators for ease of use. - friend bool operator<(const Node& n1, const Node& n2); - friend bool operator==(const Node& n1, const Node& n2); - friend bool operator>(const Node & n1, const Node & n2); - friend bool operator<=(const Node& n1, const Node& n2); - friend bool operator>=(const Node& n1, const Node& n2); - friend bool operator!=(const Node& n1, const Node& n2); - friend std::ostream& operator<<(std::ostream& out, const Node& n); -private: - // pointer to previous node so that I can backtrack without using recursion. - int x_prev, y_prev; - // pointers to the next nodes of which there are 4 in a grid. - // We do not need this as we are implementing a priority queue; - //Node *next_nodes[NEIGHBOUR_NUM]; - - // score that is used to compare to other nodes to see if the other path is - // more efficient. - int f_score; - // path length to get to that node. - int g_score; - // heuristic length to destination. - int h_score; - - // the x and y coordinates of the node in the grid - int x, y; - - // see if node has been visited. - // We don't need this as we will have an open and a closed set - //bool visited; -}; - -#endif // NODE_HPP diff --git a/include/priority_queue.hpp b/include/priority_queue.hpp index 537d6e9..3f842e8 100644 --- a/include/priority_queue.hpp +++ b/include/priority_queue.hpp @@ -2,6 +2,16 @@ #define PRIORITY_QUEUE_HPP template<typename T> +struct queue_node { + queue_node *next_node; + T item; + + queue_node(T& i) { + item = i; + } +}; + +template<typename T> class PriorityQueue { public: // creates a new object of type T @@ -10,179 +20,65 @@ public: ~PriorityQueue(); // pushes element to the queue which is sorted by smallest element first - void push(const T& element); + void push(T& element); // pops the first element from the queue and removes it while returning // it - T pop(); - - // returns the lowest priority item - T get_first(); - - // check if item is in queue - bool check_item(const T& item); - - // gets index of item - int get_index(const T& item); - - // removes item from queue - void remove_item(const T& item); - - void clear(); - - // return element at position; - const T& at(const unsigned int& i) const; - - const T& operator[](const unsigned int& i) const; + T& pop(); private: - // size of the current queue - unsigned int size; - // capacity of the queue, this will just increase in multiples of 2 - unsigned int capacity; - // array that will represent the priority queue - T *priority_array; - - // resize the queue's array accordingly by some factor - void resize_queue(double factor); - - // insert and element into the queue - // this assumes that the capacity is larger than the size - void insert_queue(const T& element, const unsigned int& loc); - - // removes an element from the queue - T remove_queue(const unsigned int& loc); + queue_node<T> *head_list; + + void delete_queue_node(queue_node<T> *node); }; template<typename T> -PriorityQueue<T>::PriorityQueue() : size(0), capacity(1) { - priority_array = new T; +PriorityQueue<T>::PriorityQueue() : head_list(nullptr) { } template<typename T> PriorityQueue<T>::~PriorityQueue() { - delete[] priority_array; -} - -template<typename T> -void PriorityQueue<T>::push(const T& element) { - unsigned int insert_loc = 0; - - if(size == capacity) - resize_queue(2); - - while(insert_loc < size && element > priority_array[insert_loc]) - ++insert_loc; - - insert_queue(element, insert_loc); -} - -template<typename T> -T PriorityQueue<T>::pop() { - if(!size) - throw "Priority Queue is empty, no item to pop"; - else if(size == capacity / 2) - resize_queue(0.5); - - T tmp = remove_queue(0); - return tmp; -} - -template<typename T> -T PriorityQueue<T>::get_first() { - return priority_array[0]; -} - -template<typename T> -bool PriorityQueue<T>::check_item(const T& item) { - for(unsigned int i = 0; i < size; ++i) - if(priority_array[i] == item) - return true; - return false; + delete_queue_node(head_list); } template<typename T> -int PriorityQueue<T>::get_index(const T& item) { - for(unsigned int i = 0; i < size; ++i) - if(priority_array[i] == item) - return i; - return -1; -} +void PriorityQueue<T>::push(T& element) { + queue_node<T> *el_node = new queue_node<T>(element); + queue_node<T> *tmp_head = head_list; + queue_node<T> *prev_head = head_list; -template<typename T> -void PriorityQueue<T>::remove_item(const T& item) { - remove_queue(get_index(item)); -} + while(tmp_head != nullptr && element > tmp_head->item) { + prev_head = tmp_head; + tmp_head = tmp_head->next_node; + } -template<typename T> -void PriorityQueue<T>::clear() { - T *tmp = new T; + // el_node->next_node = tmp_head; + // prev_head->next_node = el_node; - size = 0; - capacity = 1; - delete[] priority_array; - priority_array = tmp; + head_list = el_node; } template<typename T> -const T& PriorityQueue<T>::at(const unsigned int& i) const { - return priority_array[i]; +T& PriorityQueue<T>::pop() { + T *element = &head_list->item; + + if(head_list->next_node != nullptr) { + queue_node<T> *tmp_node = head_list; + head_list = head_list->next_node; + delete tmp_node; + } else { + delete head_list; + head_list = nullptr; + } + + return *element; } template<typename T> -const T& PriorityQueue<T>::operator[](const unsigned int& i) const { - return priority_array[i]; -} - -template<typename T> -void PriorityQueue<T>::resize_queue(double factor) { - capacity = (double)capacity * factor; - T *tmp_array = new T[capacity]; - - for(unsigned int i = 0; i < size; ++i) - tmp_array[i] = priority_array[i]; - - delete[] priority_array; - priority_array = tmp_array; -} - -template<typename T> -void PriorityQueue<T>::insert_queue(const T& element, const unsigned int& loc) { - T *tmp_array = new T[capacity]; - - for(unsigned int i = 0; i < size + 1; ++i) - if(i == loc) - tmp_array[i] = element; - else if(i < loc) - tmp_array[i] = priority_array[i]; - else - tmp_array[i] = priority_array[i - 1]; - - size++; - - delete[] priority_array; - priority_array = tmp_array; -} - -template<typename T> -T PriorityQueue<T>::remove_queue(const unsigned int& loc) { - T element; - - T *tmp_array = new T[capacity]; - - for(unsigned int i = 0; i < size; ++i) - if(i == loc) - element = priority_array[i]; - else if(i < loc) - tmp_array[i] = priority_array[i]; - else - tmp_array[i - 1] = priority_array[i]; - - size--; - - delete[] priority_array; - priority_array = tmp_array; - - return element; +void PriorityQueue<T>::delete_queue_node(queue_node<T> *node) { + if(node != nullptr) { + delete_queue_node(node->next_node); + delete node; + } } #endif // PRIORITY_QUEUE_HPP diff --git a/include/tilemap.hpp b/include/tilemap.hpp deleted file mode 100644 index 9770cb8..0000000 --- a/include/tilemap.hpp +++ /dev/null @@ -1,31 +0,0 @@ -// Tile map class that creates vertices to form a grid full of -// squares and adds texture to it. - -#ifndef TILEMAP_HPP -#define TILEMAP_HPP - -#include <SFML/Graphics.hpp> - -#include <string> - -class TileMap : public sf::Drawable, public sf::Transformable { -public: - 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; -}; - -#endif // TILEMAP_HPP diff --git a/src/astar.cpp b/src/astar.cpp deleted file mode 100644 index a8fa970..0000000 --- a/src/astar.cpp +++ /dev/null @@ -1,166 +0,0 @@ -#include "astar.hpp" - -#include <cmath> - -AStar::AStar() : graph(NULL), graph_width(0), graph_height(0), path_length(10) { -} - -AStar::AStar(int *curr_graph, const unsigned int& width, const unsigned int& height) : graph(curr_graph), graph_width(width), graph_height(height), path_length(1) { - for(unsigned int i = 0; i < graph_width * graph_height; ++i) - if(graph[i] == 3) { - start_node.x = i % graph_width; - start_node.y = i / graph_width; - start_node.g_score = 0; - calc_heuristic(start_node); - calc_f(start_node); - } else if(graph[i] == 2) { - end_node.x = i % graph_width; - end_node.y = i / graph_width; - } - open_set.push(start_node); - start_algorithm(); -} - -bool AStar::start_algorithm(int *curr_graph, const unsigned int& width, const unsigned int& height) { - graph_width = width; - graph_height = height; - graph = curr_graph; - - closed_set.clear(); - open_set.clear(); - - for(unsigned int i = 0; i < graph_width * graph_height; ++i) - if(graph[i] == 3) { - start_node.x = i % graph_width; - start_node.y = i / graph_width; - start_node.g_score = 0; - calc_heuristic(start_node); - start_node.x_prev = -1; - start_node.y_prev = -1; - calc_f(start_node); - } else if(graph[i] == 2) { - end_node.x = i % graph_width; - end_node.y = i / graph_width; - } - open_set.push(start_node); - return start_algorithm(); -} - -bool AStar::start_algorithm() { - while(open_set.get_first() != end_node) { - Node current = open_set.pop(); - closed_set.push_back(current); - for(unsigned int i = 0; i < NEIGHBOUR_NUM; ++i) { - Node n = get_neighbour(current, i); - - if(graph[n.y * graph_width + n.x] != 1 && n.x != -1 && n.y != -1) { - graph[n.x + n.y * graph_width] = 9; - graph[start_node.x + start_node.y * graph_width] = 3; - graph[end_node.x + end_node.y * graph_width] = 2; - int cost = current.g_score + path_length; - if(open_set.check_item(n)) - if(cost < open_set[open_set.get_index(n)].g_score) - open_set.remove_item(n); - if(check_item_vec(n)) - if(cost < closed_set[get_index_vec(n)].g_score) - remove_from_vec(n); - if(!open_set.check_item(n) && !check_item_vec(n)) { - n.g_score = cost; - calc_heuristic(n); - calc_f(n); - n.x_prev = current.x; - n.y_prev = current.y; - open_set.push(n); - } - } - } - } - recreate_path(open_set.get_first()); - return true; -} - -void AStar::recreate_path(Node n) { - while(n.x != -1 && n.y != -1) { - graph[n.x + n.y * graph_width] = 8; - n = find_node(n.x_prev, n.y_prev); - } -} - -Node AStar::get_neighbour(Node& n_in, const int& neighbour_num) { - Node n; - - if(neighbour_num == 0 && n_in.y > 0) { - n.x = n_in.x; - n.y = n_in.y - 1; - path_length = 10; - } else if(neighbour_num == 1 && n_in.x < graph_width - 1) { - n.x = n_in.x + 1; - n.y = n_in.y; - path_length = 10; - } else if(neighbour_num == 2 && n_in.y < graph_height - 1) { - n.x = n_in.x; - n.y = n_in.y + 1; - path_length = 10; - } else if(neighbour_num == 3 && n_in.x > 0) { - n.x = n_in.x - 1; - n.y = n_in.y; - path_length = 10; - } else if(neighbour_num == 4 && n_in.y > 0 && n_in.x < graph_height - 1) { - n.x = n_in.x + 1; - n.y = n_in.y - 1; - path_length = 14; - } else if(neighbour_num == 5 && n_in.y < graph_height - 1 && n_in.x < graph_width - 1) { - n.x = n_in.x + 1; - n.y = n_in.y + 1; - path_length = 14; - } else if(neighbour_num == 6 && n_in.y < graph_height - 1 && n_in.x > 0) { - n.x = n_in.x - 1; - n.y = n_in.y + 1; - path_length = 14; - } else if(neighbour_num == 7 && n_in.y > 0 && n_in.x > 0) { - n.x = n_in.x - 1; - n.y = n_in.y - 1; - path_length = 14; - } - - return n; -} - -void AStar::calc_heuristic(Node& n) { - n.h_score = pow(10 * (end_node.x - n.x), 2) + pow(10 * (end_node.y - n.y), 2); -} - -void AStar::calc_f(Node& n) { - n.f_score = n.g_score + n.h_score; -} - -bool AStar::check_item_vec(const Node& n) { - for(unsigned int i = 0; i < closed_set.size(); ++i) - if(closed_set[i] == n) - return true; - return false; -} - -int AStar::get_index_vec(const Node& n) { - for(unsigned int i = 0; i < closed_set.size(); ++i) - if(closed_set[i] == n) - return i; - return -1; -} - -void AStar::remove_from_vec(const Node& n) { - std::vector<Node> tmp_set; - for(unsigned int i = 0; i < closed_set.size(); ++i) - if(closed_set[i] != n) - tmp_set.push_back(closed_set[i]); - closed_set = tmp_set; -} - -Node AStar::find_node(const unsigned int& x, const unsigned int& y) { - Node n; - - for(unsigned int i = 0; i < closed_set.size(); ++i) - if(closed_set[i].x == x && closed_set[i].y == y) - n = closed_set[i]; - return n; -} diff --git a/src/main.cpp b/src/main.cpp index 14f46e8..ddde688 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -7,116 +7,17 @@ * */ -#include "tilemap.hpp" -#include "astar.hpp" - -#include <SFML/Graphics.hpp> +#include "priority_queue.hpp" #include <iostream> using namespace std; -int main(int argc, char *argv[]) { - cout << "executing " << argv[0] << endl; - cout << "arguments given: " << argc - 1 << endl; - - srand(time(NULL)); - - // create a render window of size 800x600 with a title. - sf::RenderWindow window(sf::VideoMode(2500, 1250), "A* Algorithm"); - // window.setFramerateLimit(1); - - // 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. - // for the mac - // const int tile_size = 21; - // const int rows = 41; - // const int cols = 34; - - const int tile_size = 25; - const int rows = 50; - const int cols = 100; - - int start_x = rand() % cols; - int start_y = rand() % rows; - - int end_x = rand() % cols; - int end_y = rand() % rows; - - // 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]; - - 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; - - AStar path_finder; - - // 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 prgetSizeessed. - 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; - - cout << (int)(mouse.y / tile_size) << " " << (int)(mouse.x / tile_size) << endl; - } 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)] = 0; - } - - for(int i = 0; i < cols * rows; ++i) - if(tiles[i] != 1) - tiles[i] = 0; - - // for(int i = 0; i < cols * rows; ++i) - // tiles[i] = 0; - // - // for(int i = 0; i < 1500; ++i) - // tiles[rand() % (cols * rows)] = 1; - - // start_x = rand() % cols; - // start_y = rand() % rows; - // - // end_x = rand() % cols; - // end_y = rand() % rows; - - tiles[start_y * cols + start_x] = 3; - tiles[end_y * cols + end_x] = 2; - - path_finder.start_algorithm(tiles, cols, rows); - - tiles[start_y * cols + start_x] = 3; - tiles[end_y * cols + end_x] = 2; - - // update tile map with the correct array. - map.load("res/GridTileTexture5.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(); - } - +int main() { + PriorityQueue<int> pq; + int i = 5; + pq.push(i); + int *i2 = &pq.pop(); + cout << "&i: " << &i << ", i2: " << i2 << endl; return 0; } diff --git a/src/main.cpp.test b/src/main.cpp.test deleted file mode 100644 index c0b9b00..0000000 --- a/src/main.cpp.test +++ /dev/null @@ -1,27 +0,0 @@ -/* - * - * description: Displays the A* Algorithm on a grid. - * - * author: Yann Herklotz <ymherklotz@gmail.com> - * date created: DD-MM-YYYY - * - */ - -#include "astar.hpp" -#include "priority_queue.hpp" - -#include <iostream> -#include <vector> - -using namespace std; - -int main(int argc, char *argv[]) { - int graph[9] = { - 3, 0, 0, - 0, 1, 1, - 0, 0, 2 - }; - AStar a(graph, 3, 3); - - return 0; -} diff --git a/src/main.cpp.window b/src/main.cpp.window deleted file mode 100644 index e9bb016..0000000 --- a/src/main.cpp.window +++ /dev/null @@ -1,83 +0,0 @@ -/* - * - * 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; -} diff --git a/src/node.cpp b/src/node.cpp deleted file mode 100644 index cce4b12..0000000 --- a/src/node.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "node.hpp" - -Node::Node() : x_prev(-1), y_prev(-1), f_score(-1), g_score(0), h_score(-1), x(-1), y(-1) { -} - -bool operator<(const Node& n1, const Node& n2) { - if(n1.f_score == n2.f_score) - return n1.h_score < n2.h_score; - return n1.f_score < n2.f_score; -} - -bool operator==(const Node& n1, const Node& n2) { - return (n1.x == n2.x) && (n1.y == n2.y); -} - -bool operator>(const Node& n1, const Node& n2) { - if(n1.f_score == n2.f_score) - return n1.h_score > n2.h_score; - return n1.f_score > n2.f_score; -} - -bool operator<=(const Node& n1, const Node& n2) { - return n1.f_score <= n2.f_score; -} - -bool operator>=(const Node& n1, const Node& n2) { - return n1.f_score >= n2.f_score; -} - -bool operator!=(const Node& n1, const Node& n2) { - return !(n1 == n2); -} - -std::ostream& operator<<(std::ostream& out, const Node& n) { - out << n.f_score; - return out; -} diff --git a/src/tilemap.cpp b/src/tilemap.cpp deleted file mode 100644 index 3ae167d..0000000 --- a/src/tilemap.cpp +++ /dev/null @@ -1,48 +0,0 @@ -#include "tilemap.hpp" - -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. - sf::Vertex *quad = &m_vertices[(i + j * width) * 4]; - - // 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 coordinates. - quad[0].texCoords = sf::Vector2f((tile_number % 4) * txt_size.x, (tile_number / 4) * txt_size.y); - quad[1].texCoords = sf::Vector2f((tile_number % 4 + 1) * txt_size.x, (tile_number / 4) * txt_size.y); - quad[2].texCoords = sf::Vector2f((tile_number % 4 + 1) * txt_size.x, (tile_number / 4 + 1) * txt_size.y); - quad[3].texCoords = sf::Vector2f((tile_number % 4) * txt_size.x, (tile_number / 4 + 1) * txt_size.y); - } - return true; -} - -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); -} |