Skip to content

Commit 4b8c32f

Browse files
committed
Add calculation for heuristic with text value on nodes.
1 parent 02f2786 commit 4b8c32f

File tree

7 files changed

+81
-27
lines changed

7 files changed

+81
-27
lines changed

Roboto-Regular.ttf

142 KB
Binary file not shown.

include/Engine.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,15 @@ class Engine
2121
void draw();
2222
void update();
2323

24+
int calculateHeuritics(Node* nodeFrom, Node* nodeTarget);
25+
26+
private:
2427
int gridSize;
2528
int tileSize;
2629

2730
sf::RenderWindow window;
2831
sf::Font font;
29-
sf::Texture* arrow;
32+
sf::Texture arrow;
3033
sf::Image img;
3134

3235
std::vector<std::vector<Node> > mNodes;

include/Node.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Node
88
, public sf::Transformable
99
{
1010
public:
11-
Node(int x, int y, sf::Texture *texture, bool passable);
11+
Node(int x, int y, sf::Texture* texture, sf::Font* font1, bool passable);
1212

1313
virtual ~Node();
1414

@@ -20,6 +20,10 @@ class Node
2020
void setStart() { start = true; }
2121
void setEnd() { ending = true; }
2222

23+
void setHeuristic(int heuristic);
24+
25+
int getHeuristic() { return heuristic; }
26+
2327
private:
2428

2529
void setFill();
@@ -33,8 +37,10 @@ class Node
3337
sf::Sprite sArrow;
3438

3539
sf::RectangleShape rect;
36-
sf::IntRect pos;
37-
int heuritic;
40+
sf::Vector2i pos;
41+
sf::Text h_text;
42+
43+
int heuristic;
3844
int movementCost;
3945
int fValue;
4046

include/toString.hpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <sstream>
2+
#include <string>
3+
4+
template <typename T>
5+
std::string toString(const T& value)
6+
{
7+
std::ostringstream oss;
8+
oss << value;
9+
10+
return oss.str();
11+
}

main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <iostream>
2-
#include "engine.h"
2+
#include "Engine.h"
33

44
int main(int argc, char** argv)
55
{

src/Engine.cpp

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#include "Engine.h"
22
#include "SFML/Graphics.hpp"
3+
#include <stdlib.h>
34

45
Engine::Engine()
56
{
67
if(!init())
78
throw("Init failed!");
8-
9+
auto a=42;
910
}
1011

1112
Engine::~Engine()
@@ -16,32 +17,33 @@ Engine::~Engine()
1617

1718
bool Engine::init()
1819
{
20+
1921
gridSize = 10;
2022
tileSize = 64;
2123

22-
window.create(sf::VideoMode(640, 640), "My window");
24+
window.create(sf::VideoMode(640, 800), "My window");
2325

2426
if(!img.loadFromFile("arrow.png"))
2527
{
2628
printf("load of file failed");
2729
return false;
2830
}
2931

30-
printf("proceeding to load from image\n");
3132

3233
//printf("inter %i\n", img.getSize().x);
3334

34-
if(!arrow->loadFromImage(img))
35+
if(!arrow.loadFromImage(img))
3536
{
3637
printf("load of image failed\n");
3738
return false;
3839
}
39-
else
40+
41+
42+
if(!font.loadFromFile("Roboto-Regular.ttf"))
4043
{
41-
printf("image loading complete\n");
44+
printf("load of font failed\n");
4245
}
4346

44-
printf("Init successful\n");
4547

4648
return true;
4749
}
@@ -70,27 +72,32 @@ void Engine::gridMap()
7072
for(int i=0; i < gridSize; i++)
7173
{
7274
//fills the map with empty spaces.
73-
printf("0\n");
74-
Node temp(0,0, arrow, true );
75-
printf("1\n");
75+
Node temp(0,0, nullptr, nullptr, true );
7676
mNodes.at(i).resize(gridSize, temp);
7777
}
78-
printf("2\n");
78+
7979
bool passable = true;
8080

8181
for (int i = 0; i < gridSize; i++)
8282
{
8383
for(int j = 0; j < gridSize; j++)
8484
{
85-
Node node(i, j, arrow, passable);
85+
Node node(i, j, &arrow, &font, passable);
8686
mNodes[i][j] = node;
8787
}
8888
}
8989

9090
mNodes[0][0].setStart();
9191
mNodes[9][9].setEnd();
9292

93+
for (int i = 0; i < gridSize; i++)
94+
{
95+
for(int j = 0; j < gridSize; j++)
96+
{
9397

98+
mNodes[i][j].setHeuristic(calculateHeuritics(&mNodes[i][j], &mNodes[9][9]));
99+
}
100+
}
94101
}
95102

96103
void Engine::processInput()
@@ -111,8 +118,6 @@ void Engine::processInput()
111118
int posX = (int)(position.x/64);
112119
int posY = (int)(position.y/64);
113120

114-
//printf("posX: %i, posY: %i, x: %i, y: %i\n", (int)position.x, (int)position.y , posX, posY);
115-
116121
if(event.mouseButton.button == sf::Mouse::Left)
117122
{
118123
mNodes[posX][posY].setPassable();
@@ -124,6 +129,7 @@ void Engine::processInput()
124129

125130
void Engine::draw()
126131
{
132+
window.clear();
127133
for (int i = 0; i < gridSize; i++)
128134
{
129135
for(int j = 0; j < gridSize; j++)
@@ -147,4 +153,11 @@ void Engine::update()
147153

148154
}
149155

156+
int Engine::calculateHeuritics(Node* from, Node* target)
157+
{
158+
159+
return abs(from->getPosition().x - target->getPosition().x)
160+
+ abs(from->getPosition().y - target->getPosition().y);
161+
}
162+
150163

src/Node.cpp

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,35 @@
11
#include "Node.h"
22
#include "SFML/Graphics.hpp"
3+
#include <string>
4+
#include "toString.hpp"
35

4-
Node::Node(int x, int y, sf::Texture* texture, bool passable)
6+
Node::Node(int x, int y, sf::Texture* texture, sf::Font* font1, bool passable)
57
{
68
this->passable = passable;
79
start = false;
810
ending = false;
911
nodeSize = 64;
10-
pos.height = x;
11-
pos.width = y;
12+
pos.x = x;
13+
pos.y= y;
14+
setPosition(pos.x, pos.y);
1215
rect.setSize(sf::Vector2f((float)x*nodeSize, (float)y*nodeSize));
1316
rect.setOrigin(0.0f, 0.0f);
1417
rect.setSize(sf::Vector2f(100, 100));
1518

16-
//sArrow.setTexture(*texture);
19+
//sArrow.setTexture(tex);
1720

1821
setFill();
1922

2023
rect.setOutlineThickness(2);
2124
rect.setOutlineColor(sf::Color(0, 0, 0));
25+
rect.setPosition((float)(pos.x*nodeSize),(float)(pos.y*nodeSize));
26+
27+
h_text.setFont(*font1);
28+
h_text.setCharacterSize(12);
29+
h_text.setColor(sf::Color(255,0,0,255));
30+
h_text.setOrigin(0.0f, 0.0f);
31+
h_text.setPosition((float)(pos.x*nodeSize),(float)(pos.y*nodeSize));
32+
2233

2334
}
2435

@@ -31,15 +42,16 @@ void Node::draw(sf::RenderTarget& target, sf::RenderStates states) const
3142
{
3243
//states.transform *= getTransform();
3344

34-
target.draw(rect, states);
35-
36-
//target.draw(sArrow);
45+
target.draw(rect);
46+
target.draw(h_text);
47+
target.draw(sArrow);
3748
}
3849

3950
void Node::update()
4051
{
41-
rect.setPosition((float)(pos.height*nodeSize),(float)(pos.width*nodeSize));
52+
4253
setFill();
54+
4355
}
4456

4557
void Node::setFill()
@@ -79,3 +91,12 @@ sf::FloatRect Node::getBounds() const
7991
{
8092
return getTransform().transformRect(rect.getGlobalBounds());
8193
}
94+
95+
void Node::setHeuristic(int heuristic)
96+
{
97+
this->heuristic = heuristic;
98+
99+
h_text.setString(toString(heuristic));
100+
}
101+
102+

0 commit comments

Comments
 (0)