@@ -22,51 +22,58 @@ ICell::ICell(uint x, uint y) noexcept
22
22
{}
23
23
24
24
/* ****************************************************************************/
25
- void
25
+ bool
26
26
ICell::clear (void ) noexcept
27
27
{
28
- if (EMPTY != _state)
29
- _changed = true ;
30
- _state = EMPTY;
28
+ if (EMPTY != _state) {
29
+ _state = EMPTY;
30
+ return true ;
31
+ }
32
+ return false ;
31
33
}
32
34
33
35
/* ****************************************************************************/
34
- void
36
+ bool
35
37
ICell::clean (void ) noexcept
36
38
{
37
- if (_state & PATH)
38
- _changed = true ;
39
- _state &= ~PATH;
39
+ if (_state & PATH) {
40
+ _state &= ~PATH;
41
+ return true ;
42
+ }
43
+ return false ;
40
44
}
41
45
42
46
/* ****************************************************************************/
43
- void
47
+ bool
44
48
ICell::setState (int st) noexcept
45
49
{
46
50
if (st != _state) {
47
51
_state = st;
48
- _changed = true ;
52
+ return true ;
49
53
}
54
+ return false ;
50
55
}
51
56
52
57
/* ****************************************************************************/
53
- void
58
+ bool
54
59
ICell::addState (State st) noexcept
55
60
{
56
61
if (!(_state & st)) {
57
62
_state |= st;
58
- _changed = true ;
63
+ return true ;
59
64
}
65
+ return false ;
60
66
}
61
67
62
68
/* ****************************************************************************/
63
- void
69
+ bool
64
70
ICell::remState (State st) noexcept
65
71
{
66
72
if (_state & st) {
67
73
_state &= ~st;
68
- _changed = true ;
74
+ return true ;
69
75
}
76
+ return false ;
70
77
}
71
78
72
79
/* ****************************************************************************/
@@ -76,21 +83,23 @@ PathCell::PathCell(uint x, uint y, PathCell* parent)
76
83
{}
77
84
78
85
/* ****************************************************************************/
79
- void
86
+ bool
80
87
PathCell::clean (void ) noexcept
81
88
{
82
- ICell::clean ();
83
89
_G = _H = 0 ;
84
90
_parent = nullptr ;
91
+
92
+ return ICell::clean ();
85
93
}
86
94
87
95
/* ****************************************************************************/
88
- void
96
+ bool
89
97
PathCell::clear (void ) noexcept
90
98
{
91
- ICell::clear ();
92
99
_G = _H = 0 ;
93
100
_parent = nullptr ;
101
+
102
+ return ICell::clear ();
94
103
}
95
104
96
105
/* ****************************************************************************/
@@ -101,60 +110,70 @@ PathCell::getScore() const noexcept
101
110
}
102
111
103
112
/* ****************************************************************************/
104
- Cell::Cell (uint x, uint y) noexcept
105
- : PathCell(x, y)
113
+ Grid::Grid () noexcept
114
+ : Graph<PathCell>()
115
+ , _vertexes{ _width * _height * 4 , Vertex () }
106
116
{}
107
117
108
118
/* ****************************************************************************/
109
119
void
110
- Cell::update ( void ) noexcept
120
+ Grid::draw (sf::RenderTarget& target, sf::RenderStates states) const
111
121
{
112
- if (!_changed)
113
- return ;
122
+ static double cell_width{ 0 };
123
+ static double cell_height{ 0 } ;
114
124
115
- setOutlineColor (Color::Black);
116
- setOutlineThickness (-1 .f );
125
+ bool updt{ false };
126
+ const auto new_width{ target.getSize ().x / static_cast <double >(_width) };
127
+ const auto new_height{ target.getSize ().y / static_cast <double >(_height) };
117
128
118
- if (EMPTY == _state) {
119
- setFillColor (Color (200 , 200 , 200 , 250 ));
120
- return ;
129
+ if (new_width != cell_width || new_height != cell_height) {
130
+ cell_width = new_width;
131
+ cell_height = new_height;
132
+ updt = true ;
121
133
}
122
134
123
- if (_state & (START_CELL | END_CELL))
124
- setFillColor (Color (234 , 24 , 24 ));
125
- else if (_state & WALL)
126
- setFillColor (Color::Black);
127
- else if (_state & PATH)
128
- setFillColor (Color (32 , 32 , 228 ));
135
+ for (uint i{ 0 }; i < _width; ++i)
136
+ for (uint j{ 0 }; j < _height; ++j) {
137
+ if (updt)
138
+ updateCell (i,
139
+ j,
140
+ Vector2f (cell_width * i, cell_height * j),
141
+ Vector2f (cell_width, cell_height));
129
142
130
- if (_state & SELECTED) {
131
- setOutlineColor (Color::Green);
132
- setOutlineThickness (-3 .f );
133
- }
143
+ updateCellStyle (i, j);
144
+ }
134
145
135
- _changed = false ;
146
+ target. draw (_vertexes. data (), std::size (_vertexes), sf::Quads, states) ;
136
147
}
137
148
138
- /* ****************************************************************************/
139
- Grid::Grid () noexcept
140
- : Graph<ui::Cell>()
141
- {}
142
-
143
149
/* ****************************************************************************/
144
150
void
145
- Grid::draw (sf::RenderTarget& target, sf::RenderStates states ) const
151
+ Grid::updateCellStyle (uint i, uint j ) const noexcept
146
152
{
147
- const auto cell_width{ target.getSize ().x / static_cast <double >(_width) };
148
- const auto cell_height{ target.getSize ().y / static_cast <double >(_height) };
153
+ auto st{ _grid[i + j * _width].getState () };
154
+ auto idx{ (i + j * _width) * 4 };
155
+ sf::Color color{ 200 , 200 , 200 , 250 };
156
+
157
+ if (st & (ICell::START_CELL | ICell::END_CELL)) {
158
+ color = Color (220 , 20 , 20 , 250 );
159
+ } else if (st & ICell::PATH) {
160
+ color = Color (32 , 32 , 228 );
161
+ } else if (st & ICell::WALL) {
162
+ color = Color::Black;
163
+ }
149
164
150
- for (size_t i { 0 }; i < _width ; ++i )
151
- for ( size_t j{ 0 }; j < _height; ++j) {
152
- auto & cell{ _grid[i + j * _width] };
165
+ for (auto it { 0 }; it < 4 ; ++it )
166
+ _vertexes[idx + it]. color = color;
167
+ }
153
168
154
- cell.update ();
155
- cell.setSize (Vector2f (cell_width, cell_height));
156
- cell.setPosition (Vector2f (cell_width * i, cell_height * j));
169
+ /* ****************************************************************************/
170
+ void
171
+ Grid::updateCell (uint i, uint j, Vector2f pos, Vector2f size) const noexcept
172
+ {
173
+ auto idx{ (i + j * _width) * 4 };
157
174
158
- target.draw (cell, states);
159
- }
175
+ _vertexes[idx].position = { pos.x , pos.y };
176
+ _vertexes[idx + 1 ].position = { pos.x + size.x , pos.y };
177
+ _vertexes[idx + 2 ].position = { pos.x + size.x , pos.y + size.y };
178
+ _vertexes[idx + 3 ].position = { pos.x , pos.y + size.y };
160
179
}
0 commit comments